Advertisement
Guest User

Untitled

a guest
Dec 24th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package fr.minuskube.virus.title;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5.  
  6. import org.bukkit.Bukkit;
  7.  
  8. public class Reflection {
  9. public static String getVersion() {
  10. String name = Bukkit.getServer().getClass().getPackage().getName();
  11. String version = name.substring(name.lastIndexOf('.') + 1) + ".";
  12. return version;
  13. }
  14.  
  15. public static Class<?> getNMSClass(String className) {
  16. String fullName = "net.minecraft.server." + getVersion() + className;
  17. Class<?> clazz = null;
  18. try {
  19. clazz = Class.forName(fullName);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. return clazz;
  24. }
  25.  
  26. public static Class<?> getOBCClass(String className) {
  27. String fullName = "org.bukkit.craftbukkit." + getVersion() + className;
  28. Class<?> clazz = null;
  29. try {
  30. clazz = Class.forName(fullName);
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. return clazz;
  35. }
  36.  
  37. public static Object getHandle(Object obj) {
  38. try {
  39. return getMethod(obj.getClass(), "getHandle", new Class[0]).invoke(obj, new Object[0]);
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. return null;
  44. }
  45.  
  46. public static Field getField(Class<?> clazz, String name) {
  47. try {
  48. Field field = clazz.getDeclaredField(name);
  49. field.setAccessible(true);
  50. return field;
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. return null;
  55. }
  56.  
  57. public static Method getMethod(Class<?> clazz, String name, Class<?>... args) {
  58. for (Method m : clazz.getMethods()) {
  59. if ((m.getName().equals(name)) && ((args.length == 0) || (ClassListEqual(args, m.getParameterTypes())))) {
  60. m.setAccessible(true);
  61. return m;
  62. }
  63. }
  64. return null;
  65. }
  66.  
  67. public static boolean ClassListEqual(Class<?>[] l1, Class<?>[] l2) {
  68. boolean equal = true;
  69. if (l1.length != l2.length) {
  70. return false;
  71. }
  72. for (int i = 0; i < l1.length; i++) {
  73. if (l1[i] != l2[i]) {
  74. equal = false;
  75. break;
  76. }
  77. }
  78. return equal;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement