Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. package eu.mrapik.utilities.utils;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5. import java.text.DecimalFormat;
  6. import java.text.NumberFormat;
  7. import java.util.Arrays;
  8. import java.util.Collections;
  9. import java.util.Comparator;
  10. import java.util.LinkedHashMap;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13. import java.util.Locale;
  14. import java.util.Map;
  15. import net.md_5.bungee.api.ChatColor;
  16. import org.apache.commons.lang.StringUtils;
  17. import org.bukkit.Bukkit;
  18. import static org.bukkit.Bukkit.getServer;
  19. import org.bukkit.Location;
  20. import org.bukkit.Material;
  21. import org.bukkit.configuration.file.FileConfiguration;
  22. import org.bukkit.entity.Player;
  23. import org.bukkit.inventory.Inventory;
  24. import org.bukkit.inventory.ItemStack;
  25. import org.bukkit.inventory.meta.ItemMeta;
  26.  
  27. public class Utils {
  28.  
  29. public static void makeGlass(int id, Inventory inv) {
  30. ItemStack item = new ItemStack(Material.STAINED_GLASS_PANE, 1, (byte) id);
  31. ItemMeta im = item.getItemMeta();
  32. im.setDisplayName(Utils.colorizer("&0###"));
  33. item.setItemMeta(im);
  34.  
  35. for (int i = 0; i < inv.getContents().length; i++) {
  36. ItemStack[] help = inv.getContents();
  37. if (help[i] == null) {
  38. Utils.setItem(i, item, inv);
  39. }
  40. }
  41.  
  42. }
  43.  
  44. public static void sendNiceMessage(Player player, String message) {
  45. player.sendMessage(Utils.colorizer("&8" + StringUtils.center("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■", 50)));
  46. String blank = StringUtils.center(" ", 50);
  47. blank = Utils.colorizer(blank.substring(1, blank.length() - 1));
  48. player.sendMessage(blank);
  49. String text = StringUtils.center(message, 50);
  50. text = Utils.colorizer(text.substring(1, text.length() - 1));
  51.  
  52. player.sendMessage(text);
  53. player.sendMessage(blank);
  54. player.sendMessage(Utils.colorizer("&8" + StringUtils.center("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■", 50)));
  55. }
  56.  
  57. public static void sendNiceMessage(Player player, List<String> message) {
  58. String blank = StringUtils.center(" ", 50);
  59. blank = Utils.colorizer(blank.substring(1, blank.length() - 1));
  60.  
  61. player.sendMessage(Utils.colorizer("&8" + StringUtils.center("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■", 50)));
  62.  
  63. player.sendMessage(blank);
  64.  
  65. for (String msg : message) {
  66. String text = msg;
  67. text = Utils.colorizer(text);
  68. player.sendMessage(text);
  69. }
  70. player.sendMessage(blank);
  71. player.sendMessage(Utils.colorizer("&8" + StringUtils.center("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■", 50)));
  72.  
  73. }
  74.  
  75. public static boolean isOnline(String pl) {
  76. for (Player player : Bukkit.getServer().getOnlinePlayers()) {
  77. if (player.getName().equalsIgnoreCase(pl)) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83.  
  84. public static void setItem(int position, ItemStack is, Inventory inv) {
  85. inv.setItem(position, is);
  86. }
  87.  
  88. public static ItemStack createItem(Material material, String name, String lore) {
  89. ItemStack is = new ItemStack(material);
  90. ItemMeta im = is.getItemMeta();
  91. im.setDisplayName(Utils.colorizer(name));
  92. im.setLore(Arrays.asList(Utils.colorizer(lore)));
  93. is.setItemMeta(im);
  94. return is;
  95. }
  96.  
  97. public static String colorizer(String message) {
  98. return ChatColor.translateAlternateColorCodes('&', message);
  99. }
  100.  
  101. public static String serializeLocation(Location loc) {
  102. return loc.getWorld().getName() + "=" + loc.getX() + "=" + loc.getY() + "=" + loc.getZ() + "=" + loc.getPitch() + "=" + loc.getYaw();
  103. }
  104.  
  105. public static boolean isDouble(String str) {
  106. String regExp = "[\\x00-\\x20]*[+-]?(((((\\p{Digit}+)(\\.)?((\\p{Digit}+)?)([eE][+-]?(\\p{Digit}+))?)|(\\.((\\p{Digit}+))([eE][+-]?(\\p{Digit}+))?)|(((0[xX](\\p{XDigit}+)(\\.)?)|(0[xX](\\p{XDigit}+)?(\\.)(\\p{XDigit}+)))[pP][+-]?(\\p{Digit}+)))[fFdD]?))[\\x00-\\x20]*";
  107. boolean matches = str.matches(regExp);
  108. return matches;
  109. }
  110.  
  111. public static void catchEx(Exception ex) {
  112. Utils.sendConsoleMessage(Options.PREFIX + "&c INTERNAL ERROR: " + ex.getMessage() + " EXCEPTION: " + ex.getClass().toString());
  113. for (StackTraceElement se : ex.getStackTrace()) {
  114. Utils.sendConsoleMessage(Options.PREFIX + "&e FILE: &b" + se.getFileName() + " &eLINE: &b" + se.getLineNumber() + "&e METHOD: &b" + se.getMethodName());
  115. }
  116. }
  117.  
  118. public static void broadcastMessage(boolean bold, String br) {
  119. for (Player col : Bukkit.getServer().getOnlinePlayers()) {
  120. if (bold) {
  121. col.sendMessage(Utils.colorizer(br));
  122. } else {
  123. col.sendMessage(Utils.colorizer(br));
  124. }
  125. }
  126. }
  127.  
  128. public static void sendConsoleMessage(String message) {
  129. getServer().getConsoleSender().sendMessage(colorizer(message));
  130. }
  131.  
  132. public static boolean isInteger(String str) {
  133. boolean toReturn = false;
  134. try {
  135. int op1 = Integer.parseInt(str);
  136. toReturn = true;
  137. } catch (NumberFormatException e) {
  138. toReturn = false;
  139. }
  140. return toReturn;
  141. }
  142.  
  143. public static Location unserializeLocation(String loc) {
  144.  
  145. if (loc.equals("NOEXIST")) {
  146. return null;
  147. }
  148.  
  149. String[] split = loc.split("=");
  150. return new Location(getServer().getWorld(split[0]),
  151. Double.parseDouble(split[1]), Double.parseDouble(split[2]),
  152. Double.parseDouble(split[3]), Float.parseFloat(split[4]),
  153. Float.parseFloat(split[5]));
  154. }
  155.  
  156. public static ItemStack createItem(Material m, String name, List<String> lore) {
  157. ItemStack is = new ItemStack(m);
  158. ItemMeta i = is.getItemMeta();
  159. i.setDisplayName(name);
  160. if (lore != null) {
  161. i.setLore(lore);
  162. }
  163. is.setItemMeta(i);
  164. return is;
  165. }
  166.  
  167. public static void setMessage(FileConfiguration messages, String string, String text) {
  168. if (!messages.contains(string)) {
  169. messages.set(string, text);
  170. }
  171. }
  172.  
  173. public static boolean playerHasEnoughLimit(Player p, Integer count, String perm) {
  174. //p = hráč
  175. //count = počet, který chceme ověřit
  176. //perm string bez čísla
  177.  
  178. for (int i = 1; i < 500; i++) {
  179. if (p.hasPermission(perm + "." + i) && count == i) {
  180. return true;
  181. }
  182. }
  183. return true;
  184. }
  185.  
  186. public static Integer getLimit(Player p, String perm) {
  187. int limit = 0;
  188. for (int i = 1; i < 500; i++) {
  189. if (p.hasPermission(perm + "." + i)) {
  190. if (limit < i) {
  191. limit = i;
  192. }
  193. }
  194. }
  195. return limit;
  196. }
  197.  
  198. public static String formatDouble(double amount) {
  199.  
  200. String pattern = "###,###.###";
  201. Locale locale = new Locale("en", "UK");
  202. DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale);
  203. decimalFormat.applyPattern(pattern);
  204. return decimalFormat.format(amount);
  205. }
  206.  
  207. public static double round(double value, int places) {
  208. if (places < 0) {
  209. throw new IllegalArgumentException();
  210. }
  211.  
  212. BigDecimal bd = new BigDecimal(value);
  213. bd = bd.setScale(places, RoundingMode.HALF_UP);
  214. return bd.doubleValue();
  215. }
  216.  
  217. public static double roundToInt(double value) {
  218. int test = (int) value;
  219. test = Integer.valueOf(test);
  220. double toRet = (double) test;
  221. return Double.valueOf(toRet);
  222. }
  223.  
  224. public static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap, final boolean order) {
  225.  
  226. List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());
  227.  
  228. // Sorting the list based on values
  229. Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
  230. public int compare(Map.Entry<String, Integer> o1,
  231. Map.Entry<String, Integer> o2) {
  232. if (order) {
  233. return o1.getValue().compareTo(o2.getValue());
  234. } else {
  235. return o2.getValue().compareTo(o1.getValue());
  236.  
  237. }
  238. }
  239. });
  240.  
  241. // Maintaining insertion order with the help of LinkedList
  242. Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
  243. for (Map.Entry<String, Integer> entry : list) {
  244. sortedMap.put(entry.getKey(), entry.getValue());
  245. }
  246.  
  247. return sortedMap;
  248. }
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement