Advertisement
Cool_boy21

[Bukkit] Entity glowing packet 1.12-1.14

Jun 16th, 2018
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.46 KB | None | 0 0
  1. //Made by prettydude with some code from GlowAPI
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.logging.Level;
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.entity.Entity;
  11. import org.bukkit.entity.Player;
  12.  
  13. public class GlowPacket {
  14.  
  15.     private Entity entity;
  16.     private boolean glow;
  17.     private boolean supported;
  18.     private Object packet;
  19.  
  20.     public GlowPacket(Entity entity, boolean glow) {
  21.         this(entity, glow, Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]);
  22.     }
  23.  
  24.     public GlowPacket(Entity entity, boolean glow, String version) {
  25.         this.entity = entity;
  26.         this.glow = glow;
  27.         switch (version) {
  28.             case "v1_12_R1":
  29.                 init(58);
  30.                 break;
  31.             case "v1_13_R1":
  32.             case "v1_13_R2":
  33.                 init(61);
  34.                 break;
  35.             case "v1_14_R1":
  36.                 init(56);
  37.                 break;
  38.             default:
  39.                 supported = false;
  40.         }
  41.     }
  42.     private void init(int dataWatcherObjectPos) {
  43.         try {
  44.             Class<?> packetClass = getNMSClass("PacketPlayOutEntityMetadata");
  45.  
  46.             Object nmsEntity = invokeMethod(entity.getClass().getMethod("getHandle"), entity);
  47.  
  48.             Object dataWatcher = invokeMethod(nmsEntity.getClass().getMethod("getDataWatcher"), nmsEntity);
  49.  
  50.             Field d = dataWatcher.getClass().getDeclaredField("d");
  51.             Object item = ((Map<Integer, Object>) getField(d, dataWatcher)).get(0);
  52.  
  53.             Method mb = item.getClass().getMethod("b");
  54.            
  55.             Field field = getNMSClass("Entity").getDeclaredFields()[dataWatcherObjectPos]; //magic from GlowAPI, may change in every version
  56.             Object dataWatcherObject = getField(field, nmsEntity);
  57.  
  58.             byte prev = (byte) mb.invoke(item);
  59.             byte b = (byte) (glow ? (prev | 1 << 6) : (prev & ~(1 << 6)));//6 = glowing index, also magic from GlowAPI
  60.  
  61.             List list = new ArrayList<>();
  62.             list.add(item.getClass().getConstructors()[0].newInstance(dataWatcherObject, b));
  63.  
  64.             packet = packetClass.newInstance();
  65.             setField(packetClass.getDeclaredField("a"), packet, entity.getEntityId());
  66.             setField(packetClass.getDeclaredField("b"), packet, list);
  67.  
  68.             supported = true;
  69.         } catch (Exception e) {
  70.             Bukkit.getLogger().log(Level.SEVERE, "Failed to prepare glow packet!", e);
  71.         }
  72.     }
  73.  
  74.     public void send(Player player) {
  75.         if (!supported) return;
  76.         if (!player.isOnline()) return;
  77.         sendPacket(player, packet);
  78.     }
  79.  
  80.     public Entity getEntity() {
  81.         return entity;
  82.     }
  83.  
  84.     public Object getPacket() {
  85.         return packet;
  86.     }
  87.  
  88.     public boolean shouldGlow() {
  89.         return glow;
  90.     }
  91.  
  92.     private void setField(Field field, Object object, Object value) throws IllegalArgumentException, IllegalAccessException {
  93.         field.setAccessible(true);
  94.         field.set(object, value);
  95.     }
  96.  
  97.     private Object getField(Field field, Object object) throws IllegalArgumentException, IllegalAccessException {
  98.         field.setAccessible(true);
  99.         return field.get(object);
  100.     }
  101.  
  102.     private Object invokeMethod(Method method, Object object, Object... arguments) throws IllegalAccessException, IllegalArgumentException, java.lang.reflect.InvocationTargetException {
  103.         return method.invoke(object, arguments);
  104.     }
  105.  
  106.     private Class<?> getNMSClass(String name) {
  107.         String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
  108.         try {
  109.             return Class.forName("net.minecraft.server." + version + "." + name);
  110.         } catch (ClassNotFoundException e) {
  111.             Bukkit.getLogger().log(Level.SEVERE, "Failed to get NMS class!", e);
  112.         }
  113.         return null;
  114.     }
  115.  
  116.     private void sendPacket(Player player, Object packet) {
  117.         try {
  118.             Object handle = player.getClass().getMethod("getHandle").invoke(player);
  119.             Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
  120.             playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(playerConnection, packet);
  121.         } catch (Exception e) {
  122.             Bukkit.getLogger().log(Level.SEVERE, "Failed to send packet!", e);
  123.         }
  124.     }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement