ModderDojo: Exploding Villager or other NPC

I put this together as a simple example of how we can modify the Exploding Chickens mod that was  written in Java by CodeName_B for CoderDojo Athenry and then re-written in JavaScript by Kieran Coughlan:

https://cdathenry.wordpress.com/2015/04/03/scriptcraft-an-exploding-chickens-mod/

My version does not check that the player has damaged a chicken, but instead checks the entity’s name. If the entity is called “Donald” and you hit it, it will explode in 5 seconds! It does not matter whether Donald is a Villager, a pig, or whatever, and you can have multiple NPCs called Donald and it will work on all of them. (In case you didn’t know, an NPC is a non-player character.)

// Require events
require("events");

// Create a var to represent the bukkit type that represents a player
var bkPlayer = org.bukkit.entity.Player;


// The function which we will run when we load this module
var _loadMod = function()
{
 // Announce ourselves to the console
 console.log("Exploding Villager ScriptCraft Mod loading");

 // Tie our code into the event that fires every time one entity damages another
 events.entityDamageByEntity(_entityDamageByEntity);
}

// The code that we want to run each time one entity damages another
var _entityDamageByEntity = function(event)
{
 // Find out, from the event, who's getting damaged and who did the damage
 var damagedEntity = event.getEntity();
 var damagingEntity = event.getDamager();
 
 // If it's a NPC getting damaged by a player, game on...
 if (damagedEntity.getCustomName() === "Donald" && damagingEntity instanceof bkPlayer)
 {
 // Announce in the console that we've detected a player damaging an NPC called Donald
 console.log("Exploding Villager - a player damaged an NPC called Donald");
 
 // Schedule a task to run in five seconds (20 * 5 because the Minecraft clock ticks 20 times a second.
 server.scheduler.scheduleSyncDelayedTask(__plugin, function() 
 {
 // The original Exploding Chckens mod only triggered if its health went to 0 (killed), 
 // but let's comment that out so the first hit causes an explosion
 //if (damagedEntity.getHealth() - event.getDamage() <= 0)
 //{
 // Get the NPC's location
 var loc = damagedEntity.location;

 // Create an explosion at the chicken's location. 
 // A big one...
 loc.world.createExplosion(loc, 10.0);
 //}
 }, 20 * 5); // end of the command to schedule the task
 }
}

// Run this script as soon as the file's loaded

_loadMod();

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s