Vinetos

[Bukkit NMS] Mount - 1

Sep 9th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. public enum CustomEntityType {
  2.  
  3. //ZOMBIE("Zombie", 54, EntityType.ZOMBIE, EntityZombie.class, CustomZombie.class),
  4. PIG("Pig", 90, EntityType.PIG, EntityPig.class, CustomPig.class),
  5. SHEEP("Sheep", 91, EntityType.SHEEP, EntitySheep.class, CustomSheep.class),
  6. CHICKEN("Chicken", 93, EntityType.CHICKEN, EntityChicken.class, CustomPoulet.class),
  7. WOLF("Wolf", 95, EntityType.WOLF, EntityWolf.class, CustomLoup.class),
  8. MUSHROOM("MushrommCow", 96, EntityType.MUSHROOM_COW, EntityMushroomCow.class, CustomCow.class),
  9. OCELOT("Ocelot", 98, EntityType.OCELOT, EntityOcelot.class, CustomOcelot.class),
  10. HORSE("Horse", 100, EntityType.HORSE, EntityHorse.class, CustomCheval.class);
  11. /*SKELETON("Skeleton", 51, EntityType.SKELETON, EntitySkeleton.class, UnspawnableSkeleton.class),
  12. SPIDER("Spider", 52, EntityType.SPIDER, EntitySpider.class, UnspawnableSpider.class),
  13. CREEPER("Creeper", 50, EntityType.CREEPER, EntityCreeper.class, UnspawnableCreeper.class),
  14. ENDERMAN("Enderman", 58, EntityType.ENDERMAN, EntityEnderman.class, UnspawnableEnderman.class),
  15. WITCH("Witch", 66, EntityType.WITCH, EntityWitch.class, UnspawnableWitch.class),
  16. SLIME("Slime", 55, EntityType.SLIME, EntitySlime.class, UnspawnableSlime.class),
  17. CAVE_SPIDER("CaveSpider", 59, EntityType.CAVE_SPIDER, EntityCaveSpider.class, UnspawnableCaveSpider.class);*/
  18.  
  19. private String name;
  20. private int id;
  21. private EntityType entityType;
  22. private Class<? extends EntityInsentient> nmsClass;
  23. private Class<? extends EntityInsentient> customClass;
  24.  
  25. private CustomEntityType(String name, int id, EntityType entityType, Class<? extends EntityInsentient> nmsClass, Class<? extends EntityInsentient> customClass) {
  26. this.name = name;
  27. this.id = id;
  28. this.entityType = entityType;
  29. this.nmsClass = nmsClass;
  30. this.customClass = customClass;
  31. }
  32.  
  33. public String getName() {
  34. return name;
  35. }
  36.  
  37. public int getID() {
  38. return id;
  39. }
  40.  
  41. public EntityType getEntityType() {
  42. return entityType;
  43. }
  44.  
  45. public Class<? extends EntityInsentient> getNMSClass() {
  46. return nmsClass;
  47. }
  48.  
  49. public Class<? extends EntityInsentient> getCustomClass() {
  50. return customClass;
  51. }
  52.  
  53. /**
  54. * Register our entities.
  55. */
  56. public static void registerEntities() {
  57. for (CustomEntityType entity : values()) /*Get our entities*/
  58. a(entity.getCustomClass(), entity.getName(), entity.getID());
  59. /*Get all biomes on the server*/
  60. BiomeBase[] biomes;
  61. try {
  62. biomes = (BiomeBase[]) getPrivateStatic(BiomeBase.class, "biomes");
  63. } catch (Exception exc) {
  64. return;
  65. }
  66. for (BiomeBase biomeBase : biomes) {
  67. if (biomeBase == null)
  68. break;
  69. for (String field : new String[] { "at", "au", "av", "aw" }) //Lists that hold all entity types
  70. try {
  71. Field list = BiomeBase.class.getDeclaredField(field);
  72. list.setAccessible(true);
  73. @SuppressWarnings("unchecked")
  74. List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
  75.  
  76. for (BiomeMeta meta : mobList)
  77. for (CustomEntityType entity : values())
  78. if (entity.getNMSClass().equals(meta.b)) /*Test if the entity has the custom entity type*/
  79. meta.b = entity.getCustomClass(); //Set it's meta to our custom class's meta
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. }
  85.  
  86. /**
  87. * Unregister our entities to prevent memory leaks. Call on disable.
  88. */
  89. @SuppressWarnings("rawtypes")
  90. public static void unregisterEntities() {
  91. for (CustomEntityType entity : values()) {
  92. // Remove our class references.
  93. try {
  94. ((Map) getPrivateStatic(EntityTypes.class, "d")).remove(entity.getCustomClass());
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98.  
  99. try {
  100. ((Map) getPrivateStatic(EntityTypes.class, "f")).remove(entity.getCustomClass());
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. }
  104. }
  105.  
  106. for (CustomEntityType entity : values())
  107. try {
  108. a(entity.getNMSClass(), entity.getName(), entity.getID());
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. }
  112.  
  113. BiomeBase[] biomes;
  114. try {
  115. biomes = (BiomeBase[]) getPrivateStatic(BiomeBase.class, "biomes"); /*Get all biomes again*/
  116. } catch (Exception exc) {
  117. return;
  118. }
  119. for (BiomeBase biomeBase : biomes) {
  120. if (biomeBase == null)
  121. break;
  122.  
  123. for (String field : new String[] { "at", "au", "av", "aw" }) /*The entity list*/
  124. try {
  125. Field list = BiomeBase.class.getDeclaredField(field);
  126. list.setAccessible(true);
  127. @SuppressWarnings("unchecked")
  128. List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
  129.  
  130. for (BiomeMeta meta : mobList)
  131. for (CustomEntityType entity : values())
  132. if (entity.getCustomClass().equals(meta.b))
  133. meta.b = entity.getNMSClass(); /*Set the entities meta back to the NMS one*/
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. }
  139.  
  140. /**
  141. * A convenience method.
  142. *
  143. * @param clazz
  144. * The class.
  145. * @param f
  146. * The string representation of the private static field.
  147. * @return The object found
  148. * @throws Exception
  149. * if unable to get the object.
  150. */
  151. @SuppressWarnings("rawtypes")
  152. private static Object getPrivateStatic(Class clazz, String f)
  153. throws Exception {
  154. Field field = clazz.getDeclaredField(f);
  155. field.setAccessible(true);
  156. return field.get(null);
  157. }
  158.  
  159. /*
  160. * Since 1.7.2 added a check in their entity registration, simply bypass it
  161. * and write to the maps ourself.
  162. */
  163. @SuppressWarnings({ "unchecked", "rawtypes" })
  164. private static void a(Class paramClass, String paramString, int paramInt) {
  165. try {
  166. ((Map) getPrivateStatic(EntityTypes.class, "c")).put(paramString,
  167. paramClass);
  168. ((Map) getPrivateStatic(EntityTypes.class, "d")).put(paramClass,
  169. paramString);
  170. ((Map) getPrivateStatic(EntityTypes.class, "e")).put(
  171. Integer.valueOf(paramInt), paramClass);
  172. ((Map) getPrivateStatic(EntityTypes.class, "f")).put(paramClass,
  173. Integer.valueOf(paramInt));
  174. ((Map) getPrivateStatic(EntityTypes.class, "g")).put(paramString,
  175. Integer.valueOf(paramInt));
  176. } catch (Exception exc) {
  177. // Unable to register the new class.
  178. }
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment