JavaScript Variables, Functions, and Arguments Part II: Sheep Chucking in Minecraft

This is a sheep in Minecraft.

A sheep in minecraft

In Minecraft, every block, every animal, every monster, and every NPC is an “object.” Basically an object is a package of information and functions that the program it’s in can use.

Remember how we said variables are buckets? We can put a sheep object in a variable and then use that variable’s name to find out the sheep’s information or use its functions.

So let’s put a sheep object into the world. The subspawn function is in the server I provide for download.

JavaScript Code to spawn a sheep.

What we typed was:
/js var bob = subspawn('sheep', self);

Let’s break it down:
/js tells the server to run this as JavaScript

var bob tells it to create a bucket named “bob”.

bob = subspawn('sheep', self); tells it to run the “subspawn” function, giving it the arguments ‘sheep’ (the kind of animal) and self (a variable containing object representing yourself). Note that self isn’t quotes, because it’s being used as a variable, not as a word.

Thhe sheep object is spawned into your Minecraft world and its object is put in the bob bucket you created.

The JavaScript-Spawned Minecraft Sheep

So basically bob is a bucket that contains the sheep object you just spawned. Now, as you can see, the sheep isn’t actually in a bucket. The bucket is an idea, or an “abstraction” of the space in the computer’s memory where the sheep object exists.

What can we do with bob? Bob has functions…

/js bob.setRider(bob); makes the sheep ride itself. That will make the sheep freeze in place.

/js bob.dismount(); makes whoever or whatever is riding bob get off, and your frozen sheep is unfrozen.

/js bob.destroy(); does not send bob on a rampage. It destroys bob and takes it out of the world.

/js bob.setFireTicks(500); sets bob on fire for 500 ticks of the game clock. This might kill bob.

/js bob.moveEntity(0.0, 1.3, 0.5); Will throw bob away from you. Each number is a level of force. The first goes from left to right in front of you. The second is up and down force. The third is away from you or toward you. So the arguments of (0.0, 1.3, 0.5) throw bob up and away from you.

Try some of these functions for bob.

See what negative numbers do in the moveEntity function.

And if you accidentally kill your sheep, /js var bob = subspawn('sheep', self); will spawn a new one.

Back to the Main Minecraft Modding Menu

Leave a Reply

Your email address will not be published. Required fields are marked *

*