001 package net.minecraft.src; 002 003 import java.util.Iterator; 004 005 import net.minecraftforge.common.MinecraftForge; 006 import net.minecraftforge.event.entity.player.EntityItemPickupEvent; 007 008 import cpw.mods.fml.common.registry.GameRegistry; 009 010 public class EntityItem extends Entity 011 { 012 /** The item stack of this EntityItem. */ 013 public ItemStack item; 014 015 /** 016 * The age of this EntityItem (used to animate it up and down as well as expire it) 017 */ 018 public int age = 0; 019 public int delayBeforeCanPickup; 020 021 /** The health of this EntityItem. (For example, damage for tools) */ 022 private int health = 5; 023 024 /** The EntityItem's random initial float height. */ 025 public float hoverStart = (float)(Math.random() * Math.PI * 2.0D); 026 027 public EntityItem(World par1World, double par2, double par4, double par6, ItemStack par8ItemStack) 028 { 029 super(par1World); 030 this.setSize(0.25F, 0.25F); 031 this.yOffset = this.height / 2.0F; 032 this.setPosition(par2, par4, par6); 033 this.item = par8ItemStack; 034 this.rotationYaw = (float)(Math.random() * 360.0D); 035 this.motionX = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D)); 036 this.motionY = 0.20000000298023224D; 037 this.motionZ = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D)); 038 } 039 040 /** 041 * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to 042 * prevent them from trampling crops 043 */ 044 protected boolean canTriggerWalking() 045 { 046 return false; 047 } 048 049 public EntityItem(World par1World) 050 { 051 super(par1World); 052 this.setSize(0.25F, 0.25F); 053 this.yOffset = this.height / 2.0F; 054 } 055 056 protected void entityInit() {} 057 058 /** 059 * Called to update the entity's position/logic. 060 */ 061 public void onUpdate() 062 { 063 super.onUpdate(); 064 065 if (this.delayBeforeCanPickup > 0) 066 { 067 --this.delayBeforeCanPickup; 068 } 069 070 this.prevPosX = this.posX; 071 this.prevPosY = this.posY; 072 this.prevPosZ = this.posZ; 073 this.motionY -= 0.03999999910593033D; 074 this.pushOutOfBlocks(this.posX, (this.boundingBox.minY + this.boundingBox.maxY) / 2.0D, this.posZ); 075 this.moveEntity(this.motionX, this.motionY, this.motionZ); 076 boolean var1 = (int)this.prevPosX != (int)this.posX || (int)this.prevPosY != (int)this.posY || (int)this.prevPosZ != (int)this.posZ; 077 078 if (var1) 079 { 080 if (this.worldObj.getBlockMaterial(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.posY), MathHelper.floor_double(this.posZ)) == Material.lava) 081 { 082 this.motionY = 0.20000000298023224D; 083 this.motionX = (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F); 084 this.motionZ = (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F); 085 this.worldObj.playSoundAtEntity(this, "random.fizz", 0.4F, 2.0F + this.rand.nextFloat() * 0.4F); 086 } 087 088 if (!this.worldObj.isRemote) 089 { 090 Iterator var2 = this.worldObj.getEntitiesWithinAABB(EntityItem.class, this.boundingBox.expand(0.5D, 0.0D, 0.5D)).iterator(); 091 092 while (var2.hasNext()) 093 { 094 EntityItem var3 = (EntityItem)var2.next(); 095 this.func_70289_a(var3); 096 } 097 } 098 } 099 100 float var4 = 0.98F; 101 102 if (this.onGround) 103 { 104 var4 = 0.58800006F; 105 int var5 = this.worldObj.getBlockId(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.boundingBox.minY) - 1, MathHelper.floor_double(this.posZ)); 106 107 if (var5 > 0) 108 { 109 var4 = Block.blocksList[var5].slipperiness * 0.98F; 110 } 111 } 112 113 this.motionX *= (double)var4; 114 this.motionY *= 0.9800000190734863D; 115 this.motionZ *= (double)var4; 116 117 if (this.onGround) 118 { 119 this.motionY *= -0.5D; 120 } 121 122 ++this.age; 123 124 if (this.age >= 6000) 125 { 126 this.setDead(); 127 } 128 129 if (this.item == null || this.item.stackSize <= 0) 130 { 131 this.setDead(); 132 } 133 } 134 135 public boolean func_70289_a(EntityItem par1EntityItem) 136 { 137 if (par1EntityItem == this) 138 { 139 return false; 140 } 141 else if (par1EntityItem.isEntityAlive() && this.isEntityAlive()) 142 { 143 if (par1EntityItem.item.getItem() != this.item.getItem()) 144 { 145 return false; 146 } 147 else if (par1EntityItem.item.getItem().getHasSubtypes() && par1EntityItem.item.getItemDamage() != this.item.getItemDamage()) 148 { 149 return false; 150 } 151 else if (par1EntityItem.item.stackSize < this.item.stackSize) 152 { 153 return par1EntityItem.func_70289_a(this); 154 } 155 else if (par1EntityItem.item.stackSize + this.item.stackSize > par1EntityItem.item.getMaxStackSize()) 156 { 157 return false; 158 } 159 else 160 { 161 par1EntityItem.item.stackSize += this.item.stackSize; 162 par1EntityItem.delayBeforeCanPickup = Math.max(par1EntityItem.delayBeforeCanPickup, this.delayBeforeCanPickup); 163 par1EntityItem.age = Math.min(par1EntityItem.age, this.age); 164 this.setDead(); 165 return true; 166 } 167 } 168 else 169 { 170 return false; 171 } 172 } 173 174 public void func_70288_d() 175 { 176 this.age = 4800; 177 } 178 179 /** 180 * Returns if this entity is in water and will end up adding the waters velocity to the entity 181 */ 182 public boolean handleWaterMovement() 183 { 184 return this.worldObj.handleMaterialAcceleration(this.boundingBox, Material.water, this); 185 } 186 187 /** 188 * Will deal the specified amount of damage to the entity if the entity isn't immune to fire damage. Args: 189 * amountDamage 190 */ 191 protected void dealFireDamage(int par1) 192 { 193 this.attackEntityFrom(DamageSource.inFire, par1); 194 } 195 196 /** 197 * Called when the entity is attacked. 198 */ 199 public boolean attackEntityFrom(DamageSource par1DamageSource, int par2) 200 { 201 this.setBeenAttacked(); 202 this.health -= par2; 203 204 if (this.health <= 0) 205 { 206 this.setDead(); 207 } 208 209 return false; 210 } 211 212 /** 213 * (abstract) Protected helper method to write subclass entity data to NBT. 214 */ 215 public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound) 216 { 217 par1NBTTagCompound.setShort("Health", (short)((byte)this.health)); 218 par1NBTTagCompound.setShort("Age", (short)this.age); 219 220 if (this.item != null) 221 { 222 par1NBTTagCompound.setCompoundTag("Item", this.item.writeToNBT(new NBTTagCompound())); 223 } 224 } 225 226 /** 227 * (abstract) Protected helper method to read subclass entity data from NBT. 228 */ 229 public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound) 230 { 231 this.health = par1NBTTagCompound.getShort("Health") & 255; 232 this.age = par1NBTTagCompound.getShort("Age"); 233 NBTTagCompound var2 = par1NBTTagCompound.getCompoundTag("Item"); 234 this.item = ItemStack.loadItemStackFromNBT(var2); 235 236 if (this.item == null || this.item.stackSize <= 0) 237 { 238 this.setDead(); 239 } 240 } 241 242 /** 243 * Called by a player entity when they collide with an entity 244 */ 245 public void onCollideWithPlayer(EntityPlayer par1EntityPlayer) 246 { 247 if (!this.worldObj.isRemote) 248 { 249 if (this.delayBeforeCanPickup > 0) 250 { 251 return; 252 } 253 254 EntityItemPickupEvent event = new EntityItemPickupEvent(par1EntityPlayer, this); 255 256 if (MinecraftForge.EVENT_BUS.post(event)) 257 { 258 return; 259 } 260 261 int var2 = this.item.stackSize; 262 263 if (this.delayBeforeCanPickup <= 0 && (event.isHandled() || var2 <= 0 || par1EntityPlayer.inventory.addItemStackToInventory(this.item))) 264 { 265 if (this.item.itemID == Block.wood.blockID) 266 { 267 par1EntityPlayer.triggerAchievement(AchievementList.mineWood); 268 } 269 270 if (this.item.itemID == Item.leather.shiftedIndex) 271 { 272 par1EntityPlayer.triggerAchievement(AchievementList.killCow); 273 } 274 275 if (this.item.itemID == Item.diamond.shiftedIndex) 276 { 277 par1EntityPlayer.triggerAchievement(AchievementList.diamonds); 278 } 279 280 if (this.item.itemID == Item.blazeRod.shiftedIndex) 281 { 282 par1EntityPlayer.triggerAchievement(AchievementList.blazeRod); 283 } 284 285 GameRegistry.onPickupNotification(par1EntityPlayer, this); 286 287 this.worldObj.playSoundAtEntity(this, "random.pop", 0.2F, ((this.rand.nextFloat() - this.rand.nextFloat()) * 0.7F + 1.0F) * 2.0F); 288 par1EntityPlayer.onItemPickup(this, var2); 289 290 if (this.item.stackSize <= 0) 291 { 292 this.setDead(); 293 } 294 } 295 } 296 } 297 298 /** 299 * Gets the username of the entity. 300 */ 301 public String getEntityName() 302 { 303 return StatCollector.translateToLocal("item." + this.item.getItemName()); 304 } 305 306 /** 307 * If returns false, the item will not inflict any damage against entities. 308 */ 309 public boolean canAttackWithItem() 310 { 311 return false; 312 } 313 }