| 1 | package net.minecraft.entity.item;
|
| 2 |
|
| 3 | import net.minecraft.block.material.Material;
|
| 4 | import net.minecraft.entity.Entity;
|
| 5 | import net.minecraft.entity.player.EntityPlayer;
|
| 6 | import net.minecraft.nbt.NBTTagCompound;
|
| 7 | import net.minecraft.util.BlockPos;
|
| 8 | import net.minecraft.util.DamageSource;
|
| 9 | import net.minecraft.util.MathHelper;
|
| 10 | import net.minecraft.world.World;
|
| 11 |
|
| 12 | public class EntityXPOrb extends Entity
|
| 13 | {
|
| 14 | /**
|
| 15 | * A constantly increasing value that RenderXPOrb uses to control the colour shifting (Green / yellow)
|
| 16 | */
|
| 17 | public int xpColor;
|
| 18 |
|
| 19 | /** The age of the XP orb in ticks. */
|
| 20 | public int xpOrbAge;
|
| 21 | public int delayBeforeCanPickup;
|
| 22 |
|
| 23 | /** The health of this XP orb. */
|
| 24 | private int xpOrbHealth = 5;
|
| 25 |
|
| 26 | /** This is how much XP this orb has. */
|
| 27 | private int xpValue;
|
| 28 |
|
| 29 | /** The closest EntityPlayer to this orb. */
|
| 30 | private EntityPlayer closestPlayer;
|
| 31 |
|
| 32 | /** Threshold color for tracking players */
|
| 33 | private int xpTargetColor;
|
| 34 |
|
| 35 | public EntityXPOrb(World worldIn, double x, double y, double z, int expValue)
|
| 36 | {
|
| 37 | super(worldIn);
|
| 38 | this.setSize(0.5F, 0.5F);
|
| 39 | this.setPosition(x, y, z);
|
| 40 | this.rotationYaw = (float)(Math.random() * 360.0D);
|
| 41 | this.motionX = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D) * 2.0F);
|
| 42 | this.motionY = (double)((float)(Math.random() * 0.2D) * 2.0F);
|
| 43 | this.motionZ = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D) * 2.0F);
|
| 44 | this.xpValue = expValue;
|
| 45 | }
|
| 46 |
|
| 47 | /**
|
| 48 | * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to
|
| 49 | * prevent them from trampling crops
|
| 50 | */
|
| 51 | protected boolean canTriggerWalking()
|
| 52 | {
|
| 53 | return false;
|
| 54 | }
|
| 55 |
|
| 56 | public EntityXPOrb(World worldIn)
|
| 57 | {
|
| 58 | super(worldIn);
|
| 59 | this.setSize(0.25F, 0.25F);
|
| 60 | }
|
| 61 |
|
| 62 | protected void entityInit()
|
| 63 | {
|
| 64 | }
|
| 65 |
|
| 66 | public int getBrightnessForRender(float partialTicks)
|
| 67 | {
|
| 68 | float f = 0.5F;
|
| 69 | f = MathHelper.clamp_float(f, 0.0F, 1.0F);
|
| 70 | int i = super.getBrightnessForRender(partialTicks);
|
| 71 | int j = i & 255;
|
| 72 | int k = i >> 16 & 255;
|
| 73 | j = j + (int)(f * 15.0F * 16.0F);
|
| 74 |
|
| 75 | if (j > 240)
|
| 76 | {
|
| 77 | j = 240;
|
| 78 | }
|
| 79 |
|
| 80 | return j | k << 16;
|
| 81 | }
|
| 82 |
|
| 83 | /**
|
| 84 | * Called to update the entity's position/logic.
|
| 85 | */
|
| 86 | public void onUpdate()
|
| 87 | {
|
| 88 | super.onUpdate();
|
| 89 |
|
| 90 | if (this.delayBeforeCanPickup > 0)
|
| 91 | {
|
| 92 | --this.delayBeforeCanPickup;
|
| 93 | }
|
| 94 |
|
| 95 | this.prevPosX = this.posX;
|
| 96 | this.prevPosY = this.posY;
|
| 97 | this.prevPosZ = this.posZ;
|
| 98 | this.motionY -= 0.029999999329447746D;
|
| 99 |
|
| 100 | if (this.worldObj.getBlockState(new BlockPos(this)).getBlock().getMaterial() == Material.lava)
|
| 101 | {
|
| 102 | this.motionY = 0.20000000298023224D;
|
| 103 | this.motionX = (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F);
|
| 104 | this.motionZ = (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F);
|
| 105 | this.playSound("random.fizz", 0.4F, 2.0F + this.rand.nextFloat() * 0.4F);
|
| 106 | }
|
| 107 |
|
| 108 | this.pushOutOfBlocks(this.posX, (this.getEntityBoundingBox().minY + this.getEntityBoundingBox().maxY) / 2.0D, this.posZ);
|
| 109 | double d0 = 8.0D;
|
| 110 |
|
| 111 | if (this.xpTargetColor < this.xpColor - 20 + this.getEntityId() % 100)
|
| 112 | {
|
| 113 | if (this.closestPlayer == null || this.closestPlayer.getDistanceSqToEntity(this) > d0 * d0)
|
| 114 | {
|
| 115 | this.closestPlayer = this.worldObj.getClosestPlayerToEntity(this, d0);
|
| 116 | }
|
| 117 |
|
| 118 | this.xpTargetColor = this.xpColor;
|
| 119 | }
|
| 120 |
|
| 121 | if (this.closestPlayer != null && this.closestPlayer.isSpectator())
|
| 122 | {
|
| 123 | this.closestPlayer = null;
|
| 124 | }
|
| 125 |
|
| 126 | if (this.closestPlayer != null)
|
| 127 | {
|
| 128 | double d1 = (this.closestPlayer.posX - this.posX) / d0;
|
| 129 | double d2 = (this.closestPlayer.posY + (double)this.closestPlayer.getEyeHeight() - this.posY) / d0;
|
| 130 | double d3 = (this.closestPlayer.posZ - this.posZ) / d0;
|
| 131 | double d4 = Math.sqrt(d1 * d1 + d2 * d2 + d3 * d3);
|
| 132 | double d5 = 1.0D - d4;
|
| 133 |
|
| 134 | if (d5 > 0.0D)
|
| 135 | {
|
| 136 | d5 = d5 * d5;
|
| 137 | this.motionX += d1 / d4 * d5 * 0.1D;
|
| 138 | this.motionY += d2 / d4 * d5 * 0.1D;
|
| 139 | this.motionZ += d3 / d4 * d5 * 0.1D;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | this.moveEntity(this.motionX, this.motionY, this.motionZ);
|
| 144 | float f = 0.98F;
|
| 145 |
|
| 146 | if (this.onGround)
|
| 147 | {
|
| 148 | f = this.worldObj.getBlockState(new BlockPos(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.getEntityBoundingBox().minY) - 1, MathHelper.floor_double(this.posZ))).getBlock().slipperiness * 0.98F;
|
| 149 | }
|
| 150 |
|
| 151 | this.motionX *= (double)f;
|
| 152 | this.motionY *= 0.9800000190734863D;
|
| 153 | this.motionZ *= (double)f;
|
| 154 |
|
| 155 | if (this.onGround)
|
| 156 | {
|
| 157 | this.motionY *= -0.8999999761581421D;
|
| 158 | }
|
| 159 |
|
| 160 | ++this.xpColor;
|
| 161 | ++this.xpOrbAge;
|
| 162 |
|
| 163 | if (this.xpOrbAge >= 6000)
|
| 164 | {
|
| 165 | this.setDead();
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | /**
|
| 170 | * Returns if this entity is in water and will end up adding the waters velocity to the entity
|
| 171 | */
|
| 172 | public boolean handleWaterMovement()
|
| 173 | {
|
| 174 | return this.worldObj.handleMaterialAcceleration(this.getEntityBoundingBox(), Material.water, this);
|
| 175 | }
|
| 176 |
|
| 177 | /**
|
| 178 | * Will deal the specified amount of damage to the entity if the entity isn't immune to fire damage. Args:
|
| 179 | * amountDamage
|
| 180 | */
|
| 181 | protected void dealFireDamage(int amount)
|
| 182 | {
|
| 183 | this.attackEntityFrom(DamageSource.inFire, (float)amount);
|
| 184 | }
|
| 185 |
|
| 186 | /**
|
| 187 | * Called when the entity is attacked.
|
| 188 | */
|
| 189 | public boolean attackEntityFrom(DamageSource source, float amount)
|
| 190 | {
|
| 191 | if (this.isEntityInvulnerable(source))
|
| 192 | {
|
| 193 | return false;
|
| 194 | }
|
| 195 | else
|
| 196 | {
|
| 197 | this.setBeenAttacked();
|
| 198 | this.xpOrbHealth = (int)((float)this.xpOrbHealth - amount);
|
| 199 |
|
| 200 | if (this.xpOrbHealth <= 0)
|
| 201 | {
|
| 202 | this.setDead();
|
| 203 | }
|
| 204 |
|
| 205 | return false;
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | /**
|
| 210 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 211 | */
|
| 212 | public void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 213 | {
|
| 214 | tagCompound.setShort("Health", (short)((byte)this.xpOrbHealth));
|
| 215 | tagCompound.setShort("Age", (short)this.xpOrbAge);
|
| 216 | tagCompound.setShort("Value", (short)this.xpValue);
|
| 217 | }
|
| 218 |
|
| 219 | /**
|
| 220 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 221 | */
|
| 222 | public void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 223 | {
|
| 224 | this.xpOrbHealth = tagCompund.getShort("Health") & 255;
|
| 225 | this.xpOrbAge = tagCompund.getShort("Age");
|
| 226 | this.xpValue = tagCompund.getShort("Value");
|
| 227 | }
|
| 228 |
|
| 229 | /**
|
| 230 | * Called by a player entity when they collide with an entity
|
| 231 | */
|
| 232 | public void onCollideWithPlayer(EntityPlayer entityIn)
|
| 233 | {
|
| 234 | if (!this.worldObj.isRemote)
|
| 235 | {
|
| 236 | if (this.delayBeforeCanPickup == 0 && entityIn.xpCooldown == 0)
|
| 237 | {
|
| 238 | entityIn.xpCooldown = 2;
|
| 239 | this.worldObj.playSoundAtEntity(entityIn, "random.orb", 0.1F, 0.5F * ((this.rand.nextFloat() - this.rand.nextFloat()) * 0.7F + 1.8F));
|
| 240 | entityIn.onItemPickup(this, 1);
|
| 241 | entityIn.addExperience(this.xpValue);
|
| 242 | this.setDead();
|
| 243 | }
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | /**
|
| 248 | * Returns the XP value of this XP orb.
|
| 249 | */
|
| 250 | public int getXpValue()
|
| 251 | {
|
| 252 | return this.xpValue;
|
| 253 | }
|
| 254 |
|
| 255 | /**
|
| 256 | * Returns a number from 1 to 10 based on how much XP this orb is worth. This is used by RenderXPOrb to determine
|
| 257 | * what texture to use.
|
| 258 | */
|
| 259 | public int getTextureByXP()
|
| 260 | {
|
| 261 | 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)))))))));
|
| 262 | }
|
| 263 |
|
| 264 | /**
|
| 265 | * Get a fragment of the maximum experience points value for the supplied value of experience points value.
|
| 266 | */
|
| 267 | public static int getXPSplit(int expValue)
|
| 268 | {
|
| 269 | return expValue >= 2477 ? 2477 : (expValue >= 1237 ? 1237 : (expValue >= 617 ? 617 : (expValue >= 307 ? 307 : (expValue >= 149 ? 149 : (expValue >= 73 ? 73 : (expValue >= 37 ? 37 : (expValue >= 17 ? 17 : (expValue >= 7 ? 7 : (expValue >= 3 ? 3 : 1)))))))));
|
| 270 | }
|
| 271 |
|
| 272 | /**
|
| 273 | * If returns false, the item will not inflict any damage against entities.
|
| 274 | */
|
| 275 | public boolean canAttackWithItem()
|
| 276 | {
|
| 277 | return false;
|
| 278 | }
|
| 279 | }
|
| 280 |
|