Today we looked at how to manipulate entities by lighting them on fire and tossing them around.
Entities implement the Entity Class and many of its methods (functions) and properties (values) are exposed through ScriptCraft.
For example, the setFireTicks() method:
Sets the number of ticks the entity will be on fire.
Setting to 0 or less will extinguish the entity.
If we want to set an entity on fire, we can use its setFireTicks() method to do that programmatically. Let’s assume you want to make it rain cows from the sky, but it just feels incomplete. What if you lit them on fire?
The script below was the first of three shown in our March 21st (Week 5) workshop.
“Yes,” you say. “That’s all well and good, but you’re not actually throwing the cows. You’re just materializing them in mid air and letting them fall. How do I THROW something?”
Let’s go look at the Entity Class again. We can find a few different methods related to motion.
Each of these methods lets you give an entity a push. The moveEntity() method lets you set the force of the push in three vectors: X (across in front of you), Y (the up/down axis), and Z (toward or away from you). The setMotionX(), setMotionY, and setMotionZ() methods set the force on a single vector. So if you use setMotionY() like we did in the workshop, you toss your entity straight up into the air.
In the ScriptCraft plugins folder you’ll find spawn.js, which is the script that lets you spawn entities in the game with “jsp spawn [ENTITY TYPE]”. As we’ve explained, a JSP command is one available to any user and it has very limited functionality by design. We’re going to strip some of the bits you don’t need out of spawn.js and make it function you can call that not only spawns an entity, but returns a pointer to that entity.
The advantage of this is that you can assign that pointer to a variable when you create your entity, then use the variable to represent the entity for applying Entity Class methods.
To test this simply, run it in the command prompt within Minecraft.
/js var bob = subspawn('SHEEP',self);
The two parameters you pass from within the game are the entity type you want to spawn and the player at whose location it should be spawned (i.e. yourself, or “self”). Next thing you know, there’s a sheep, and it’s been assigned to the ‘bob’ variable. You can then access the sheep’s EntityClass methods via the ‘bob’ variable.
This will toss the sheep straight up in the air high enough that the fall will kill it. You could also use moveEntity().
That will apply force to the sheep on all three axes for one big hop.
Last, but not least, we have the Pigramid. It’s exactly what it sounds like, a pyramid made out of pigs. But as soon as the pigs were spawned, they started moving and the pyramid would collapse. So we revisit the events from last week.
The main functions of this are that it uses the events.entityMove() event’s own cancel() method to cancel all motion. This essentially freezes all the animals in the world until the code you registered to listen for the event is unregistered. We could have done a more elegant solution to just freeze the animals in the pyramid, but that’s more complex.
If you recall, all that’s needed to call this is:
/jsp pigramid ENTITYNAME
In the workshop, we made a pyramid of wolves, then a pyramid of sheep, and the wolves tore into the sheep. Not pretty, but hard not to look at.
Next week is demo week. Write your best mods!