[LIB] Instant Fireworks! No Launch Sound, No Visible Rocket, Just the Firework Effect

Discussion in 'Resources' started by NerdsWBNerds, May 19, 2014.

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

    NerdsWBNerds

    Spawn Exploding Fireworks!

    Finally, the code we've all been waiting for. An instant firework explosion effect!
    Finally, a reliable way to launch exploding fireworks.
    • No launch sound
    • No visible rocket (well, for 1 tick, but the firework effect basically hides it)
    • Just the explosion!
    • Works 100% of the time!
    You can send the firework effect to specific people, and the other people in the world won't see it (but those other people will, for about 1 tick, see the rocket and a trail).

    Code:java
    1.  
    2.  
    3. import net.minecraft.server.v1_7_R3.EntityFireworks;
    4. import net.minecraft.server.v1_7_R3.PacketPlayOutEntityStatus;
    5. import net.minecraft.server.v1_7_R3.World;
    6. import org.bukkit.FireworkEffect;
    7. import org.bukkit.Location;
    8. import org.bukkit.craftbukkit.v1_7_R3.CraftWorld;
    9. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftPlayer;
    10. import org.bukkit.entity.Firework;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.inventory.meta.FireworkMeta;
    13.  
    14. public class CustomEntityFirework extends EntityFireworks {
    15. Player[] players = null;
    16.  
    17. public CustomEntityFirework(World world, Player... p) {
    18. super(world);
    19. players = p;
    20. this.a(0.25F, 0.25F);
    21. }
    22.  
    23. boolean gone = false;
    24.  
    25. @Override
    26. public void h() {
    27. if (gone) {
    28. return;
    29. }
    30.  
    31. if (!this.world.isStatic) {
    32. gone = true;
    33.  
    34. if (players != null) {
    35. if (players.length > 0) {
    36. for (Player player : players) {
    37. (((CraftPlayer) player).getHandle()).playerConnection.sendPacket(new PacketPlayOutEntityStatus(this, (byte) 17));
    38. }
    39.  
    40. this.die();
    41. return;
    42. }
    43. }
    44.  
    45. world.broadcastEntityEffect(this, (byte) 17);
    46. this.die();
    47. }
    48. }
    49.  
    50. public static void spawn(Location location, FireworkEffect effect, Player... players) {
    51. try {
    52. CustomEntityFirework firework = new CustomEntityFirework(((CraftWorld) location.getWorld()).getHandle(), players);
    53. FireworkMeta meta = ((Firework) firework.getBukkitEntity()).getFireworkMeta();
    54. meta.addEffect(effect);
    55. ((Firework) firework.getBukkitEntity()).setFireworkMeta(meta);
    56. firework.setPosition(location.getX(), location.getY(), location.getZ());
    57.  
    58. if ((((CraftWorld) location.getWorld()).getHandle()).addEntity(firework)) {
    59. firework.setInvisible(true);
    60. }
    61. } catch (Exception e) {
    62. e.printStackTrace();
    63. }
    64. }
    65. }
    66.  
    67.  


    This is all the code you'll need to spawn it.

    Now to spawn a custom exploding firework, you'll have to first create a FireworkEffect (Search the forums for FireworkEffectBuilder), then you'll have to have the location you'd like to spawn it at.

    Now, simply use the following code.
    Code:java
    1.  
    2. // Get firework builder
    3. FireworkEffect.Builder builder = FireworkEffect.builder();
    4.  
    5. // Create a firework effect with the builder
    6. FireworkEffect effect = builder.flicker(false).trail(false).with(FireworkEffect.Type.BALL)withColor(Color.RED).withFade(Color.BLUE).build();
    7.  
    8. // Spawn our firework
    9. CustomEntityFirework.spawn(location, effect);
    10.  
    11. // Only show it to player player1, player2, and player3
    12. CustomEntityFirework.spawn(location, effect, player1, player2, player3);
    13.  
    14.  


    Updated Code:
    Allow sending firework explosion to only specific people (shows others rocket/trail for a tick, can't find a way to fix this)
    Cleaned up my code, removed a bunch of unnecessary stuff.
    Commented so people can understand what's happening.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  2. Offline

    Cryptite

    Works great. Nice work!
     
    NerdsWBNerds likes this.
  3. Offline

    Garris0n

    How, exactly, does this make the rocket invisible? Obviously, you can mask the sounds by removing the makeSound calls, however the rocket would still be visible, correct? Also, why not just send the spawn packet to the player instead of adding the entity to the world? Then nobody else will see the firework. Alternatively, you could do everything with packets.
     
    DSH105 and LegitJava like this.
  4. Offline

    NerdsWBNerds

    Currently, it doesn't make it invisible, but the rocket only appears for one tick, and the explosion covers it up so it's basically like it's not even there.

    I'll update the code to an all-packet version tomorrow, I'm going to bed for now.
     
  5. Offline

    DevRosemberg

    Being able to listen to the explosion is actually nice and a lot of people like it.
     
  6. Offline

    NerdsWBNerds


    Then you can just spawn a firework and detonate it after 1 tick.
     
  7. Offline

    Garris0n

    Or after 0 ticks?
     
  8. Offline

    NerdsWBNerds


    I only got 1 hour of sleep last night, so I'm just throwing out numbers at this point. Yeah you could probably spawn it and detonate it in the same tick, not sure though. For some reason I was thinking you had to wait a tick before you could detonate it, but you're probably right.


    This still has the explosion sound, as far as I'm aware there's no way to fix that (although there probably is, I'm looking into it). This only remove's the launching sound.

    I hope soon to be able to expand on this a little more so that if people want to have the launch/explode sounds, they can easily choose to.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
    mine-care likes this.
  9. Offline

    DevRosemberg

    NerdsWBNerds There is a simpler way of also playing the effect which is explained in my Lib.
     
  10. Offline

    ChipDev

  11. Offline

    MineStein

    Very nice work @NerdsWBNerds I am interested in using this in the future! Fantastic work!
     
  12. Offline

    Elimnator

    I "Searched the forums for FireworkEffectBuilder" but I didn't find any.
     
  13. Offline

    Regablith

    Elimnator Please don't bump this next time.
     
  14. Offline

    THEREDBARON24

    This is not working with 1.8 builds. http://prntscr.com/5nipvo I tried changing this
    FireworkMeta meta = ((Firework) firework.getBukkitEntity()).getFireworkMeta();
    so that it cast to CraftFirework, however, it still does not work. Any ideas?

    http://prntscr.com/5nitm9 Here is the full error with the Firework cast. Tell me if there is a way to fix it.
     
    Last edited by a moderator: Jan 2, 2015
  15. Offline

    ChipDev

    What is your problem? (Like in the code, No pun neither harm intended at all :p)
    Why do cast to (Firework) to get CraftFirework?
     
  16. Offline

    THEREDBARON24

    What? The issue is that it does not work with the latest builds. I understand that the cast is there so that the meta can be retrieved, however, that is also the line of code that throws the null pointer. Ideas?
     
  17. Offline

    recon88 Retired Staff

    I just updated my plugins which are using that LIB to 1.8 and encountered the same problem.
    Fixed the LIB if you still need it: https://github.com/recon88/Instant-Fireworks
    Didn't try yet but I'm pretty sure it works.
     
    Joiner likes this.
  18. Offline

    THEREDBARON24

    Great thanks.
     
  19. Offline

    recon88 Retired Staff

    Eballer48 likes this.
  20. Offline

    Eballer48

Thread Status:
Not open for further replies.

Share This Page