Jump to content

Edited EntityAINearestAttackableTarget, zombies now standaround like...zombies


Gnomorian

Recommended Posts

Hi, this is a follow up to this thread.

 

so the setting the setTarget(null) and setRevengeTarget(null) yielded no results, so i decided to reperpose the EntityAINearestAttackableTarget task. i essentualy changed the shouldExecute() method to check if a player is in the vicinity and is wearing a ceretain helmet. if so re-search the area for hostile mobs and players without the helmet. then set the target to the closest as per useual.

 

now this works perfectly with creepers, skeletons. but with zombies and wither skeletons they just stand there doing nothing even if hostile mobs are around. i have a demo of what happens

(the end shows the zombies)

 

i thought it may be the way i am replacing the tasks with the zombie, as it has 2 instances of EntityAINearestAttackableTarget (one for players, one for villagers), but the same issue is happening for wither skeletons, or because they behave normaly without the helmet, so that means it replaced correctly.

 

does anyone know what i have done wrong, or overlooked.

source code is here, you can download a compiled mod to test it yourself here.

 

Event class

 

@SubscribeEvent
public void onJoinEvent(EntityJoinWorldEvent event)
{
	if(event.entity instanceof EntityZombie)
	{
		EntityZombie zomb = (EntityZombie)event.entity;
		List list = zomb.targetTasks.taskEntries;

		//zomb.targetTasks.addTask(1, new WYEMEntityAIOwnerHurtTarget(zomb, WYEMItem.zombieCrown));
		/*
		 * this exists because there are 2 instances of the object we want to replace, but the 
		 * one we want to replace was added first, this is used to stop the replacement of the second.
		 */
		//TODO: marker here so i know where the trouble is if the list isnt sorted the way i like.
		int foundClasses = 0;
		for(int i = 0; i < zomb.targetTasks.taskEntries.size(); i++)
		{
			if(list.get(i) instanceof EntityAITaskEntry)
			{
				if(((EntityAITaskEntry)list.get(i)).action instanceof EntityAINearestAttackableTarget)
				{
					foundClasses++;
					if(foundClasses < 2 )
					{
						((EntityAITaskEntry)list.get(i)).action = new WYEMEntityAINearestAttackableTarget(zomb, EntityPlayer.class, 0, true, WYEMItem.zombieCrown);
					}
				}
			}
		}
	}
	if(event.entity instanceof EntityCreeper)
	{
		EntityCreeper crep = (EntityCreeper)event.entity;
		//crep.targetTasks.addTask(1, new WYEMEntityAIOwnerHurtTarget(crep, WYEMItem.creeperCrown));
		List list = crep.targetTasks.taskEntries;
		for(int i = 0; i < crep.targetTasks.taskEntries.size(); i++)
		{
			if(list.get(i) instanceof EntityAITaskEntry)
			{
				if(((EntityAITaskEntry)list.get(i)).action instanceof EntityAINearestAttackableTarget)
				{
					((EntityAITaskEntry)list.get(i)).action = new WYEMEntityAINearestAttackableTarget(crep, EntityPlayer.class, 0, true, WYEMItem.creeperCrown);
				}
			}
			if(list.get(i) instanceof EntityAICreeperSwell)
			{
				if(((EntityAITaskEntry)list.get(i)).action instanceof EntityAICreeperSwell)
				{
					((EntityAITaskEntry)list.get(i)).action = new WYEMEntityAICreeperSwell(crep);
				}
			}
		}
	}
	/* if wither skeleton */
	if(event.entity instanceof EntitySkeleton && ((EntitySkeleton)event.entity).getSkeletonType() == 1)
	{
		EntitySkeleton skel = (EntitySkeleton)event.entity;
		List list = skel.targetTasks.taskEntries;
		for(int i = 0; i < skel.targetTasks.taskEntries.size(); i++)
		{
			if(list.get(i) instanceof EntityAITaskEntry)
			{
				if(((EntityAITaskEntry)list.get(i)).action instanceof EntityAINearestAttackableTarget)
				{
					((EntityAITaskEntry)list.get(i)).action = new WYEMEntityAINearestAttackableTarget(skel, EntityPlayer.class, 0, true, WYEMItem.witherCrown);
				}
			}
		}
	}
	/* if regular skeleton */
	if(event.entity instanceof EntitySkeleton && ((EntitySkeleton)event.entity).getSkeletonType() == 0)
	{
		EntitySkeleton skel = (EntitySkeleton)event.entity;
		//skel.targetTasks.addTask(1, new WYEMEntityAIOwnerHurtTarget(skel, WYEMItem.skeletonCrown));
		List list = skel.targetTasks.taskEntries;
		for(int i = 0; i < skel.targetTasks.taskEntries.size(); i++)
		{
			if(list.get(i) instanceof EntityAITaskEntry)
			{
				if(((EntityAITaskEntry)list.get(i)).action instanceof EntityAINearestAttackableTarget)
				{
					((EntityAITaskEntry)list.get(i)).action = new WYEMEntityAINearestAttackableTarget(skel, EntityPlayer.class, 0, true, WYEMItem.skeletonCrown);
				}
			}
		}
	}
}

 

Modified EntityAINearestAttackableTarget class.

 

public class WYEMEntityAINearestAttackableTarget extends EntityAITarget
{
private EntityCreature owner;
private Item crown;
    private final Class targetClass;
    private final int targetChance;
    /** Instance of EntityAINearestAttackableTargetSorter. */
    private final EntityAINearestAttackableTarget.Sorter theNearestAttackableTargetSorter;
    /**
     * This filter is applied to the Entity search.  Only matching entities will be targetted.  (null -> no
     * restrictions)
     */
    private final IEntitySelector targetEntitySelector;
    private EntityLivingBase targetEntity;

    public WYEMEntityAINearestAttackableTarget(EntityCreature p_i1663_1_, Class p_i1663_2_, int p_i1663_3_, boolean p_i1663_4_, Item crown)
    {
        this(p_i1663_1_, p_i1663_2_, p_i1663_3_, p_i1663_4_, false);
        this.crown = crown;
    }

    public WYEMEntityAINearestAttackableTarget(EntityCreature p_i1664_1_, Class p_i1664_2_, int p_i1664_3_, boolean p_i1664_4_, boolean p_i1664_5_)
    {
        this(p_i1664_1_, p_i1664_2_, p_i1664_3_, p_i1664_4_, p_i1664_5_, (IEntitySelector)null);
    }

    public WYEMEntityAINearestAttackableTarget(EntityCreature p_i1665_1_, Class p_i1665_2_, int p_i1665_3_, boolean p_i1665_4_, boolean p_i1665_5_, final IEntitySelector p_i1665_6_)
    {
        super(p_i1665_1_, p_i1665_4_, p_i1665_5_);
        this.owner = p_i1665_1_;
        this.targetClass = p_i1665_2_;
        this.targetChance = p_i1665_3_;
        this.theNearestAttackableTargetSorter = new EntityAINearestAttackableTarget.Sorter(p_i1665_1_);
        this.setMutexBits(1);
        this.targetEntitySelector = new IEntitySelector()
        {
            private static final String __OBFID = "CL_00001621";
            /**
             * Return whether the specified entity is applicable to this filter.
             */
            public boolean isEntityApplicable(Entity p_82704_1_)
            {
                return !(p_82704_1_ instanceof EntityLivingBase) ? false : (p_i1665_6_ != null && !p_i1665_6_.isEntityApplicable(p_82704_1_) ? false : WYEMEntityAINearestAttackableTarget.this.isSuitableTarget((EntityLivingBase)p_82704_1_, false));
            }
        };
    }

    /**
     * Returns whether the EntityAIBase should begin execution.
     */
    public boolean shouldExecute()
    {
        if (this.targetChance > 0 && this.taskOwner.getRNG().nextInt(this.targetChance) != 0)
        {
            return false;
        }
        else
        {
            double d0 = this.getTargetDistance();
            List list = this.taskOwner.worldObj.selectEntitiesWithinAABB(this.targetClass, this.taskOwner.boundingBox.expand(d0, 4.0D, d0), this.targetEntitySelector);
            Collections.sort(list, this.theNearestAttackableTargetSorter);
            if (list.isEmpty())
            {
                return false;
            }
            else
            {
            	for(Object entities : list)
            	{
            		if(entities instanceof EntityPlayer)
            		{
            			EntityPlayer player = (EntityPlayer)entities;
            			ItemStack itemstack = player.inventory.armorInventory[3];
            			if(itemstack != null && itemstack.getItem() == this.crown)
            			{
            				//list.remove(player);
            				List toAttack = getCrownsEnemies(list);
            				if(toAttack.isEmpty())
            				{
            					return false;
            				}
            				this.targetEntity = (EntityLivingBase)toAttack.get(0);
            				return true;
            			}
            		}
            	}
            	if(list.size() > 0)
            	{
            		this.targetEntity = (EntityLivingBase)list.get(0);
            		return true;
            	}
            	return false;
            }
        }
    }

    /**
     * Execute a one shot task or start executing a continuous task
     */
    public void startExecuting()
    {
    	if(this.targetEntity != null && this.targetEntity instanceof EntityPlayer &&((EntityPlayer)this.targetEntity).inventory.armorInventory[3] != null && ((EntityPlayer)this.targetEntity).inventory.armorInventory[3].getItem() instanceof WYEMItemCrown && ((EntityPlayer)this.targetEntity).inventory.armorInventory[3].getItem() == this.crown)
    	{
    		this.taskOwner.setAttackTarget(null);
    	}
        this.taskOwner.setAttackTarget(this.targetEntity);
        super.startExecuting();
    }

    public static class Sorter implements Comparator
        {
            private final Entity theEntity;
            
            public Sorter(Entity p_i1662_1_)
            {
                this.theEntity = p_i1662_1_;
            }

            public int compare(Entity p_compare_1_, Entity p_compare_2_)
            {
                double d0 = this.theEntity.getDistanceSqToEntity(p_compare_1_);
                double d1 = this.theEntity.getDistanceSqToEntity(p_compare_2_);
                return d0 < d1 ? -1 : (d0 > d1 ? 1 : 0);
            }

            public int compare(Object p_compare_1_, Object p_compare_2_)
            {
                return this.compare((Entity)p_compare_1_, (Entity)p_compare_2_);
            }
        }
    private List getCrownsEnemies(List list)
    {
        double dis = this.getTargetDistance();
    	list = this.taskOwner.worldObj.getEntitiesWithinAABBExcludingEntity(this.taskOwner, this.taskOwner.boundingBox.expand(dis, 4.0D, dis));
    	Iterator itr = list.iterator();
    	while(itr.hasNext())
    	{
    		Object entity = itr.next();
    		if(entity instanceof EntityPlayer)
    		{
    			if(((EntityPlayer)entity).inventory.armorInventory[3] != null && ((EntityPlayer)entity).inventory.armorInventory[3].getItem() == this.crown)
    			{
    				itr.remove();
    			}
    		}
    		else if(entity instanceof EntityAnimal)
    		{
    			itr.remove();
    		}
    		else if(entity instanceof EntityVillager)
    		{
    			itr.remove();
    		}
    		else if(!(entity instanceof EntityLivingBase))
    		{
    			itr.remove();
    		}
    	}
    	Collections.sort(list, this.theNearestAttackableTargetSorter);
    	return list;
    }
}

 

<iframe src="http://widget.mcf.li/mc-mods/minecraft/225523-gnomgnoms-utils" width="100%" style="border: none;"></iframe>

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.