Cleaned up Firework util.

This commit is contained in:
Jonathan Williams 2014-07-29 21:09:16 -07:00
parent 46cc2576dd
commit d129f722a3

View File

@ -1,69 +1,28 @@
package mineplex.core.common.util; package mineplex.core.common.util;
import java.lang.reflect.Method;
import org.bukkit.FireworkEffect; import org.bukkit.FireworkEffect;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_7_R3.entity.CraftEntity;
import org.bukkit.entity.Firework; import org.bukkit.entity.Firework;
import org.bukkit.inventory.meta.FireworkMeta; import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
public class UtilFirework public class UtilFirework
{ {
private static Method world_getHandle = null;
private static Method nms_world_broadcastEntityEffect = null;
private static Method firework_getHandle = null;
public static void playFirework(Location loc, FireworkEffect fe) public static void playFirework(Location loc, FireworkEffect fe)
{ {
try Firework firework = (Firework) loc.getWorld().spawn(loc, Firework.class);
{
Firework fw = (Firework) loc.getWorld().spawn(loc, Firework.class);
Object nms_world = null; FireworkMeta data = (FireworkMeta) firework.getFireworkMeta();
Object nms_firework = null; data.clearEffects();
data.setPower(1);
data.addEffect(fe);
firework.setFireworkMeta(data);
((CraftWorld)loc.getWorld()).getHandle().broadcastEntityEffect(((CraftEntity)firework).getHandle(), (byte)17);
if(world_getHandle == null) firework.remove();
{
world_getHandle = getMethod(loc.getWorld().getClass(), "getHandle");
firework_getHandle = getMethod(fw.getClass(), "getHandle");
}
nms_world = world_getHandle.invoke(loc.getWorld(), (Object[]) null);
nms_firework = firework_getHandle.invoke(fw, (Object[]) null);
if(nms_world_broadcastEntityEffect == null)
{
nms_world_broadcastEntityEffect = getMethod(nms_world.getClass(), "broadcastEntityEffect");
}
FireworkMeta data = (FireworkMeta) fw.getFireworkMeta();
data.clearEffects();
data.setPower(1);
data.addEffect(fe);
fw.setFireworkMeta(data);
nms_world_broadcastEntityEffect.invoke(nms_world, new Object[] {nms_firework, (byte) 17});
fw.remove();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static Method getMethod(Class<?> cl, String method)
{
for(Method m : cl.getMethods())
{
if(m.getName().equals(method))
{
return m;
}
}
return null;
} }
public static Firework launchFirework(Location loc, FireworkEffect fe, Vector dir, int power) public static Firework launchFirework(Location loc, FireworkEffect fe, Vector dir, int power)
@ -91,35 +50,10 @@ public class UtilFirework
return null; return null;
} }
public void detonateFirework(Firework fw) public void detonateFirework(Firework firework)
{ {
try ((CraftWorld)firework.getWorld()).getHandle().broadcastEntityEffect(((CraftEntity)firework).getHandle(), (byte)17);
{
Object nms_world = null;
Object nms_firework = null;
firework.remove();
if(world_getHandle == null)
{
world_getHandle = getMethod(fw.getWorld().getClass(), "getHandle");
firework_getHandle = getMethod(fw.getClass(), "getHandle");
}
nms_world = world_getHandle.invoke(fw.getWorld(), (Object[]) null);
nms_firework = firework_getHandle.invoke(fw, (Object[]) null);
if(nms_world_broadcastEntityEffect == null)
{
nms_world_broadcastEntityEffect = getMethod(nms_world.getClass(), "broadcastEntityEffect");
}
nms_world_broadcastEntityEffect.invoke(nms_world, new Object[] {nms_firework, (byte) 17});
fw.remove();
}
catch (Exception e)
{
e.printStackTrace();
}
} }
} }