Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ServerUtil
- {
- private static List<String> strings;
- public static Random random;
- static {
- ServerUtil.strings = new ArrayList<String>();
- ServerUtil.random = new Random();
- }
- public static void respawnPlayer(final Player player) {
- new BukkitRunnable() {
- public void run() {
- final PacketPlayInClientCommand packet = new PacketPlayInClientCommand(PacketPlayInClientCommand.EnumClientCommand.PERFORM_RESPAWN);
- ((CraftPlayer)player).getHandle().playerConnection.a(packet);
- }
- }.runTaskLater((Plugin)Main.getInstance(), 2L);
- }
- public static void removePotionEffects(final Player player) {
- for (final PotionEffect effects : player.getActivePotionEffects()) {
- player.removePotionEffect(effects.getType());
- }
- }
- public static void playSound(final Player player, final Sound sound) {
- player.playSound(player.getLocation(), sound, 10.0f, 10.0f);
- }
- public static void playEffect(final Player player, final Effect effect) {
- player.playEffect(player.getLocation(), effect, 10);
- }
- public static void removeScoreboard(final Player player) {
- player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard());
- }
- public static String fixColor(final String message) {
- return ChatColor.translateAlternateColorCodes('&', message.replace(">>", "»").replace("<<", "«").replace("{CHECK}", "\u2714").replace("{X}", "\u2716").replace(">", "\u279c").replace("{O}", "\u25cf"));
- }
- public static List<String> fixColor(final List<String> stringList) {
- for (final String s : stringList) {
- ServerUtil.strings.add(fixColor(s));
- }
- return stringList;
- }
- public static void sendTitle(final Player player, final String title, final String subtitle) {
- if (title != null) {
- final PlayerConnection connection = ((CraftPlayer)player).getHandle().playerConnection;
- final IChatBaseComponent titleSub = IChatBaseComponent.ChatSerializer.a(fixColor("{\"text\": \"" + title + "\"}"));
- final PacketPlayOutTitle sendTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TITLE, titleSub);
- final PacketPlayOutTitle sendLength = new PacketPlayOutTitle(7, 35, 7);
- connection.sendPacket((Packet)sendTitle);
- connection.sendPacket((Packet)sendLength);
- if (subtitle != null) {
- final IChatBaseComponent subTitleSub = IChatBaseComponent.ChatSerializer.a(fixColor("{\"text\": \"" + subtitle + "\"}"));
- final PacketPlayOutTitle sendSubTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.SUBTITLE, subTitleSub);
- final PacketPlayOutTitle sendSubLength = new PacketPlayOutTitle(7, 35, 7);
- connection.sendPacket((Packet)sendSubTitle);
- connection.sendPacket((Packet)sendSubLength);
- }
- }
- }
- public static void sendActionbar(final Player player, final String message) {
- final IChatBaseComponent icbc = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + fixColor(message) + "\"}");
- final PacketPlayOutChat bar = new PacketPlayOutChat(icbc, (byte)2);
- ((CraftPlayer)player).getHandle().playerConnection.sendPacket((Packet)bar);
- }
- public static void sendTablist(final Player player, final String header, final String footer) {
- final CraftPlayer craftplayer = (CraftPlayer)player;
- final PlayerConnection connection = craftplayer.getHandle().playerConnection;
- final IChatBaseComponent headerJSON = IChatBaseComponent.ChatSerializer.a(fixColor("{\"text\": \"" + header + "\"}"));
- final IChatBaseComponent footerJSON = IChatBaseComponent.ChatSerializer.a(fixColor("{\"text\": \"" + footer + "\"}"));
- final PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter();
- try {
- final Field headerField = packet.getClass().getDeclaredField("a");
- headerField.setAccessible(true);
- headerField.set(packet, headerJSON);
- headerField.setAccessible(!headerField.isAccessible());
- final Field footerField = packet.getClass().getDeclaredField("b");
- footerField.setAccessible(true);
- footerField.set(packet, footerJSON);
- footerField.setAccessible(!footerField.isAccessible());
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- connection.sendPacket((Packet)packet);
- }
- public static void sendClickMessage(final Player player, final String message, final String hover, final String cmdClick) {
- final IChatBaseComponent chat = IChatBaseComponent.ChatSerializer.a(fixColor("{\"text\":\"\",\"extra\":[{\"text\":\"" + message + "\",\"hoverEvent\":{\"action\":\"show_text\", \"value\":\"" + hover + "\"},\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + cmdClick + "\"}}]}"));
- final PacketPlayOutChat packet = new PacketPlayOutChat(chat);
- ((CraftPlayer)player).getHandle().playerConnection.sendPacket((Packet)packet);
- }
- public static void clearPlayer(final Player player) {
- player.getInventory().clear();
- player.getInventory().setArmorContents((ItemStack[])null);
- removePotionEffects(player);
- player.setLevel(0);
- player.setHealth(20.0);
- player.setFoodLevel(20);
- player.setFireTicks(0);
- }
- public static void sendPacket(final Packet<?> packet, final Player player) {
- ((CraftPlayer)player).getHandle().playerConnection.sendPacket((Packet)packet);
- }
- public static String getTime(final long time) {
- return new SimpleDateFormat("HH:mm").format(new Date(time));
- }
- public static String getDate(final long time) {
- return new SimpleDateFormat("dd/MM/yyyy").format(new Date(time));
- }
- public static int randomInt(final int min, final int max) {
- return ServerUtil.random.nextInt(max - min) + min;
- }
- public static World resetWorld(final File backup, final File toReset, final String worldname) {
- Bukkit.getServer().unloadWorld(worldname, true);
- try {
- FileUtils.deleteDirectory(toReset);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- if (!toReset.exists()) {
- try {
- FileUtils.copyDirectory(backup, toReset);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- final World w = Bukkit.createWorld(new WorldCreator(worldname));
- return w;
- }
- public static void setHologram(final String name, final Location loc) {
- final ArmorStand as = (ArmorStand)loc.getWorld().spawnEntity(loc, EntityType.ARMOR_STAND);
- as.setArms(false);
- as.setGravity(false);
- as.setCanPickupItems(false);
- as.setVisible(false);
- as.setCustomName(fixColor(name));
- as.setCustomNameVisible(true);
- }
- public static void sendForWorldTitle(final String title, final String subtitle, final String world) {
- for (final Player all : Bukkit.getOnlinePlayers()) {
- if (all.getWorld().getName().equals(world)) {
- sendTitle(all, title, subtitle);
- }
- }
- }
- public static int getPlayersInWorld(final String world) {
- return Bukkit.getWorld(world).getPlayers().size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement