From b61657e35c5fafa1bfb1634a804b291f91aa7a10 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Sun, 24 Aug 2014 08:19:31 -0400 Subject: [PATCH] Added a FireworkEffectPlayer Added some methods to play Firework effects --- .../projectkorra/ProjectKorra/Methods.java | 10 +++ .../Utilities/FireworkEffectPlayer.java | 67 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/com/projectkorra/ProjectKorra/Utilities/FireworkEffectPlayer.java diff --git a/src/com/projectkorra/ProjectKorra/Methods.java b/src/com/projectkorra/ProjectKorra/Methods.java index ab880255..49b80cb5 100644 --- a/src/com/projectkorra/ProjectKorra/Methods.java +++ b/src/com/projectkorra/ProjectKorra/Methods.java @@ -28,7 +28,10 @@ import net.sacredlabyrinth.Phaed.PreciousStones.PreciousStones; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Color; import org.bukkit.Effect; +import org.bukkit.FireworkEffect; +import org.bukkit.FireworkEffect.Type; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -69,6 +72,7 @@ import com.palmergames.bukkit.towny.war.flagwar.TownyWarConfig; import com.projectkorra.ProjectKorra.Ability.AbilityModule; import com.projectkorra.ProjectKorra.Ability.AbilityModuleManager; import com.projectkorra.ProjectKorra.Ability.AvatarState; +import com.projectkorra.ProjectKorra.Utilities.FireworkEffectPlayer; import com.projectkorra.ProjectKorra.Utilities.ParticleEffect; import com.projectkorra.ProjectKorra.airbending.AirBlast; import com.projectkorra.ProjectKorra.airbending.AirBubble; @@ -1852,5 +1856,11 @@ public class Methods { } } } + + public static FireworkEffectPlayer fireworkeffectplayer = new FireworkEffectPlayer(); + + public static FireworkEffect customFireworkEffect(Type type, Color color) { + return FireworkEffect.builder().with(type).withColor(color).build(); + } } diff --git a/src/com/projectkorra/ProjectKorra/Utilities/FireworkEffectPlayer.java b/src/com/projectkorra/ProjectKorra/Utilities/FireworkEffectPlayer.java new file mode 100644 index 00000000..eaffe1d8 --- /dev/null +++ b/src/com/projectkorra/ProjectKorra/Utilities/FireworkEffectPlayer.java @@ -0,0 +1,67 @@ +package com.projectkorra.ProjectKorra.Utilities; + +import java.lang.reflect.Method; + +import org.bukkit.FireworkEffect; +import org.bukkit.entity.Firework; +import org.bukkit.inventory.meta.FireworkMeta; +import org.bukkit.Location; +import org.bukkit.World; + +/** + * FireworkEffectPlayer v1.0 + * + * FireworkEffectPlayer provides a thread-safe and (reasonably) version independant way to instantly explode a FireworkEffect at a given location. + * You are welcome to use, redistribute, modify and destroy your own copies of this source with the following conditions: + * + * 1. No warranty is given or implied. + * 2. All damage is your own responsibility. + * 3. You provide credit publicly to the original source should you release the plugin. + * + * @author codename_B + */ +public class FireworkEffectPlayer { + + private Method world_getHandle = null; + private Method nms_world_broadcastEntityEffect = null; + private Method firework_getHandle = null; + + public void playFirework(World world, Location loc, FireworkEffect fe) throws Exception { + Firework fw = (Firework) world.spawn(loc, Firework.class); + Object nms_world = null; + Object nms_firework = null; + if(world_getHandle == null) { + world_getHandle = getMethod(world.getClass(), "getHandle"); + firework_getHandle = getMethod(fw.getClass(), "getHandle"); + } + nms_world = world_getHandle.invoke(world, (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(); + } + + private static Method getMethod(Class cl, String method) { + for(Method m : cl.getMethods()) { + if(m.getName().equals(method)) { + return m; + } + } + return null; + } + +} \ No newline at end of file