SHARE
TWEET

Untitled

a guest Apr 18th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package fr.hidozia.api.title;
  2.  
  3. import java.lang.reflect.Field;
  4.  
  5. import org.bukkit.ChatColor;
  6. import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
  7. import org.bukkit.entity.Player;
  8.  
  9. import net.minecraft.server.v1_8_R3.IChatBaseComponent;
  10. import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
  11. import net.minecraft.server.v1_8_R3.PacketPlayOutPlayerListHeaderFooter;
  12. import net.minecraft.server.v1_8_R3.PacketPlayOutTitle;
  13. import net.minecraft.server.v1_8_R3.PlayerConnection;
  14. import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
  15.  
  16. public class Title {
  17.  
  18.     public void sendTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String message) {
  19.         sendTitle(player, fadeIn, stay, fadeOut, message, null);
  20.     }
  21.  
  22.     @Deprecated
  23.     public void sendSubtitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String message) {
  24.         sendTitle(player, fadeIn, stay, fadeOut, null, message);
  25.     }
  26.  
  27.     @Deprecated
  28.     public void sendFullTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String title,
  29.                               String subtitle) {
  30.         sendTitle(player, fadeIn, stay, fadeOut, title, subtitle);
  31.     }
  32.  
  33.     public void sendTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String title,
  34.                           String subtitle) {
  35.         PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
  36.         PacketPlayOutTitle packetPlayOutTimes = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TIMES, null,
  37.                 fadeIn.intValue(), stay.intValue(), fadeOut.intValue());
  38.         connection.sendPacket(packetPlayOutTimes);
  39.         if (subtitle != null) {
  40.             subtitle = subtitle.replaceAll("%player%", player.getDisplayName());
  41.             subtitle = ChatColor.translateAlternateColorCodes('&', subtitle);
  42.             IChatBaseComponent titleSub = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + subtitle + "\"}");
  43.             PacketPlayOutTitle packetPlayOutSubTitle = new PacketPlayOutTitle(
  44.                     PacketPlayOutTitle.EnumTitleAction.SUBTITLE, titleSub);
  45.             connection.sendPacket(packetPlayOutSubTitle);
  46.         }
  47.         if (title != null) {
  48.             title = title.replaceAll("%player%", player.getDisplayName());
  49.             title = ChatColor.translateAlternateColorCodes('&', title);
  50.             IChatBaseComponent titleMain = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + title + "\"}");
  51.             PacketPlayOutTitle packetPlayOutTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TITLE,
  52.                     titleMain);
  53.             connection.sendPacket(packetPlayOutTitle);
  54.         }
  55.     }
  56.  
  57.     public void sendTabTitle(Player player, String header, String footer) {
  58.         if (header == null) {
  59.             header = "";
  60.         }
  61.         header = ChatColor.translateAlternateColorCodes('&', header);
  62.         if (footer == null) {
  63.             footer = "";
  64.         }
  65.         footer = ChatColor.translateAlternateColorCodes('&', footer);
  66.  
  67.         header = header.replaceAll("%player%", player.getDisplayName());
  68.         footer = footer.replaceAll("%player%", player.getDisplayName());
  69.  
  70.         PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
  71.         IChatBaseComponent tabTitle = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + header + "\"}");
  72.         IChatBaseComponent tabFoot = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + footer + "\"}");
  73.         PacketPlayOutPlayerListHeaderFooter headerPacket = new PacketPlayOutPlayerListHeaderFooter(tabTitle);
  74.         try {
  75.             Field field = headerPacket.getClass().getDeclaredField("b");
  76.             field.setAccessible(true);
  77.             field.set(headerPacket, tabFoot);
  78.         } catch (Exception e) {
  79.             e.printStackTrace();
  80.         } finally {
  81.             connection.sendPacket(headerPacket);
  82.         }
  83.     }
  84.  
  85.     public void sendActionBar(Player player, String message) {
  86.         CraftPlayer p = (CraftPlayer) player;
  87.         IChatBaseComponent cbc = ChatSerializer.a("{\"text\": \"" + message + "\"}");
  88.         PacketPlayOutChat ppoc = new PacketPlayOutChat(cbc, (byte) 2);
  89.         ((CraftPlayer) p).getHandle().playerConnection.sendPacket(ppoc);
  90.     }
  91. }
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