Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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!

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.

  • Author

Sorry could you please explain this a bit further or maybe post some code?

 

Thanks,

Romejanic

Romejanic

 

Creator of Witch Hats, Explosive Chickens and Battlefield!

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.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.