This tutorial is set by the author's setting without permission.
Foreword
What can Kubejs do?If so, how to achieve it?How to use better code to implement?This article tries to answer these questions and will provide many common techniques and cases for your reference.
Minecraft FORGEKEKUJSARCHITECTURYRHINOPROBEJS1.19.243.51902.2-Build.426.3.4902.2- Build.2805.3.2
Use Visual Studio Code and Probejs
After being installed according to Visual Studio Code and Probejs, right -click your version folder and open through the code, you can see the following interface:
Open the LOGS folder and drag the file inside long press to the lower right corner
Three files are like this, and it is more convenient to check the log in the future
When writing the code, you can see the prompts provided by Probejs. Press the Ctrl+space to evoke the prompt again
Thinking when using Kubejs
When you want to use Kubejs to do something, you must first know where it should start, usually using an event
Event composition
The event is divided into several objects, and there are many ways in each object
Playerevents method:DecorateChat, CheStopened, Advancement, Chat, ChestClosed, Loggedin, Loggedout, InventoryClosed, InventoryChanged, InventoryOpened, Tick, Respawned
You can see the event list in Wudji tutorial or official wiki.The code can be executed when these incidents occur.
You can directly see the details of the event using Probejs.
Extra explains that this event can add an additional parameter. In this physical injury event, it means the type of injured entity. If it is not filled, it is possible.@AT indicates whether the event occurs on the server or client or both.@cancelLABLE shows that this incident can be canceled, and if not, it cannot be canceled.
Case:
EntityEvents.Hurt ((EVENT) => {Event.cancel (); // When the entity is injured, cancel the event
});
At this time, no physical injury will be injured because the incident is canceled.If you change it to:
EntityEvents.HURT ('Minecraft: Pig', (Event) => {Event.cancel (); when minecraft: pig is injured, cancel the event
});
The incident will be canceled only when Minecraft: Pig is injured.It is equivalent to simplifying the following code:
EntityEvents.Hurt ((EVENT) => {If (event.entity.type == "minecraft: pig") {{
// When minecraft: pig is injured
Event.cancel (); // Cancel the event
}
});
Some events cannot be canceled, such as:
serverEvents.loadedloaded (handler: (event: internal.servereventjs) => void): void@at - server
Event.cancel () also has the function of Return, which will directly exit the function.If you only use Return, you will only exit the function without canceling the event, for example:
EntityEvents.Hurt ((EVENT) => {If (event.entity.type! = "Minecraft: Pig") {{
// When it is not minecraft: pig is injured
Return; // Exit the function, follow -up code will not execute
}
Event.cancel (); // Cancel the event
});
Callback function
Some callback functions can start executing code. For example, when registering an item, it will run some code when it is eaten: it will run some code:
StartuPEVENTS.Registry ('item', event => {{Event.create ('kubejs: test_food'). Maxstacksize (16). Food (food => {{{{
FOOD.HUNGER (5);
FOOD.EATEN (Eaten => {{
Eaten.player.tell ('Eat and eat');
})
})
})
Food.eaten () needs to use a callback function, you can write the required code in this parameter.
If you want to update some of the code, you can rewrite it as follows:
StartuPEVENTS.Registry ("item", (event) => {Event
.Create ("Kubejs: test_food")
.Maxstacksize (16)
.Food ((FOOD) => {{{
FOOD.HUNGER (5);
FOOD.Saturation (1.5);
FOOD.EATEN ((Eaten) => {{
Global ["test_food_eaten"] (Eaten);
});
});
});
global ["test_food_eaten"] = (Eaten) => {Eaten.player.tell ("Eat 1 eat");
};
In order to get automatic completion in the Global function, you can use TypeScript or JSDOC.No matter which one is used, you need to get the type of the parameter of this callback function.Place the mouse on Eaten, and then copy the following content:
If you use JSDOC, first enter/* above the function declaration (allowing indentation, space and empty line), and then press TAB to automatically make up:
Paste the content just copied, and then the Eaten parameter below will be considered to be an automatic complement.
/***
*@Param {Internet.foodeenEventjs} Eaten
*/
global ["test_food_eaten"] = (Eaten) => {
Eaten.player.tell ("Eat 1 eat");
};
When you use Global for the first time, you need to restart the game once to make Global be loaded. After that, you only need to use the following instructions to use the following instruction
/kubejs reload startup_scripts
Get and modify attributes
We always need to obtain and modify some attributes.Methods to start with GET or IS to obtain attributes:
let p = event.getplayer (); // Players who get the incidentlet f = p.isonfire (); // Whether the player is on fire
However, if these methods do not have parameters, you can remove the GET or IS to change the first letter uppercase to a lowercase and directly obtain the attribute:
let p = event.player;Let f = p.Onfire; // can only change the first letter
Let s = p.gethelditedm ('main_hand'); // Need a parameter cannot be briefly written
Similarly, modify the attribute (if there are only 1 parameter):
p.Sethealth (10); // Use the method of setting to modify the attributeLet H = P.Health; // Remove the set and change the first letter to get the Health attribute
h = 10; // Direct assignment
The above code can be optimized using deconstruction assignment:
let p = event.player;let l = event.level;
Const {player, level} = Event; // Except for different variable names, there is no difference
const {health} = player; // deconstruct again
const {player: {health}} = event; // directly get health
health = 10; // Direct assignment
The deconstruction assignment will be used in the subsequent code. The above use of a single letter will be used as a variable name for demonstration.
Some JavaScript features
The characteristics of JavaScript can optimize the code. All the contents of this section will be included in the link of Cainiao Tutorial and Liao Xuefeng's official website. I hope you will first learn JavaScript and then learn Kubejs.
Array (array)
Cainiao tutorial Liao Xuefeng's official website
In the game, you can enter the following command to get an array of an item:
/kubejs hotbar/kubejs inventory
#Are the shortcut column and item bar
Click to copy the chat bar, which may be similar:
['minecraft:white_wool', 'minecraft:oak_wood', 'minecraft:oak_stairs', 'minecraft:jungle_planks']
Then you can use it in the code:
serverEvents.Rcipes ((event) => {Let arr = [
"Minecraft: White_wool", "
"Minecraft: Oak_wood",
"Minecraft: Oak_stairs",
"Minecraft: jungle_planks",, "
];
Arr.Foreach ((f) => {{
Event.remove ({Output: `$ {f}`}); // Batch removal of items
});
});
In addition to getting an array yourself, you can also use item.list, which is an array of all items that appear in the creation mode items.
// Print all the enchanting items in the logItem.list
.Filter ((f) => {{{
Return f.enchantable;
})
.Foreach ((f) => {{{
Console.log (f.toitemstring ());
});
// part of the possible output:
// [15:17:48] [Info] Example.js#26: Item.of ('Minecraft: Netherite_pickaxe', '{damage: 0}')
String (string)
Cainiao tutorial Liao Xuefeng's official website
Strings are generally used to indicate the entity of the items, etc., and can also be used in chat events.
"Naming Space ID" can represent 1 item, and "10X Naming Space ID" can represent 10 items.
If you want a more convenient test, you can try:
<<<<<<] = "assh: none;">> useRe archives. block
Const {player, message, level, server} = event; if (message! = "Ts") {//
Return;
}
// When you enter TS in the chat bar, you will execute the code
Item.list
.Filter ((f) => {{{
Return f.enchantable;
})
.Foreach ((f) => {{{
Console.log (f.toitemstring ());
});
});
Some tips and examples
Here are some examples of Kubejs.
Quick refresh
All Kubejs can refresh the script at one time, and faster than the original/RELOAD
<<<<<<] = "assh: none;">> useRe archives. block
Const {player, message, level} = Event;
Const commit = [
"Client_scripts",
"Config",
"Lang",
"Server_scripts",
"Startup_scripts",
"Textures",
];
If (message == "re") {{
Command.Foreach ((c) => level.runcommandsilent
Player.tell (text.of ("Reloaded all scripts!"). Green ());
}
});