Kubejs [Moon Phase Forecast] [KJS] Kubejs Minecraft Game

This tutorial is set by the author's setting without permission.

Project goal

After the player gets up, the type and time of the next moon incident will be output in the chat bar.

text

 levelevents.tick (event => {

// Write the code here

})

Key code

1. Get the list of moon incidents and take the first value: Don't ask me why I take 0, and ask because the list of lists starts from 0

 var moon = event.level.getlunarcontext (). Lunarforecast.Forecast [0] 

2. Get the type of moon event:

 Let Moon_type = Moon.getlunareventkey (). Path 

3. Planning date for obtaining the moon incident:

 let scheduleddy = moon.scheDuledDay () 

Complete code

 levelevents.tick (event => {

Var moon = event.level.getlunarcontext (). Lunarforecast.Forecast [0]

Let moon_type = moon.getlunareventkey (). PATH

// Get the current game days (game day)

Let today = math.trunc (event.level.daytime ()/24000)

// Get the planning date of the recent moon incident

Let scheduleDding = Moon.scheDuledDay ()

// Get the interval

Let interval = (sduleddy)-(today)

// Get the current game time (for planning tasks)

If (event.level.getdytime ()%24000 == 10) {{

Event.server.runcommandsilent (`say $ {` `distance $ {moon_type} and $ {intermodring ()` `` ``)

}

})

Code optimization

However, when the above code is running, a large amount of error information will be output in the log, because this is the case where the Event is NULL in Kubejs6.(Figure) Solution:

Divide the first step of the key code into two steps, first obtain and judge whether the value of the Event.Leveel.GetlunarContext () is null, and if it is NULL, it is over.

Otherwise, get the moon event list and take the first value.

 var moon_context = event.level.getlunarcontext ()

Var Moon

if (moon_context == null) {

Return;

} Else {

Moon = Moon_Context.lunarforecast.Forecast [0]

}

Optimized complete code

 levelevents.tick (event => {

Var Moon_Context = Event.Level.GetlunarContext ()

Var Moon

If (moon_context == null) {

Return;

} Else {

Moon = Moon_Context.lunarforecast.Forecast [0]

}

Let moon_type = moon.getlunareventkey (). PATH

// Get the current game days (game day)

Let today = math.trunc (event.level.daytime ()/24000)

// Get the plan of the recent incident month

Let scheduleDding = Moon.scheDuledDay ()

// Get the interval

Let interval = (sduleddy)-(today)

// Get the current game time (for planning tasks)

If (event.level.getdytime ()%24000 == 10) {{

Event.server.runcommandsilent (`say $ {` `distance $ {moon_type} and $ {Internet.tostring ()}` `}}

})

Module version

Kubejs: 1902.6.2-build.45

Moon event: 2.1.0.6

Minecraft: 1.19.2

Effect display