funkemunky

Untitled

Jul 22nd, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 28.42 KB | None | 0 0
  1. package cc.funkemunky.api.utils;
  2.  
  3. import cc.funkemunky.api.Atlas;
  4. import cc.funkemunky.api.tinyprotocol.api.ProtocolVersion;
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Location;
  7. import org.bukkit.Material;
  8. import org.bukkit.Server;
  9. import org.bukkit.block.Block;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.inventory.ItemStack;
  12. import org.bukkit.material.Step;
  13. import org.bukkit.material.WoodenStep;
  14. import org.bukkit.util.Vector;
  15.  
  16. import java.io.File;
  17. import java.lang.reflect.Field;
  18. import java.lang.reflect.InvocationTargetException;
  19. import java.lang.reflect.Method;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.logging.Level;
  25.  
  26. public class ReflectionsUtil {
  27.     public static Class<?> blockPosition = null;
  28.     private static String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
  29.     public static Class<?> EntityPlayer = getNMSClass("EntityPlayer");
  30.     public static Class<?> Entity = getNMSClass("Entity");
  31.     public static Class<?> CraftPlayer = getCBClass("entity.CraftPlayer");
  32.     public static Class<?> CraftEntity = getCBClass("entity.CraftEntity");
  33.     public static Class<?> CraftWorld = getCBClass("CraftWorld");
  34.     private static Class<?> craftServer = getCBClass("CraftServer");
  35.     public static Class<?> World = getNMSClass("World");
  36.     public static Class<?> worldServer = getNMSClass("WorldServer");
  37.     public static Class<?> playerConnection = getNMSClass("PlayerConnection");
  38.     public static Class<?> networkManager = getNMSClass("NetworkManager");
  39.     public static Class<?> minecraftServer = getNMSClass("MinecraftServer");
  40.     public static Class<?> packet = getNMSClass("Packet");
  41.     public static Class<?> iBlockData = null;
  42.     public static Class<?> iBlockAccess = null;
  43.     private static Class<?> vanillaBlock = getNMSClass("Block");
  44.     private static Method getCubes = getMethod(World, "a", getNMSClass("AxisAlignedBB"));
  45.     private static Method getCubes1_12 = getMethod(World, "getCubes", getNMSClass("Entity"), getNMSClass("AxisAlignedBB"));
  46.  
  47.     public ReflectionsUtil() {
  48.         if (ProtocolVersion.getGameVersion().isOrAbove(ProtocolVersion.V1_8)) {
  49.             iBlockData = getNMSClass("IBlockData");
  50.             blockPosition = getNMSClass("BlockPosition");
  51.             iBlockAccess = getNMSClass("IBlockAccess");
  52.         }
  53.     }
  54.  
  55.     public static Object getEntityPlayer(Player player) {
  56.         return getMethodValue(getMethod(CraftPlayer, "getHandle"), player);
  57.     }
  58.  
  59.     public static Object getEntity(org.bukkit.entity.Entity entity) {
  60.         return getMethodValue(getMethod(CraftEntity, "getHandle"), entity);
  61.     }
  62.  
  63.     public static Object getExpandedBoundingBox(Object box, double x, double y, double z) {
  64.         return getMethodValue(getMethod(box.getClass(), "grow", double.class, double.class, double.class), box, x, y, z);
  65.     }
  66.  
  67.     public static Object modifyBoundingBox(Object box, double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
  68.         double newminX = (double) getFieldValue(getFieldByName(box.getClass(), "a"), box) - minX;
  69.         double newminY = (double) getFieldValue(getFieldByName(box.getClass(), "b"), box) - minY;
  70.         double newminZ = (double) getFieldValue(getFieldByName(box.getClass(), "c"), box) - minZ;
  71.         double newmaxX = (double) getFieldValue(getFieldByName(box.getClass(), "d"), box) + maxX;
  72.         double newmaxY = (double) getFieldValue(getFieldByName(box.getClass(), "e"), box) + maxY;
  73.         double newmaxZ = (double) getFieldValue(getFieldByName(box.getClass(), "f"), box) + maxZ;
  74.  
  75.         return newInstance(getNMSClass("AxisAlignedBB"), newminX, newminY, newminZ, newmaxX, newmaxY, newmaxZ);
  76.     }
  77.  
  78.     private static Vector getBoxMin(Object box) {
  79.         if (hasField(box.getClass(), "a")) {
  80.             double x = (double) getFieldValue(getFieldByName(box.getClass(), "a"), box);
  81.             double y = (double) getFieldValue(getFieldByName(box.getClass(), "b"), box);
  82.             double z = (double) getFieldValue(getFieldByName(box.getClass(), "c"), box);
  83.             return new Vector(x, y, z);
  84.         } else {
  85.             double x = (double) getFieldValue(getFieldByName(box.getClass(), "minX"), box);
  86.             double y = (double) getFieldValue(getFieldByName(box.getClass(), "minY"), box);
  87.             double z = (double) getFieldValue(getFieldByName(box.getClass(), "minZ"), box);
  88.             return new Vector(x, y, z);
  89.         }
  90.     }
  91.  
  92.     public static Object getMinecraftServer() {
  93.         return getMethodValue(getMethod(craftServer, "getServer"), Bukkit.getServer());
  94.     }
  95.  
  96.     private static Vector getBoxMax(Object box) {
  97.         if (hasField(box.getClass(), "d")) {
  98.             double x = (double) getFieldValue(getFieldByName(box.getClass(), "d"), box);
  99.             double y = (double) getFieldValue(getFieldByName(box.getClass(), "e"), box);
  100.             double z = (double) getFieldValue(getFieldByName(box.getClass(), "f"), box);
  101.             return new Vector(x, y, z);
  102.         } else {
  103.             double x = (double) getFieldValue(getFieldByName(box.getClass(), "maxX"), box);
  104.             double y = (double) getFieldValue(getFieldByName(box.getClass(), "maxY"), box);
  105.             double z = (double) getFieldValue(getFieldByName(box.getClass(), "maxZ"), box);
  106.             return new Vector(x, y, z);
  107.         }
  108.     }
  109.  
  110.     public static BoundingBox toBoundingBox(Object aaBB) {
  111.         Vector min = getBoxMin(aaBB);
  112.         Vector max = getBoxMax(aaBB);
  113.  
  114.         return new BoundingBox((float) min.getX(), (float) min.getY(), (float) min.getZ(), (float) max.getX(), (float) max.getY(), (float) max.getZ());
  115.     }
  116.  
  117.     public static float getBlockDurability(Block block) {
  118.         Object vanillaBlock = getVanillaBlock(block);
  119.         return (float) getFieldValue(getFieldByName(getNMSClass("Block"), "strength"), vanillaBlock);
  120.     }
  121.  
  122.     public static boolean canDestroyBlock(Player player, Block block) {
  123.         Object inventory = getVanillaInventory(player);
  124.         return (boolean) getMethodValue(getMethod(getNMSClass("PlayerInventory"), "b", getNMSClass("Block")), inventory, ProtocolVersion.getGameVersion().isAbove(ProtocolVersion.V1_8_9) ? getBlockData(block) : getVanillaBlock(block));
  125.     }
  126.  
  127.     public static Object getVanillaInventory(Player player) {
  128.         return getMethodValue(getMethod(getCBClass("inventory.CraftInventoryPlayer"), "getInventory"), player.getInventory());
  129.     }
  130.  
  131.     public static float getFriction(Block block) {
  132.         Object blockNMS = getVanillaBlock(block);
  133.  
  134.         return (float) getFieldValue(getFieldByName(vanillaBlock, "frictionFactor"), blockNMS);
  135.     }
  136.  
  137.     public static Object getBlockPosition(Location location) {
  138.         if (ProtocolVersion.getGameVersion().isOrAbove(ProtocolVersion.V1_8)) {
  139.             return newInstance(blockPosition, location.getBlockX(), location.getBlockY(), location.getBlockZ());
  140.         }
  141.         return null;
  142.     }
  143.  
  144.     public static List<org.bukkit.entity.Entity> getEntitiesInWorld(org.bukkit.World world) {
  145.         Object worldHandle = getWorldHandle(world);
  146.         List<org.bukkit.entity.Entity> toReturn = new ArrayList<>();
  147.         List<Object> entityList = new ArrayList<>((List<Object>) getFieldValue(getFieldByName(getNMSClass("World"), "entityList"), worldHandle));
  148.  
  149.         Class<?> entity = getNMSClass("Entity");
  150.         entityList.forEach(object -> {
  151.             Object bEntity = getMethodValue(getMethod(entity, "getBukkitEntity"), object);
  152.             if(bEntity != null) {
  153.                 toReturn.add((org.bukkit.entity.Entity) bEntity);
  154.             }
  155.         });
  156.         return toReturn;
  157.     }
  158.  
  159.     public static BoundingBox getBlockBoundingBox(Block block) {
  160.         try {
  161.             if (!isBukkitVerison("1_7")) {
  162.                 Object bPos = blockPosition.getConstructor(int.class, int.class, int.class).newInstance(block.getLocation().getBlockX(), block.getLocation().getBlockY(), block.getLocation().getBlockZ());
  163.                 Object world = getWorldHandle(block.getWorld());
  164.                 Object data = getMethodValue(getMethod(world.getClass(), "getType", blockPosition), world, bPos);
  165.                 Object blockNMS = getMethodValue(getMethod(getNMSClass("IBlockData"), "getBlock"), data);
  166.  
  167.                 if (ProtocolVersion.getGameVersion().isBelow(ProtocolVersion.V1_13)) {
  168.                     if (!isNewVersion()) {
  169.  
  170.                         if (getMethodValueNoST(getMethodNoST(blockNMS.getClass(), "a", World, blockPosition, iBlockData), blockNMS, world, bPos, data) != null
  171.                                 && !BlockUtils.isSlab(block)) {
  172.                             BoundingBox box = toBoundingBox(getMethodValue(getMethod(blockNMS.getClass(), "a", World, blockPosition, iBlockData), blockNMS, world, bPos, data));
  173.  
  174.                             if (ProtocolVersion.getGameVersion().isBelow(ProtocolVersion.V1_13)) {
  175.                                 if (block.getType().toString().contains("STEP") && !block.getType().toString().contains("WOOD")) {
  176.                                     Step slab = (Step) block.getType().getNewData(block.getData());
  177.  
  178.                                     box.minY = block.getY();
  179.                                     box.maxY = block.getY();
  180.                                     if (slab.isInverted()) {
  181.                                         box = box.add(0, 0.5f, 0, 0, 1f, 0);
  182.                                     } else {
  183.                                         box = box.add(0, 0f, 0, 0, 0.5f, 0);
  184.                                     }
  185.                                 } else if (block.getType().toString().contains("STEP")) {
  186.                                     WoodenStep slab = (WoodenStep) block.getType().getNewData(block.getData());
  187.  
  188.                                     box.minY = block.getY();
  189.                                     box.maxY = block.getY();
  190.                                     if (slab.isInverted()) {
  191.                                         box = box.add(0, 0.5f, 0, 0, 1f, 0);
  192.                                     } else {
  193.                                         box = box.add(0, 0f, 0, 0, 0.5f, 0);
  194.                                     }
  195.                                 }
  196.                             }
  197.                             return box;
  198.                         } else if (getMethodValueNoST(getMethodNoST(vanillaBlock, "a", World, blockPosition, iBlockData), blockNMS, world, bPos, data) != null) {
  199.                             BoundingBox box = toBoundingBox(getMethodValue(getMethod(vanillaBlock, "a", World, blockPosition, iBlockData), blockNMS, world, bPos, data));
  200.  
  201.                             if (ProtocolVersion.getGameVersion().isBelow(ProtocolVersion.V1_13)) {
  202.                                 if (block.getType().toString().contains("STEP") && !block.getType().toString().contains("WOOD")) {
  203.                                     Step slab = (Step) block.getType().getNewData(block.getData());
  204.  
  205.                                     box.minY = block.getY();
  206.                                     box.maxY = block.getY();
  207.                                     if (slab.isInverted()) {
  208.                                         box = box.add(0, 0.5f, 0, 0, 1f, 0);
  209.                                     } else {
  210.                                         box = box.add(0, 0f, 0, 0, 0.5f, 0);
  211.                                     }
  212.                                 } else if (block.getType().toString().contains("STEP")) {
  213.                                     WoodenStep slab = (WoodenStep) block.getType().getNewData(block.getData());
  214.  
  215.                                     box.minY = block.getY();
  216.                                     box.maxY = block.getY();
  217.                                     if (slab.isInverted()) {
  218.                                         box = box.add(0, 0.5f, 0, 0, 1f, 0);
  219.                                     } else {
  220.                                         box = box.add(0, 0f, 0, 0, 0.5f, 0);
  221.                                     }
  222.                                 }
  223.                             }
  224.                             return box;
  225.                         } else {
  226.                             return new BoundingBox(block.getX(), block.getY(), block.getZ(), block.getX(), block.getY(), block.getZ());
  227.                         }
  228.                     } else {
  229.                         if (getMethodValueNoST(getMethodNoST(blockNMS.getClass(), "a", iBlockData, getNMSClass("IBlockAccess"), blockPosition), blockNMS, data, world, bPos) != null) {
  230.                             return toBoundingBox(getMethodValue(getMethod(blockNMS.getClass(), "a", iBlockData, getNMSClass("IBlockAccess"), blockPosition), blockNMS, data, world, bPos)).add(block.getX(), block.getY(), block.getZ(), block.getX(), block.getY(), block.getZ());
  231.                         } else if (getMethodValueNoST(getMethodNoST(vanillaBlock, "a", iBlockData, getNMSClass("IBlockAccess"), blockPosition), blockNMS, data, world, bPos) != null) {
  232.                             return toBoundingBox(getMethodValue(getMethod(vanillaBlock, "a", iBlockData, getNMSClass("IBlockAccess"), blockPosition), blockNMS, data, world, bPos)).add(block.getX(), block.getY(), block.getZ(), block.getX(), block.getY(), block.getZ());
  233.                         } else {
  234.                             return new BoundingBox(block.getX(), block.getY(), block.getZ(), block.getX(), block.getY(), block.getZ());
  235.                         }
  236.                     }
  237.                 } else {
  238.                     Object voxelShape = getMethodValue(getMethod(vanillaBlock, "a", iBlockData, getNMSClass("IBlockAccess"), blockPosition), blockNMS, data, world, bPos);
  239.                     Object axisAlignedBB = getMethodValue(getMethod(getNMSClass("VoxelShape"), "a"), voxelShape);
  240.  
  241.  
  242.                     return toBoundingBox(axisAlignedBB);
  243.  
  244.                 }
  245.             } else {
  246.                 Object blockNMS = getVanillaBlock(block);
  247.                 Object world = getWorldHandle(block.getWorld());
  248.                 if (getMethodValueNoST(getMethodNoST(vanillaBlock, "a", getNMSClass("World"), int.class, int.class, int.class), blockNMS, world, block.getLocation().getBlockX(), block.getLocation().getBlockY(), block.getLocation().getBlockZ()) != null) {
  249.                     return toBoundingBox(getMethodValue(getMethod(vanillaBlock, "a", getNMSClass("World"), int.class, int.class, int.class), blockNMS, world, block.getLocation().getBlockX(), block.getLocation().getBlockY(), block.getLocation().getBlockZ()));
  250.                 } else {
  251.                     //Bukkit.broadcastMessage(block.getType().name());
  252.                     return new BoundingBox(block.getX(), block.getY(), block.getZ(), block.getX(), block.getY(), block.getZ());
  253.                 }
  254.             }
  255.         } catch (Exception e) {
  256.             Bukkit.getLogger().log(Level.SEVERE, "Error occured with block: " + block.getType().toString());
  257.             e.printStackTrace();
  258.         }
  259.         return null;
  260.     }
  261.  
  262.     public static double getTPS(Server server) {
  263.         Object handle = getMethodValue(getMethod(getCBClass("CraftServer"), "getHandle"), server);
  264.  
  265.         return (int) getFieldValue(getFieldByName(getNMSClass("MinecraftServer"), "TPS"), handle);
  266.     }
  267.  
  268.     public static float getBlockHardness(final Material material) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
  269.         if (!material.isBlock())
  270.             return 0;
  271.  
  272.         final int blockId = material.getId();
  273.         final Object nmsBlock = getNMSClass("Block").getMethod("getById", Integer.TYPE).invoke(null, blockId);
  274.  
  275.         try {
  276.             final Field field = nmsBlock.getClass().getDeclaredField("strength");
  277.             field.setAccessible(true);
  278.             return (float) field.get(nmsBlock);
  279.         } catch (final NoSuchFieldException e) {
  280.             return 0.0F;
  281.         }
  282.     }
  283.  
  284.     public static float getDestroySpeed(Block block, Player player) {
  285.  
  286.         if (ProtocolVersion.getGameVersion().isAbove(ProtocolVersion.V1_8_9)) {
  287.             Object item = getVanillaItem(player.getItemInHand());
  288.             return (float) getMethodValue(getMethod(getNMSClass("Item"), "getDestroySpeed", getNMSClass("ItemStack"), getNMSClass("IBlockData")), item, getVanillaItemStack(player.getItemInHand()), getBlockData(block));
  289.         } else {
  290.             Object item = getVanillaItem(player.getInventory().getItemInHand());
  291.             return (float) getMethodValue(getMethod(getNMSClass("Item"), "getDestroySpeed", getNMSClass("ItemStack"), getNMSClass("Block")), item, getVanillaItemStack(player.getInventory().getItemInHand()), getVanillaBlock(block));
  292.         }
  293.     }
  294.  
  295.     public static Object getVanillaItem(ItemStack itemStack) {
  296.         return getMethodValue(getMethod(getNMSClass("ItemStack"), "getItem"), getVanillaItemStack(itemStack));
  297.     }
  298.  
  299.     public static Object getVanillaItemStack(ItemStack itemStack) {
  300.         return getMethodValue(getMethod(getCBClass("inventory.CraftItemStack"), "asNMSCopy", getClass("org.bukkit.inventory.ItemStack")), itemStack, itemStack);
  301.     }
  302.  
  303.     public static Object getVanillaBlock(Block block) {
  304.  
  305.         if (!isBukkitVerison("1_7")) {
  306.             Object getType = getBlockData(block);
  307.             return getMethodValue(getMethod(iBlockData, "getBlock"), getType);
  308.         } else {
  309.             Object world = getWorldHandle(block.getWorld());
  310.             return getMethodValue(getMethod(worldServer, "getType", int.class, int.class, int.class), world, block.getLocation().getBlockX(), block.getLocation().getBlockY(), block.getLocation().getBlockZ());
  311.         }
  312.     }
  313.  
  314.     public static Object getBlockData(Block block) {
  315.         try {
  316.             if (!isBukkitVerison("1_7")) {
  317.                 Object bPos = blockPosition.getConstructor(int.class, int.class, int.class).newInstance(block.getLocation().getBlockX(), block.getLocation().getBlockY(), block.getLocation().getBlockZ());
  318.                 Object world = getWorldHandle(block.getWorld());
  319.                 return getMethodValue(getMethod(worldServer, "getType", blockPosition), world, bPos);
  320.             } else {
  321.                 Object world = getWorldHandle(block.getWorld());
  322.                 return getMethodValue(getMethod(worldServer, "getType", int.class, int.class, int.class), world, block.getLocation().getBlockX(), block.getLocation().getBlockY(), block.getLocation().getBlockZ());
  323.             }
  324.         } catch (Exception e) {
  325.             e.printStackTrace();
  326.         }
  327.         return null;
  328.     }
  329.  
  330.     public static Object getBelowBox(Player player, double below) {
  331.         Object box = getBoundingBox(player);
  332.         double minX = (double) getFieldValue(getFieldByName(box.getClass(), "a"), box);
  333.         double minY = (double) getFieldValue(getFieldByName(box.getClass(), "b"), box) - below;
  334.         double minZ = (double) getFieldValue(getFieldByName(box.getClass(), "c"), box);
  335.         double maxX = (double) getFieldValue(getFieldByName(box.getClass(), "d"), box);
  336.         double maxY = (double) getFieldValue(getFieldByName(box.getClass(), "e"), box);
  337.         double maxZ = (double) getFieldValue(getFieldByName(box.getClass(), "f"), box);
  338.  
  339.         try {
  340.             return getNMSClass("AxisAlignedBB").getConstructor(double.class, double.class, double.class, double.class, double.class, double.class).newInstance(minX, minY, minZ, maxX, maxY, maxZ);
  341.         } catch (Exception e) {
  342.             e.printStackTrace();
  343.         }
  344.         return null;
  345.     }
  346.  
  347.     public static Object getBoundingBox(Player player) {
  348.         return getBoundingBox((org.bukkit.entity.Entity) player);
  349.     }
  350.  
  351.     public static Object getBoundingBox(org.bukkit.entity.Entity entity) {
  352.         return isBukkitVerison("1_7") ? getFieldValue(getFieldByName(Entity, "boundingBox"), getEntity(entity)) : getMethodValue(getMethod(Entity, "getBoundingBox"), getEntity(entity));
  353.     }
  354.  
  355.     public static boolean isBukkitVerison(String version) {
  356.         return ProtocolVersion.getGameVersion().getServerVersion().contains(version);
  357.     }
  358.  
  359.     public static File getPluginFolder() {
  360.         Object console = getMethodValue(getMethod(getCBClass("CraftServer"), "console"), Atlas.getInstance().getServer());
  361.         Object options = getFieldValue(getFieldByName(getNMSClass("MinecraftServer"), "options"), console);
  362.         return (File) getMethodValue(getMethod(getNMSClass("OptionSet"), "valueOf", String.class), options, "plugins");
  363.     }
  364.  
  365.     public static boolean isNewVersion() {
  366.         return isBukkitVerison("1_9") || isBukkitVerison("1_1");
  367.     }
  368.  
  369.     public static Class<?> getCBClass(String string) {
  370.         return getClass("org.bukkit.craftbukkit." + version + "." + string);
  371.     }
  372.  
  373.  
  374.     public static Object newAxisAlignedBB(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
  375.         try {
  376.             return isBukkitVerison("1_7") ? getMethodValue(getMethod(getNMSClass("AxisAlignedBB"), "a", double.class, double.class, double.class, double.class, double.class, double.class), null, minX, minY, minZ, maxX, maxY, maxZ) : getNMSClass("AxisAlignedBB").getConstructor(double.class, double.class, double.class, double.class, double.class, double.class).newInstance(minX, minY, minZ, maxX, maxY, maxZ);
  377.         } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
  378.             e.printStackTrace();
  379.         }
  380.         return null;
  381.     }
  382.  
  383.     public static Object newVoxelShape(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
  384.         try {
  385.             return getNMSClass("AxisAlignedBB").getConstructor(double.class, double.class, double.class, double.class, double.class, double.class).newInstance(minX, minY, minZ, maxX, maxY, maxZ);
  386.         } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
  387.             e.printStackTrace();
  388.         }
  389.         return null;
  390.     }
  391.  
  392.     public static double getMotionY(Player player) {
  393.         double motionY = 0;
  394.         try {
  395.             motionY = (double) ReflectionsUtil.getEntityPlayer(player).getClass().getField("motY").get(ReflectionsUtil.getEntityPlayer(player));
  396.         } catch (IllegalAccessException e) {
  397.             e.printStackTrace();
  398.         } catch (NoSuchFieldException e) {
  399.             e.printStackTrace();
  400.         }
  401.  
  402.         return motionY;
  403.     }
  404.  
  405.     public static Object newAxisAlignedBB(Location from, Location to) {
  406.         double minX = Math.min(from.getX(), to.getX());
  407.         double minY = Math.min(from.getY(), to.getY());
  408.         double minZ = Math.min(from.getZ(), to.getZ());
  409.         double maxX = Math.max(from.getX(), to.getX());
  410.         double maxY = Math.max(from.getY(), to.getY());
  411.         double maxZ = Math.max(from.getZ(), to.getZ());
  412.  
  413.         try {
  414.             return getNMSClass("AxisAlignedBB").getConstructor(double.class, double.class, double.class, double.class, double.class, double.class).newInstance(minX, minY, minZ, maxX, maxY, maxZ);
  415.         } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
  416.             e.printStackTrace();
  417.         }
  418.         return null;
  419.     }
  420.  
  421.     public static Class<?> getClass(String string) {
  422.         try {
  423.             return Class.forName(string);
  424.         } catch (ClassNotFoundException e) {
  425.             e.printStackTrace();
  426.         }
  427.         return null;
  428.     }
  429.  
  430.     @SuppressWarnings("unchecked")
  431.     public static Enum<?> getEnum(Class<?> clazz, String enumName) {
  432.         return Enum.valueOf((Class<Enum>) clazz, enumName);
  433.     }
  434.  
  435.     public static Field getFieldByName(Class<?> clazz, String fieldName) {
  436.         try {
  437.             Field field = clazz.getDeclaredField(fieldName) != null ? clazz.getDeclaredField(fieldName) : clazz.getSuperclass().getDeclaredField(fieldName);
  438.             field.setAccessible(true);
  439.  
  440.             return field;
  441.         } catch (Exception e) {
  442.             e.printStackTrace();
  443.             return null;
  444.         }
  445.     }
  446.  
  447.     public static Object setFieldValue(Object object, Field field, Object value) {
  448.         try {
  449.             field.set(object, value);
  450.         } catch (IllegalAccessException e) {
  451.             e.printStackTrace();
  452.         }
  453.  
  454.         return field.getDeclaringClass();
  455.     }
  456.  
  457.  
  458.     public static boolean inBlock(Player player, Object axisAlignedBB) {
  459.         return getCollidingBlocks(player, axisAlignedBB).size() > 0;
  460.     }
  461.  
  462.     /**
  463.      * Method removed in 1.12 and later versions in NMS
  464.      **/
  465.     public static Collection<?> getCollidingBlocks(Player player, Object axisAlignedBB) {
  466.         Object world = getWorldHandle(player.getWorld());
  467.         return (Collection<?>) (isNewVersion()
  468.                 ? getMethodValue(getCubes1_12, world, null, axisAlignedBB)
  469.                 : getMethodValue(getCubes, world, axisAlignedBB));
  470.     }
  471.  
  472.     public static Object getWorldHandle(org.bukkit.World world) {
  473.         return getMethodValue(getMethod(CraftWorld, "getHandle"), world);
  474.     }
  475.  
  476.     public static Field getFirstFieldByType(Class<?> clazz, Class<?> type) {
  477.         try {
  478.             for (Field field : clazz.getDeclaredFields()) {
  479.                 if (field.getType().equals(type)) {
  480.                     field.setAccessible(true);
  481.                     return field;
  482.                 }
  483.             }
  484.         } catch (Exception e) {
  485.             e.printStackTrace();
  486.         }
  487.         return null;
  488.     }
  489.  
  490.     public static Method getMethod(Class<?> clazz, String methodName, Class<?>... args) {
  491.         try {
  492.             Method method = clazz.getMethod(methodName, args);
  493.             method.setAccessible(true);
  494.             return method;
  495.         } catch (Exception e) {
  496.             e.printStackTrace();
  497.         }
  498.         return null;
  499.     }
  500.  
  501.     private static Method getMethodNoST(Class<?> clazz, String methodName, Class<?>... args) {
  502.         try {
  503.             Method method = clazz.getMethod(methodName, args);
  504.             method.setAccessible(true);
  505.             return method;
  506.         } catch (Exception e) {
  507.         }
  508.         return null;
  509.     }
  510.  
  511.     public static boolean hasMethod(Class clazz, Method method) {
  512.         return Arrays.stream(clazz.getMethods()).anyMatch(methodLoop -> methodLoop.getName().equals(method.getName()));
  513.     }
  514.  
  515.     public static boolean hasMethod(Class clazz, String methodName) {
  516.         return Arrays.stream(clazz.getMethods()).anyMatch(methodLoop -> methodLoop.getName().equals(methodName));
  517.     }
  518.  
  519.     public static Object getMethodValue(Method method, Object object, Object... args) {
  520.         try {
  521.             return method.invoke(object, args);
  522.         } catch (Exception e) {
  523.             e.printStackTrace();
  524.             return null;
  525.         }
  526.     }
  527.  
  528.     public static boolean hasField(Class<?> object, String fieldName) {
  529.         return Arrays.stream(object.getFields()).anyMatch(field -> field.getName().equalsIgnoreCase(fieldName));
  530.     }
  531.  
  532.     public static Object getMethodValueNoST(Method method, Object object, Object... args) {
  533.         try {
  534.             return method.invoke(object, args);
  535.         } catch (Exception e) {
  536.             return null;
  537.         }
  538.     }
  539.  
  540.     public static Object getFieldValue(Field field, Object object) {
  541.         try {
  542.             field.setAccessible(true);
  543.             return field.get(object);
  544.         } catch (Exception e) {
  545.             e.printStackTrace();
  546.             return null;
  547.         }
  548.     }
  549.  
  550.     public static Object getFieldValueNoST(Field field, Object object) {
  551.         try {
  552.             field.setAccessible(true);
  553.             return field.get(object);
  554.         } catch (Exception e) {
  555.             return null;
  556.         }
  557.     }
  558.  
  559.     public static Field getFieldByNameNoST(Class<?> clazz, String fieldName) {
  560.         try {
  561.             Field field = clazz.getDeclaredField(fieldName) != null ? clazz.getDeclaredField(fieldName) : clazz.getSuperclass().getDeclaredField(fieldName);
  562.             field.setAccessible(true);
  563.  
  564.             return field;
  565.         } catch (Exception e) {
  566.             return null;
  567.         }
  568.     }
  569.  
  570.     public static Object newInstance(Class<?> objectClass, Object... args) {
  571.         try {
  572.             return objectClass.getConstructor(args.getClass()).newInstance(args);
  573.         } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
  574.             e.printStackTrace();
  575.         }
  576.         return null;
  577.     }
  578.  
  579.     public static Class<?> getNMSClass(String string) {
  580.         return getClass("net.minecraft.server." + version + "." + string);
  581.     }
  582. }
Advertisement
Add Comment
Please, Sign In to add comment