Vinetos

Minecraft Reflection Utils

Jan 16th, 2017
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.60 KB | None | 0 0
  1. public class Reflection {
  2.  
  3.     // Get a class
  4.     public static Class<?> getClass(String classname) {
  5.         try {
  6.             String version = getNmsVersion();
  7.             String path = classname.replace("{nms}", "net.minecraft.server." + version)
  8.                     .replace("{nm}", "net.minecraft." + version)
  9.                     .replace("{cb}", "org.bukkit.craftbukkit." + version);
  10.             return Class.forName(path);
  11.         } catch (Throwable t) {
  12.             t.printStackTrace();
  13.             return null;
  14.         }
  15.     }
  16.  
  17.     // Get a class as Array
  18.     public static Class<?> getArrayClass(String classname, int arraySize) {
  19.         try {
  20.             return Array.newInstance(getClass(classname), arraySize).getClass();
  21.         } catch (Throwable t) {
  22.             t.printStackTrace();
  23.             return null;
  24.         }
  25.     }
  26.  
  27.  
  28.     // Get a net.minecraft.server version
  29.     public static String getNmsVersion() {
  30.         return Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
  31.     }
  32.  
  33.  
  34.     // Get a nms player
  35.     public static Object getNmsPlayer(Player p) throws Exception {
  36.         Method getHandle = p.getClass().getMethod("getHandle");
  37.         return getHandle.invoke(p);
  38.     }
  39.  
  40.     // Get a nms scoreboard
  41.     public static Object getNmsScoreboard(Scoreboard s) throws Exception {
  42.         Method getHandle = s.getClass().getMethod("getHandle");
  43.         return getHandle.invoke(s);
  44.     }
  45.  
  46.     // Get a field value
  47.     public static Object getFieldValue(Object instance, String fieldName) throws Exception {
  48.         Field field = instance.getClass().getDeclaredField(fieldName);
  49.         field.setAccessible(true);
  50.         return field.get(instance);
  51.     }
  52.  
  53.     // Get a field value
  54.     @SuppressWarnings("rawtypes")
  55.     public static Object getFieldValue(Object instance, Class clazz, String fieldName) throws Exception {
  56.         Field field = clazz.getDeclaredField(fieldName);
  57.         field.setAccessible(true);
  58.         return field.get(instance);
  59.     }
  60.  
  61.  
  62.     // get a field
  63.     public static Field getField(Class<?> clazz, String fieldName) {
  64.         Field field = null;
  65.         try {
  66.             field = clazz.getDeclaredField(fieldName);
  67.             field.setAccessible(true);
  68.         } catch (NoSuchFieldException e) {
  69.             e.printStackTrace();
  70.         }
  71.         return field;
  72.     }
  73.  
  74.     // Getting fields
  75.     public static ArrayList<Field> getFields(Object instance, Class<?> fieldType) throws Exception {
  76.         Field[] fields = instance.getClass().getDeclaredFields();
  77.         ArrayList<Field> fieldArrayList = new ArrayList<Field>();
  78.         for (Field field : fields) {
  79.             if (field.getType() == fieldType) {
  80.                 field.setAccessible(true);
  81.                 fieldArrayList.add(field);
  82.             }
  83.         }
  84.  
  85.         return fieldArrayList;
  86.     }
  87.  
  88.     // Get a Fields who is an Array
  89.     public static ArrayList<Field> getArraysFields(Object instance, Class<?> fieldType) throws Exception {
  90.         String[] values = fieldType.toString().split(" ");
  91.         String fieldName = values[values.length - 1];
  92.         Field[] fields = instance.getClass().getDeclaredFields();
  93.         ArrayList<Field> fieldArrayList = new ArrayList<Field>();
  94.         for (Field field : fields) {
  95.             if (field.getType().isArray()) {
  96.                 if (field.getType().toString().contains(fieldName)) {
  97.                     field.setAccessible(true);
  98.                     fieldArrayList.add(field);
  99.                 }
  100.             }
  101.         }
  102.         return fieldArrayList;
  103.     }
  104.  
  105.     // Get a field value
  106.     @SuppressWarnings("unchecked")
  107.     public static <T> T getFieldValue(Field field, Object obj) {
  108.         try {
  109.             return (T) field.get(obj);
  110.         } catch (Exception e) {
  111.             e.printStackTrace();
  112.             return null;
  113.         }
  114.     }
  115.  
  116.     // Set a field value
  117.     public static void setFieldValue(Object instance, String field, Object value) {
  118.         try {
  119.             Field f = instance.getClass().getDeclaredField(field);
  120.             f.setAccessible(true);
  121.             f.set(instance, value);
  122.         } catch (Throwable t) {
  123.             t.printStackTrace();
  124.         }
  125.     }
  126.  
  127.     // Set a field value
  128.     public static void setFieldValue(Object instance, Field field, Object value) {
  129.         field.setAccessible(true);
  130.         try {
  131.             field.set(instance, value);
  132.         } catch (IllegalAccessException e) {
  133.             e.printStackTrace();
  134.         }
  135.     }
  136.  
  137.     // Get the first field by her type
  138.     public static Field getFirstFieldByType(Class<?> clazz, Class<?> type) {
  139.         for (Field field : clazz.getDeclaredFields()) {
  140.             field.setAccessible(true);
  141.             if (field.getType() == type) {
  142.                 return field;
  143.             }
  144.         }
  145.         return null;
  146.     }
  147.  
  148.     // Get player connection
  149.     public static Object getPlayerConnection(Player player) throws Exception {
  150.         Object nmsPlayer = getNmsPlayer(player);
  151.         return  nmsPlayer.getClass().getField("playerConnection").get(nmsPlayer);
  152.     }
  153.    
  154.     // Send packet to a connection player
  155.     public static void sendPacket(Object packet, Object connection) throws Exception {
  156.         connection.getClass().getMethod("sendPacket", Reflection.getClass("{nms}.Packet")).invoke(connection, packet);
  157.     }
  158.    
  159.     // Send packet to all connected player
  160.     public static void sendPacket(Object packet) throws Exception {
  161.         sendPacket(Bukkit.getOnlinePlayers(), packet);
  162.     }
  163.  
  164.     // Send packet to a list of player
  165.     public static void sendPacket(Collection<? extends Player> players, Object packet) {
  166.         try {
  167.             for (Player p : players)
  168.                 sendPacket(packet, getPlayerConnection(p));
  169.         } catch (Throwable t) {
  170.             t.printStackTrace();
  171.         }
  172.     }
  173.  
  174.     // Send a packet to a player
  175.     public static void sendPacket(Player p, Object packet) throws Exception {
  176.         ArrayList<Player> list = new ArrayList<>();
  177.         list.add(p);
  178.         sendPacket(list, packet);
  179.     }
  180.  
  181.     // Send a message with IChatBaseComponent
  182.     public static void sendMessage(Player p, Object message) throws Exception {
  183.         Object nmsPlayer = getNmsPlayer(p);
  184.         nmsPlayer.getClass().getMethod("sendMessage", Reflection.getClass("{nms}.IChatBaseComponent")).invoke(nmsPlayer, message);
  185.  
  186.     }
  187.  
  188.     // Get the ping of player
  189.     public static int ping(Player p) throws Exception {
  190.         Object nmsPlayer = Reflection.getNmsPlayer(p);
  191.         return Integer.valueOf(getFieldValue(nmsPlayer, "ping").toString());
  192.     }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment