Advertisement
sheepcraft

Sheepcraft API

Jun 4th, 2015
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 42.90 KB | None | 0 0
  1. package uk.sheepcraft.api;
  2.  
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;
  7. import java.util.Arrays;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Random;
  12.  
  13. import net.milkbowl.vault.economy.Economy;
  14. import net.minecraft.server.v1_8_R1.ChatSerializer;
  15. import net.minecraft.server.v1_8_R1.EnumTitleAction;
  16. import net.minecraft.server.v1_8_R1.IChatBaseComponent;
  17. import net.minecraft.server.v1_8_R1.Packet;
  18. import net.minecraft.server.v1_8_R1.PacketPlayOutChat;
  19. import net.minecraft.server.v1_8_R1.PacketPlayOutTitle;
  20. import net.minecraft.server.v1_8_R1.PlayerConnection;
  21.  
  22. import org.apache.commons.lang.math.RandomUtils;
  23. import org.bukkit.Bukkit;
  24. import org.bukkit.ChatColor;
  25. import org.bukkit.Color;
  26. import org.bukkit.FireworkEffect;
  27. import org.bukkit.Location;
  28. import org.bukkit.Material;
  29. import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
  30. import org.bukkit.entity.Firework;
  31. import org.bukkit.entity.Player;
  32. import org.bukkit.inventory.meta.FireworkMeta;
  33. import org.bukkit.plugin.RegisteredServiceProvider;
  34. import org.bukkit.util.Vector;
  35.  
  36. import com.google.common.collect.Lists;
  37.  
  38. public class API {
  39.    
  40.     //You are welcome to use this in your plugins
  41.     //but please leave this in the Code :D
  42.     //Made by Sheepcraft
  43.    
  44.    
  45.     //more soon
  46.     public void moresoon()
  47.     {
  48.        
  49.     }
  50.    
  51.    
  52.     //Depend
  53.      public static boolean VAULT;
  54.       public static Economy VAULT_ECONOMY;
  55.      
  56.       public static final void checkDependencies()
  57.       {
  58.         VAULT = Bukkit.getPluginManager().getPlugin("Vault") != null;
  59.         if (VAULT)
  60.         {
  61.           for (RegisteredServiceProvider<?> t : Bukkit.getServicesManager().getRegistrations(Bukkit.getPluginManager().getPlugin("Vault"))) {
  62.             System.out.println(t.getProvider().getClass());
  63.           }
  64.           RegisteredServiceProvider<Economy> economyProvider = Bukkit.getServicesManager().getRegistration(Economy.class);
  65.           VAULT_ECONOMY = economyProvider != null ? economyProvider.getProvider() : null;
  66.           VAULT = VAULT_ECONOMY != null;
  67.         }
  68.       }
  69.    
  70.    
  71.    
  72.     //Money
  73.         public static void giveMoneyString(Player p, double string)
  74.         {
  75.             if (VAULT)
  76.             {
  77.               if (!VAULT_ECONOMY.hasAccount(p)) {
  78.                 VAULT_ECONOMY.createPlayerAccount(p);
  79.               }
  80.               if (string > 0.0D) {
  81.                 VAULT_ECONOMY.depositPlayer(p, string);
  82.               } else if (string > 0.0D) {
  83.                 VAULT_ECONOMY.withdrawPlayer(p, -string);
  84.               }
  85.             }
  86.         }
  87.      
  88.       public static void giveMoney(Player p, double price)
  89.       {
  90.         if (VAULT)
  91.         {
  92.           if (!VAULT_ECONOMY.hasAccount(p)) {
  93.             VAULT_ECONOMY.createPlayerAccount(p);
  94.           }
  95.           if (price > 0.0D) {
  96.             VAULT_ECONOMY.depositPlayer(p, price);
  97.           } else if (price < 0.0D) {
  98.             VAULT_ECONOMY.withdrawPlayer(p, -price);
  99.           }
  100.         }
  101.       }
  102.      
  103.       public static void setMoney(Player p, double price)
  104.       {
  105.         if (VAULT)
  106.         {
  107.           if (!VAULT_ECONOMY.hasAccount(p)) {
  108.             VAULT_ECONOMY.createPlayerAccount(p);
  109.           }
  110.           double balance = VAULT_ECONOMY.getBalance(p);
  111.           if (balance > 0.0D) {
  112.             VAULT_ECONOMY.withdrawPlayer(p, balance);
  113.           } else if (balance < 0.0D) {
  114.             VAULT_ECONOMY.depositPlayer(p, balance);
  115.           }
  116.           if (price > 0.0D) {
  117.             VAULT_ECONOMY.depositPlayer(p, price);
  118.           } else {
  119.             VAULT_ECONOMY.withdrawPlayer(p, -price);
  120.           }
  121.         }
  122.       }
  123.      
  124.       public static double getMoney(Player p)
  125.       {
  126.         if (VAULT)
  127.         {
  128.           if (!VAULT_ECONOMY.hasAccount(p)) {
  129.             VAULT_ECONOMY.createPlayerAccount(p);
  130.           }
  131.           return VAULT_ECONOMY.getBalance(p);
  132.         }
  133.         return 0.0D;
  134.       }
  135.      
  136.       public static boolean hasMoney(Player p, double price)
  137.       {
  138.         if ((VAULT) && (getMoney(p) >= price)) {
  139.           return true;
  140.         }
  141.         return false;
  142.       }
  143.    
  144.    
  145.    
  146.     //ReflectionUtils
  147.     public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... parameterTypes)
  148.             throws NoSuchMethodException
  149.           {
  150.             Class<?>[] primitiveTypes = DataType.getPrimitive(parameterTypes);
  151.             for (Constructor<?> constructor : clazz.getConstructors()) {
  152.               if (DataType.compare(DataType.getPrimitive(constructor.getParameterTypes()), primitiveTypes)) {
  153.                 return constructor;
  154.               }
  155.             }
  156.             throw new NoSuchMethodException("There is no such constructor in this class with the specified parameter types");
  157.           }
  158.          
  159.           public static Constructor<?> getConstructor(String className, PackageType packageType, Class<?>... parameterTypes)
  160.             throws NoSuchMethodException, ClassNotFoundException
  161.           {
  162.             return getConstructor(packageType.getClass(className), parameterTypes);
  163.           }
  164.          
  165.           public static Object instantiateObject(Class<?> clazz, Object... arguments)
  166.             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException
  167.           {
  168.             return getConstructor(clazz, DataType.getPrimitive(arguments)).newInstance(arguments);
  169.           }
  170.          
  171.           public static Object instantiateObject(String className, PackageType packageType, Object... arguments)
  172.             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException
  173.           {
  174.             return instantiateObject(packageType.getClass(className), arguments);
  175.           }
  176.          
  177.           public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
  178.             throws NoSuchMethodException
  179.           {
  180.             Class<?>[] primitiveTypes = DataType.getPrimitive(parameterTypes);
  181.             for (Method method : clazz.getMethods()) {
  182.               if ((method.getName().equals(methodName)) && (DataType.compare(DataType.getPrimitive(method.getParameterTypes()), primitiveTypes))) {
  183.                 return method;
  184.               }
  185.             }
  186.             throw new NoSuchMethodException("There is no such method in this class with the specified name and parameter types");
  187.           }
  188.          
  189.           public static Method getMethod(String className, PackageType packageType, String methodName, Class<?>... parameterTypes)
  190.             throws NoSuchMethodException, ClassNotFoundException
  191.           {
  192.             return getMethod(packageType.getClass(className), methodName, parameterTypes);
  193.           }
  194.          
  195.           public static Object invokeMethod(Object instance, String methodName, Object... arguments)
  196.             throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException
  197.           {
  198.             return getMethod(instance.getClass(), methodName, DataType.getPrimitive(arguments)).invoke(instance, arguments);
  199.           }
  200.          
  201.           public static Object invokeMethod(Object instance, Class<?> clazz, String methodName, Object... arguments)
  202.             throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException
  203.           {
  204.             return getMethod(clazz, methodName, DataType.getPrimitive(arguments)).invoke(instance, arguments);
  205.           }
  206.          
  207.           public static Object invokeMethod(Object instance, String className, PackageType packageType, String methodName, Object... arguments)
  208.             throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException
  209.           {
  210.             return invokeMethod(instance, packageType.getClass(className), methodName, arguments);
  211.           }
  212.          
  213.           public static Field getField(Class<?> clazz, boolean declared, String fieldName)
  214.             throws NoSuchFieldException, SecurityException
  215.           {
  216.             Field field = declared ? clazz.getDeclaredField(fieldName) : clazz.getField(fieldName);
  217.             field.setAccessible(true);
  218.             return field;
  219.           }
  220.          
  221.           public static Field getField(String className, PackageType packageType, boolean declared, String fieldName)
  222.             throws NoSuchFieldException, SecurityException, ClassNotFoundException
  223.           {
  224.             return getField(packageType.getClass(className), declared, fieldName);
  225.           }
  226.          
  227.           public static Object getValue(Object instance, Class<?> clazz, boolean declared, String fieldName)
  228.             throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
  229.           {
  230.             return getField(clazz, declared, fieldName).get(instance);
  231.           }
  232.          
  233.           public static Object getValue(Object instance, String className, PackageType packageType, boolean declared, String fieldName)
  234.             throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException
  235.           {
  236.             return getValue(instance, packageType.getClass(className), declared, fieldName);
  237.           }
  238.          
  239.           public static Object getValue(Object instance, boolean declared, String fieldName)
  240.             throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
  241.           {
  242.             return getValue(instance, instance.getClass(), declared, fieldName);
  243.           }
  244.          
  245.           public static void setValue(Object instance, Class<?> clazz, boolean declared, String fieldName, Object value)
  246.             throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
  247.           {
  248.             getField(clazz, declared, fieldName).set(instance, value);
  249.           }
  250.          
  251.           public static void setValue(Object instance, String className, PackageType packageType, boolean declared, String fieldName, Object value)
  252.             throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException
  253.           {
  254.             setValue(instance, packageType.getClass(className), declared, fieldName, value);
  255.           }
  256.          
  257.           public static void setValue(Object instance, boolean declared, String fieldName, Object value)
  258.             throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
  259.           {
  260.             setValue(instance, instance.getClass(), declared, fieldName, value);
  261.           }
  262.          
  263.           public static enum PackageType
  264.           {
  265.             MINECRAFT_SERVER("net.minecraft.server." + getServerVersion()),
  266.             CRAFTBUKKIT("org.bukkit.craftbukkit." + getServerVersion()),
  267.             CRAFTBUKKIT_BLOCK(CRAFTBUKKIT, "block"),
  268.             CRAFTBUKKIT_CHUNKIO(CRAFTBUKKIT, "chunkio"),
  269.             CRAFTBUKKIT_COMMAND(CRAFTBUKKIT, "command"),
  270.             CRAFTBUKKIT_CONVERSATIONS(CRAFTBUKKIT, "conversations"),
  271.             CRAFTBUKKIT_ENCHANTMENS(CRAFTBUKKIT, "enchantments"),
  272.             CRAFTBUKKIT_ENTITY(CRAFTBUKKIT, "entity"),
  273.             CRAFTBUKKIT_EVENT(CRAFTBUKKIT, "event"),
  274.             CRAFTBUKKIT_GENERATOR(CRAFTBUKKIT, "generator"),
  275.             CRAFTBUKKIT_HELP(CRAFTBUKKIT, "help"),
  276.             CRAFTBUKKIT_INVENTORY(CRAFTBUKKIT, "inventory"),
  277.             CRAFTBUKKIT_MAP(CRAFTBUKKIT, "map"),
  278.             CRAFTBUKKIT_METADATA(CRAFTBUKKIT, "metadata"),
  279.             CRAFTBUKKIT_POTION(CRAFTBUKKIT, "potion"),
  280.             CRAFTBUKKIT_PROJECTILES(CRAFTBUKKIT, "projectiles"),
  281.             CRAFTBUKKIT_SCHEDULER(CRAFTBUKKIT, "scheduler"),
  282.             CRAFTBUKKIT_SCOREBOARD(CRAFTBUKKIT, "scoreboard"),
  283.             CRAFTBUKKIT_UPDATER(CRAFTBUKKIT, "updater"),
  284.             CRAFTBUKKIT_UTIL(CRAFTBUKKIT, "util");
  285.            
  286.             private final String path;
  287.            
  288.             private PackageType(String path)
  289.             {
  290.               this.path = path;
  291.             }
  292.            
  293.             private PackageType(PackageType parent, String path)
  294.             {
  295.               this(parent + "." + path);
  296.             }
  297.            
  298.             public String getPath()
  299.             {
  300.               return this.path;
  301.             }
  302.            
  303.             public Class<?> getClass(String className)
  304.               throws ClassNotFoundException
  305.             {
  306.               return Class.forName(this + "." + className);
  307.             }
  308.            
  309.             public String toString()
  310.             {
  311.               return this.path;
  312.             }
  313.            
  314.             public static String getServerVersion()
  315.             {
  316.               return Bukkit.getServer().getClass().getPackage().getName().substring(23);
  317.             }
  318.           }
  319.          
  320.           public static enum DataType
  321.           {
  322.             BYTE(Byte.TYPE, Byte.class),
  323.             SHORT(Short.TYPE, Short.class),
  324.             INTEGER(Integer.TYPE, Integer.class),
  325.             LONG(Long.TYPE, Long.class),
  326.             CHARACTER(Character.TYPE, Character.class),
  327.             FLOAT(Float.TYPE, Float.class),
  328.             DOUBLE(Double.TYPE, Double.class),
  329.             BOOLEAN(Boolean.TYPE, Boolean.class);
  330.            
  331.             private static final Map<Class<?>, DataType> CLASS_MAP;
  332.             private final Class<?> primitive;
  333.             private final Class<?> reference;
  334.            
  335.             static
  336.             {
  337.               CLASS_MAP = new HashMap<Class<?>, DataType>();
  338.               for (DataType type : values())
  339.               {
  340.                 CLASS_MAP.put(type.primitive, type);
  341.                 CLASS_MAP.put(type.reference, type);
  342.               }
  343.             }
  344.            
  345.             private DataType(Class<?> primitive, Class<?> reference)
  346.             {
  347.               this.primitive = primitive;
  348.               this.reference = reference;
  349.             }
  350.            
  351.             public Class<?> getPrimitive()
  352.             {
  353.               return this.primitive;
  354.             }
  355.            
  356.             public Class<?> getReference()
  357.             {
  358.               return this.reference;
  359.             }
  360.            
  361.             public static DataType fromClass(Class<?> clazz)
  362.             {
  363.               return CLASS_MAP.get(clazz);
  364.             }
  365.            
  366.             public static Class<?> getPrimitive(Class<?> clazz)
  367.             {
  368.               DataType type = fromClass(clazz);
  369.               return type == null ? clazz : type.getPrimitive();
  370.             }
  371.            
  372.             public static Class<?> getReference(Class<?> clazz)
  373.             {
  374.               DataType type = fromClass(clazz);
  375.               return type == null ? clazz : type.getReference();
  376.             }
  377.            
  378.             public static Class<?>[] getPrimitive(Class<?>[] classes)
  379.             {
  380.               int length = classes == null ? 0 : classes.length;
  381.               Class<?>[] types = new Class[length];
  382.               for (int index = 0; index < length; index++) {
  383.                 types[index] = getPrimitive(classes[index]);
  384.               }
  385.               return types;
  386.             }
  387.            
  388.             public static Class<?>[] getReference(Class<?>[] classes)
  389.             {
  390.               int length = classes == null ? 0 : classes.length;
  391.               Class<?>[] types = new Class[length];
  392.               for (int index = 0; index < length; index++) {
  393.                 types[index] = getReference(classes[index]);
  394.               }
  395.               return types;
  396.             }
  397.            
  398.             public static Class<?>[] getPrimitive(Object[] objects)
  399.             {
  400.               int length = objects == null ? 0 : objects.length;
  401.               Class<?>[] types = new Class[length];
  402.               for (int index = 0; index < length; index++) {
  403.                 types[index] = getPrimitive(objects[index].getClass());
  404.               }
  405.               return types;
  406.             }
  407.            
  408.             public static Class<?>[] getReference(Object[] objects)
  409.             {
  410.               int length = objects == null ? 0 : objects.length;
  411.               Class<?>[] types = new Class[length];
  412.               for (int index = 0; index < length; index++) {
  413.                 types[index] = getReference(objects[index].getClass());
  414.               }
  415.               return types;
  416.             }
  417.            
  418.             public static boolean compare(Class<?>[] primary, Class<?>[] secondary)
  419.             {
  420.               if ((primary == null) || (secondary == null) || (primary.length != secondary.length)) {
  421.                 return false;
  422.               }
  423.               for (int index = 0; index < primary.length; index++)
  424.               {
  425.                 Class<?> primaryClass = primary[index];
  426.                 Class<?> secondaryClass = secondary[index];
  427.                 if ((!primaryClass.equals(secondaryClass)) && (!primaryClass.isAssignableFrom(secondaryClass))) {
  428.                   return false;
  429.                 }
  430.               }
  431.               return true;
  432.             }
  433.           }
  434.    
  435.    
  436.    
  437.    
  438.    
  439.             //cooldown time
  440.     private static HashMap<String, Long> cooldown = new HashMap<String, Long>();
  441.    
  442.     public static void putCooldown(Player p, int Seconds) {
  443.     cooldown.put(p.getName(), System.currentTimeMillis() + (Seconds * 1000L));
  444.     }
  445.  
  446.     public static int getCooldown(Player p) {
  447.     if (cooldown.containsKey(p.getName())) {
  448.     return Long.valueOf(((System.currentTimeMillis() - cooldown.get(p.getName())) / 1000)).intValue();
  449.     } else {
  450.     return 0;
  451.     }
  452.     }
  453.  
  454.     public static boolean isOnCooldown(Player p) {
  455.     if (cooldown.containsKey(p.getName()) && cooldown.get(p.getName()) > System.currentTimeMillis()) {
  456.     return true;
  457.     } else {
  458.     return false;
  459.     }
  460.     }
  461.  
  462.     public static void removeCooldown(Player p) {
  463.     if (cooldown.containsKey(p.getName())) {
  464.     cooldown.remove(p.getName());
  465.     }
  466.     }
  467.            
  468.            
  469.            
  470.            
  471.     //Colorize
  472.     private static final Random RANDOM = new Random();
  473.  
  474.     private static final char[] COLORS = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  475.     private static final char[] STYLES = {'l', 'n', 'o', 'k', 'm'};
  476.     private static final char[] ALL_COLORS = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'l', 'n', 'o', 'k', 'm'};
  477.  
  478.     public static String getRandomColorCode(boolean withExtra) {
  479.         char[] toRandomize = (withExtra ? ALL_COLORS : COLORS);
  480.  
  481.         return "&" + String.valueOf(toRandomize[RANDOM.nextInt(toRandomize.length)]);
  482.     }
  483.  
  484.     public static Stylize style(String toStyle) {
  485.         return new Stylize(toStyle);
  486.     }
  487.  
  488.     public static char[] getStyles() {
  489.         return STYLES;
  490.     }
  491.  
  492.     public static class Stylize {
  493.  
  494.         private final String toStyle;
  495.  
  496.         protected Stylize(String toStyle) {
  497.             this.toStyle = toStyle;
  498.         }
  499.  
  500.         public String toRainbow() {
  501.             StringBuilder sb = new StringBuilder();
  502.  
  503.             for (char c : toStyle.toCharArray()) {
  504.                 sb.append(API.getRandomColorCode(false) + String.valueOf(c));
  505.             }
  506.  
  507.             return ChatColor.translateAlternateColorCodes('&', sb.toString());
  508.         }
  509.  
  510.         public String toGarbage() {
  511.             List<Character> characters = Lists.newArrayList();
  512.            
  513.             for (char c : toStyle.toCharArray()) {
  514.                 characters.add(c);
  515.             }
  516.  
  517.             StringBuilder sb = new StringBuilder(toStyle.length());
  518.             while (!characters.isEmpty()) {
  519.                 int randPicker = (int) (Math.random() * characters.size());
  520.                 sb.append(characters.remove(randPicker));
  521.             }
  522.            
  523.             return sb.toString();
  524.         }
  525.        
  526.         public String toStripe(ChatColor colorOne, ChatColor colorTwo) {
  527.             StringBuilder sb = new StringBuilder();
  528.             boolean a = true;
  529.             for (char c : toStyle.toCharArray()) {
  530.                 sb.append(a ? colorOne : colorTwo);
  531.                 sb.append(c);
  532.             }
  533.            
  534.             return sb.toString();
  535.         }
  536.     }
  537.    
  538.  
  539.    
  540.    
  541.       //Titles
  542.     public static void sendTitle(Player player, String title, String subtitle, int fadeIn, int stay, int fadeOut)
  543.       {
  544.         CraftPlayer craftplayer = (CraftPlayer)player;
  545.         PlayerConnection connection = craftplayer.getHandle().playerConnection;
  546.         IChatBaseComponent titleJSON = ChatSerializer.a("{'text': '" + ChatColor.translateAlternateColorCodes('&', title) + "'}");
  547.         IChatBaseComponent subtitleJSON = ChatSerializer.a("{'text': '" + ChatColor.translateAlternateColorCodes('&', subtitle) + "'}");
  548.         Packet length = new PacketPlayOutTitle(EnumTitleAction.TIMES, titleJSON, fadeIn, stay, fadeOut);
  549.         Packet titlePacket = new PacketPlayOutTitle(EnumTitleAction.TITLE, titleJSON, fadeIn, stay, fadeOut);
  550.         Packet subtitlePacket = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, subtitleJSON, fadeIn, stay, fadeOut);
  551.         connection.sendPacket(titlePacket);
  552.         connection.sendPacket(length);
  553.         connection.sendPacket(subtitlePacket);
  554.       }
  555.    
  556.    
  557.     //Actionbar
  558.       public static boolean sendActionBar(Player player, String message)
  559.       {
  560.         CraftPlayer p = (CraftPlayer)player;
  561.         IChatBaseComponent cbc = ChatSerializer.a("{\"text\": \"" + message + "\"}");
  562.         PacketPlayOutChat ppoc = new PacketPlayOutChat(cbc, (byte)2);
  563.         p.getHandle().playerConnection.sendPacket(ppoc);
  564.         return false;
  565.       }
  566.      
  567.      
  568.       //Firework
  569.       public static void LaunchRandomFirework(Location location)
  570.       {
  571.         FireworkEffect.Builder builder = FireworkEffect.builder();
  572.         if (RandomUtils.nextInt(3) == 0) {
  573.           builder.withTrail();
  574.         } else if (RandomUtils.nextInt(2) == 0) {
  575.           builder.withFlicker();
  576.         }
  577.         builder.with(org.bukkit.FireworkEffect.Type.values()[RandomUtils.nextInt(org.bukkit.FireworkEffect.Type.values().length)]);
  578.        
  579.         int colorCount = 17;
  580.        
  581.         builder.withColor(Color.fromRGB(RandomUtils.nextInt(255), RandomUtils.nextInt(255), RandomUtils.nextInt(255)));
  582.         while (RandomUtils.nextInt(colorCount) != 0)
  583.         {
  584.           builder.withColor(Color.fromRGB(RandomUtils.nextInt(255), RandomUtils.nextInt(255), RandomUtils.nextInt(255)));
  585.           colorCount--;
  586.         }
  587.         Firework firework = location.getWorld().spawn(location, Firework.class);
  588.         FireworkMeta data = firework.getFireworkMeta();
  589.         data.addEffects(new FireworkEffect[] { builder.build() });
  590.         data.setPower(RandomUtils.nextInt(3));
  591.         firework.setFireworkMeta(data);
  592.       }
  593.      
  594.  
  595.     //ParticleEffect
  596.       public enum ParticleEffect
  597.       {
  598.         EXPLOSION_NORMAL("explode", 0, -1),
  599.         EXPLOSION_LARGE("largeexplode", 1, -1),
  600.         EXPLOSION_HUGE("hugeexplosion", 2, -1),
  601.         FIREWORKS_SPARK("fireworksSpark", 3, -1),
  602.         WATER_BUBBLE("bubble", 4, -1, false, true),
  603.         WATER_SPLASH("splash", 5, -1),
  604.         WATER_WAKE("wake", 6, 7),
  605.         SUSPENDED("suspended", 7, -1, false, true),
  606.         SUSPENDED_DEPTH("depthSuspend", 8, -1),
  607.         CRIT("crit", 9, -1),
  608.         CRIT_MAGIC("magicCrit", 10, -1),
  609.         SMOKE_NORMAL("smoke", 11, -1),
  610.         SMOKE_LARGE("largesmoke", 12, -1),
  611.         SPELL("spell", 13, -1),
  612.         SPELL_INSTANT("instantSpell", 14, -1),
  613.         SPELL_MOB("mobSpell", 15, -1),
  614.         SPELL_MOB_AMBIENT("mobSpellAmbient", 16, -1),
  615.         SPELL_WITCH("witchMagic", 17, -1),
  616.         DRIP_WATER("dripWater", 18, -1),
  617.         DRIP_LAVA("dripLava", 19, -1),
  618.         VILLAGER_ANGRY("angryVillager", 20, -1),
  619.         VILLAGER_HAPPY("happyVillager", 21, -1),
  620.         TOWN_AURA("townaura", 22, -1),
  621.         NOTE("note", 23, -1),
  622.         PORTAL("portal", 24, -1),
  623.         ENCHANTMENT_TABLE("enchantmenttable", 25, -1),
  624.         FLAME("flame", 26, -1),
  625.         LAVA("lava", 27, -1),
  626.         FOOTSTEP("footstep", 28, -1),
  627.         CLOUD("cloud", 29, -1),
  628.         REDSTONE("reddust", 30, -1),
  629.         SNOWBALL("snowballpoof", 31, -1),
  630.         SNOW_SHOVEL("snowshovel", 32, -1),
  631.         SLIME("slime", 33, -1),
  632.         HEART("heart", 34, -1),
  633.         BARRIER("barrier", 35, 8),
  634.         ITEM_CRACK("iconcrack", 36, -1, true),
  635.         BLOCK_CRACK("blockcrack", 37, -1, true),
  636.         BLOCK_DUST("blockdust", 38, 7, true),
  637.         WATER_DROP("droplet", 39, 8),
  638.         ITEM_TAKE("take", 40, 8),
  639.         CHEESE("cheese",41, 8),
  640.         MOB_APPEARANCE("mobappearance", 41, 8);
  641.        
  642.         private static final Map<String, ParticleEffect> NAME_MAP;
  643.         private static final Map<Integer, ParticleEffect> ID_MAP;
  644.         private final String name;
  645.         private final int id;
  646.         private final int requiredVersion;
  647.         private final boolean requiresData;
  648.         private final boolean requiresWater;
  649.        
  650.         static
  651.         {
  652.           NAME_MAP = new HashMap<String, ParticleEffect>();
  653.           ID_MAP = new HashMap<Integer, ParticleEffect>();
  654.           for (ParticleEffect effect : values())
  655.           {
  656.             NAME_MAP.put(effect.name, effect);
  657.             ID_MAP.put(Integer.valueOf(effect.id), effect);
  658.           }
  659.         }
  660.        
  661.         private ParticleEffect(String name, int id, int requiredVersion, boolean requiresData, boolean requiresWater)
  662.         {
  663.           this.name = name;
  664.           this.id = id;
  665.           this.requiredVersion = requiredVersion;
  666.           this.requiresData = requiresData;
  667.           this.requiresWater = requiresWater;
  668.         }
  669.        
  670.         private ParticleEffect(String name, int id, int requiredVersion, boolean requiresData)
  671.         {
  672.           this(name, id, requiredVersion, requiresData, false);
  673.         }
  674.        
  675.         private ParticleEffect(String name, int id, int requiredVersion)
  676.         {
  677.           this(name, id, requiredVersion, false);
  678.         }
  679.        
  680.         public String getName()
  681.         {
  682.           return this.name;
  683.         }
  684.        
  685.         public int getId()
  686.         {
  687.           return this.id;
  688.         }
  689.        
  690.         public int getRequiredVersion()
  691.         {
  692.           return this.requiredVersion;
  693.         }
  694.        
  695.         public boolean getRequiresData()
  696.         {
  697.           return this.requiresData;
  698.         }
  699.        
  700.         public boolean getRequiresWater()
  701.         {
  702.           return this.requiresWater;
  703.         }
  704.        
  705.         public boolean isSupported()
  706.         {
  707.           if (this.requiredVersion == -1) {
  708.             return true;
  709.           }
  710.           return ParticlePacket.getVersion() >= this.requiredVersion;
  711.         }
  712.        
  713.         public static ParticleEffect fromName(String name)
  714.         {
  715.           for (Map.Entry<String, ParticleEffect> entry : NAME_MAP.entrySet()) {
  716.             if (((String)entry.getKey()).equalsIgnoreCase(name)) {
  717.               return (ParticleEffect)entry.getValue();
  718.             }
  719.           }
  720.           return null;
  721.         }
  722.        
  723.         public static ParticleEffect fromId(int id)
  724.         {
  725.           for (Map.Entry<Integer, ParticleEffect> entry : ID_MAP.entrySet()) {
  726.             if (((Integer)entry.getKey()).intValue() == id) {
  727.               return (ParticleEffect)entry.getValue();
  728.             }
  729.           }
  730.           return null;
  731.         }
  732.        
  733.         private static boolean isWater(Location location)
  734.         {
  735.           Material material = location.getBlock().getType();
  736.           return (material == Material.WATER) || (material == Material.STATIONARY_WATER);
  737.         }
  738.        
  739.         private static boolean isLongDistance(Location location, List<Player> players)
  740.         {
  741.           for (Player player : players) {
  742.             if (player.getLocation().distanceSquared(location) >= 65536.0D) {
  743.               return true;
  744.             }
  745.           }
  746.           return false;
  747.         }
  748.        
  749.         private static boolean isDataCorrect(ParticleEffect effect, ParticleData data)
  750.         {
  751.           return ((effect != BLOCK_CRACK) && (effect != BLOCK_DUST)) || (((data instanceof BlockData)) || ((effect == ITEM_CRACK) && ((data instanceof ItemData))));
  752.         }
  753.        
  754.         public void display(float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, double range)
  755.           throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException, IllegalArgumentException
  756.         {
  757.           if (!isSupported()) {
  758.             throw new ParticleVersionException("This particle effect is not supported by your server version");
  759.           }
  760.           if (this.requiresData) {
  761.             throw new ParticleDataException("This particle effect requires additional data");
  762.           }
  763.           if ((this.requiresWater) && (!isWater(center))) {
  764.             throw new IllegalArgumentException("There is no water at the center location");
  765.           }
  766.           new ParticlePacket(this, offsetX, offsetY, offsetZ, speed, amount, range > 256.0D, null).sendTo(center, range);
  767.         }
  768.        
  769.         public void display(float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, List<Player> players)
  770.           throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException, IllegalArgumentException
  771.         {
  772.           if (!isSupported()) {
  773.             throw new ParticleVersionException("This particle effect is not supported by your server version");
  774.           }
  775.           if (this.requiresData) {
  776.             throw new ParticleDataException("This particle effect requires additional data");
  777.           }
  778.           if ((this.requiresWater) && (!isWater(center))) {
  779.             throw new IllegalArgumentException("There is no water at the center location");
  780.           }
  781.           new ParticlePacket(this, offsetX, offsetY, offsetZ, speed, amount, isLongDistance(center, players), null).sendTo(center, players);
  782.         }
  783.        
  784.         public void display(float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, Player... players)
  785.           throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException, IllegalArgumentException
  786.         {
  787.           display(offsetX, offsetY, offsetZ, speed, amount, center, Arrays.asList(players));
  788.         }
  789.        
  790.         public void display(Vector direction, float speed, Location center, double range)
  791.           throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException, IllegalArgumentException
  792.         {
  793.           if (!isSupported()) {
  794.             throw new ParticleVersionException("This particle effect is not supported by your server version");
  795.           }
  796.           if (this.requiresData) {
  797.             throw new ParticleDataException("This particle effect requires additional data");
  798.           }
  799.           if ((this.requiresWater) && (!isWater(center))) {
  800.             throw new IllegalArgumentException("There is no water at the center location");
  801.           }
  802.           new ParticlePacket(this, direction, speed, range > 256.0D, null).sendTo(center, range);
  803.         }
  804.        
  805.         public void display(Vector direction, float speed, Location center, List<Player> players)
  806.           throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException, IllegalArgumentException
  807.         {
  808.           if (!isSupported()) {
  809.             throw new ParticleVersionException("This particle effect is not supported by your server version");
  810.           }
  811.           if (this.requiresData) {
  812.             throw new ParticleDataException("This particle effect requires additional data");
  813.           }
  814.           if ((this.requiresWater) && (!isWater(center))) {
  815.             throw new IllegalArgumentException("There is no water at the center location");
  816.           }
  817.           new ParticlePacket(this, direction, speed, isLongDistance(center, players), null).sendTo(center, players);
  818.         }
  819.        
  820.         public void display(Vector direction, float speed, Location center, Player... players)
  821.           throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException, IllegalArgumentException
  822.         {
  823.           display(direction, speed, center, Arrays.asList(players));
  824.         }
  825.        
  826.         public void display(ParticleData data, float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, double range)
  827.           throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException
  828.         {
  829.           if (!isSupported()) {
  830.             throw new ParticleVersionException("This particle effect is not supported by your server version");
  831.           }
  832.           if (!this.requiresData) {
  833.             throw new ParticleDataException("This particle effect does not require additional data");
  834.           }
  835.           if (!isDataCorrect(this, data)) {
  836.             throw new ParticleDataException("The particle data type is incorrect");
  837.           }
  838.           new ParticlePacket(this, offsetX, offsetY, offsetZ, speed, amount, range > 256.0D, data).sendTo(center, range);
  839.         }
  840.        
  841.         public void display(ParticleData data, float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, List<Player> players)
  842.           throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException
  843.         {
  844.           if (!isSupported()) {
  845.             throw new ParticleVersionException("This particle effect is not supported by your server version");
  846.           }
  847.           if (!this.requiresData) {
  848.             throw new ParticleDataException("This particle effect does not require additional data");
  849.           }
  850.           if (!isDataCorrect(this, data)) {
  851.             throw new ParticleDataException("The particle data type is incorrect");
  852.           }
  853.           new ParticlePacket(this, offsetX, offsetY, offsetZ, speed, amount, isLongDistance(center, players), data).sendTo(center, players);
  854.         }
  855.        
  856.         public void display(ParticleData data, float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, Player... players)
  857.           throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException
  858.         {
  859.           display(data, offsetX, offsetY, offsetZ, speed, amount, center, Arrays.asList(players));
  860.         }
  861.        
  862.         public void display(ParticleData data, Vector direction, float speed, Location center, double range)
  863.           throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException
  864.         {
  865.           if (!isSupported()) {
  866.             throw new ParticleVersionException("This particle effect is not supported by your server version");
  867.           }
  868.           if (!this.requiresData) {
  869.             throw new ParticleDataException("This particle effect does not require additional data");
  870.           }
  871.           if (!isDataCorrect(this, data)) {
  872.             throw new ParticleDataException("The particle data type is incorrect");
  873.           }
  874.           new ParticlePacket(this, direction, speed, range > 256.0D, data).sendTo(center, range);
  875.         }
  876.        
  877.         public void display(ParticleData data, Vector direction, float speed, Location center, List<Player> players)
  878.           throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException
  879.         {
  880.           if (!isSupported()) {
  881.             throw new ParticleVersionException("This particle effect is not supported by your server version");
  882.           }
  883.           if (!this.requiresData) {
  884.             throw new ParticleDataException("This particle effect does not require additional data");
  885.           }
  886.           if (!isDataCorrect(this, data)) {
  887.             throw new ParticleDataException("The particle data type is incorrect");
  888.           }
  889.           new ParticlePacket(this, direction, speed, isLongDistance(center, players), data).sendTo(center, players);
  890.         }
  891.        
  892.         public void display(ParticleData data, Vector direction, float speed, Location center, Player... players)
  893.           throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException
  894.         {
  895.           display(data, direction, speed, center, Arrays.asList(players));
  896.         }
  897.        
  898.         public static abstract class ParticleData
  899.         {
  900.           private final Material material;
  901.           private final byte data;
  902.           private final int[] packetData;
  903.          
  904.           @SuppressWarnings("deprecation")
  905.         public ParticleData(Material material, byte data)
  906.           {
  907.             this.material = material;
  908.             this.data = data;
  909.             this.packetData = new int[] { material.getId(), data };
  910.           }
  911.          
  912.           public Material getMaterial()
  913.           {
  914.             return this.material;
  915.           }
  916.          
  917.           public byte getData()
  918.           {
  919.             return this.data;
  920.           }
  921.          
  922.           public int[] getPacketData()
  923.           {
  924.             return this.packetData;
  925.           }
  926.          
  927.           public String getPacketDataString()
  928.           {
  929.             return "_" + this.packetData[0] + "_" + this.packetData[1];
  930.           }
  931.         }
  932.        
  933.         public static final class ItemData
  934.           extends ParticleEffect.ParticleData
  935.         {
  936.           public ItemData(Material material, byte data)
  937.           {
  938.             super(material, data);
  939.           }
  940.         }
  941.        
  942.         public static final class BlockData
  943.           extends ParticleEffect.ParticleData
  944.         {
  945.           public BlockData(Material material, byte data)
  946.             throws IllegalArgumentException
  947.           {
  948.             super(material, data);
  949.             if (!material.isBlock()) {
  950.               throw new IllegalArgumentException("The material is not a block");
  951.             }
  952.           }
  953.         }
  954.        
  955.         private static final class ParticleDataException
  956.           extends RuntimeException
  957.         {
  958.           private static final long serialVersionUID = 3203085387160737484L;
  959.          
  960.           public ParticleDataException(String message)
  961.           {
  962.             super();
  963.           }
  964.         }
  965.        
  966.         private static final class ParticleVersionException
  967.           extends RuntimeException
  968.         {
  969.           private static final long serialVersionUID = 3203085387160737484L;
  970.          
  971.           public ParticleVersionException(String message)
  972.           {
  973.             super();
  974.           }
  975.         }
  976.        
  977.         public static final class ParticlePacket
  978.         {
  979.           private static int version;
  980.           private static Class<?> enumParticle;
  981.           private static Constructor<?> packetConstructor;
  982.           private static Method getHandle;
  983.           private static Field playerConnection;
  984.           private static Method sendPacket;
  985.           private static boolean initialized;
  986.           private final ParticleEffect effect;
  987.           private final float offsetX;
  988.           private final float offsetY;
  989.           private final float offsetZ;
  990.           private final float speed;
  991.           private final int amount;
  992.           private final boolean longDistance;
  993.           private final ParticleEffect.ParticleData data;
  994.           private Object packet;
  995.          
  996.           public ParticlePacket(ParticleEffect effect, float offsetX, float offsetY, float offsetZ, float speed, int amount, boolean longDistance, ParticleEffect.ParticleData data)
  997.             throws IllegalArgumentException
  998.           {
  999.             initialize();
  1000.             if (speed < 0.0F) {
  1001.               throw new IllegalArgumentException("The speed is lower than 0");
  1002.             }
  1003.             if (amount < 1) {
  1004.               throw new IllegalArgumentException("The amount is lower than 1");
  1005.             }
  1006.             this.effect = effect;
  1007.             this.offsetX = offsetX;
  1008.             this.offsetY = offsetY;
  1009.             this.offsetZ = offsetZ;
  1010.             this.speed = speed;
  1011.             this.amount = amount;
  1012.             this.longDistance = longDistance;
  1013.             this.data = data;
  1014.           }
  1015.          
  1016.           public ParticlePacket(ParticleEffect effect, Vector direction, float speed, boolean longDistance, ParticleEffect.ParticleData data)
  1017.             throws IllegalArgumentException
  1018.           {
  1019.             initialize();
  1020.             if (speed < 0.0F) {
  1021.               throw new IllegalArgumentException("The speed is lower than 0");
  1022.             }
  1023.             this.effect = effect;
  1024.             this.offsetX = ((float)direction.getX());
  1025.             this.offsetY = ((float)direction.getY());
  1026.             this.offsetZ = ((float)direction.getZ());
  1027.             this.speed = speed;
  1028.             this.amount = 0;
  1029.             this.longDistance = longDistance;
  1030.             this.data = data;
  1031.           }
  1032.          
  1033.           public static void initialize()
  1034.             throws ParticleEffect.ParticlePacket.VersionIncompatibleException
  1035.           {
  1036.             if (initialized) {
  1037.               return;
  1038.             }
  1039.             try
  1040.             {
  1041.               version = Integer.parseInt(Character.toString(PackageType.getServerVersion().charAt(3)));
  1042.               if (version > 7) {
  1043.                 enumParticle = PackageType.MINECRAFT_SERVER.getClass("EnumParticle");
  1044.               }
  1045.               Class<?> packetClass = PackageType.MINECRAFT_SERVER.getClass(version < 7 ? "Packet63WorldParticles" : "PacketPlayOutWorldParticles");
  1046.               packetConstructor = getConstructor(packetClass, new Class[0]);
  1047.               getHandle = getMethod("CraftPlayer", API.PackageType.CRAFTBUKKIT_ENTITY, "getHandle", new Class[0]);
  1048.               playerConnection = getField("EntityPlayer", API.PackageType.MINECRAFT_SERVER, false, "playerConnection");
  1049.               sendPacket = getMethod(playerConnection.getType(), "sendPacket", new Class[] { PackageType.MINECRAFT_SERVER.getClass("Packet") });
  1050.             }
  1051.             catch (Exception exception)
  1052.             {
  1053.               throw new VersionIncompatibleException("Your current bukkit version seems to be incompatible with this library", exception);
  1054.             }
  1055.             initialized = true;
  1056.           }
  1057.          
  1058.           public static int getVersion()
  1059.           {
  1060.             return version;
  1061.           }
  1062.          
  1063.           public static boolean isInitialized()
  1064.           {
  1065.             return initialized;
  1066.           }
  1067.          
  1068.           public void sendTo(Location center, Player player)
  1069.             throws ParticleEffect.ParticlePacket.PacketInstantiationException, ParticleEffect.ParticlePacket.PacketSendingException
  1070.           {
  1071.             if (this.packet == null) {
  1072.               try
  1073.               {
  1074.                 this.packet = packetConstructor.newInstance(new Object[0]);
  1075.                 Object id;
  1076.                 if (version < 8) {
  1077.                   id = this.effect.getName() + (this.data == null ? "" : this.data.getPacketDataString());
  1078.                 } else {
  1079.                   id = enumParticle.getEnumConstants()[this.effect.getId()];
  1080.                 }
  1081.                 setValue(this.packet, true, "a", id);
  1082.                 setValue(this.packet, true, "b", Float.valueOf((float)center.getX()));
  1083.                 setValue(this.packet, true, "c", Float.valueOf((float)center.getY()));
  1084.                 setValue(this.packet, true, "d", Float.valueOf((float)center.getZ()));
  1085.                 setValue(this.packet, true, "e", Float.valueOf(this.offsetX));
  1086.                 setValue(this.packet, true, "f", Float.valueOf(this.offsetY));
  1087.                 setValue(this.packet, true, "g", Float.valueOf(this.offsetZ));
  1088.                 setValue(this.packet, true, "h", Float.valueOf(this.speed));
  1089.                 setValue(this.packet, true, "i", Integer.valueOf(this.amount));
  1090.                 if (version > 7)
  1091.                 {
  1092.                     API.setValue(this.packet, true, "j", Boolean.valueOf(this.longDistance));
  1093.                     API.setValue(this.packet, true, "k", this.data == null ? new int[0] : this.data.getPacketData());
  1094.                 }
  1095.               }
  1096.               catch (Exception exception)
  1097.               {
  1098.                 throw new PacketInstantiationException("Packet instantiation failed", exception);
  1099.               }
  1100.             }
  1101.             try
  1102.             {
  1103.               sendPacket.invoke(playerConnection.get(getHandle.invoke(player, new Object[0])), new Object[] { this.packet });
  1104.             }
  1105.             catch (Exception exception)
  1106.             {
  1107.               throw new PacketSendingException("Failed to send the packet to player '" + player.getName() + "'", exception);
  1108.             }
  1109.           }
  1110.          
  1111.           public void sendTo(Location center, List<Player> players)
  1112.             throws IllegalArgumentException
  1113.           {
  1114.             if (players.isEmpty()) {
  1115.               throw new IllegalArgumentException("The player list is empty");
  1116.             }
  1117.             for (Player player : players) {
  1118.               sendTo(center, player);
  1119.             }
  1120.           }
  1121.          
  1122.           public void sendTo(Location center, double range)
  1123.             throws IllegalArgumentException
  1124.           {
  1125.             if (range < 1.0D) {
  1126.               throw new IllegalArgumentException("The range is lower than 1");
  1127.             }
  1128.             String worldName = center.getWorld().getName();
  1129.             double squared = range * range;
  1130.             for (Player player : Bukkit.getOnlinePlayers()) {
  1131.               if ((player.getWorld().getName().equals(worldName)) && (player.getLocation().distanceSquared(center) <= squared)) {
  1132.                 sendTo(center, player);
  1133.               }
  1134.             }
  1135.           }
  1136.          
  1137.           private static final class VersionIncompatibleException
  1138.             extends RuntimeException
  1139.           {
  1140.             private static final long serialVersionUID = 3203085387160737484L;
  1141.            
  1142.             public VersionIncompatibleException(String message, Throwable cause)
  1143.             {
  1144.               super(cause);
  1145.             }
  1146.           }
  1147.          
  1148.           private static final class PacketInstantiationException
  1149.             extends RuntimeException
  1150.           {
  1151.             private static final long serialVersionUID = 3203085387160737484L;
  1152.            
  1153.             public PacketInstantiationException(String message, Throwable cause)
  1154.             {
  1155.               super(cause);
  1156.             }
  1157.           }
  1158.          
  1159.           private static final class PacketSendingException
  1160.             extends RuntimeException
  1161.           {
  1162.             private static final long serialVersionUID = 3203085387160737484L;
  1163.            
  1164.             public PacketSendingException(String message, Throwable cause)
  1165.             {
  1166.               super(cause);
  1167.             }
  1168.           }
  1169.         }
  1170.       }
  1171.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement