    001package net.minecraft.entity.monster;
    002
    003import net.minecraft.enchantment.EnchantmentHelper;
    004import net.minecraft.enchantment.EnchantmentThorns;
    005import net.minecraft.entity.Entity;
    006import net.minecraft.entity.EntityCreature;
    007import net.minecraft.entity.EntityLiving;
    008import net.minecraft.entity.player.EntityPlayer;
    009import net.minecraft.potion.Potion;
    010import net.minecraft.util.DamageSource;
    011import net.minecraft.util.MathHelper;
    012import net.minecraft.world.EnumSkyBlock;
    013import net.minecraft.world.World;
    014
    015public abstract class EntityMob extends EntityCreature implements IMob
    016{
    017    public EntityMob(World par1World)
    018    {
    019        super(par1World);
    020        this.experienceValue = 5;
    021    }
    022
    023    /**
    024     * Called frequently so the entity can update its state every tick as required. For example, zombies and skeletons
    025     * use this to react to sunlight and start to burn.
    026     */
    027    public void onLivingUpdate()
    028    {
    029        this.updateArmSwingProgress();
    030        float var1 = this.getBrightness(1.0F);
    031
    032        if (var1 > 0.5F)
    033        {
    034            this.entityAge += 2;
    035        }
    036
    037        super.onLivingUpdate();
    038    }
    039
    040    /**
    041     * Called to update the entity's position/logic.
    042     */
    043    public void onUpdate()
    044    {
    045        super.onUpdate();
    046
    047        if (!this.worldObj.isRemote && this.worldObj.difficultySetting == 0)
    048        {
    049            this.setDead();
    050        }
    051    }
    052
    053    /**
    054     * Finds the closest player within 16 blocks to attack, or null if this Entity isn't interested in attacking
    055     * (Animals, Spiders at day, peaceful PigZombies).
    056     */
    057    protected Entity findPlayerToAttack()
    058    {
    059        EntityPlayer var1 = this.worldObj.getClosestVulnerablePlayerToEntity(this, 16.0D);
    060        return var1 != null && this.canEntityBeSeen(var1) ? var1 : null;
    061    }
    062
    063    /**
    064     * Called when the entity is attacked.
    065     */
    066    public boolean attackEntityFrom(DamageSource par1DamageSource, int par2)
    067    {
    068        if (this.isEntityInvulnerable())
    069        {
    070            return false;
    071        }
    072        else if (super.attackEntityFrom(par1DamageSource, par2))
    073        {
    074            Entity var3 = par1DamageSource.getEntity();
    075
    076            if (this.riddenByEntity != var3 && this.ridingEntity != var3)
    077            {
    078                if (var3 != this)
    079                {
    080                    this.entityToAttack = var3;
    081                }
    082
    083                return true;
    084            }
    085            else
    086            {
    087                return true;
    088            }
    089        }
    090        else
    091        {
    092            return false;
    093        }
    094    }
    095
    096    public boolean attackEntityAsMob(Entity par1Entity)
    097    {
    098        int var2 = this.getAttackStrength(par1Entity);
    099
    100        if (this.isPotionActive(Potion.damageBoost))
    101        {
    102            var2 += 3 << this.getActivePotionEffect(Potion.damageBoost).getAmplifier();
    103        }
    104
    105        if (this.isPotionActive(Potion.weakness))
    106        {
    107            var2 -= 2 << this.getActivePotionEffect(Potion.weakness).getAmplifier();
    108        }
    109
    110        int var3 = 0;
    111
    112        if (par1Entity instanceof EntityLiving)
    113        {
    114            var2 += EnchantmentHelper.getEnchantmentModifierLiving(this, (EntityLiving)par1Entity);
    115            var3 += EnchantmentHelper.getKnockbackModifier(this, (EntityLiving)par1Entity);
    116        }
    117
    118        boolean var4 = par1Entity.attackEntityFrom(DamageSource.causeMobDamage(this), var2);
    119
    120        if (var4)
    121        {
    122            if (var3 > 0)
    123            {
    124                par1Entity.addVelocity((double)(-MathHelper.sin(this.rotationYaw * (float)Math.PI / 180.0F) * (float)var3 * 0.5F), 0.1D, (double)(MathHelper.cos(this.rotationYaw * (float)Math.PI / 180.0F) * (float)var3 * 0.5F));
    125                this.motionX *= 0.6D;
    126                this.motionZ *= 0.6D;
    127            }
    128
    129            int var5 = EnchantmentHelper.getFireAspectModifier(this);
    130
    131            if (var5 > 0)
    132            {
    133                par1Entity.setFire(var5 * 4);
    134            }
    135
    136            if (par1Entity instanceof EntityLiving)
    137            {
    138                EnchantmentThorns.func_92096_a(this, (EntityLiving)par1Entity, this.rand);
    139            }
    140        }
    141
    142        return var4;
    143    }
    144
    145    /**
    146     * Basic mob attack. Default to touch of death in EntityCreature. Overridden by each mob to define their attack.
    147     */
    148    protected void attackEntity(Entity par1Entity, float par2)
    149    {
    150        if (this.attackTime <= 0 && par2 < 2.0F && par1Entity.boundingBox.maxY > this.boundingBox.minY && par1Entity.boundingBox.minY < this.boundingBox.maxY)
    151        {
    152            this.attackTime = 20;
    153            this.attackEntityAsMob(par1Entity);
    154        }
    155    }
    156
    157    /**
    158     * Takes a coordinate in and returns a weight to determine how likely this creature will try to path to the block.
    159     * Args: x, y, z
    160     */
    161    public float getBlockPathWeight(int par1, int par2, int par3)
    162    {
    163        return 0.5F - this.worldObj.getLightBrightness(par1, par2, par3);
    164    }
    165
    166    /**
    167     * Checks to make sure the light is not too bright where the mob is spawning
    168     */
    169    protected boolean isValidLightLevel()
    170    {
    171        int var1 = MathHelper.floor_double(this.posX);
    172        int var2 = MathHelper.floor_double(this.boundingBox.minY);
    173        int var3 = MathHelper.floor_double(this.posZ);
    174
    175        if (this.worldObj.getSavedLightValue(EnumSkyBlock.Sky, var1, var2, var3) > this.rand.nextInt(32))
    176        {
    177            return false;
    178        }
    179        else
    180        {
    181            int var4 = this.worldObj.getBlockLightValue(var1, var2, var3);
    182
    183            if (this.worldObj.isThundering())
    184            {
    185                int var5 = this.worldObj.skylightSubtracted;
    186                this.worldObj.skylightSubtracted = 10;
    187                var4 = this.worldObj.getBlockLightValue(var1, var2, var3);
    188                this.worldObj.skylightSubtracted = var5;
    189            }
    190
    191            return var4 <= this.rand.nextInt(8);
    192        }
    193    }
    194
    195    /**
    196     * Checks if the entity's current position is a valid location to spawn this entity.
    197     */
    198    public boolean getCanSpawnHere()
    199    {
    200        return this.isValidLightLevel() && super.getCanSpawnHere();
    201    }
    202
    203    /**
    204     * Returns the amount of damage a mob should deal.
    205     */
    206    public int getAttackStrength(Entity par1Entity)
    207    {
    208        return 2;
    209    }
    210}