package game.ai; import java.util.Collections; import java.util.List; import com.google.common.base.Predicate; import game.entity.attributes.AttributeInstance; import game.entity.attributes.Attributes; import game.entity.types.EntityLiving; public class EntityAIFindEntityNearest extends EntityAIBase { private EntityLiving mob; private final Predicate field_179443_c; private final EntityAINearestAttackableTarget.Sorter field_179440_d; private EntityLiving target; private Class field_179439_f; public EntityAIFindEntityNearest(EntityLiving mobIn, Class p_i45884_2_) { this.mob = mobIn; this.field_179439_f = p_i45884_2_; // if (mobIn instanceof EntityCreature) // { // Log.CONFIG.warn("Nutze NearestAttackableTargetGoal.class für PathfinderMob-Mobs!"); // } this.field_179443_c = new Predicate() { public boolean apply(EntityLiving p_apply_1_) { double d0 = EntityAIFindEntityNearest.this.getFollowRange(); if (p_apply_1_.isSneaking()) { d0 *= 0.800000011920929D; } return (double)p_apply_1_.getDistanceToEntity(EntityAIFindEntityNearest.this.mob) > d0 ? false : EntityAITarget.isSuitableTarget(EntityAIFindEntityNearest.this.mob, p_apply_1_, true); } }; this.field_179440_d = new EntityAINearestAttackableTarget.Sorter(mobIn); } /** * Returns whether the EntityAIBase should begin execution. */ public boolean shouldExecute() { double d0 = this.getFollowRange(); List list = this.mob.worldObj.getEntitiesWithinAABB(this.field_179439_f, this.mob.getEntityBoundingBox().expand(d0, 4.0D, d0), this.field_179443_c); Collections.sort(list, this.field_179440_d); if (list.isEmpty()) { return false; } else { this.target = (EntityLiving)list.get(0); return true; } } /** * Returns whether an in-progress EntityAIBase should continue executing */ public boolean continueExecuting() { EntityLiving entitylivingbase = this.mob.getAttackTarget(); if (entitylivingbase == null) { return false; } else if (!entitylivingbase.isEntityAlive()) { return false; } else { double d0 = this.getFollowRange(); return this.mob.getDistanceSqToEntity(entitylivingbase) <= d0 * d0; // ? false : !(entitylivingbase.isPlayer()) || !((EntityNPCMP)entitylivingbase).creative; } } /** * Execute a one shot task or start executing a continuous task */ public void startExecuting() { this.mob.setAttackTarget(this.target); super.startExecuting(); } /** * Resets the task */ public void resetTask() { this.mob.setAttackTarget((EntityLiving)null); super.startExecuting(); } protected double getFollowRange() { AttributeInstance iattributeinstance = this.mob.getEntityAttribute(Attributes.FOLLOW_RANGE); return iattributeinstance == null ? 16.0D : iattributeinstance.getAttributeValue(); } }