Jump to content

Making mob scared of players with certain item


Romejanic

Recommended Posts

Hello everyone!

 

I am developing the mob and I have a mob. It attacks the player properly and just how I want it to, but how can I make it avoid the player when holding a certain item. I have an item which the mob drops called ectoplasm. I want the mob to avoid players who are holding ectoplasm. How do I do this? This is my code so far:

 

	public EntityGhost(World par1World) {

	super(par1World);

	this.tasks.addTask(1, new EntityAIWander(this, 1.0d));
	this.tasks.addTask(2, new EntityAISwimming(this));
	this.tasks.addTask(3, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
	this.tasks.addTask(4, new EntityAIAttackOnCollide(this, EntityPlayer.class, 1.0D, false));
	this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));
	this.targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 0, true));

	this.dataWatcher.updateObject(6, 300f);

	isAngry = false;

}

public void onLivingUpdate()
{

	if(!(findPlayerToAttack() == null)) {

		isAngry = true;


		// This is the code for the scared of.
					if(((EntityPlayer) findPlayerToAttack()).getCurrentEquippedItem() == new ItemStack(BetterMinecraft.ectoplasm, 1)) {

			this.tasks.removeTask((EntityAIBase)tasks.taskEntries.get(4));
	        this.tasks.addTask(4, new EntityAIAvoidEntity(this, EntityPlayer.class, 8.0F, 0.6D, 0.6D));

		}
		else {

			this.tasks.removeTask((EntityAIBase)tasks.taskEntries.get(4));
	        this.tasks.addTask(4, new EntityAIAttackOnCollide(this, EntityPlayer.class, 1.0D, false));

		}
	}
	else {

		isAngry = false;

	}

	if (this.worldObj.isDaytime() && !this.worldObj.isRemote)
	{
		float f = this.getBrightness(1.0F);

		if (f > 0.5F && this.rand.nextFloat() * 30.0F < (f - 0.4F) * 2.0F && this.worldObj.canBlockSeeTheSky(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.posY), MathHelper.floor_double(this.posZ)))
		{
			boolean flag = true;
			if (flag)
			{
				isDaytime = true;
			}
		}
		else {

			isDaytime = false;

		}
	}

	super.onLivingUpdate();
}

 

What I have tried to do is every time the target player is holding ectoplasm, the AI for attack is removed and the AI for flee is added. And when ectoplasm is unequipped, it attacks the player again.

 

EDIT: Instead of calling new EntityAIBase() when removing the AI, I tried this:

 

tasks.removeTask((EntityAIBase)tasks.taskEntries.get(4));

 

But this crashes the game when the mob spawns.

 

Does anyone know what I need to do here? Any help is greatly appreciated.

 

Thanks,

Romejanic

Romejanic

 

Creator of Witch Hats, Explosive Chickens and Battlefield!

Link to comment
Share on other sites

It's probably better to not add and remove tasks on the fly. Instead I'd just add both and on their shouldExecute/continueExecuting functions check if the attack target is valid, a player, and if it's holding the ectoplasm. If so return true or false depending on if that behavior should start or end.

Link to comment
Share on other sites

Remove all the code in onLivingUpdate and just add the EntityAIAvoidEntity behaviour to the constructor like the other tasks.

 

Create your own custom versions of the Avoid and Attack behaviours that are basically copies except for an additional check in their shouldExecute/continueExecuting functions to see if their .getAttackTarget() is a player holding the ectoplasm, then return true or false depending on if that behaviour should fire or stop executing while the player is holding that item.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.