package com.elmakers.mine.bukkit.effect;

import java.lang.reflect.Method;

import org.bukkit.FireworkEffect;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Firework;
import org.bukkit.inventory.meta.FireworkMeta;

import com.elmakers.mine.bukkit.utility.NMSUtils;

public class EffectUtils extends NMSUtils {

    public static void spawnFireworkEffect(Location location, FireworkEffect effect, int power) {
        World world = location.getWorld();

        try {
            // Initialize the Firework object
            Firework firework = world.spawn(location, Firework.class);
            FireworkMeta meta = firework.getFireworkMeta();
            meta.addEffect(effect);
            meta.setPower(power);
            firework.setFireworkMeta(meta);

            // Use the firework entity to spawn an effect
            Object worldHandle = getHandle(world);
            Object fireworkHandle = getHandle(firework);
            Method broadcastMethod = class_World.getMethod("broadcastEntityEffect", class_Entity, Byte.TYPE);

            // 17 is a magic number from EntityFireworks.. I do not know what it means :\
            broadcastMethod.invoke(worldHandle,  fireworkHandle, (byte)17);

            // Remove the firework, we only needed it for the effec.t
            firework.remove();
        } catch (Exception ex) {

        }
    }
}

@@ -782,17 +782,16 @@
 (meta);%0A
-%0A
         
@@ -798,436 +798,26 @@
     
-// Use the firework entity to spawn an effect%0A            Object worldHandle = getHandle(world);%0A            Object fireworkHandle = getHandle(firework);%0A            Method broadcastMethod = class_World.getMethod(%22broadcastEntityEffect%22, class_Entity, Byte.TYPE);%0A%0A            // 17 is a magic number from EntityFireworks.. I do not know what it means :%5C%0A            broadcastMethod.invoke(worldHandle,  fireworkHandle, (byte)17
+firework.detonate(
 );%0A%0A
@@ -883,17 +883,16 @@
 he effec
-.
 t%0A      

Remove NMS for firework effects