    001package net.minecraft.entity.monster;
    002
    003import net.minecraft.block.Block;
    004import net.minecraft.block.BlockSilverfish;
    005import net.minecraft.entity.Entity;
    006import net.minecraft.entity.EnumCreatureAttribute;
    007import net.minecraft.entity.player.EntityPlayer;
    008import net.minecraft.util.DamageSource;
    009import net.minecraft.util.EntityDamageSource;
    010import net.minecraft.util.Facing;
    011import net.minecraft.util.MathHelper;
    012import net.minecraft.world.World;
    013
    014public class EntitySilverfish extends EntityMob
    015{
    016    /**
    017     * A cooldown before this entity will search for another Silverfish to join them in battle.
    018     */
    019    private int allySummonCooldown;
    020
    021    public EntitySilverfish(World par1World)
    022    {
    023        super(par1World);
    024        this.texture = "/mob/silverfish.png";
    025        this.setSize(0.3F, 0.7F);
    026        this.moveSpeed = 0.6F;
    027    }
    028
    029    public int getMaxHealth()
    030    {
    031        return 8;
    032    }
    033
    034    /**
    035     * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to
    036     * prevent them from trampling crops
    037     */
    038    protected boolean canTriggerWalking()
    039    {
    040        return false;
    041    }
    042
    043    /**
    044     * Finds the closest player within 16 blocks to attack, or null if this Entity isn't interested in attacking
    045     * (Animals, Spiders at day, peaceful PigZombies).
    046     */
    047    protected Entity findPlayerToAttack()
    048    {
    049        double d0 = 8.0D;
    050        return this.worldObj.getClosestVulnerablePlayerToEntity(this, d0);
    051    }
    052
    053    /**
    054     * Returns the sound this mob makes while it's alive.
    055     */
    056    protected String getLivingSound()
    057    {
    058        return "mob.silverfish.say";
    059    }
    060
    061    /**
    062     * Returns the sound this mob makes when it is hurt.
    063     */
    064    protected String getHurtSound()
    065    {
    066        return "mob.silverfish.hit";
    067    }
    068
    069    /**
    070     * Returns the sound this mob makes on death.
    071     */
    072    protected String getDeathSound()
    073    {
    074        return "mob.silverfish.kill";
    075    }
    076
    077    /**
    078     * Called when the entity is attacked.
    079     */
    080    public boolean attackEntityFrom(DamageSource par1DamageSource, int par2)
    081    {
    082        if (this.isEntityInvulnerable())
    083        {
    084            return false;
    085        }
    086        else
    087        {
    088            if (this.allySummonCooldown <= 0 && (par1DamageSource instanceof EntityDamageSource || par1DamageSource == DamageSource.magic))
    089            {
    090                this.allySummonCooldown = 20;
    091            }
    092
    093            return super.attackEntityFrom(par1DamageSource, par2);
    094        }
    095    }
    096
    097    /**
    098     * Basic mob attack. Default to touch of death in EntityCreature. Overridden by each mob to define their attack.
    099     */
    100    protected void attackEntity(Entity par1Entity, float par2)
    101    {
    102        if (this.attackTime <= 0 && par2 < 1.2F && par1Entity.boundingBox.maxY > this.boundingBox.minY && par1Entity.boundingBox.minY < this.boundingBox.maxY)
    103        {
    104            this.attackTime = 20;
    105            this.attackEntityAsMob(par1Entity);
    106        }
    107    }
    108
    109    /**
    110     * Plays step sound at given x, y, z for the entity
    111     */
    112    protected void playStepSound(int par1, int par2, int par3, int par4)
    113    {
    114        this.playSound("mob.silverfish.step", 0.15F, 1.0F);
    115    }
    116
    117    /**
    118     * Returns the item ID for the item the mob drops on death.
    119     */
    120    protected int getDropItemId()
    121    {
    122        return 0;
    123    }
    124
    125    /**
    126     * Called to update the entity's position/logic.
    127     */
    128    public void onUpdate()
    129    {
    130        this.renderYawOffset = this.rotationYaw;
    131        super.onUpdate();
    132    }
    133
    134    protected void updateEntityActionState()
    135    {
    136        super.updateEntityActionState();
    137
    138        if (!this.worldObj.isRemote)
    139        {
    140            int i;
    141            int j;
    142            int k;
    143            int l;
    144
    145            if (this.allySummonCooldown > 0)
    146            {
    147                --this.allySummonCooldown;
    148
    149                if (this.allySummonCooldown == 0)
    150                {
    151                    i = MathHelper.floor_double(this.posX);
    152                    j = MathHelper.floor_double(this.posY);
    153                    k = MathHelper.floor_double(this.posZ);
    154                    boolean flag = false;
    155
    156                    for (l = 0; !flag && l <= 5 && l >= -5; l = l <= 0 ? 1 - l : 0 - l)
    157                    {
    158                        for (int i1 = 0; !flag && i1 <= 10 && i1 >= -10; i1 = i1 <= 0 ? 1 - i1 : 0 - i1)
    159                        {
    160                            for (int j1 = 0; !flag && j1 <= 10 && j1 >= -10; j1 = j1 <= 0 ? 1 - j1 : 0 - j1)
    161                            {
    162                                int k1 = this.worldObj.getBlockId(i + i1, j + l, k + j1);
    163
    164                                if (k1 == Block.silverfish.blockID)
    165                                {
    166                                    this.worldObj.destroyBlock(i + i1, j + l, k + j1, false);
    167                                    Block.silverfish.onBlockDestroyedByPlayer(this.worldObj, i + i1, j + l, k + j1, 0);
    168
    169                                    if (this.rand.nextBoolean())
    170                                    {
    171                                        flag = true;
    172                                        break;
    173                                    }
    174                                }
    175                            }
    176                        }
    177                    }
    178                }
    179            }
    180
    181            if (this.entityToAttack == null && !this.hasPath())
    182            {
    183                i = MathHelper.floor_double(this.posX);
    184                j = MathHelper.floor_double(this.posY + 0.5D);
    185                k = MathHelper.floor_double(this.posZ);
    186                int l1 = this.rand.nextInt(6);
    187                l = this.worldObj.getBlockId(i + Facing.offsetsXForSide[l1], j + Facing.offsetsYForSide[l1], k + Facing.offsetsZForSide[l1]);
    188
    189                if (BlockSilverfish.getPosingIdByMetadata(l))
    190                {
    191                    this.worldObj.setBlock(i + Facing.offsetsXForSide[l1], j + Facing.offsetsYForSide[l1], k + Facing.offsetsZForSide[l1], Block.silverfish.blockID, BlockSilverfish.getMetadataForBlockType(l), 3);
    192                    this.spawnExplosionParticle();
    193                    this.setDead();
    194                }
    195                else
    196                {
    197                    this.updateWanderPath();
    198                }
    199            }
    200            else if (this.entityToAttack != null && !this.hasPath())
    201            {
    202                this.entityToAttack = null;
    203            }
    204        }
    205    }
    206
    207    /**
    208     * Takes a coordinate in and returns a weight to determine how likely this creature will try to path to the block.
    209     * Args: x, y, z
    210     */
    211    public float getBlockPathWeight(int par1, int par2, int par3)
    212    {
    213        return this.worldObj.getBlockId(par1, par2 - 1, par3) == Block.stone.blockID ? 10.0F : super.getBlockPathWeight(par1, par2, par3);
    214    }
    215
    216    /**
    217     * Checks to make sure the light is not too bright where the mob is spawning
    218     */
    219    protected boolean isValidLightLevel()
    220    {
    221        return true;
    222    }
    223
    224    /**
    225     * Checks if the entity's current position is a valid location to spawn this entity.
    226     */
    227    public boolean getCanSpawnHere()
    228    {
    229        if (super.getCanSpawnHere())
    230        {
    231            EntityPlayer entityplayer = this.worldObj.getClosestPlayerToEntity(this, 5.0D);
    232            return entityplayer == null;
    233        }
    234        else
    235        {
    236            return false;
    237        }
    238    }
    239
    240    /**
    241     * Returns the amount of damage a mob should deal.
    242     */
    243    public int getAttackStrength(Entity par1Entity)
    244    {
    245        return 1;
    246    }
    247
    248    /**
    249     * Get this Entity's EnumCreatureAttribute
    250     */
    251    public EnumCreatureAttribute getCreatureAttribute()
    252    {
    253        return EnumCreatureAttribute.ARTHROPOD;
    254    }
    255}