001    package net.minecraft.src;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    
006    public class EntityXPOrb extends Entity
007    {
008        /**
009         * A constantly increasing value that RenderXPOrb uses to control the colour shifting (Green / yellow)
010         */
011        public int xpColor;
012    
013        /** The age of the XP orb in ticks. */
014        public int xpOrbAge = 0;
015        public int field_70532_c;
016    
017        /** The health of this XP orb. */
018        private int xpOrbHealth = 5;
019    
020        /** This is how much XP this orb has. */
021        private int xpValue;
022        private EntityPlayer field_80001_f;
023        private int field_80002_g;
024    
025        public EntityXPOrb(World par1World, double par2, double par4, double par6, int par8)
026        {
027            super(par1World);
028            this.setSize(0.5F, 0.5F);
029            this.yOffset = this.height / 2.0F;
030            this.setPosition(par2, par4, par6);
031            this.rotationYaw = (float)(Math.random() * 360.0D);
032            this.motionX = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D) * 2.0F);
033            this.motionY = (double)((float)(Math.random() * 0.2D) * 2.0F);
034            this.motionZ = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D) * 2.0F);
035            this.xpValue = par8;
036        }
037    
038        /**
039         * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to
040         * prevent them from trampling crops
041         */
042        protected boolean canTriggerWalking()
043        {
044            return false;
045        }
046    
047        public EntityXPOrb(World par1World)
048        {
049            super(par1World);
050            this.setSize(0.25F, 0.25F);
051            this.yOffset = this.height / 2.0F;
052        }
053    
054        protected void entityInit() {}
055    
056        @SideOnly(Side.CLIENT)
057        public int getBrightnessForRender(float par1)
058        {
059            float var2 = 0.5F;
060    
061            if (var2 < 0.0F)
062            {
063                var2 = 0.0F;
064            }
065    
066            if (var2 > 1.0F)
067            {
068                var2 = 1.0F;
069            }
070    
071            int var3 = super.getBrightnessForRender(par1);
072            int var4 = var3 & 255;
073            int var5 = var3 >> 16 & 255;
074            var4 += (int)(var2 * 15.0F * 16.0F);
075    
076            if (var4 > 240)
077            {
078                var4 = 240;
079            }
080    
081            return var4 | var5 << 16;
082        }
083    
084        /**
085         * Called to update the entity's position/logic.
086         */
087        public void onUpdate()
088        {
089            super.onUpdate();
090    
091            if (this.field_70532_c > 0)
092            {
093                --this.field_70532_c;
094            }
095    
096            this.prevPosX = this.posX;
097            this.prevPosY = this.posY;
098            this.prevPosZ = this.posZ;
099            this.motionY -= 0.029999999329447746D;
100    
101            if (this.worldObj.getBlockMaterial(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.posY), MathHelper.floor_double(this.posZ)) == Material.lava)
102            {
103                this.motionY = 0.20000000298023224D;
104                this.motionX = (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F);
105                this.motionZ = (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F);
106                this.worldObj.playSoundAtEntity(this, "random.fizz", 0.4F, 2.0F + this.rand.nextFloat() * 0.4F);
107            }
108    
109            this.pushOutOfBlocks(this.posX, (this.boundingBox.minY + this.boundingBox.maxY) / 2.0D, this.posZ);
110            double var1 = 8.0D;
111    
112            if (this.field_80002_g < this.xpColor - 20 + this.entityId % 100)
113            {
114                if (this.field_80001_f == null || this.field_80001_f.getDistanceSqToEntity(this) > var1 * var1)
115                {
116                    this.field_80001_f = this.worldObj.getClosestPlayerToEntity(this, var1);
117                }
118    
119                this.field_80002_g = this.xpColor;
120            }
121    
122            if (this.field_80001_f != null)
123            {
124                double var3 = (this.field_80001_f.posX - this.posX) / var1;
125                double var5 = (this.field_80001_f.posY + (double)this.field_80001_f.getEyeHeight() - this.posY) / var1;
126                double var7 = (this.field_80001_f.posZ - this.posZ) / var1;
127                double var9 = Math.sqrt(var3 * var3 + var5 * var5 + var7 * var7);
128                double var11 = 1.0D - var9;
129    
130                if (var11 > 0.0D)
131                {
132                    var11 *= var11;
133                    this.motionX += var3 / var9 * var11 * 0.1D;
134                    this.motionY += var5 / var9 * var11 * 0.1D;
135                    this.motionZ += var7 / var9 * var11 * 0.1D;
136                }
137            }
138    
139            this.moveEntity(this.motionX, this.motionY, this.motionZ);
140            float var13 = 0.98F;
141    
142            if (this.onGround)
143            {
144                var13 = 0.58800006F;
145                int var4 = this.worldObj.getBlockId(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.boundingBox.minY) - 1, MathHelper.floor_double(this.posZ));
146    
147                if (var4 > 0)
148                {
149                    var13 = Block.blocksList[var4].slipperiness * 0.98F;
150                }
151            }
152    
153            this.motionX *= (double)var13;
154            this.motionY *= 0.9800000190734863D;
155            this.motionZ *= (double)var13;
156    
157            if (this.onGround)
158            {
159                this.motionY *= -0.8999999761581421D;
160            }
161    
162            ++this.xpColor;
163            ++this.xpOrbAge;
164    
165            if (this.xpOrbAge >= 6000)
166            {
167                this.setDead();
168            }
169        }
170    
171        /**
172         * Returns if this entity is in water and will end up adding the waters velocity to the entity
173         */
174        public boolean handleWaterMovement()
175        {
176            return this.worldObj.handleMaterialAcceleration(this.boundingBox, Material.water, this);
177        }
178    
179        /**
180         * Will deal the specified amount of damage to the entity if the entity isn't immune to fire damage. Args:
181         * amountDamage
182         */
183        protected void dealFireDamage(int par1)
184        {
185            this.attackEntityFrom(DamageSource.inFire, par1);
186        }
187    
188        /**
189         * Called when the entity is attacked.
190         */
191        public boolean attackEntityFrom(DamageSource par1DamageSource, int par2)
192        {
193            this.setBeenAttacked();
194            this.xpOrbHealth -= par2;
195    
196            if (this.xpOrbHealth <= 0)
197            {
198                this.setDead();
199            }
200    
201            return false;
202        }
203    
204        /**
205         * (abstract) Protected helper method to write subclass entity data to NBT.
206         */
207        public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
208        {
209            par1NBTTagCompound.setShort("Health", (short)((byte)this.xpOrbHealth));
210            par1NBTTagCompound.setShort("Age", (short)this.xpOrbAge);
211            par1NBTTagCompound.setShort("Value", (short)this.xpValue);
212        }
213    
214        /**
215         * (abstract) Protected helper method to read subclass entity data from NBT.
216         */
217        public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
218        {
219            this.xpOrbHealth = par1NBTTagCompound.getShort("Health") & 255;
220            this.xpOrbAge = par1NBTTagCompound.getShort("Age");
221            this.xpValue = par1NBTTagCompound.getShort("Value");
222        }
223    
224        /**
225         * Called by a player entity when they collide with an entity
226         */
227        public void onCollideWithPlayer(EntityPlayer par1EntityPlayer)
228        {
229            if (!this.worldObj.isRemote)
230            {
231                if (this.field_70532_c == 0 && par1EntityPlayer.xpCooldown == 0)
232                {
233                    par1EntityPlayer.xpCooldown = 2;
234                    this.worldObj.playSoundAtEntity(this, "random.orb", 0.1F, 0.5F * ((this.rand.nextFloat() - this.rand.nextFloat()) * 0.7F + 1.8F));
235                    par1EntityPlayer.onItemPickup(this, 1);
236                    par1EntityPlayer.addExperience(this.xpValue);
237                    this.setDead();
238                }
239            }
240        }
241    
242        /**
243         * Returns the XP value of this XP orb.
244         */
245        public int getXpValue()
246        {
247            return this.xpValue;
248        }
249    
250        @SideOnly(Side.CLIENT)
251    
252        /**
253         * Returns a number from 1 to 10 based on how much XP this orb is worth. This is used by RenderXPOrb to determine
254         * what texture to use.
255         */
256        public int getTextureByXP()
257        {
258            return this.xpValue >= 2477 ? 10 : (this.xpValue >= 1237 ? 9 : (this.xpValue >= 617 ? 8 : (this.xpValue >= 307 ? 7 : (this.xpValue >= 149 ? 6 : (this.xpValue >= 73 ? 5 : (this.xpValue >= 37 ? 4 : (this.xpValue >= 17 ? 3 : (this.xpValue >= 7 ? 2 : (this.xpValue >= 3 ? 1 : 0)))))))));
259        }
260    
261        /**
262         * Get xp split rate (Is called until the xp drop code in EntityLiving.onEntityUpdate is complete)
263         */
264        public static int getXPSplit(int par0)
265        {
266            return par0 >= 2477 ? 2477 : (par0 >= 1237 ? 1237 : (par0 >= 617 ? 617 : (par0 >= 307 ? 307 : (par0 >= 149 ? 149 : (par0 >= 73 ? 73 : (par0 >= 37 ? 37 : (par0 >= 17 ? 17 : (par0 >= 7 ? 7 : (par0 >= 3 ? 3 : 1)))))))));
267        }
268    
269        /**
270         * If returns false, the item will not inflict any damage against entities.
271         */
272        public boolean canAttackWithItem()
273        {
274            return false;
275        }
276    }