Skip to content

Seattle CoderDojo

A Free Tech Meetup for Kids

Menu
  • 2023 Reboot
  • Teaching Kids To Code
  • Volunteers
  • Frequently Asked Questions
Menu

Minecraft Week 5 CheatSheet

Posted on March 21, 2015March 28, 2015 by Greg Bulmash

Today we looked at how to manipulate entities by lighting them on fire and tossing them around.

An entity is one of the NPCs (non-player characters) in Minecraft, like a wolf or a pig or a villager. While there is a lot of good information in the ScriptCraft API Reference, what ScriptCraft does at its core is expose the Minecraft APIs to JavaScript. Those are documented in the CanaryMod API reference.

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?

cowrain.js

The script below was the first of three shown in our March 21st (Week 5) workshop.

//cowrain.js
//setting up the entity list (borrowed from Spawn.js)
var entities = [];
var entityType = null;
if (__plugin.canary){  
  entityType = Packages.net.canarymod.api.entity.EntityType;
}else {
  entityType = org.bukkit.entity.EntityType;
}
var entitytypes = entityType.values();
for ( var t in entitytypes ) {
  if ( entitytypes[t] && entitytypes[t].ordinal ) { 
    entities.push(entitytypes[t].name());
  }
}

var Canary = Packages.net.canarymod.Canary;

var countcows = 0;
var thisdrone;

function cowrain(params, player){
    //set up the drone, raise it up 40 blocks, and set a checkpoint
    thisdrone = new Drone(player.location);
    thisdrone.up(40);
    thisdrone.chkpt('first');
    //set a randomly long time between cows and call the dropper
    tov = setTimeout(dropper, intervaler());
}


function dropper(){
    //control function
    countcows++;
    //move the drone to the checkpoint 40 blocks above you
    thisdrone.move('first');
    //move the drone to a random location within 13 blocks
    mover();
    //create the cow and set it on fire
    spawn('COW',thisdrone.getLocation());
    //set a randomly long time to wait and then another cow
    if(countcows < 300) tov = setTimeout(dropper, intervaler());
}

function mover(){
    //generate random movement and turn values
    var turn = Math.round(Math.random() * 3);
    var left = genXY();
    var fwd = genXY();
    //apply the random movement and turn values to the drone
    thisdrone.turn(turn);
    thisdrone.left(left);
    thisdrone.fwd(fwd);
}


function genXY(){
    //returns a random integer between -13 and 13
    var isneg = Math.round(Math.random() * 1);
    var move = Math.floor(Math.random() * 13);
    if(isneg === 1) move = move * -1;
    return move;
}

function intervaler(){
    //sets the value of the random interval between cows
    var interval = Math.floor(Math.random() * 200);
    return interval;
}    
    

function spawn(type, location){
    //create a new Cow entity
    var entity = Canary.factory().entityFactory.newEntity(entityType[type], location);
    //bring it into the world
    entity.spawn();
    //set it on fire
    entity.setFireTicks(500);
}
//create the command so it can be called with "/jsp cowrain"
command( cowrain );

"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.

  • moveEntity()
  • setMotionX()
  • setMotionY()
  • setMotionZ()

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.

Subspawn.js

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.

//sets up entity list and Canary variable (borrowed from spawn.js)
var entities = [];  
var entityType = null;
entityType = Packages.net.canarymod.api.entity.EntityType;
var entitytypes = entityType.values();
for ( var t in entitytypes ) {
    if ( entitytypes[t] && entitytypes[t].ordinal ) { 
        entities.push(entitytypes[t].name());
    }
}
var Canary = Packages.net.canarymod.Canary;

// instead of using command ( subspawn ) to create a "jsp" command
// we export the function for use in Scriptcraft by other functions
exports.subspawn = function(type, sender){
    // checks to make sure type is in the entity list and sets it to "PIG" if not.
    if(entities.indexOf(type) == -1) type = "PIG"; 

    var entity = Canary.factory().entityFactory.newEntity(entityType[type], sender.getLocation());
    entity.spawn();
    return entity;
}

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.

/js bob.setMotionY(2)

This will toss the sheep straight up in the air high enough that the fall will kill it. You could also use moveEntity().

/js bob.moveEntity(1.2,1.2,.8);

That will apply force to the sheep on all three axes for one big hop.

pigramid.js

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.

//setting up the entity list & Canary object (borrowed from Spawn.js)
var entities = [];
var entityType = null;
entityType = Packages.net.canarymod.api.entity.EntityType;
var entitytypes = entityType.values();
for ( var t in entitytypes ) {
    if ( entitytypes[t] && entitytypes[t].ordinal ) { 
        entities.push(entitytypes[t].name());
    }
}
var Canary = Packages.net.canarymod.Canary;

//setting up a global var to contain the hook between functions
var hook;

function pigramid (params, sender){
    //freeze all entity motion by setting a listener on the move event that cancels it every time it tries to happen.
    hook = events.entityMove(function(evt, cancel){
        cancel();
    });

    // Get the type of animal, defaulting to PIG, AVOID CHICKENS - they crash the server
    type = (params.length > 0) ? params[0].toUpperCase() : "PIG"; // ternary, defaults to PIG if there are no parameters
    if(entities.indexOf(type) == -1) type = "PIG"; // checks to make sure type is in the entity list and sets it to "PIG" if not.

    //set offsets
    var width = 7; // a width of 7 creates 84 entities in a 4-level pyramid.
  
    //Create DRONE to make location easier
    var drone = new Drone(sender.location); 

    drone.fwd(2); // move it a smidge away from the player
    
    //create checkpoint
    drone.chkpt('home');
    //set a loop that creates a series of smaller and smaller layers
    //moving inward each time
    for(var i = 0; i < width; i += 2){
        //positions the layer
        drone.move('home');
        drone.fwd(i/2);
        drone.right(i/2);
        drone.up(i/2);
        //make the layer
        makeLayer( (width-i) , type, drone); 
    }

    //Pigramid has been created, set timer to unfreeze motion in 8 seconds
    var once = setTimeout(unhook, 8000);
} 
 
function makeLayer(width, type, drone){
    var sideto = "left";
    //loop to make all the lines
    for(b = 0; b < width; b++){
        /    oggle right or left turn
        if(sideto === "left"){
            sideto = "right";
        } else {
            sideto = "left";
        }
    //nesting a loop to make a line
        for(x = 0; x < width; x++){
            spawn(type, drone.getLocation());
            drone.fwd(1);
        }
    //move right/left, turn around, fwd 1
        eval("drone." + sideto + "(1)");
        drone.turn(2);
        drone.fwd(1);
    }    
}    

function spawn(type, location){
    var entity = Canary.factory().entityFactory.newEntity(entityType[type], location);
    entity.spawn();
    //entity.setFireTicks(500);
}

function unhook(){    
    hook.unregister();
}

command ( pigramid );

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!

Upcoming Events

  • CANCELLED: SEATTLE CODERDOJO – March 7, 2020

  • CANCELLED: SEATTLE CODERDOJO – March 14, 2020

  • CANCELLED: SEATTLE CODERDOJO – March 21, 2020

Get News and New Ticket Notices

Join our mailing list. We not only won’t spam you, but you’ll probably complain we don’t mail you enough.

Support Seattle CoderDojo

Make a $5 or $10 donation - not tax deductible.
To make a larger donation ($20 or more) that IS tax deductible, follow this link.
©2023 Seattle CoderDojo | Design: Newspaperly WordPress Theme