Event (Events) Summoning entity -the preliminary use of array and collection, circulation and traversal [CRT] Crafttweak

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

Less nonsense, code on the code!

 Import Crafttweaker.events.ieventmanager;

Import Crafttweaker.event.playeruseHoeEvent; // Import event package, the "PlayeruseHoeEvent" is located in the official English Wiki: vanilla/events/playeruseHoeEvent;

Import crafttweaker.World.iworld;

Import craftweaker.World.iblockpos; // The world package is used to obtain world information and is used in this article to obtain player location information;

// Import Crafttweaker.Item.INGREDIENT; // useless, just import it when the type is needed

Import crafttweaker.entity.ientity;

Import Crafttweaker.entity.ientityDefinition; // This entity package is used to define the entity, and it will be described in detail below;

Import mods.ctUtils.utils.math; // Import mathematical packages, if there is no chance of demand, it can be ignored;

events.onplayerusehoe (function (event as crafttweaker.event.playeruseHeEvent) {// event "When players use hoe",, when players use hoe, ", when players use hoe",, when players use hoe, ", when players use hoe,", when players use hoe, ", when players use hoe,", when players use, ", when players use hoe,", when players use hoe, ", when players use hoe,", when players use hoe, ", when players use,", when players use hoe, ", when players use hoe,",, when players use hoe, ", when players use hoe,", when players use hoe, ", when players use hoe,", when players use hoe, ", when players use hoe,", when players use hoe, ", when players use hoe",, when players use hoe, ", when players use hoe,", when players use hoe, "," when players use hoe ","

VAR MOBENTITY = EVENT.WORLD.GetentIes (); // Define the collection of all entities from the world, and make the collection represent this entity set; please note that the entity you can get is a collection;

Var p = event.player.position; // Get the location of the player to guide the creature position in the following

Var w = event.player.World; // Please see the comment area

VAR A As IENTITYDEFINITION [] = []; // Define "A" as a collection, and its type is "IENTITYDEFINITION". After adding [] after "IENTITYDEFINITION", it is expressed as a collection of this type;

Var x = math.random (); // Define "X" as a random number of 0 ~ 1;

For MOBENTITYDEFINITION in Mobentity {// for loop, the mobile method, such as ; Mobentity is a entity collection obtained from the world defined above;

if (! Isnull (mobentityDefinition.Definition) && MobentityDefinition.Definition.id == .id) {// Is the "entity" specified/target/specific Gas; A+= MobentityDefinition.definition; // If if IF determines that "entity" is a specified/target/specific entity, put it into the "A" defined before;

}

}

If (a.Length == 0) {//a.length means the number of "elements" in the "A" collection; here is the determination of the number of "elements" to be 0, then continue;

If (x <0.1) {// The "X" belongs to the random number defined above, where the probability is judged; x <0.1 is 10 %;

if (! Event.World.remote) {// This judgment guarantee event is only running on the server. Please note that if there is no such judgment, two creatures will be generated, one is a fake creature; please refer to: https: //CRT.IMC.WIKI/zen/advanced/event-OverView/tips.html

.spawentity (w, p); // Get the dimension and location information of the player's locality above.

}

}

}

});

Okay, I believe you can't wait, go and try it

Some broken thoughts:


在很多教程中,IEntity和IEntityDefinition两者的差别并未完全指出,借用youyi大佬的话,其实IEntity是一堆生物,IEntityDefinition是一只生物,就是这样......大概

When we get a collection of "all entities in the world", how can we judge that "the entity I want" is not inside?

cycle.

The answer is the cycle for I in [1,2,3], which will "substitute" i in the collection of I, in the above example, this meansAt this time, judge at this time, if (if) "inspire" to find equal (==), then put it in a new collection. In this way, when the FOR loop is rounded, all eligible objects will be selected.Putting into a new collection means how many target entities in the world have.

For the above if (! Isnull (mobentityDefinition.Definition) && MobentityDefinition.Definition.id == .id), first isnull () is used to determine whether the things in the brackets are in the brackets For empty, if it is emptyReturning True, returning True indicates that if the IF continues to judge the next step, and the next step is to be hollow == .id, obviously unreasonable, so the number of empty pointers (NullPointerexception);

Therefore, you should return to FALSE, so use it! Take the opposite, that is,! Isnull (), it means that if it is empty, it returns false;

&& means harmony;

The difference between equal to the number is: = indicate the assignment, == represents matching;

Finally, I would like to thank Youyi for the help and the help of many groups of friends (the ID is too outrageous, it is strange to fight), and thank you for the enlightenment of the big brother of station B (he learned a lot from his small tutorial for changingCome (laughs))

If you have any errors, please point out ~ If you have any questions, please ask ~ (=  ̄ ω ̄ =)