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