Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. package me.minebuilders.MMP;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5.  
  6. import org.bukkit.FireworkEffect;
  7. import org.bukkit.entity.Firework;
  8. import org.bukkit.inventory.meta.FireworkMeta;
  9. import org.bukkit.Location;
  10. import org.bukkit.World;
  11.  
  12. /**
  13. *
  14. * FireworkEffectPlayer provides a thread-safe and (reasonably) version independant way to instantly explode a FireworkEffect at a given location.
  15. * You are welcome to use, redistribute, modify and destroy your own copies of this source with the following conditions:
  16. *
  17. * 1. No warranty is given or implied.
  18. * 2. All damage is your own responsibility.
  19. * 3. You provide credit publicly to the original source should you release the plugin.
  20. *
  21. * @author codename_B
  22. */
  23. public class FireworkEffectPlayer {
  24.  
  25. /*
  26. * Example use:
  27. *
  28. * public class FireWorkPlugin implements Listener {
  29. *
  30. * FireworkEffectPlayer fplayer = new FireworkEffectPlayer();
  31. *
  32. * @EventHandler
  33. * public void onPlayerLogin(PlayerLoginEvent event) {
  34. * fplayer.playFirework(event.getPlayer().getWorld(), event.getPlayer.getLocation(), Util.getRandomFireworkEffect());
  35. * }
  36. *
  37. * }
  38. */
  39.  
  40. // internal references, performance improvements
  41. private Method firework_getHandle = null;
  42.  
  43. /**
  44. * Play a pretty firework at the location with the FireworkEffect when called
  45. * @param world
  46. * @param loc
  47. * @param fe
  48. * @throws Exception
  49. */
  50. public void playFirework(World world, Location loc, FireworkEffect fe) throws Exception {
  51.  
  52. final Firework fw = (Firework) world.spawn(loc, Firework.class);
  53.  
  54. if(firework_getHandle == null) {
  55. firework_getHandle = getMethod(fw.getClass(), "getHandle");
  56. }
  57.  
  58. final Object nms_firework = firework_getHandle.invoke(fw, (Object[]) null);
  59.  
  60. FireworkMeta data = (FireworkMeta) fw.getFireworkMeta();
  61. data.clearEffects();
  62. data.setPower(1);
  63. data.addEffect(fe);
  64. fw.setFireworkMeta(data);
  65.  
  66. Field field = nms_firework.getClass().getDeclaredField("ticksFlown");
  67. field.setAccessible(true);
  68. field.set(nms_firework, 123);
  69. }
  70.  
  71. /**
  72. * Internal method, used as shorthand to grab our method in a nice friendly manner
  73. * @param cl
  74. * @param method
  75. * @return Method (or null)
  76. */
  77. private static Method getMethod(Class<?> cl, String method) {
  78. for(Method m : cl.getMethods()) {
  79. if(m.getName().equals(method)) {
  80. return m;
  81. }
  82. }
  83. return null;
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement