Guest User

Untitled

a guest
Dec 30th, 2012
3,293
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.bpermissions;
  2.  
  3. import java.lang.reflect.Method;
  4.  
  5. import org.bukkit.FireworkEffect;
  6. import org.bukkit.entity.Firework;
  7. import org.bukkit.inventory.meta.FireworkMeta;
  8. import org.bukkit.Location;
  9. import org.bukkit.World;
  10.  
  11. /**
  12.  * FireworkEffectPlayer v1.0
  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 world_getHandle = null;
  42.     private Method nms_world_broadcastEntityEffect = null;
  43.     private Method firework_getHandle = null;
  44.    
  45.     /**
  46.      * Play a pretty firework at the location with the FireworkEffect when called
  47.      * @param world
  48.      * @param loc
  49.      * @param fe
  50.      * @throws Exception
  51.      */
  52.     public void playFirework(World world, Location loc, FireworkEffect fe) throws Exception {
  53.         // Bukkity load (CraftFirework)
  54.         Firework fw = (Firework) world.spawn(loc, Firework.class);
  55.         // the net.minecraft.server.World
  56.         Object nms_world = null;
  57.         Object nms_firework = null;
  58.         /*
  59.          * The reflection part, this gives us access to funky ways of messing around with things
  60.          */
  61.         if(world_getHandle == null) {
  62.             // get the methods of the craftbukkit objects
  63.             world_getHandle = getMethod(world.getClass(), "getHandle");
  64.             firework_getHandle = getMethod(fw.getClass(), "getHandle");
  65.         }
  66.         // invoke with no arguments
  67.         nms_world = world_getHandle.invoke(world, (Object[]) null);
  68.         nms_firework = firework_getHandle.invoke(fw, (Object[]) null);
  69.         // null checks are fast, so having this seperate is ok
  70.         if(nms_world_broadcastEntityEffect == null) {
  71.             // get the method of the nms_world
  72.             nms_world_broadcastEntityEffect = getMethod(nms_world.getClass(), "broadcastEntityEffect");
  73.         }
  74.         /*
  75.          * Now we mess with the metadata, allowing nice clean spawning of a pretty firework (look, pretty lights!)
  76.          */
  77.         // metadata load
  78.         FireworkMeta data = (FireworkMeta) fw.getFireworkMeta();
  79.         // clear existing
  80.         data.clearEffects();
  81.         // power of one
  82.         data.setPower(1);
  83.         // add the effect
  84.         data.addEffect(fe);
  85.         // set the meta
  86.         fw.setFireworkMeta(data);
  87.         /*
  88.          * Finally, we broadcast the entity effect then kill our fireworks object
  89.          */
  90.         // invoke with arguments
  91.         nms_world_broadcastEntityEffect.invoke(nms_world, new Object[] {nms_firework, (byte) 17});
  92.         // remove from the game
  93.         fw.remove();
  94.     }
  95.    
  96.     /**
  97.      * Internal method, used as shorthand to grab our method in a nice friendly manner
  98.      * @param cl
  99.      * @param method
  100.      * @return Method (or null)
  101.      */
  102.     private static Method getMethod(Class<?> cl, String method) {
  103.         for(Method m : cl.getMethods()) {
  104.             if(m.getName().equals(method)) {
  105.                 return m;
  106.             }
  107.         }
  108.         return null;
  109.     }
  110.  
  111. }
RAW Paste Data