Advertisement
Guest User

NMSUtils.java

a guest
Apr 15th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. package de.souperfish.bot.main;
  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.  
  9. import net.minecraft.server.v1_7_R3.BiomeBase;
  10. import net.minecraft.server.v1_7_R3.BiomeMeta;
  11. import net.minecraft.server.v1_7_R3.EntityInsentient;
  12. import net.minecraft.server.v1_7_R3.EntityTypes;
  13.  
  14.  
  15.  
  16. public class NMSUtils {
  17. public void registerEntity(String name, int id, Class<? extends EntityInsentient> nmsClass, Class<? extends EntityInsentient> customClass) {
  18. try {
  19.  
  20. /*
  21. * First, we make a list of all HashMap's in the EntityTypes class
  22. * by looping through all fields. I am using reflection here so we
  23. * have no problems later when minecraft changes the field's name.
  24. * By creating a list of these maps we can easily modify them later
  25. * on.
  26. */
  27. List<Map<?, ?>> dataMaps = new ArrayList<Map<?, ?>>();
  28. for (Field f : EntityTypes.class.getDeclaredFields()) {
  29. if (f.getType().getSimpleName().equals(Map.class.getSimpleName())) {
  30. f.setAccessible(true);
  31. dataMaps.add((Map<?, ?>) f.get(null));
  32. }
  33. }
  34.  
  35. /*
  36. * since minecraft checks if an id has already been registered, we
  37. * have to remove the old entity class before we can register our
  38. * custom one
  39. *
  40. * map 0 is the map with names and map 2 is the map with ids
  41. */
  42. if (dataMaps.get(2).containsKey(id)) {
  43. dataMaps.get(0).remove(name);
  44. dataMaps.get(2).remove(id);
  45. }
  46.  
  47. /*
  48. * now we call the method which adds the entity to the lists in the
  49. * EntityTypes class, now we are actually 'registering' our entity
  50. */
  51. Method method = EntityTypes.class.getDeclaredMethod("a", Class.class, String.class, int.class);
  52. method.setAccessible(true);
  53. method.invoke(null, customClass, name, id);
  54.  
  55. /*
  56. * after doing the basic registering stuff , we have to register our
  57. * mob as to be the default for every biome. This can be done by
  58. * looping through all BiomeBase fields in the BiomeBase class, so
  59. * we can loop though all available biomes afterwards. Here, again,
  60. * I am using reflection so we have no problems later when minecraft
  61. * changes the fields name
  62. */
  63. for (Field f : BiomeBase.class.getDeclaredFields()) {
  64. if (f.getType().getSimpleName().equals(BiomeBase.class.getSimpleName())) {
  65. if (f.get(null) != null) {
  66.  
  67. /*
  68. * this peace of code is being called for every biome,
  69. * we are going to loop through all fields in the
  70. * BiomeBase class so we can detect which of them are
  71. * Lists (again, to prevent problems when the field's
  72. * name changes), by doing this we can easily get the 4
  73. * required lists without using the name (which probably
  74. * changes every version)
  75. */
  76. for (Field list : BiomeBase.class.getDeclaredFields()) {
  77. if (list.getType().getSimpleName().equals(List.class.getSimpleName())) {
  78. list.setAccessible(true);
  79. @SuppressWarnings("unchecked")
  80. List<BiomeMeta> metaList = (List<BiomeMeta>) list.get(f.get(null));
  81.  
  82. /*
  83. * now we are almost done. This peace of code
  84. * we're in now is called for every biome. Loop
  85. * though the list with BiomeMeta, if the
  86. * BiomeMeta's entity is the one you want to
  87. * change (for example if EntitySkeleton matches
  88. * EntitySkeleton) we will change it to our
  89. * custom entity class
  90. */
  91. for (BiomeMeta meta : metaList) {
  92. Field clazz = BiomeMeta.class.getDeclaredFields()[0];
  93. if (clazz.get(meta).equals(nmsClass)) {
  94. clazz.set(meta, customClass);
  95. }
  96. }
  97. }
  98. }
  99.  
  100. }
  101. }
  102. }
  103.  
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement