Advertisement
Guest User

dsds

a guest
Jul 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. public class ServerUtil
  2. {
  3. private static List<String> strings;
  4. public static Random random;
  5.  
  6. static {
  7. ServerUtil.strings = new ArrayList<String>();
  8. ServerUtil.random = new Random();
  9. }
  10.  
  11. public static void respawnPlayer(final Player player) {
  12. new BukkitRunnable() {
  13. public void run() {
  14. final PacketPlayInClientCommand packet = new PacketPlayInClientCommand(PacketPlayInClientCommand.EnumClientCommand.PERFORM_RESPAWN);
  15. ((CraftPlayer)player).getHandle().playerConnection.a(packet);
  16. }
  17. }.runTaskLater((Plugin)Main.getInstance(), 2L);
  18. }
  19.  
  20. public static void removePotionEffects(final Player player) {
  21. for (final PotionEffect effects : player.getActivePotionEffects()) {
  22. player.removePotionEffect(effects.getType());
  23. }
  24. }
  25.  
  26. public static void playSound(final Player player, final Sound sound) {
  27. player.playSound(player.getLocation(), sound, 10.0f, 10.0f);
  28. }
  29.  
  30. public static void playEffect(final Player player, final Effect effect) {
  31. player.playEffect(player.getLocation(), effect, 10);
  32. }
  33.  
  34. public static void removeScoreboard(final Player player) {
  35. player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard());
  36. }
  37.  
  38. public static String fixColor(final String message) {
  39. return ChatColor.translateAlternateColorCodes('&', message.replace(">>", "»").replace("<<", "«").replace("{CHECK}", "\u2714").replace("{X}", "\u2716").replace(">", "\u279c").replace("{O}", "\u25cf"));
  40. }
  41.  
  42. public static List<String> fixColor(final List<String> stringList) {
  43. for (final String s : stringList) {
  44. ServerUtil.strings.add(fixColor(s));
  45. }
  46. return stringList;
  47. }
  48.  
  49. public static void sendTitle(final Player player, final String title, final String subtitle) {
  50. if (title != null) {
  51. final PlayerConnection connection = ((CraftPlayer)player).getHandle().playerConnection;
  52. final IChatBaseComponent titleSub = IChatBaseComponent.ChatSerializer.a(fixColor("{\"text\": \"" + title + "\"}"));
  53. final PacketPlayOutTitle sendTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TITLE, titleSub);
  54. final PacketPlayOutTitle sendLength = new PacketPlayOutTitle(7, 35, 7);
  55. connection.sendPacket((Packet)sendTitle);
  56. connection.sendPacket((Packet)sendLength);
  57. if (subtitle != null) {
  58. final IChatBaseComponent subTitleSub = IChatBaseComponent.ChatSerializer.a(fixColor("{\"text\": \"" + subtitle + "\"}"));
  59. final PacketPlayOutTitle sendSubTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.SUBTITLE, subTitleSub);
  60. final PacketPlayOutTitle sendSubLength = new PacketPlayOutTitle(7, 35, 7);
  61. connection.sendPacket((Packet)sendSubTitle);
  62. connection.sendPacket((Packet)sendSubLength);
  63. }
  64. }
  65. }
  66.  
  67. public static void sendActionbar(final Player player, final String message) {
  68. final IChatBaseComponent icbc = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + fixColor(message) + "\"}");
  69. final PacketPlayOutChat bar = new PacketPlayOutChat(icbc, (byte)2);
  70. ((CraftPlayer)player).getHandle().playerConnection.sendPacket((Packet)bar);
  71. }
  72.  
  73. public static void sendTablist(final Player player, final String header, final String footer) {
  74. final CraftPlayer craftplayer = (CraftPlayer)player;
  75. final PlayerConnection connection = craftplayer.getHandle().playerConnection;
  76. final IChatBaseComponent headerJSON = IChatBaseComponent.ChatSerializer.a(fixColor("{\"text\": \"" + header + "\"}"));
  77. final IChatBaseComponent footerJSON = IChatBaseComponent.ChatSerializer.a(fixColor("{\"text\": \"" + footer + "\"}"));
  78. final PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter();
  79. try {
  80. final Field headerField = packet.getClass().getDeclaredField("a");
  81. headerField.setAccessible(true);
  82. headerField.set(packet, headerJSON);
  83. headerField.setAccessible(!headerField.isAccessible());
  84. final Field footerField = packet.getClass().getDeclaredField("b");
  85. footerField.setAccessible(true);
  86. footerField.set(packet, footerJSON);
  87. footerField.setAccessible(!footerField.isAccessible());
  88. }
  89. catch (Exception e) {
  90. e.printStackTrace();
  91. }
  92. connection.sendPacket((Packet)packet);
  93. }
  94.  
  95. public static void sendClickMessage(final Player player, final String message, final String hover, final String cmdClick) {
  96. final IChatBaseComponent chat = IChatBaseComponent.ChatSerializer.a(fixColor("{\"text\":\"\",\"extra\":[{\"text\":\"" + message + "\",\"hoverEvent\":{\"action\":\"show_text\", \"value\":\"" + hover + "\"},\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + cmdClick + "\"}}]}"));
  97. final PacketPlayOutChat packet = new PacketPlayOutChat(chat);
  98. ((CraftPlayer)player).getHandle().playerConnection.sendPacket((Packet)packet);
  99. }
  100.  
  101. public static void clearPlayer(final Player player) {
  102. player.getInventory().clear();
  103. player.getInventory().setArmorContents((ItemStack[])null);
  104. removePotionEffects(player);
  105. player.setLevel(0);
  106. player.setHealth(20.0);
  107. player.setFoodLevel(20);
  108. player.setFireTicks(0);
  109. }
  110.  
  111. public static void sendPacket(final Packet<?> packet, final Player player) {
  112. ((CraftPlayer)player).getHandle().playerConnection.sendPacket((Packet)packet);
  113. }
  114.  
  115. public static String getTime(final long time) {
  116. return new SimpleDateFormat("HH:mm").format(new Date(time));
  117. }
  118.  
  119. public static String getDate(final long time) {
  120. return new SimpleDateFormat("dd/MM/yyyy").format(new Date(time));
  121. }
  122.  
  123. public static int randomInt(final int min, final int max) {
  124. return ServerUtil.random.nextInt(max - min) + min;
  125. }
  126.  
  127. public static World resetWorld(final File backup, final File toReset, final String worldname) {
  128. Bukkit.getServer().unloadWorld(worldname, true);
  129. try {
  130. FileUtils.deleteDirectory(toReset);
  131. }
  132. catch (IOException e) {
  133. e.printStackTrace();
  134. }
  135. if (!toReset.exists()) {
  136. try {
  137. FileUtils.copyDirectory(backup, toReset);
  138. }
  139. catch (IOException e) {
  140. e.printStackTrace();
  141. }
  142. }
  143. final World w = Bukkit.createWorld(new WorldCreator(worldname));
  144. return w;
  145. }
  146.  
  147. public static void setHologram(final String name, final Location loc) {
  148. final ArmorStand as = (ArmorStand)loc.getWorld().spawnEntity(loc, EntityType.ARMOR_STAND);
  149. as.setArms(false);
  150. as.setGravity(false);
  151. as.setCanPickupItems(false);
  152. as.setVisible(false);
  153. as.setCustomName(fixColor(name));
  154. as.setCustomNameVisible(true);
  155. }
  156.  
  157. public static void sendForWorldTitle(final String title, final String subtitle, final String world) {
  158. for (final Player all : Bukkit.getOnlinePlayers()) {
  159. if (all.getWorld().getName().equals(world)) {
  160. sendTitle(all, title, subtitle);
  161. }
  162. }
  163. }
  164.  
  165. public static int getPlayersInWorld(final String world) {
  166. return Bukkit.getWorld(world).getPlayers().size();
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement