package com.fireball1725.twitchnotifier.entity; import com.google.common.base.Optional; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.SoundEvents; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.datasync.DataParameter; import net.minecraft.network.datasync.DataSerializers; import net.minecraft.network.datasync.EntityDataManager; import net.minecraft.util.EnumParticleTypes; import net.minecraft.util.SoundCategory; import net.minecraft.util.math.MathHelper; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import sun.rmi.log.LogHandler; public class EntityFakeFireworkRocket extends Entity { private static final DataParameter<Optional<ItemStack>> FIREWORK_ITEM = EntityDataManager.<Optional<ItemStack>>createKey(EntityFakeFireworkRocket.class, DataSerializers.OPTIONAL_ITEM_STACK); /** The age of the firework in ticks. */ private int fireworkAge; /** The lifetime of the firework in ticks. When the age reaches the lifetime the firework explodes. */ private int lifetime; public EntityFakeFireworkRocket(World worldIn) { super(worldIn); this.setSize(0.25F, 0.25F); } protected void entityInit() { this.dataWatcher.register(FIREWORK_ITEM, Optional.<ItemStack>absent()); } /** * Checks if the entity is in range to render by using the past in distance and comparing it to its average edge * length * 64 * renderDistanceWeight Args: distance */ @SideOnly(Side.CLIENT) public boolean isInRangeToRenderDist(double distance) { return distance < 4096.0D; } public EntityFakeFireworkRocket(World worldIn, double x, double y, double z, ItemStack givenItem) { super(worldIn); this.fireworkAge = 0; this.setSize(0.25F, 0.25F); this.setPosition(x, y, z); int i = 1; if (givenItem != null && givenItem.hasTagCompound()) { this.dataWatcher.set(FIREWORK_ITEM, Optional.of(givenItem)); NBTTagCompound nbttagcompound = givenItem.getTagCompound(); NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("Fireworks"); i += nbttagcompound1.getByte("Flight"); } this.motionX = this.rand.nextGaussian() * 0.001D; this.motionZ = this.rand.nextGaussian() * 0.001D; this.motionY = 0.05D; this.lifetime = 10 * i + this.rand.nextInt(6) + this.rand.nextInt(7); } /** * Sets the velocity to the args. Args: x, y, z */ @SideOnly(Side.CLIENT) public void setVelocity(double x, double y, double z) { this.motionX = x; this.motionY = y; this.motionZ = z; if (this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F) { float f = MathHelper.sqrt_double(x * x + z * z); this.prevRotationYaw = this.rotationYaw = (float)(MathHelper.atan2(x, z) * (180D / Math.PI)); this.prevRotationPitch = this.rotationPitch = (float)(MathHelper.atan2(y, (double)f) * (180D / Math.PI)); } } /** * Called to update the entity's position/logic. */ public void onUpdate() { this.lastTickPosX = this.posX; this.lastTickPosY = this.posY; this.lastTickPosZ = this.posZ; super.onUpdate(); this.motionX *= 1.15D; this.motionZ *= 1.15D; this.motionY += 0.04D; this.moveEntity(this.motionX, this.motionY, this.motionZ); float f = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ); this.rotationYaw = (float)(MathHelper.atan2(this.motionX, this.motionZ) * (180D / Math.PI)); for (this.rotationPitch = (float)(MathHelper.atan2(this.motionY, (double)f) * (180D / Math.PI)); this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F) { ; } while (this.rotationPitch - this.prevRotationPitch >= 180.0F) { this.prevRotationPitch += 360.0F; } while (this.rotationYaw - this.prevRotationYaw < -180.0F) { this.prevRotationYaw -= 360.0F; } while (this.rotationYaw - this.prevRotationYaw >= 180.0F) { this.prevRotationYaw += 360.0F; } this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F; this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F; if (this.fireworkAge == 0 && this.worldObj.isRemote) { this.worldObj.playSound(this.posX, this.posY, this.posZ, SoundEvents.entity_firework_launch, SoundCategory.AMBIENT, 3.0F, 1.0F, true); } ++this.fireworkAge; if (this.worldObj.isRemote && this.fireworkAge % 2 < 2) { 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]); } if (this.worldObj.isRemote && this.fireworkAge > this.lifetime) { this.handleStatusUpdate((byte)17); this.setDead(); } } @SideOnly(Side.CLIENT) public void handleStatusUpdate(byte id) { if (id == 17 && this.worldObj.isRemote) { ItemStack itemstack = (ItemStack)((Optional)this.dataWatcher.get(FIREWORK_ITEM)).orNull(); NBTTagCompound nbttagcompound = null; if (itemstack != null && itemstack.hasTagCompound()) { nbttagcompound = itemstack.getTagCompound().getCompoundTag("Fireworks"); } this.worldObj.makeFireworks(this.posX, this.posY, this.posZ, this.motionX, this.motionY, this.motionZ, nbttagcompound); } super.handleStatusUpdate(id); } /** * (abstract) Protected helper method to write subclass entity data to NBT. */ public void writeEntityToNBT(NBTTagCompound tagCompound) { tagCompound.setInteger("Life", this.fireworkAge); tagCompound.setInteger("LifeTime", this.lifetime); ItemStack itemstack = (ItemStack)((Optional)this.dataWatcher.get(FIREWORK_ITEM)).orNull(); if (itemstack != null) { NBTTagCompound nbttagcompound = new NBTTagCompound(); itemstack.writeToNBT(nbttagcompound); tagCompound.setTag("FireworksItem", nbttagcompound); } } /** * (abstract) Protected helper method to read subclass entity data from NBT. */ public void readEntityFromNBT(NBTTagCompound tagCompund) { this.fireworkAge = tagCompund.getInteger("Life"); this.lifetime = tagCompund.getInteger("LifeTime"); NBTTagCompound nbttagcompound = tagCompund.getCompoundTag("FireworksItem"); if (nbttagcompound != null) { ItemStack itemstack = ItemStack.loadItemStackFromNBT(nbttagcompound); if (itemstack != null) { this.dataWatcher.set(FIREWORK_ITEM, Optional.of(itemstack)); } } } /** * Gets how bright this entity is. */ public float getBrightness(float partialTicks) { return super.getBrightness(partialTicks); } @SideOnly(Side.CLIENT) public int getBrightnessForRender(float partialTicks) { return super.getBrightnessForRender(partialTicks); } /** * If returns false, the item will not inflict any damage against entities. */ public boolean canAttackWithItem() { return false; } }