    001package net.minecraft.entity.monster;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.util.List;
    006import net.minecraft.entity.Entity;
    007import net.minecraft.entity.player.EntityPlayer;
    008import net.minecraft.item.Item;
    009import net.minecraft.item.ItemStack;
    010import net.minecraft.nbt.NBTTagCompound;
    011import net.minecraft.util.DamageSource;
    012import net.minecraft.world.World;
    013
    014public class EntityPigZombie extends EntityZombie
    015{
    016    /** Above zero if this PigZombie is Angry. */
    017    private int angerLevel = 0;
    018
    019    /** A random delay until this PigZombie next makes a sound. */
    020    private int randomSoundDelay = 0;
    021
    022    public EntityPigZombie(World par1World)
    023    {
    024        super(par1World);
    025        this.texture = "/mob/pigzombie.png";
    026        this.moveSpeed = 0.5F;
    027        this.isImmuneToFire = true;
    028    }
    029
    030    /**
    031     * Returns true if the newer Entity AI code should be run
    032     */
    033    protected boolean isAIEnabled()
    034    {
    035        return false;
    036    }
    037
    038    /**
    039     * Called to update the entity's position/logic.
    040     */
    041    public void onUpdate()
    042    {
    043        this.moveSpeed = this.entityToAttack != null ? 0.95F : 0.5F;
    044
    045        if (this.randomSoundDelay > 0 && --this.randomSoundDelay == 0)
    046        {
    047            this.playSound("mob.zombiepig.zpigangry", this.getSoundVolume() * 2.0F, ((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F + 1.0F) * 1.8F);
    048        }
    049
    050        super.onUpdate();
    051    }
    052
    053    @SideOnly(Side.CLIENT)
    054
    055    /**
    056     * Returns the texture's file path as a String.
    057     */
    058    public String getTexture()
    059    {
    060        return "/mob/pigzombie.png";
    061    }
    062
    063    /**
    064     * Checks if the entity's current position is a valid location to spawn this entity.
    065     */
    066    public boolean getCanSpawnHere()
    067    {
    068        return this.worldObj.difficultySetting > 0 && this.worldObj.checkNoEntityCollision(this.boundingBox) && this.worldObj.getCollidingBoundingBoxes(this, this.boundingBox).isEmpty() && !this.worldObj.isAnyLiquid(this.boundingBox);
    069    }
    070
    071    /**
    072     * (abstract) Protected helper method to write subclass entity data to NBT.
    073     */
    074    public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
    075    {
    076        super.writeEntityToNBT(par1NBTTagCompound);
    077        par1NBTTagCompound.setShort("Anger", (short)this.angerLevel);
    078    }
    079
    080    /**
    081     * (abstract) Protected helper method to read subclass entity data from NBT.
    082     */
    083    public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
    084    {
    085        super.readEntityFromNBT(par1NBTTagCompound);
    086        this.angerLevel = par1NBTTagCompound.getShort("Anger");
    087    }
    088
    089    /**
    090     * Finds the closest player within 16 blocks to attack, or null if this Entity isn't interested in attacking
    091     * (Animals, Spiders at day, peaceful PigZombies).
    092     */
    093    protected Entity findPlayerToAttack()
    094    {
    095        return this.angerLevel == 0 ? null : super.findPlayerToAttack();
    096    }
    097
    098    /**
    099     * Called when the entity is attacked.
    100     */
    101    public boolean attackEntityFrom(DamageSource par1DamageSource, int par2)
    102    {
    103        if (this.isEntityInvulnerable())
    104        {
    105            return false;
    106        }
    107        else
    108        {
    109            Entity entity = par1DamageSource.getEntity();
    110
    111            if (entity instanceof EntityPlayer)
    112            {
    113                List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.boundingBox.expand(32.0D, 32.0D, 32.0D));
    114
    115                for (int j = 0; j < list.size(); ++j)
    116                {
    117                    Entity entity1 = (Entity)list.get(j);
    118
    119                    if (entity1 instanceof EntityPigZombie)
    120                    {
    121                        EntityPigZombie entitypigzombie = (EntityPigZombie)entity1;
    122                        entitypigzombie.becomeAngryAt(entity);
    123                    }
    124                }
    125
    126                this.becomeAngryAt(entity);
    127            }
    128
    129            return super.attackEntityFrom(par1DamageSource, par2);
    130        }
    131    }
    132
    133    /**
    134     * Causes this PigZombie to become angry at the supplied Entity (which will be a player).
    135     */
    136    private void becomeAngryAt(Entity par1Entity)
    137    {
    138        this.entityToAttack = par1Entity;
    139        this.angerLevel = 400 + this.rand.nextInt(400);
    140        this.randomSoundDelay = this.rand.nextInt(40);
    141    }
    142
    143    /**
    144     * Returns the sound this mob makes while it's alive.
    145     */
    146    protected String getLivingSound()
    147    {
    148        return "mob.zombiepig.zpig";
    149    }
    150
    151    /**
    152     * Returns the sound this mob makes when it is hurt.
    153     */
    154    protected String getHurtSound()
    155    {
    156        return "mob.zombiepig.zpighurt";
    157    }
    158
    159    /**
    160     * Returns the sound this mob makes on death.
    161     */
    162    protected String getDeathSound()
    163    {
    164        return "mob.zombiepig.zpigdeath";
    165    }
    166
    167    /**
    168     * Drop 0-2 items of this living's type. @param par1 - Whether this entity has recently been hit by a player. @param
    169     * par2 - Level of Looting used to kill this mob.
    170     */
    171    protected void dropFewItems(boolean par1, int par2)
    172    {
    173        int j = this.rand.nextInt(2 + par2);
    174        int k;
    175
    176        for (k = 0; k < j; ++k)
    177        {
    178            this.dropItem(Item.rottenFlesh.itemID, 1);
    179        }
    180
    181        j = this.rand.nextInt(2 + par2);
    182
    183        for (k = 0; k < j; ++k)
    184        {
    185            this.dropItem(Item.goldNugget.itemID, 1);
    186        }
    187    }
    188
    189    /**
    190     * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
    191     */
    192    public boolean interact(EntityPlayer par1EntityPlayer)
    193    {
    194        return false;
    195    }
    196
    197    protected void dropRareDrop(int par1)
    198    {
    199        this.dropItem(Item.ingotGold.itemID, 1);
    200    }
    201
    202    /**
    203     * Returns the item ID for the item the mob drops on death.
    204     */
    205    protected int getDropItemId()
    206    {
    207        return Item.rottenFlesh.itemID;
    208    }
    209
    210    /**
    211     * Makes entity wear random armor based on difficulty
    212     */
    213    protected void addRandomArmor()
    214    {
    215        this.setCurrentItemOrArmor(0, new ItemStack(Item.swordGold));
    216    }
    217
    218    /**
    219     * Initialize this creature.
    220     */
    221    public void initCreature()
    222    {
    223        super.initCreature();
    224        this.setVillager(false);
    225    }
    226
    227    /**
    228     * Returns the amount of damage a mob should deal.
    229     */
    230    public int getAttackStrength(Entity par1Entity)
    231    {
    232        ItemStack itemstack = this.getHeldItem();
    233        int i = 5;
    234
    235        if (itemstack != null)
    236        {
    237            i += itemstack.getDamageVsEntity(this);
    238        }
    239
    240        return i;
    241    }
    242}