This tutorial is set by the author's setting without permission.
There are many events in the game, you can view it here, but most of them are English, so let me explain it.
This tutorial will explain two examples.
Here I highly recommend the Zen Utils module written by Youyihj. After joining, you can heat up the game event.
Example 1: Only when the player's experience level is greater than equal to two levels can the iron ore be destroyed.
Guide package: excavation block event package, event cancellation package, iBlock object, iPlayer object
Import Crafttweaker.event.blockBreakevent;Import Crafttweaker.event.ieventCanceLable;
import crafttweaker.block.iblock;
import crafttweaker.player.iplayer;
Example code: (Don’t double -click the keyword, otherwise you will tell you in the java format to tell you all the error is reported)
Events.onBlockBreak (Function (event as blockbreakevent) {// Create a square destruction event and obtain it with EventVar block as iBlock = event.block; // Create IBLOCK objects
Var Player as iPlayer = Event.player; // Create an iPlayer object
If (! Isnull (block) && Block.definition.id == "Minecraft: Iron_ore") {// If the block is not empty, and it is an iron ore
If (player.creative) {
Return; // Creation mode ignores restrictions
}
If (Player.xp <2) {// Experience level is less than 2
Event.cancel (); // Cancel the event (only part of the interface of the cancellation of the incident, pay attention to check it))
Event.player.sendstatusMessage (format.red ("You need at least 2 levels to destroy iron ore"); // Give prompts
}
}
});
Example 2: Only when the player's experience level is greater than or equal to level 30 can we attack the boss (blood bars are counted)
Guide bag
Import Crafttweaker.event.playEratacockEvent; Import Crafttweaker.eventCanceLABLE;Import crafttweaker.entity.ientity;
import crafttweaker.player.iplayer;
Implement code: (iPlayer class inherits the IENTITY class)
Events.onPlayEratackentity (Function (Event As PlayRattackenityevent) {Var Entity as IENTITY = EVENT.TARGET; // Create an INTITY object, here refers to the attack target
Var Player as iPlayer = EVENT.Player; // Create an iPlayer object, here refers to attacking players
If (! Isnull (Entity) && Player.creative) {
Return; // Create mode ignore
}
If (! Isnull (Entity) && Entity.dimension! = 7 && Entity.isboss) {// Decision whether it is boss [Note 1]
If (Player.xp <30) {// Determine whether the player's experience is less than 30
Event.cancel (); // Cancel the event
Event.player.sendstatusMessage (format.red ("You need at least 30 levels to attack"+Entity.Definition.name); // Give prompts
}
}
});
Note 1: Why do you want to exclude all bosses of DIM = 7?
This is just an example. If a BOSS consists of multiple entities (for example: Naga, Nine -headed snakes), when the player level is not required, if the attack can directly cause the damage part (for example: the head), the error:java.lang.nullpointerexception.
For this situation, you can use Print for analysis. You can know that the definition of the physical objects corresponding to these bosses is the NULL value, so the seventh row of the above code is changed to the following: This is as follows:
if (! isnull (entity.definition) && entity.isboss)