daily pastebin goal
47%
SHARE
TWEET

Untitled

a guest Mar 19th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.kewi.Teste;
  2.  
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Method;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.entity.Player;
  8.  
  9. public class ActionBarSender
  10. {
  11.     private static Class<?> CRAFTPLAYERCLASS;
  12.     private static Class<?> PACKET_PLAYER_CHAT_CLASS;
  13.     private static Class<?> ICHATCOMP;
  14.     private static Class<?> CHATMESSAGE;
  15.     private static Class<?> PACKET_CLASS;
  16.     private static Constructor<?> PACKET_PLAYER_CHAT_CONSTRUCTOR;
  17.     private static Constructor<?> CHATMESSAGE_CONSTRUCTOR;
  18.     private static final String SERVER_VERSION;
  19.    
  20.     static {
  21.         String name = Bukkit.getServer().getClass().getName();
  22.         name = name.substring(name.indexOf("craftbukkit.") + "craftbukkit.".length());
  23.         name = (SERVER_VERSION = name.substring(0, name.indexOf(".")));
  24.         try {
  25.             ActionBarSender.CRAFTPLAYERCLASS = Class.forName("org.bukkit.craftbukkit." + ActionBarSender.SERVER_VERSION + ".entity.CraftPlayer");
  26.             ActionBarSender.PACKET_PLAYER_CHAT_CLASS = Class.forName("net.minecraft.server." + ActionBarSender.SERVER_VERSION + ".PacketPlayOutChat");
  27.             ActionBarSender.PACKET_CLASS = Class.forName("net.minecraft.server." + ActionBarSender.SERVER_VERSION + ".Packet");
  28.             ActionBarSender.ICHATCOMP = Class.forName("net.minecraft.server." + ActionBarSender.SERVER_VERSION + ".IChatBaseComponent");
  29.             ActionBarSender.PACKET_PLAYER_CHAT_CONSTRUCTOR = ActionBarSender.PACKET_PLAYER_CHAT_CLASS.getConstructor(ActionBarSender.ICHATCOMP, Byte.TYPE);
  30.             ActionBarSender.CHATMESSAGE = Class.forName("net.minecraft.server." + ActionBarSender.SERVER_VERSION + ".ChatMessage");
  31.             ActionBarSender.CHATMESSAGE_CONSTRUCTOR = ActionBarSender.CHATMESSAGE.getDeclaredConstructor(String.class, Object[].class);
  32.         }
  33.         catch (Exception e) {
  34.             e.printStackTrace();
  35.         }
  36.     }
  37.    
  38.     public static void sendActionBarMessage(final Player player, final String message) {
  39.         try {
  40.             final Object icb = ActionBarSender.CHATMESSAGE_CONSTRUCTOR.newInstance(message, new Object[0]);
  41.             final Object packet = ActionBarSender.PACKET_PLAYER_CHAT_CONSTRUCTOR.newInstance(icb, (byte)2);
  42.             final Object craftplayerInst = ActionBarSender.CRAFTPLAYERCLASS.cast(player);
  43.             final Method methodHandle = ActionBarSender.CRAFTPLAYERCLASS.getMethod("getHandle", (Class<?>[])new Class[0]);
  44.             final Object methodhHandle = methodHandle.invoke(craftplayerInst, new Object[0]);
  45.             final Object playerConnection = methodhHandle.getClass().getField("playerConnection").get(methodhHandle);
  46.             playerConnection.getClass().getMethod("sendPacket", ActionBarSender.PACKET_CLASS).invoke(playerConnection, packet);
  47.         }
  48.         catch (Exception e) {
  49.             e.printStackTrace();
  50.         }
  51.     }
  52.    
  53. }
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