Facebook
From xd, 4 Months ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 82
  1. {
  2.     public static String iiIi;
  3.     public static String nmsver;
  4.    
  5.     static {
  6.         ChatUtil.nmsver = Bukkit.getServer().getClass().getPackage().getName();
  7.         ChatUtil.nmsver = ChatUtil.nmsver.substring(ChatUtil.nmsver.lastIndexOf(".") + 1);
  8.     }
  9.    
  10.     public static Location locFromString(final String str) {
  11.         final String[] str2loc = str.split(":");
  12.         final Location loc = new Location((World)Bukkit.getWorlds().get(0), 0.0, 0.0, 0.0, 0.0f, 0.0f);
  13.         loc.setX(Double.parseDouble(str2loc[0]));
  14.         loc.setY(Double.parseDouble(str2loc[1]));
  15.         loc.setZ(Double.parseDouble(str2loc[2]));
  16.         loc.setYaw(Float.parseFloat(str2loc[3]));
  17.         loc.setPitch(Float.parseFloat(str2loc[4]));
  18.         return loc;
  19.     }
  20.    
  21.     public static ItemStack getItemStackFromString(final String itemstack) {
  22.         final String[] splits = itemstack.split("@");
  23.         final String type = splits[0];
  24.         final String data = (splits.length == 2) ? splits[1] : null;
  25.         if (data == null) {
  26.             return new ItemStack(Material.getMaterial(type), 1);
  27.         }
  28.         return new ItemStack(Material.getMaterial(type), 1, (short)Integer.parseInt(data));
  29.     }
  30.    
  31.     public static String locToString(final double x, final double y, final double z) {
  32.         return String.valueOf(String.valueOf(x)) + ":" + y + ":" + z + ":" + 0.0f + ":" + 0.0f;
  33.     }
  34.    
  35.     public static String locToString(final Location loc) {
  36.         return String.valueOf(String.valueOf(loc.getX())) + ":" + loc.getY() + ":" + loc.getZ() + ":" + loc.getYaw() + ":" + loc.getPitch();
  37.     }
  38.    
  39.     public static String fixColor(final String s) {
  40.         if (s == null) {
  41.             return "";
  42.         }
  43.         s.replace("$gp", "&6&lGILDIE &8\u25cf");
  44.         return ChatColor.translateAlternateColorCodes('&', s);
  45.     }
  46.    
  47.     public static Collection<String> fixColor(final Collection<String> collection) {
  48.         final Collection<String> local = new ArrayList<String>();
  49.         for (final String s : collection) {
  50.             local.add(fixColor(s));
  51.         }
  52.         return local;
  53.     }
  54.    
  55.     public static int floor(final double value) {
  56.         final int i = (int)value;
  57.         return (value < i) ? (i - 1) : i;
  58.     }
  59.    
  60.     public static double round(final double value, final int decimals) {
  61.         final double p = Math.pow(10.0, decimals);
  62.         return Math.round(value * p) / p;
  63.     }
  64.    
  65.     public static String[] fixColor(final String[] array) {
  66.         for (int i = 0; i < array.length; ++i) {
  67.             array[i] = fixColor(array[i]);
  68.         }
  69.         return array;
  70.     }
  71.    
  72.     public static boolean sendMessage(final CommandSender sender, final String message, final String permission) {
  73.         if (sender instanceof ConsoleCommandSender) {
  74.             sendMessage(sender, message);
  75.         }
  76.         return permission != null && permission != "" && sender.hasPermission(permission) && sendMessage(sender, message);
  77.     }
  78.    
  79.     public static boolean sendMessage(final CommandSender sender, final String message) {
  80.         if (sender instanceof Player) {
  81.             if (message != null || message != "") {
  82.                 sender.sendMessage(fixColor(message));
  83.             }
  84.         }
  85.         else {
  86.             sender.sendMessage(ChatColor.stripColor(fixColor(message)));
  87.         }
  88.         return true;
  89.     }
  90.    
  91.     public static boolean error(final Player p, final String msg) {
  92.         return sendMessage((CommandSender)p, "&c&lBlad: &7" + msg);
  93.     }
  94.    
  95.     public static void sendTitleMessage(final Player player, String title, String subtitle, final int fadeIn, final int stay, final int fadeOut) {
  96.         if (title == null) {
  97.             title = "";
  98.         }
  99.         if (subtitle == null) {
  100.             subtitle = "";
  101.         }
  102.         title = title.replace("&", "\u00c2§");
  103.         subtitle = subtitle.replace("&", "\u00c2§");
  104.         final CraftPlayer craftPlayer = (CraftPlayer)player;
  105.         final IChatBaseComponent chatTitle = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + title + "\"}");
  106.         final PacketPlayOutTitle packetTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TITLE, chatTitle);
  107.         craftPlayer.getHandle().playerConnection.sendPacket((Packet)packetTitle);
  108.         final IChatBaseComponent chatSubtitle = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + subtitle + "\"}");
  109.         final PacketPlayOutTitle packetSubtitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.SUBTITLE, chatSubtitle);
  110.         craftPlayer.getHandle().playerConnection.sendPacket((Packet)packetSubtitle);
  111.     }
  112.    
  113.     public static void sendActionBar(final Player player, final String message) {
  114.         final ActionBarUtil actionBarMessageEvent = new ActionBarUtil(player, message);
  115.         Bukkit.getPluginManager().callEvent((Event)actionBarMessageEvent);
  116.         if (actionBarMessageEvent.isCancelled()) {
  117.             return;
  118.         }
  119.         try {
  120.             final Class<?> c1 = Class.forName("org.bukkit.craftbukkit." + ChatUtil.nmsver + ".entity.CraftPlayer");
  121.             final Object p = c1.cast(player);
  122.             final Class<?> c2 = Class.forName("net.minecraft.server." + ChatUtil.nmsver + ".PacketPlayOutChat");
  123.             final Class<?> c3 = Class.forName("net.minecraft.server." + ChatUtil.nmsver + ".Packet");
  124.             final Class<?> c4 = Class.forName("net.minecraft.server." + ChatUtil.nmsver + ".ChatComponentText");
  125.             final Class<?> c5 = Class.forName("net.minecraft.server." + ChatUtil.nmsver + ".IChatBaseComponent");
  126.             final Object o = c4.getConstructor(String.class).newInstance(message);
  127.             final Object ppoc = c2.getConstructor(c5, Byte.TYPE).newInstance(o, 2);
  128.             final Method m1 = c1.getDeclaredMethod("getHandle", (Class<?>[])new Class[0]);
  129.             final Object h = m1.invoke(p, new Object[0]);
  130.             final Field f1 = h.getClass().getDeclaredField("playerConnection");
  131.             final Object pc = f1.get(h);
  132.             final Method m2 = pc.getClass().getDeclaredMethod("sendPacket", c3);
  133.             m2.invoke(pc, ppoc);
  134.         }
  135.         catch (Exception ex) {
  136.             ex.printStackTrace();
  137.         }
  138.     }
  139.    
  140.     public static void sendActionBar(final Player player, final String message, int duration) {
  141.         sendActionBar(player, message);
  142.         if (duration >= 0) {
  143.             new BukkitRunnable() {
  144.                 public void run() {
  145.                     ChatUtil.sendActionBar(player, "");
  146.                 }
  147.             }.runTaskLater((Plugin)ServerPlugin.getPlugin(), (long)(duration + 1));
  148.         }
  149.         while (duration > 60) {
  150.             duration -= 60;
  151.             final int sched = duration % 60;
  152.             new BukkitRunnable() {
  153.                 public void run() {
  154.                     ChatUtil.sendActionBar(player, message);
  155.                 }
  156.             }.runTaskLater((Plugin)ServerPlugin.getPlugin(), (long)sched);
  157.         }
  158.     }
  159.    
  160.     public static void removeItems(final Player p, final ItemStack... items) {
  161.         final Inventory i = (Inventory)p.getInventory();
  162.         final HashMap<Integer, ItemStack> notStored = (HashMap<Integer, ItemStack>)i.removeItem(items);
  163.         for (Map.Entry<Integer, ItemStack> entry : notStored.entrySet()) {}
  164.     }
  165.    
  166.     public static boolean sendMessage(final Collection<? extends CommandSender> collection, final String message) {
  167.         for (final CommandSender cs : collection) {
  168.             sendMessage(cs, message);
  169.         }
  170.         return true;
  171.     }
  172.    
  173.     public static boolean sendMessage(final Collection<? extends CommandSender> collection, final String message, final String permission) {
  174.         for (final CommandSender cs : collection) {
  175.             sendMessage(cs, message, permission);
  176.         }
  177.         return true;
  178.     }
  179.    
  180.     public static boolean containsIgnoreCase(final String[] array, final String element) {
  181.         for (final String s : array) {
  182.             if (s.equalsIgnoreCase(element)) {
  183.                 return true;
  184.             }
  185.         }
  186.         return false;
  187.     }
  188.    
  189.     public static void copy(final InputStream in, final File file) {
  190.         try {
  191.             final OutputStream out = new FileOutputStream(file);
  192.             final byte[] buf = new byte[1024];
  193.             int len;
  194.             while ((len = in.read(buf)) > 0) {
  195.                 out.write(buf, 0, len);
  196.             }
  197.             out.close();
  198.             in.close();
  199.         }
  200.         catch (Exception e) {
  201.             e.printStackTrace();
  202.         }
  203.     }
  204.    
  205.     public static Material getMaterial(final String materialName) {
  206.         Material returnMaterial = null;
  207.         if (isInteger(materialName)) {
  208.             final int id = Integer.parseInt(materialName);
  209.             returnMaterial = Material.getMaterial(id);
  210.         }
  211.         else {
  212.             returnMaterial = Material.matchMaterial(materialName);
  213.         }
  214.         return returnMaterial;
  215.     }
  216.    
  217.     public static void giveItems(final Player p, final ItemStack... items) {
  218.         final Inventory i = (Inventory)p.getInventory();
  219.         final HashMap<Integer, ItemStack> notStored = (HashMap<Integer, ItemStack>)i.addItem(items);
  220.         for (final Map.Entry<Integer, ItemStack> e : notStored.entrySet()) {
  221.             p.getWorld().dropItemNaturally(p.getLocation(), (ItemStack)e.getValue());
  222.         }
  223.     }
  224.    
  225.     public static Player getDamager(final EntityDamageByEntityEvent e) {
  226.         final Entity damager = e.getDamager();
  227.         if (damager instanceof Player) {
  228.             return (Player)damager;
  229.         }
  230.         if (damager instanceof Projectile) {
  231.             final Projectile p = (Projectile)damager;
  232.             if (p.getShooter() instanceof Player) {
  233.                 return (Player)p.getShooter();
  234.             }
  235.         }
  236.         return null;
  237.     }
  238.    
  239.     public static boolean isAlphaNumeric(final String s) {
  240.         return s.matches("^[a-zA-Z0-9_]*$");
  241.     }
  242.    
  243.     public static boolean isFloat(final String string) {
  244.         return Pattern.matches("([0-9]*)\\.([0-9]*)", string);
  245.     }
  246.    
  247.     public static boolean isInteger(final String string) {
  248.         return Pattern.matches("-?[0-9]+", string.subSequence(0, string.length()));
  249.     }
  250.    
  251.     public static void sendHoverMessageCommand(final Player p, final String s1, final String s2, final String cmd) {
  252.         final IChatBaseComponent msg = IChatBaseComponent.ChatSerializer.a(fixColor("{\"text\":\"" + s1 + "\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"" + s2 + "\"}]}},\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"" + cmd + "\"}}"));
  253.         final PacketPlayOutChat hover = new PacketPlayOutChat(msg);
  254.         ((CraftPlayer)p).getHandle().playerConnection.sendPacket((Packet)hover);
  255.     }
  256.    
  257.     public static void sendHoverMessage(final CommandSender p, final String s1, final String s2) {
  258.         final IChatBaseComponent msg = IChatBaseComponent.ChatSerializer.a(fixColor("{\"text\":\"" + s1 + "\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"" + s2 + "\"}]}},\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"\"}}"));
  259.         final PacketPlayOutChat hover = new PacketPlayOutChat(msg);
  260.         ((CraftPlayer)p).getHandle().playerConnection.sendPacket((Packet)hover);
  261.     }
  262. }