001    package net.minecraft.src;
002    
003    public class EntityAIAttackOnCollide extends EntityAIBase
004    {
005        World worldObj;
006        EntityLiving attacker;
007        EntityLiving entityTarget;
008    
009        /**
010         * An amount of decrementing ticks that allows the entity to attack once the tick reaches 0.
011         */
012        int attackTick;
013        float field_75440_e;
014        boolean field_75437_f;
015    
016        /** The PathEntity of our entity. */
017        PathEntity entityPathEntity;
018        Class classTarget;
019        private int field_75445_i;
020    
021        public EntityAIAttackOnCollide(EntityLiving par1EntityLiving, Class par2Class, float par3, boolean par4)
022        {
023            this(par1EntityLiving, par3, par4);
024            this.classTarget = par2Class;
025        }
026    
027        public EntityAIAttackOnCollide(EntityLiving par1EntityLiving, float par2, boolean par3)
028        {
029            this.attackTick = 0;
030            this.attacker = par1EntityLiving;
031            this.worldObj = par1EntityLiving.worldObj;
032            this.field_75440_e = par2;
033            this.field_75437_f = par3;
034            this.setMutexBits(3);
035        }
036    
037        /**
038         * Returns whether the EntityAIBase should begin execution.
039         */
040        public boolean shouldExecute()
041        {
042            EntityLiving var1 = this.attacker.getAttackTarget();
043    
044            if (var1 == null)
045            {
046                return false;
047            }
048            else if (this.classTarget != null && !this.classTarget.isAssignableFrom(var1.getClass()))
049            {
050                return false;
051            }
052            else
053            {
054                this.entityTarget = var1;
055                this.entityPathEntity = this.attacker.getNavigator().getPathToEntityLiving(this.entityTarget);
056                return this.entityPathEntity != null;
057            }
058        }
059    
060        /**
061         * Returns whether an in-progress EntityAIBase should continue executing
062         */
063        public boolean continueExecuting()
064        {
065            EntityLiving var1 = this.attacker.getAttackTarget();
066            return var1 == null ? false : (!this.entityTarget.isEntityAlive() ? false : (!this.field_75437_f ? !this.attacker.getNavigator().noPath() : this.attacker.isWithinHomeDistance(MathHelper.floor_double(this.entityTarget.posX), MathHelper.floor_double(this.entityTarget.posY), MathHelper.floor_double(this.entityTarget.posZ))));
067        }
068    
069        /**
070         * Execute a one shot task or start executing a continuous task
071         */
072        public void startExecuting()
073        {
074            this.attacker.getNavigator().setPath(this.entityPathEntity, this.field_75440_e);
075            this.field_75445_i = 0;
076        }
077    
078        /**
079         * Resets the task
080         */
081        public void resetTask()
082        {
083            this.entityTarget = null;
084            this.attacker.getNavigator().clearPathEntity();
085        }
086    
087        /**
088         * Updates the task
089         */
090        public void updateTask()
091        {
092            this.attacker.getLookHelper().setLookPositionWithEntity(this.entityTarget, 30.0F, 30.0F);
093    
094            if ((this.field_75437_f || this.attacker.getEntitySenses().canSee(this.entityTarget)) && --this.field_75445_i <= 0)
095            {
096                this.field_75445_i = 4 + this.attacker.getRNG().nextInt(7);
097                this.attacker.getNavigator().tryMoveToEntityLiving(this.entityTarget, this.field_75440_e);
098            }
099    
100            this.attackTick = Math.max(this.attackTick - 1, 0);
101            double var1 = (double)(this.attacker.width * 2.0F * this.attacker.width * 2.0F);
102    
103            if (this.attacker.getDistanceSq(this.entityTarget.posX, this.entityTarget.boundingBox.minY, this.entityTarget.posZ) <= var1)
104            {
105                if (this.attackTick <= 0)
106                {
107                    this.attackTick = 20;
108    
109                    if (this.attacker.getHeldItem() != null)
110                    {
111                        this.attacker.swingItem();
112                    }
113    
114                    this.attacker.attackEntityAsMob(this.entityTarget);
115                }
116            }
117        }
118    }