Created
December 16, 2014 09:43
-
-
Save keepoff07/3fdf0c723ae4b42e02cd to your computer and use it in GitHub Desktop.
[Bukkit] FireworkEffectAPI: Easy API to create FireworkEffect.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Method; | |
import org.bukkit.Color; | |
import org.bukkit.FireworkEffect; | |
import org.bukkit.Location; | |
import org.bukkit.World; | |
import org.bukkit.entity.Firework; | |
import org.bukkit.inventory.meta.FireworkMeta; | |
public class FireworkEffectAPI { | |
/** | |
* If you correctly create, return true. | |
* @param loc createLocation | |
* @param effects | |
* @return boolean | |
*/ | |
public static boolean createFireworkEffect(Location loc, FireworkEffect... effects){ | |
try{ | |
if(effects.length == 0) return false; | |
World world = loc.getWorld(); | |
Firework firework = (Firework)world.spawn(loc, Firework.class); | |
FireworkMeta fireworkMeta = firework.getFireworkMeta(); | |
for(FireworkEffect effect : effects){ | |
fireworkMeta.addEffect(effect); | |
} | |
fireworkMeta.setPower(0); | |
firework.setFireworkMeta(fireworkMeta); | |
Method worldHandle = getMethod(world, "getHandle"); | |
Method fireworkHandle = getMethod(firework, "getHandle"); | |
Object WorldServer = worldHandle.invoke(world, (Object[])null); | |
Object EntityFireworks = fireworkHandle.invoke(firework, (Object[])null); | |
Method broadcastEntityEffect = getMethod(WorldServer, "broadcastEntityEffect"); | |
broadcastEntityEffect.invoke(WorldServer, new Object[] { EntityFireworks, Byte.valueOf((byte)17) }); | |
firework.remove(); | |
return true; | |
}catch(Exception ex){ | |
return false; | |
} | |
} | |
private static Method getMethod(Object obj, String method) throws Exception{ | |
for (Method m : obj.getClass().getDeclaredMethods()) { | |
if (m.getName().equals(method)) { | |
return m; | |
} | |
}return null; | |
} | |
public static FireworkEffect buildEffect(Color[] color, Color[] fadecolor, FireworkEffect.Type type, boolean flicker, boolean trail){ | |
FireworkEffect.Builder build = FireworkEffect.builder(); | |
if(color != null) build.withColor(color); | |
else build.withColor(Color.RED); | |
if(fadecolor != null) build.withFade(fadecolor); | |
if(type != null) build.with(type); | |
else build.with(FireworkEffect.Type.BALL); | |
build.flicker(flicker); | |
build.trail(trail); | |
return build.build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Location location; | |
FireworkEffect[] effects = new FireworkEffect[3]; | |
effects[0] = FireworkEffectAPI.buildEffect(new Color[]{Color.AQUA}, null, FireworkEffect.Type.BALL, false, false); | |
effects[1] = FireworkEffectAPI.buildEffect(new Color[]{Color.ORANGE}, new Color[]{Color.YELLOW}, FireworkEffect.Type.BALL_LARGE, true, false); | |
effects[1] = FireworkEffectAPI.buildEffect(new Color[]{Color.GREEN,Color.WHITE,Color.OLIVE}, null, FireworkEffect.Type.CREEPER, false, false); | |
FireworkEffectAPI.createFireworkEffect(location, effects); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
概要 | |
花火のエフェクトを発生させるサポートをするクラスです。 | |
FireworkEffectAPI.createFireworkEffect よりエフェクトを発生させられます。 | |
また、buildEffectを使うことでFireworkEffectの作成もサポートします。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice ;)