Resource InstantFirework - Create firework explosion effects without launch sound

Discussion in 'Plugin Help/Development/Requests' started by TehHypnoz, Jan 27, 2015.

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

    TehHypnoz

    I made this class for my EmpireWand plugin because I wanted to spawn instantly-exploding fireworks without launch sound. If anyone has any suggestions on how to improve it, post them please. This is pretty much the second time I have worked with reflection, so sorry if I made any stupid mistakes.

    Class:
    Code:
    package yourpackage;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    import org.bukkit.Bukkit;
    import org.bukkit.FireworkEffect;
    import org.bukkit.Location;
    import org.bukkit.entity.Firework;
    import org.bukkit.inventory.meta.FireworkMeta;
    
    public class InstantFirework {
    
       
        /*
        * InstantFirework class made by TehHypnoz.
        *
        * Credits to:
        * 
        * - fromgate, for explaining that setting the ticksFlown field to the expectedLifespan field will create instant fireworks.
        * - Skionz, for the getNMSClass() method.
        * 
        * Example usage:
        * FireworkEffect fireworkEffect = FireworkEffect.builder().flicker(false).trail(true).with(Type.BALL).withColor(Color.ORANGE).withFade(Color.RED).build();
        * Location location = p.getLocation();
        * new InstantFirework(fireworkEffect, location);
        */
       
       
        public InstantFirework(FireworkEffect fe, Location loc) {
            Firework f = (Firework) loc.getWorld().spawn(loc, Firework.class);
            FireworkMeta fm = f.getFireworkMeta();
            fm.addEffect(fe);
            f.setFireworkMeta(fm);
            try {
                Class<?> entityFireworkClass = getClass("net.minecraft.server.", "EntityFireworks");
                Class<?> craftFireworkClass = getClass("org.bukkit.craftbukkit.", "entity.CraftFirework");
                Object firework = craftFireworkClass.cast(f);
                Method handle = firework.getClass().getMethod("getHandle");
                Object entityFirework = handle.invoke(firework);
                Field expectedLifespan = entityFireworkClass.getDeclaredField("expectedLifespan");
                Field ticksFlown = entityFireworkClass.getDeclaredField("ticksFlown");
                ticksFlown.setAccessible(true);
                ticksFlown.setInt(entityFirework, expectedLifespan.getInt(entityFirework) - 1);
                ticksFlown.setAccessible(false);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
       
        private Class<?> getClass(String prefix, String nmsClassString) throws ClassNotFoundException {
            String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + ".";
            String name = prefix + version + nmsClassString;
            Class<?> nmsClass = Class.forName(name);
            return nmsClass;
        }
    
    }
    
    Credits and example usage are in the code.
     
    Last edited: Jan 27, 2015
  2. Offline

    ChipDev

    Does it support 1.8?
     
  3. Offline

    TehHypnoz

    Haven't tested it with 1.8, only with Spigot build #1649.
     
  4. Offline

    ChipDev

    It works on 1.8! My god! Awesome!
     
    MrAwellstein likes this.
  5. Online

    timtower Moderator Moderator

    Moved to Bukkit alternatives.
     
    ChipDev likes this.
  6. Offline

    TehHypnoz

    Could it be moved back if I test it with Craftbukkit?
     
  7. Online

    timtower Moderator Moderator

    Nope.
     
  8. Offline

    TehHypnoz

    No one else needs this?
     
  9. Offline

    Madricas

    Thank you so much it's pretty useful
     
  10. Offline

    TehHypnoz

    No problem :)
     
    Madricas likes this.
Thread Status:
Not open for further replies.

Share This Page