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