Added a FireworkEffectPlayer

Added some methods to play Firework effects
This commit is contained in:
AlexTheCoder 2014-08-24 08:19:31 -04:00
parent 2c7fd4c74f
commit b61657e35c
2 changed files with 77 additions and 0 deletions

View file

@ -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();
}
}

View file

@ -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;
}
}