Jump to content

[1.9][SOLVED]Overriding Vanilla Mobs AI


Recommended Posts

I have an item that when right clicked spawns a mob with my custom AI in a radius around the player. It's working perfectly except that it spawns the mob with its default AI. I've been testing on Zombies only, I debugged the code and it does step into the condition and execute all the removeTask code. I'm not really sure what do from here so any help would be appreciated.

@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand)
    {
	if(itemStackIn.getTagCompound().getInteger("MobCounter") > 0 && !worldIn.isRemote)
	{
		int mobCounter = itemStackIn.getTagCompound().getInteger("MobCounter");
		final int spawnRange = 4;
		--mobCounter;
		EntityMob entity = (EntityMob) EntityList.createEntityByName(itemStackIn.getTagCompound().getString("Mob"+Integer.toString(mobCounter)), worldIn);
		entity.setPosition((double)playerIn.getPosition().getX() + (worldIn.rand.nextDouble() - worldIn.rand.nextDouble()) * (double)spawnRange + 0.5D, (double)(playerIn.getPosition().getY() + worldIn.rand.nextInt(2)), (double)playerIn.getPosition().getZ() + (worldIn.rand.nextDouble() - worldIn.rand.nextDouble()) * (double)spawnRange + 0.5D);
		if(entity instanceof EntityZombie)
		{
			entity.tasks.removeTask(new EntityAIMoveThroughVillage(entity, 1.0D, false));
			entity.tasks.removeTask(new EntityAIHurtByTarget(entity, true, new Class[] {EntityPigZombie.class}));
			entity.tasks.removeTask(new EntityAINearestAttackableTarget(entity, EntityPlayer.class, true));
			entity.tasks.removeTask(new EntityAINearestAttackableTarget(entity, EntityVillager.class, false));
			entity.tasks.removeTask(new EntityAINearestAttackableTarget(entity, EntityIronGolem.class, true));
			entity.tasks.addTask(5, new EntityMobAiFollow(entity, playerIn, 1.0D,10.0F,2.0F)); //Custom AI Class
		}
		worldIn.spawnEntityInWorld(entity);
        itemStackIn.getTagCompound().setInteger("MobCounter", mobCounter);
	}
        return new ActionResult(EnumActionResult.PASS, itemStackIn);
    }

Link to comment
Share on other sites

I have an item that when right clicked spawns a mob with my custom AI in a radius around the player. It's working perfectly except that it spawns the mob with its default AI. I've been testing on Zombies only, I debugged the code and it does step into the condition and execute all the removeTask code. I'm not really sure what do from here so any help would be appreciated.

@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand)
    {
	if(itemStackIn.getTagCompound().getInteger("MobCounter") > 0 && !worldIn.isRemote)
	{
		int mobCounter = itemStackIn.getTagCompound().getInteger("MobCounter");
		final int spawnRange = 4;
		--mobCounter;
		EntityMob entity = (EntityMob) EntityList.createEntityByName(itemStackIn.getTagCompound().getString("Mob"+Integer.toString(mobCounter)), worldIn);
		entity.setPosition((double)playerIn.getPosition().getX() + (worldIn.rand.nextDouble() - worldIn.rand.nextDouble()) * (double)spawnRange + 0.5D, (double)(playerIn.getPosition().getY() + worldIn.rand.nextInt(2)), (double)playerIn.getPosition().getZ() + (worldIn.rand.nextDouble() - worldIn.rand.nextDouble()) * (double)spawnRange + 0.5D);
		if(entity instanceof EntityZombie)
		{
			entity.tasks.removeTask(new EntityAIMoveThroughVillage(entity, 1.0D, false));
			entity.tasks.removeTask(new EntityAIHurtByTarget(entity, true, new Class[] {EntityPigZombie.class}));
			entity.tasks.removeTask(new EntityAINearestAttackableTarget(entity, EntityPlayer.class, true));
			entity.tasks.removeTask(new EntityAINearestAttackableTarget(entity, EntityVillager.class, false));
			entity.tasks.removeTask(new EntityAINearestAttackableTarget(entity, EntityIronGolem.class, true));
			entity.tasks.addTask(5, new EntityMobAiFollow(entity, playerIn, 1.0D,10.0F,2.0F)); //Custom AI Class
		}
		worldIn.spawnEntityInWorld(entity);
        itemStackIn.getTagCompound().setInteger("MobCounter", mobCounter);
	}
        return new ActionResult(EnumActionResult.PASS, itemStackIn);
    }

Link to comment
Share on other sites

removeTask(new Task())

isn't going to do anything.  It won't find the newly created object in the array, so it won't remove it from the array.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

removeTask(new Task())

isn't going to do anything.  It won't find the newly created object in the array, so it won't remove it from the array.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I kind of assumed this so I edited the code like this but it's still not working.

public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand)
    {
	if(itemStackIn.getTagCompound().getInteger("MobCounter") > 0 && !worldIn.isRemote)
	{
		int mobCounter = itemStackIn.getTagCompound().getInteger("MobCounter");
		final int spawnRange = 4;
		--mobCounter;
		EntityMob entity = (EntityMob) EntityList.createEntityByName(itemStackIn.getTagCompound().getString("Mob"+Integer.toString(mobCounter)), worldIn);
		entity.setPosition((double)playerIn.getPosition().getX() + (worldIn.rand.nextDouble() - worldIn.rand.nextDouble()) * (double)spawnRange + 0.5D, (double)(playerIn.getPosition().getY() + worldIn.rand.nextInt(2)), (double)playerIn.getPosition().getZ() + (worldIn.rand.nextDouble() - worldIn.rand.nextDouble()) * (double)spawnRange + 0.5D);
		if(entity instanceof EntityZombie)
		{
			for(Object task : entity.tasks.taskEntries.toArray())
			{
				 EntityAIBase ai = ((EntityAITaskEntry) task).action;
				 if(ai instanceof EntityAIMoveThroughVillage || ai instanceof EntityAIHurtByTarget || ai instanceof EntityAINearestAttackableTarget)
					 entity.tasks.removeTask(ai);		 
			}
			entity.tasks.addTask(5, new EntityMobAiFollow(entity, playerIn, 1.0D,10.0F,2.0F));
		}
		worldIn.spawnEntityInWorld(entity);
        itemStackIn.getTagCompound().setInteger("MobCounter", mobCounter);
	}

Link to comment
Share on other sites

I kind of assumed this so I edited the code like this but it's still not working.

public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand)
    {
	if(itemStackIn.getTagCompound().getInteger("MobCounter") > 0 && !worldIn.isRemote)
	{
		int mobCounter = itemStackIn.getTagCompound().getInteger("MobCounter");
		final int spawnRange = 4;
		--mobCounter;
		EntityMob entity = (EntityMob) EntityList.createEntityByName(itemStackIn.getTagCompound().getString("Mob"+Integer.toString(mobCounter)), worldIn);
		entity.setPosition((double)playerIn.getPosition().getX() + (worldIn.rand.nextDouble() - worldIn.rand.nextDouble()) * (double)spawnRange + 0.5D, (double)(playerIn.getPosition().getY() + worldIn.rand.nextInt(2)), (double)playerIn.getPosition().getZ() + (worldIn.rand.nextDouble() - worldIn.rand.nextDouble()) * (double)spawnRange + 0.5D);
		if(entity instanceof EntityZombie)
		{
			for(Object task : entity.tasks.taskEntries.toArray())
			{
				 EntityAIBase ai = ((EntityAITaskEntry) task).action;
				 if(ai instanceof EntityAIMoveThroughVillage || ai instanceof EntityAIHurtByTarget || ai instanceof EntityAINearestAttackableTarget)
					 entity.tasks.removeTask(ai);		 
			}
			entity.tasks.addTask(5, new EntityMobAiFollow(entity, playerIn, 1.0D,10.0F,2.0F));
		}
		worldIn.spawnEntityInWorld(entity);
        itemStackIn.getTagCompound().setInteger("MobCounter", mobCounter);
	}

Link to comment
Share on other sites

Be careful when looping and removing at the same time. Some of these objects will put you in the wrong place so that you miss something. Check the forum for discussions of when to use an iterator etc.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

Be careful when looping and removing at the same time. Some of these objects will put you in the wrong place so that you miss something. Check the forum for discussions of when to use an iterator etc.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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