//OnAttacked

if (hiding)
{
	false;
}
else
{
	sound.playSoundUI("sound.nejironHit");
	entity.setHealth(entity.getHealth() - attackingDamage);
	false;	// So normal hurt sound doesn't play as well
}

//OnCreated

var hiding = true;			// Whether Nejiron is hiding or not
var yawMax = 20;			// Max yaw difference for hiding/emerging animation
var yawSpeed = 5;			// Yaw speed for hiding/emerging animation
var p = entity.getPosition();
var minRange = 0;			// Distance player can be before the Nejiron hides
var maxRange = 6;			// Distance player has to be for the Nejiron to appear
var scrubHeight = 1;		// Height of Nejiron when revealed. Only affects collision box
var explodeDelay = 75;		// Number of ticks before explosion
var explodeTime = explodeDelay;
var explodeRange = 5;		// Distance player has to be to get hit

// Create the model
var m = Model();
m.addBoxExpanded("Body", -4, -4, -4, 8, 8, 8, 0, 0, 2.0);
m.addBoxExpanded("Body2", -5, -7, -5, 10, 5, 6, 64, 0, 6.0);
m.addBoxExpanded("Leaves", -4, -5, -4, 8, 8, 8, 32, 0, 6.0);
m.y = 0.7;

// Assign texture
m.texture = "/models/mob/nejiron.png";

// Attach to this entity
m.attachedTo = entity;

//OnDeath

// creates the explosion and plays sound


for (var i=0; i<20; i++)
{
var dx = entity.position.x + ((Math.random() * 1.25) - 1);
var dy = entity.position.y + ((Math.random() * 1) - 0.75);
var dz = entity.position.z + ((Math.random() * 1.25) - 1);


effect.spawnParticle("smoke", dx, dy, dz, -0.25, 0.25, 0.25);

sound.playSoundUI("sound.nejironExplode");
}



for (var j=0; j<100; j++)
{
	var ex = entity.position.x + ((Math.random() * 9) - 4);
	var ey = entity.position.y + ((Math.random() * 4) - 2);
	var ez = entity.position.z + ((Math.random() * 9) - 4);

	effect.spawnParticle("smoke", ex, ey, ez, -0.1, 0.1, 0.1);


}

for (var i=0; i<20; i++)
{
	var cx = entity.position.x + ((Math.random() * 1.25) - 1);
	var cy = entity.position.y + ((Math.random() * 1) - 0.75);
	var cz = entity.position.z + ((Math.random() * 1.25) - 1);

	effect.spawnParticle("smoke", dx, dy, dz, 0.25, -0.25, 0.25);
}

for (var i=0; i<20; i++)
{
	var bx = entity.position.x + ((Math.random() * 1.25) - 1);
	var by = entity.position.y + ((Math.random() * 1) - 0.75);
	var bz = entity.position.z + ((Math.random() * 1.25) - 1);

	effect.spawnParticle("smoke", dx, dy, dz, 0.25, 0.25, -0.25);
}

// Remove entity
m.removeFromRendering();

//OnUpdate

entity.setPosition(p.x, p.y, p.z);	// Prevent Nejiron from moving

if (hiding)
{
	if (entity.height != 0.0)
	{
		entity.height = 0.0;
	}
	// Make model move under the floor and create "dirt" particles
	if (m.y > -0.6)
	{
		m.moveBy(0, -0.1, 0);
		m.rotateBy(yawSpeed, 0, 0);
		if (m.yaw >= yawMax)
		{
			yawSpeed *= -1;
		}
		else if(m.yaw <= -yawMax)
		{
			yawSpeed *= -1;
		}

		// Create random particles
		for (var i=0; i<10; i++)
		{
			var px = entity.position.x + ((Math.random() * 1.5) - 0.75);
			var py = entity.position.y + ((Math.random() * 0.25) - 0.125);
			var pz = entity.position.z + ((Math.random() * 1.5) - 0.75);
			var arg1 = Math.random() * 0.75;
			var arg2 = arg1 / 2.0;
			var arg3 = arg1 / 4.0;

			effect.spawnParticle("reddust", px, py, pz, arg1, arg2, arg3);
		}
	}
	else if (m.yaw != 0)
	{
		m.yaw = 0;
	}

	// See if player is in a certain range
	var ents = entity.getEntitiesWithinRange(maxRange);
	for (var i=0; i<ents.length; i++)
	{
		if (ents[i].getClassType() == "Player")
		{
			hiding = false;
		}
	}
	if (!hiding)
	{
		// Check if player is too close
		ents = entity.getEntitiesWithinRange(minRange);
		for (var i=0; i<ents.length; i++)
		{
			if (ents[i].getClassType() == "Player")
			{
				hiding = true;
			}
		}
	}
	// If still not hiding play a sound
	if (!hiding)
	{
		sound.playSound3D("sound.nejiron_appear", p.x, p.y, p.z);
	}
}
else
{
	if (entity.height != scrubHeight)
	{
		entity.height = scrubHeight;
	}
	// Make model appear from the ground
	if (m.y < 0.7)
	{
		m.moveBy(0, 0.1, 0);
		m.rotateBy(yawSpeed, 0, 0);
		if (m.yaw >= yawMax)
		{
			yawSpeed *= -1;
		}
		else if(m.yaw <= -yawMax)
		{
			yawSpeed *= -1;
		}

		// Create random particles
		for (var i=0; i<20; i++)
		{
			var px = entity.position.x + ((Math.random() * 1.5) - 0.75);
			var py = entity.position.y + ((Math.random() * 0.25) - 0.125);
			var pz = entity.position.z + ((Math.random() * 1.5) - 0.75);
			var arg1 = Math.random() * 0.75;
			var arg2 = arg1 / 2.0;
			var arg3 = arg1 / 4.0;

			effect.spawnParticle("reddust", px, py, pz, arg1, arg2, arg3);
		}
	}
	else if (m.yaw != 0)
	{
		m.yaw = 0;
	}
}
	// Face the player
	var pPos = player.getPosition();
	var angle = Math.atan2(p.z - pPos.z, p.x - pPos.x);
	angle = (angle / Math.PI) * 180;
	angle += 90;
	entity.setRotation(angle, entity.rotation.pitch);


	// Count down shot timer
	explodeDelay--;
	if (explodeDelay <= 0)
	//Make explosion
{
	ents = entity.getEntitiesWithinRange(explodeRange);
	for (var i=0; i<ents.length; i++)
	{
		if (ents[i].getClassType() == "Player")
		{

			// Whether player has shield out or not
			var curItem = player.inventory.getCurrentItem();
			var offItem = player.inventory.getOffhandItem();
			if ((curItem != null && curItem.itemID == 461) || (offItem != null && offItem.itemID == 461))
			{
				time.sleep(0.7);
				sound.playSoundUI("sound.shieldBlock");
				entity.setDead();
				break;
			}
			else
			{
				time.sleep(0.7);
				player.attackEntityFrom(player, 1);
				entity.setDead();
			}
		}	
	}
}