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 f = this.getBrightness(1.0F); 031 032 if (f > 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 entityplayer = this.worldObj.getClosestVulnerablePlayerToEntity(this, 16.0D); 060 return entityplayer != null && this.canEntityBeSeen(entityplayer) ? entityplayer : 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 entity = par1DamageSource.getEntity(); 075 076 if (this.riddenByEntity != entity && this.ridingEntity != entity) 077 { 078 if (entity != this) 079 { 080 this.entityToAttack = entity; 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 i = this.getAttackStrength(par1Entity); 099 100 if (this.isPotionActive(Potion.damageBoost)) 101 { 102 i += 3 << this.getActivePotionEffect(Potion.damageBoost).getAmplifier(); 103 } 104 105 if (this.isPotionActive(Potion.weakness)) 106 { 107 i -= 2 << this.getActivePotionEffect(Potion.weakness).getAmplifier(); 108 } 109 110 int j = 0; 111 112 if (par1Entity instanceof EntityLiving) 113 { 114 i += EnchantmentHelper.getEnchantmentModifierLiving(this, (EntityLiving)par1Entity); 115 j += EnchantmentHelper.getKnockbackModifier(this, (EntityLiving)par1Entity); 116 } 117 118 boolean flag = par1Entity.attackEntityFrom(DamageSource.causeMobDamage(this), i); 119 120 if (flag) 121 { 122 if (j > 0) 123 { 124 par1Entity.addVelocity((double)(-MathHelper.sin(this.rotationYaw * (float)Math.PI / 180.0F) * (float)j * 0.5F), 0.1D, (double)(MathHelper.cos(this.rotationYaw * (float)Math.PI / 180.0F) * (float)j * 0.5F)); 125 this.motionX *= 0.6D; 126 this.motionZ *= 0.6D; 127 } 128 129 int k = EnchantmentHelper.getFireAspectModifier(this); 130 131 if (k > 0) 132 { 133 par1Entity.setFire(k * 4); 134 } 135 136 if (par1Entity instanceof EntityLiving) 137 { 138 EnchantmentThorns.func_92096_a(this, (EntityLiving)par1Entity, this.rand); 139 } 140 } 141 142 return flag; 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 i = MathHelper.floor_double(this.posX); 172 int j = MathHelper.floor_double(this.boundingBox.minY); 173 int k = MathHelper.floor_double(this.posZ); 174 175 if (this.worldObj.getSavedLightValue(EnumSkyBlock.Sky, i, j, k) > this.rand.nextInt(32)) 176 { 177 return false; 178 } 179 else 180 { 181 int l = this.worldObj.getBlockLightValue(i, j, k); 182 183 if (this.worldObj.isThundering()) 184 { 185 int i1 = this.worldObj.skylightSubtracted; 186 this.worldObj.skylightSubtracted = 10; 187 l = this.worldObj.getBlockLightValue(i, j, k); 188 this.worldObj.skylightSubtracted = i1; 189 } 190 191 return l <= 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}