Advertisement
Guest User

Untitled

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