This tutorial is set by the author's setting without permission.
Foreword
Kubejs's script file is .js, placed in the Kubejs folder of the game directory, and placed in the Server_Scripts or Startup_scripts subfolders according to different modified content.Remember the/RELOAD overload after modifying or adding a script.
Kubejs's wiki lists all the prison events, but not every incident gives a corresponding example.
This tutorial will mainly introduce how to listen to interactive events in the world with Kubejs and make magic reforms.In a .js file, you can put multiple Onevent to monitor different events separately.In this tutorial, take the left/right of the square block, and use items to interact with the entity. The four types of events of the block are used as an example.
Let ’s take some real ones. If the tutorial just gives a few functions that can be performed on behalf of the parameters, then the things that can be done after reading are relatively limited. For an interactive event, you can call the target XYZ coordinate and player’s master hand.The items held, but how do you know these attributes and how to use it?Fortunately, Kubejs has provided name space.You can find it in Kubejs's Wiki2 (although not well):
After clicking, you can see a large string of Classes.But don't worry, the main use of this tutorial is:
Entityjs
BlockContainerjs
Itemstackjs
Playerjs
Severjs
Serverplayerjs
These are basically enough.
text
Right -click/Left -click the square
This is a simple event: player holds diamonds and right -click crack bricks, cracks bricks are repaired as stone bricks, and the server shows "Crazy Diamond!"
During the logic: Player right -click the square event. If the target block is crack stone brick, and the player's hand items are diamonds, they will replace it with stone bricks at the location of the cracks brick, and the server speaks.
For the incident, you can use Event.block (right -clicking), event.player (players who do this action), event.hand (player's hand), etc.
How to know the player's handheld items?Then open Playerjs.
In Properties, it was not found that "player holding items" related entries, and in Functions, found:
This indicates that this is a function that needs to specify which hand (hand), click the Internethand:
You can fill in the main_hand (main hand) or OFF_HAND (deputy), or you can also use event.hand.
To let the server execute commands, you can use a function:
Event.server.runcommand ()event.server.runcommandsilent ()
The latter will not report information in the chat bar.These two functions must have many more interesting gameplay under the command of the command block.
The xyz coordinates of the block are event.block.x, event.block.y, event.block.z.
So there is:
Onevent ('block.richt_click', event => {{If (event.block == 'Minecraft: Cracked_stone_bricks' &&PENT.Player.GetheldiTem (event.hand) == 'Minecraft: Diamond')
{{
Event.server.runcommandsilent (`Particle Minecraft: spit $ {event.block.x} $ {event.block.y+0.2} $ {event.block.z} 1 0.4 0.4 1 1`)
event.server.runcommandsilent (`setBlock $ {event.block.x} $ {event.block.y} $ {event.block.z} minecraft: stone_bricks`) event.server.run Command (`Say Crazy Diamond!`)
}
})
The effect is as follows:
The crack stone brick successfully became a stone brick.In fact, this mechanism also acts on fake players, so the right -clicking of a robotic handheld diamond with mechanical power will take effect.
We can also add a mechanism: "When the main hand holds diamonds and the auxiliary hand holds gold apples, the crack brick is converted into white birch saplings."
Onevent ('block.richt_click', event => {{If (event.block == 'Minecraft: Cracked_stone_bricks'
&& event.player.GetheldiTem (event.hand) == 'Minecraft: Diamond'
&& event.player.GetheldiTem (off_hand) == 'MINECRAFT: GOLDEN_APLE')
{{
Event.server.runcommandsilent (`Particle Minecraft: spit $ {event.block.x} $ {event.block.y+0.2} $ {event.block.z} 1 0.4 0.4 1 1`)
event.server.runcommandsilent (`setBlock $ {event.block.x} $ {event.block.y} $ {event.block.z} minecraft: Birch_sapling`)
event.server.runcommand (`Say Golden Experience!`)
}
})
Master Diamond, Deputy Golden Apple, right -click on crack bricks.
The cracks tiles turned into white birch seedlings.
I forgot to remove the previous "crazy diamond".
The left -clicking incident is also the same. You only need to replace the event type with the block.left_click.
Based on this incident, the effect of digging pebbles such as crystal gardens from the crystal garden can only determine whether the player is empty and sneak at the mud, and then generate pebbles in the specified location.
For another example, you can calculate whether the surrounding blocks of the block of the interactive block can meet the conditions through simple coordinates, so as to achieve novel world synthesis.
Physical interaction
For the incident, you can get Event.Target (interactive entity) and event.player (players who do this action).
Example: Use a mechanical chocolate stick to interact with wolves (right -click), the number of chocolate sticks is reduced by 1 (was eaten), and then the wolf died.
The problem that needs to be solved here: The type of entity of the interaction is a wolf, which reduces the number of chocolate rods by 1 and let the wolf death.
In the name space, you can find the physical type named Type:
At the same time, I found a function to kill the entity:
Found the number of items in Itemstackjs, it is an integer number:
So there is:
Onevent ('item.entity_interact', event => {if (event.target.type == 'minecraft: wolf'&& event.player.Gethelditem (event.hand) == 'Create: bar_of_chocolate'))
{{
Event.player.mainhanditem.Count- = 1
Event.target.kill ()
Event.server.runcommand (`Say dog cannot eat chocolate!
}
})
Feed chocolate for wolves (dogs).
It is dead.
Based on this principle, the "master ball" that captures the entity by saving all NBT data in a certain item and clearing the original entity method.
Block destruction
For the incident, you can get Event.Block (will be destroyed) and event.player (players doing this action).
Example: When the player level of non -creative model is less than equal to level 2, iron ore cannot be destroyed.
Find the player level and whether to create a model under Playerjs:
At the same time, to prevent the damage from the block, you need to use it
event.cancel ()
Cancel the incident.
So there is:
Onevent ('BLOCK.BREAK', Event => {If (event.block == 'Minecraft: Iron_ore' &&PERER.Xplevel <= 2 &&! Event.player.creativemode) {
Event.server.runcommand (`SAY is not enough!`)
Event.cancel ()
}
})
Level 2, try to destroy iron ore.
The incident of destroying iron ore was canceled, and the server prompts to be destroyed.
The level is successful at 3.
Summarize
Surveillance incidents, obtain parameters, functions, properties and operate through Wiki and other methods.