| 1 | package net.minecraft.entity.item;
|
| 2 |
|
| 3 | import net.minecraft.entity.Entity;
|
| 4 | import net.minecraft.item.ItemStack;
|
| 5 | import net.minecraft.nbt.NBTTagCompound;
|
| 6 | import net.minecraft.util.EnumParticleTypes;
|
| 7 | import net.minecraft.util.MathHelper;
|
| 8 | import net.minecraft.world.World;
|
| 9 |
|
| 10 | public class EntityFireworkRocket extends Entity
|
| 11 | {
|
| 12 | /** The age of the firework in ticks. */
|
| 13 | private int fireworkAge;
|
| 14 |
|
| 15 | /**
|
| 16 | * The lifetime of the firework in ticks. When the age reaches the lifetime the firework explodes.
|
| 17 | */
|
| 18 | private int lifetime;
|
| 19 |
|
| 20 | public EntityFireworkRocket(World worldIn)
|
| 21 | {
|
| 22 | super(worldIn);
|
| 23 | this.setSize(0.25F, 0.25F);
|
| 24 | }
|
| 25 |
|
| 26 | protected void entityInit()
|
| 27 | {
|
| 28 | this.dataWatcher.addObjectByDataType(8, 5);
|
| 29 | }
|
| 30 |
|
| 31 | /**
|
| 32 | * Checks if the entity is in range to render by using the past in distance and comparing it to its average edge
|
| 33 | * length * 64 * renderDistanceWeight Args: distance
|
| 34 | */
|
| 35 | public boolean isInRangeToRenderDist(double distance)
|
| 36 | {
|
| 37 | return distance < 4096.0D;
|
| 38 | }
|
| 39 |
|
| 40 | public EntityFireworkRocket(World worldIn, double x, double y, double z, ItemStack givenItem)
|
| 41 | {
|
| 42 | super(worldIn);
|
| 43 | this.fireworkAge = 0;
|
| 44 | this.setSize(0.25F, 0.25F);
|
| 45 | this.setPosition(x, y, z);
|
| 46 | int i = 1;
|
| 47 |
|
| 48 | if (givenItem != null && givenItem.hasTagCompound())
|
| 49 | {
|
| 50 | this.dataWatcher.updateObject(8, givenItem);
|
| 51 | NBTTagCompound nbttagcompound = givenItem.getTagCompound();
|
| 52 | NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("Fireworks");
|
| 53 |
|
| 54 | if (nbttagcompound1 != null)
|
| 55 | {
|
| 56 | i += nbttagcompound1.getByte("Flight");
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | this.motionX = this.rand.nextGaussian() * 0.001D;
|
| 61 | this.motionZ = this.rand.nextGaussian() * 0.001D;
|
| 62 | this.motionY = 0.05D;
|
| 63 | this.lifetime = 10 * i + this.rand.nextInt(6) + this.rand.nextInt(7);
|
| 64 | }
|
| 65 |
|
| 66 | /**
|
| 67 | * Sets the velocity to the args. Args: x, y, z
|
| 68 | */
|
| 69 | public void setVelocity(double x, double y, double z)
|
| 70 | {
|
| 71 | this.motionX = x;
|
| 72 | this.motionY = y;
|
| 73 | this.motionZ = z;
|
| 74 |
|
| 75 | if (this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F)
|
| 76 | {
|
| 77 | float f = MathHelper.sqrt_double(x * x + z * z);
|
| 78 | this.prevRotationYaw = this.rotationYaw = (float)(MathHelper.atan2(x, z) * 180.0D / Math.PI);
|
| 79 | this.prevRotationPitch = this.rotationPitch = (float)(MathHelper.atan2(y, (double)f) * 180.0D / Math.PI);
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | /**
|
| 84 | * Called to update the entity's position/logic.
|
| 85 | */
|
| 86 | public void onUpdate()
|
| 87 | {
|
| 88 | this.lastTickPosX = this.posX;
|
| 89 | this.lastTickPosY = this.posY;
|
| 90 | this.lastTickPosZ = this.posZ;
|
| 91 | super.onUpdate();
|
| 92 | this.motionX *= 1.15D;
|
| 93 | this.motionZ *= 1.15D;
|
| 94 | this.motionY += 0.04D;
|
| 95 | this.moveEntity(this.motionX, this.motionY, this.motionZ);
|
| 96 | float f = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
|
| 97 | this.rotationYaw = (float)(MathHelper.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);
|
| 98 |
|
| 99 | for (this.rotationPitch = (float)(MathHelper.atan2(this.motionY, (double)f) * 180.0D / Math.PI); this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F)
|
| 100 | {
|
| 101 | ;
|
| 102 | }
|
| 103 |
|
| 104 | while (this.rotationPitch - this.prevRotationPitch >= 180.0F)
|
| 105 | {
|
| 106 | this.prevRotationPitch += 360.0F;
|
| 107 | }
|
| 108 |
|
| 109 | while (this.rotationYaw - this.prevRotationYaw < -180.0F)
|
| 110 | {
|
| 111 | this.prevRotationYaw -= 360.0F;
|
| 112 | }
|
| 113 |
|
| 114 | while (this.rotationYaw - this.prevRotationYaw >= 180.0F)
|
| 115 | {
|
| 116 | this.prevRotationYaw += 360.0F;
|
| 117 | }
|
| 118 |
|
| 119 | this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
|
| 120 | this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
|
| 121 |
|
| 122 | if (this.fireworkAge == 0 && !this.isSilent())
|
| 123 | {
|
| 124 | this.worldObj.playSoundAtEntity(this, "fireworks.launch", 3.0F, 1.0F);
|
| 125 | }
|
| 126 |
|
| 127 | ++this.fireworkAge;
|
| 128 |
|
| 129 | if (this.worldObj.isRemote && this.fireworkAge % 2 < 2)
|
| 130 | {
|
| 131 | this.worldObj.spawnParticle(EnumParticleTypes.FIREWORKS_SPARK, this.posX, this.posY - 0.3D, this.posZ, this.rand.nextGaussian() * 0.05D, -this.motionY * 0.5D, this.rand.nextGaussian() * 0.05D, new int[0]);
|
| 132 | }
|
| 133 |
|
| 134 | if (!this.worldObj.isRemote && this.fireworkAge > this.lifetime)
|
| 135 | {
|
| 136 | this.worldObj.setEntityState(this, (byte)17);
|
| 137 | this.setDead();
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | public void handleStatusUpdate(byte id)
|
| 142 | {
|
| 143 | if (id == 17 && this.worldObj.isRemote)
|
| 144 | {
|
| 145 | ItemStack itemstack = this.dataWatcher.getWatchableObjectItemStack(8);
|
| 146 | NBTTagCompound nbttagcompound = null;
|
| 147 |
|
| 148 | if (itemstack != null && itemstack.hasTagCompound())
|
| 149 | {
|
| 150 | nbttagcompound = itemstack.getTagCompound().getCompoundTag("Fireworks");
|
| 151 | }
|
| 152 |
|
| 153 | this.worldObj.makeFireworks(this.posX, this.posY, this.posZ, this.motionX, this.motionY, this.motionZ, nbttagcompound);
|
| 154 | }
|
| 155 |
|
| 156 | super.handleStatusUpdate(id);
|
| 157 | }
|
| 158 |
|
| 159 | /**
|
| 160 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 161 | */
|
| 162 | public void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 163 | {
|
| 164 | tagCompound.setInteger("Life", this.fireworkAge);
|
| 165 | tagCompound.setInteger("LifeTime", this.lifetime);
|
| 166 | ItemStack itemstack = this.dataWatcher.getWatchableObjectItemStack(8);
|
| 167 |
|
| 168 | if (itemstack != null)
|
| 169 | {
|
| 170 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 171 | itemstack.writeToNBT(nbttagcompound);
|
| 172 | tagCompound.setTag("FireworksItem", nbttagcompound);
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | /**
|
| 177 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 178 | */
|
| 179 | public void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 180 | {
|
| 181 | this.fireworkAge = tagCompund.getInteger("Life");
|
| 182 | this.lifetime = tagCompund.getInteger("LifeTime");
|
| 183 | NBTTagCompound nbttagcompound = tagCompund.getCompoundTag("FireworksItem");
|
| 184 |
|
| 185 | if (nbttagcompound != null)
|
| 186 | {
|
| 187 | ItemStack itemstack = ItemStack.loadItemStackFromNBT(nbttagcompound);
|
| 188 |
|
| 189 | if (itemstack != null)
|
| 190 | {
|
| 191 | this.dataWatcher.updateObject(8, itemstack);
|
| 192 | }
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | /**
|
| 197 | * Gets how bright this entity is.
|
| 198 | */
|
| 199 | public float getBrightness(float partialTicks)
|
| 200 | {
|
| 201 | return super.getBrightness(partialTicks);
|
| 202 | }
|
| 203 |
|
| 204 | public int getBrightnessForRender(float partialTicks)
|
| 205 | {
|
| 206 | return super.getBrightnessForRender(partialTicks);
|
| 207 | }
|
| 208 |
|
| 209 | /**
|
| 210 | * If returns false, the item will not inflict any damage against entities.
|
| 211 | */
|
| 212 | public boolean canAttackWithItem()
|
| 213 | {
|
| 214 | return false;
|
| 215 | }
|
| 216 | }
|
| 217 |
|