Spawning a Custom Firework Entity

Discussion in 'Plugin Development' started by Fuzzwolf, Jun 2, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Fuzzwolf

    EDIT: I have a temporary fix below but the question of why the rocket was deleting itself still remains

    I'm very new to tweaking the net.minecraft.server classes and I appear to be running in to issues getting a custom firework entity to spawn. I think what I'm missing is rather simple; I am attempting to spawn a firework which adheres to some redefined physics algorithms for my plugin. My question is: how do I spawn that custom rocket?

    A better question to ask would be that of spawning an EntityFireworks object. At this time, Rockets is an exact duplicate of the EntityFireworks class it extends for testing purposes. Whenever the entity is spawned (or EntityFireworks in general), it launches and ascends a minuscule amount before despawning (the launch sound is audible and a brief particle effect is visible) which is the problem. I've tried removing the despawn function built within the Fireworks nms class which runs whenever the particular rocket's ticksFlown is equal to its expectedLifeSpan, but the rocket still disappears.

    If anybody could shed some light on how I should go about spawning a custom rocket entity, I would be most appreciative!

    The Spawn Method:
    Code:java
    1.  
    2. @EventHandler
    3. public void onRightClick(PlayerInteractEvent i){
    4. if(i.getAction() == Action.RIGHT_CLICK_AIR && i.getItem().getType() == Material.GOLD_HOE){
    5.  
    6.  
    7. Location l = i.getPlayer().getLocation();
    8. net.minecraft.server.v1_5_R3.World mcWorld = ((CraftWorld) i.getPlayer().getWorld()).getHandle();
    9. Rockets r = new Rockets(mcWorld);
    10. r.setPosition(l.getX()+3, l.getY(), l.getZ());
    11. mcWorld.addEntity(r);
    12. }
    13.  
    14.  
    15. }
    16.  


    The Rockets class (exact copy of NMS EntityFireworks at this time):
    Code:java
    1.  
    2. package FireRockets;
    3.  
    4. import net.minecraft.server.v1_5_R3.Entity;
    5. import net.minecraft.server.v1_5_R3.EntityFireworks;
    6. import net.minecraft.server.v1_5_R3.ItemStack;
    7. import net.minecraft.server.v1_5_R3.MathHelper;
    8. import net.minecraft.server.v1_5_R3.NBTTagCompound;
    9. import net.minecraft.server.v1_5_R3.World;
    10.  
    11. public class Rockets extends EntityFireworks {
    12.  
    13. private int ticksFlown;
    14. public int expectedLifespan; // CraftBukkit - private -> public
    15.  
    16. public Rockets(World world) {
    17. super(world);
    18. super.a(0.25F, 0.25F);
    19. }
    20.  
    21. protected void a() {
    22. this.datawatcher.a(8, 5);
    23. }
    24.  
    25. public Rockets(World world, double d0, double d1, double d2, ItemStack itemstack) {
    26. super(world);
    27. this.ticksFlown = 0;
    28. this.a(0.25F, 0.25F);
    29. this.setPosition(d0, d1, d2);
    30. this.height = 0.0F;
    31. int i = 1;
    32.  
    33. if (itemstack != null && itemstack.hasTag()) {
    34. this.datawatcher.watch(8, itemstack);
    35. NBTTagCompound nbttagcompound = itemstack.getTag();
    36. NBTTagCompound nbttagcompound1 = nbttagcompound.getCompound("Fireworks");
    37.  
    38. if (nbttagcompound1 != null) {
    39. i += nbttagcompound1.getByte("Flight");
    40. }
    41. }
    42.  
    43. this.motX = this.random.nextGaussian() * 0.001D;
    44. this.motZ = this.random.nextGaussian() * 0.001D;
    45. this.motY = 0.05D;
    46. this.expectedLifespan = 10 * i + this.random.nextInt(6) + this.random.nextInt(7);
    47. }
    48.  
    49. public void l_() {
    50. this.U = this.locX;
    51. this.V = this.locY;
    52. this.W = this.locZ;
    53. super.l_();
    54. this.motX *= 1.15D;
    55. this.motZ *= 1.15D;
    56. this.motY += 0.04D;
    57. this.move(this.motX, this.motY, this.motZ);
    58. float f = MathHelper.sqrt(this.motX * this.motX + this.motZ * this.motZ);
    59.  
    60. this.yaw = (float) (Math.atan2(this.motX, this.motZ) * 180.0D / 3.1415927410125732D);
    61.  
    62. for (this.pitch = (float) (Math.atan2(this.motY, (double) f) * 180.0D / 3.1415927410125732D); this.pitch - this.lastPitch < -180.0F; this.lastPitch -= 360.0F) {
    63. ;
    64. }
    65.  
    66. while (this.pitch - this.lastPitch >= 180.0F) {
    67. this.lastPitch += 360.0F;
    68. }
    69.  
    70. while (this.yaw - this.lastYaw < -180.0F) {
    71. this.lastYaw -= 360.0F;
    72. }
    73.  
    74. while (this.yaw - this.lastYaw >= 180.0F) {
    75. this.lastYaw += 360.0F;
    76. }
    77.  
    78. this.pitch = this.lastPitch + (this.pitch - this.lastPitch) * 0.2F;
    79. this.yaw = this.lastYaw + (this.yaw - this.lastYaw) * 0.2F;
    80. if (this.ticksFlown == 0) {
    81. this.world.makeSound(this, "fireworks.launch", 3.0F, 1.0F);
    82. }
    83.  
    84. ++this.ticksFlown;
    85. if (this.world.isStatic && this.ticksFlown % 2 < 2) {
    86. this.world.addParticle("fireworksSpark", this.locX, this.locY - 0.3D, this.locZ, this.random.nextGaussian() * 0.05D, -this.motY * 0.5D, this.random.nextGaussian() * 0.05D);
    87. }
    88.  
    89.  
    90. if (!this.world.isStatic && this.ticksFlown > this.expectedLifespan) {
    91. this.world.broadcastEntityEffect(this, (byte) 17);
    92. this.die();
    93. }
    94. }
    95.  
    96. public void b(NBTTagCompound nbttagcompound) {
    97. nbttagcompound.setInt("Life", this.ticksFlown);
    98. nbttagcompound.setInt("LifeTime", this.expectedLifespan);
    99. ItemStack itemstack = this.datawatcher.getItemStack(8);
    100.  
    101. if (itemstack != null) {
    102. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
    103.  
    104. itemstack.save(nbttagcompound1);
    105. nbttagcompound.setCompound("FireworksItem", nbttagcompound1);
    106. }
    107. }
    108.  
    109. public void a(NBTTagCompound nbttagcompound) {
    110. this.ticksFlown = nbttagcompound.getInt("Life");
    111. this.expectedLifespan = nbttagcompound.getInt("LifeTime");
    112. NBTTagCompound nbttagcompound1 = nbttagcompound.getCompound("FireworksItem");
    113.  
    114. if (nbttagcompound1 != null) {
    115. ItemStack itemstack = ItemStack.createStack(nbttagcompound1);
    116.  
    117. if (itemstack != null) {
    118. this.datawatcher.watch(8, itemstack);
    119. }
    120. }
    121. }
    122.  
    123. public float c(float f) {
    124. return super.c(f);
    125. }
    126.  
    127. public boolean ap() {
    128. return false;
    129. }
    130. }
    131.  
     
  2. Offline

    microgeek

    You don't need all that code to achieve what you want. You can just use some reflection to call the method to explode the FireWork.
     
  3. Offline

    Fuzzwolf

    Could you elaborate a bit? Currently I'm trying to get the firework to travel at a straight vector from the player's eye location, though I haven't implemented that function yet as I still am having troubles preventing the firework from disappearing after a second or so.
     
  4. Offline

    Fuzzwolf

    Still not sure why the entity was deleting itself, though I have created a temporary fix. I override the die method and only execute it in the context of the superclass whenever the ticksFlown is equal to the lifeSpan, how the firework SHOULD work.

    Code:
        @Override
        public void die(){
            if (!this.world.isStatic && this.ticksFlown > this.expectedLifespan) {
                this.world.broadcastEntityEffect(this, (byte) 17);
                super.die();
            }
        }
    I still don't understand why the firework was deleting itself so I'll leave this open, though if I had to guess I'd say that die() was being called from the superclass Entity.
     
  5. I think it is caused by the client, because the client think it already has exploded, it despawns it
     
Thread Status:
Not open for further replies.

Share This Page