Solved How to make a firework explosion when you hit a player.

Discussion in 'Spigot Plugin Development' started by fujiboy4, Jun 7, 2016.

Thread Status:
Not open for further replies.
  1. Here is my current code. I'm trying to make it so that way when I hit a player it will launch a firework at the location of the player and instantly explode...
    Code (Text):
    final Firework f = (Firework) p.getWorld().spawn(p.getLocation(), Firework.class);
                    FireworkMeta fm = f.getFireworkMeta();
                            .withColor(Color.BLUE, Color.AQUA).build());

                    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {

                        public void run() {
                    }, 1L);
    All I hear is the noise of the firework. No explosion.

    Any help?
  2. Code (Text):
    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();
           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.setInt(entityFirework, expectedLifespan.getInt(entityFirework) - 1);
           } catch (Exception ex) {
       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;

  3. CraftBukkit or spigot?
  4. Either one should work fine. Try it and if it doesn't work try the other one. @fujiboy4
  5. Ok
  6. Works. :D Thanks.
Thread Status:
Not open for further replies.