daily pastebin goal
45%
SHARE
TWEET

Untitled

a guest Mar 19th, 2019 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package kwcash.metodos;
  2.  
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Field;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.entity.Player;
  9.  
  10. public class TitleAPI {
  11.    
  12.      @Deprecated
  13.       public static void sendTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String message)
  14.       {
  15.         sendTitle(player, fadeIn, stay, fadeOut, message, null);
  16.       }
  17.      
  18.       @Deprecated
  19.       public static void sendSubtitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String message)
  20.       {
  21.         sendTitle(player, fadeIn, stay, fadeOut, null, message);
  22.       }
  23.      
  24.       @Deprecated
  25.       public static void sendFullTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String title, String subtitle)
  26.       {
  27.         sendTitle(player, fadeIn, stay, fadeOut, title, subtitle);
  28.       }
  29.      
  30.       public static void sendPacket(Player player, Object packet)
  31.       {
  32.         try
  33.         {
  34.           Object handle = player.getClass().getMethod("getHandle", new Class[0]).invoke(player, new Object[0]);
  35.           Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
  36.           playerConnection.getClass().getMethod("sendPacket", new Class[] { getNMSClass("Packet") }).invoke(playerConnection, new Object[] { packet });
  37.         }
  38.         catch (Exception e)
  39.         {
  40.           e.printStackTrace();
  41.         }
  42.       }
  43.      
  44.       public static Class<?> getNMSClass(String name)
  45.       {
  46.         String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
  47.         try
  48.         {
  49.           return Class.forName("net.minecraft.server." + version + "." + name);
  50.         }
  51.         catch (ClassNotFoundException e)
  52.         {
  53.           e.printStackTrace();
  54.         }
  55.         return null;
  56.       }
  57.      
  58.       public static void sendTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String title, String subtitle)
  59.       {
  60.         try
  61.         {
  62.           if (title != null)
  63.           {
  64.             title = ChatColor.translateAlternateColorCodes('&', title);
  65.             title = title.replaceAll("%player%", player.getDisplayName());
  66.             Object enumTitle = getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("TITLE").get(null);
  67.             Object chatTitle = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", new Class[] { String.class }).invoke(null, new Object[] { "{\"text\":\"" + title + "\"}" });
  68.             Constructor<?> titleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(new Class[] { getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent"), Integer.TYPE, Integer.TYPE, Integer.TYPE });
  69.             Object titlePacket = titleConstructor.newInstance(new Object[] { enumTitle, chatTitle, fadeIn, stay, fadeOut });
  70.             sendPacket(player, titlePacket);
  71.           }
  72.           if (subtitle != null)
  73.           {
  74.             subtitle = ChatColor.translateAlternateColorCodes('&', subtitle);
  75.             subtitle = subtitle.replaceAll("%player%", player.getDisplayName());
  76.             Object enumSubtitle = getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("SUBTITLE").get(null);
  77.             Object chatSubtitle = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", new Class[] { String.class }).invoke(null, new Object[] { "{\"text\":\"" + subtitle + "\"}" });
  78.             Constructor<?> subtitleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(new Class[] { getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent"), Integer.TYPE, Integer.TYPE, Integer.TYPE });
  79.             Object subtitlePacket = subtitleConstructor.newInstance(new Object[] { enumSubtitle, chatSubtitle, fadeIn, stay, fadeOut });
  80.             sendPacket(player, subtitlePacket);
  81.           }
  82.         }
  83.         catch (Exception e)
  84.         {
  85.           e.printStackTrace();
  86.         }
  87.       }
  88.      
  89.       public static void sendTabTitle(Player player, String header, String footer)
  90.       {
  91.         if (header == null) {
  92.           header = "";
  93.         }
  94.         header = ChatColor.translateAlternateColorCodes('&', header);
  95.         if (footer == null) {
  96.           footer = "";
  97.         }
  98.         footer = ChatColor.translateAlternateColorCodes('&', footer);
  99.        
  100.         header = header.replaceAll("%player%", player.getDisplayName());
  101.         footer = footer.replaceAll("%player%", player.getDisplayName());
  102.         try
  103.         {
  104.           Object tabHeader = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", new Class[] { String.class }).invoke(null, new Object[] { "{\"text\":\"" + header + "\"}" });
  105.           Object tabFooter = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", new Class[] { String.class }).invoke(null, new Object[] { "{\"text\":\"" + footer + "\"}" });
  106.           Constructor<?> titleConstructor = getNMSClass("PacketPlayOutPlayerListHeaderFooter").getConstructor(new Class[] { getNMSClass("IChatBaseComponent") });
  107.           Object packet = titleConstructor.newInstance(new Object[] { tabHeader });
  108.           Field field = packet.getClass().getDeclaredField("b");
  109.           field.setAccessible(true);
  110.           field.set(packet, tabFooter);
  111.           sendPacket(player, packet);
  112.         }
  113.         catch (Exception ex)
  114.         {
  115.           ex.printStackTrace();
  116.         }
  117.       }
  118.  
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top