Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.76 KB | None | 0 0
  1. package me.staticjava;
  2.  
  3. import net.minecraft.server.v1_7_R4.*;
  4. import org.bukkit.entity.EntityType;
  5.  
  6. import java.lang.reflect.Field;
  7. import java.util.List;
  8. import java.util.Map;
  9.  
  10. /**
  11.  * Created by StaticJava.
  12.  */
  13. public enum CustomEntityType {
  14.  
  15.     VILLAGER("Villager", 120, EntityType.VILLAGER, EntityVillager.class, CustomEntityVillager.class);
  16.  
  17.     private String name;
  18.     private int id;
  19.     private EntityType entityType;
  20.     private Class<? extends EntityInsentient> nmsClass;
  21.     private Class<? extends EntityInsentient> customClass;
  22.  
  23.     private CustomEntityType(String name, int id, EntityType entityType, Class<? extends EntityInsentient> nmsClass,
  24.                              Class<? extends EntityInsentient> customClass) {
  25.         this.name = name;
  26.         this.id = id;
  27.         this.entityType = entityType;
  28.         this.nmsClass = nmsClass;
  29.         this.customClass = customClass;
  30.     }
  31.  
  32.     public String getName() {
  33.         return name;
  34.     }
  35.  
  36.     public int getID() {
  37.         return id;
  38.     }
  39.  
  40.     public EntityType getEntityType() {
  41.         return entityType;
  42.     }
  43.  
  44.     public Class<? extends EntityInsentient> getNMSClass() {
  45.         return nmsClass;
  46.     }
  47.  
  48.     public Class<? extends EntityInsentient> getCustomClass() {
  49.         return customClass;
  50.     }
  51.  
  52.     /**
  53.      * Register our entities.
  54.      */
  55.     public static void registerEntities() {
  56.         for (CustomEntityType entity : values()) {
  57.             a(entity.getCustomClass(), entity.getName(), entity.getID());
  58.         }
  59.  
  60.         BiomeBase[] biomes;
  61.  
  62.         try {
  63.             biomes = (BiomeBase[]) getPrivateStatic(BiomeBase.class, "biomes");
  64.         } catch (Exception exc) {
  65.             return;
  66.         }
  67.  
  68.         for (BiomeBase biomeBase : biomes) {
  69.             if (biomeBase == null) {
  70.                 break;
  71.             }
  72.  
  73.             for (String field : new String[] { "as", "at", "au", "av" }) {
  74.                 try {
  75.                     Field list = BiomeBase.class.getDeclaredField(field);
  76.                     list.setAccessible(true);
  77.                     @SuppressWarnings("unchecked")
  78.                     List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
  79.  
  80.                     for (BiomeMeta meta : mobList) {
  81.                         for (CustomEntityType entity : values()) {
  82.                             if (entity.getNMSClass().equals(meta.b)) {
  83.                                 meta.b = entity.getCustomClass();
  84.                             }
  85.                         }
  86.                     }
  87.                 } catch (Exception e) {
  88.                     e.printStackTrace();
  89.                 }
  90.             }
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * Unregister our entities to prevent memory leaks. Call on disable.
  96.      */
  97.     public static void unregisterEntities() {
  98.         for (CustomEntityType entity : values()) {
  99.             try {
  100.                 ((Map) getPrivateStatic(EntityTypes.class, "d")).remove(entity.getCustomClass());
  101.             } catch (Exception e) {
  102.                 e.printStackTrace();
  103.             }
  104.  
  105.             try {
  106.                 ((Map) getPrivateStatic(EntityTypes.class, "f")).remove(entity.getCustomClass());
  107.             } catch (Exception e) {
  108.                 e.printStackTrace();
  109.             }
  110.         }
  111.  
  112.         for (CustomEntityType entity : values()) {
  113.             try {
  114.                 a(entity.getNMSClass(), entity.getName(), entity.getID());
  115.             } catch (Exception e) {
  116.                 e.printStackTrace();
  117.             }
  118.         }
  119.  
  120.         BiomeBase[] biomes;
  121.  
  122.         try {
  123.             biomes = (BiomeBase[]) getPrivateStatic(BiomeBase.class, "biomes");
  124.         } catch (Exception exc) {
  125.             return;
  126.         }
  127.  
  128.         for (BiomeBase biomeBase : biomes) {
  129.             if (biomeBase == null)
  130.                 break;
  131.  
  132.             for (String field : new String[] { "as", "at", "au", "av" }) {
  133.                 try {
  134.                     Field list = BiomeBase.class.getDeclaredField(field);
  135.                     list.setAccessible(true);
  136.                     @SuppressWarnings("unchecked")
  137.                     List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
  138.  
  139.                     for (BiomeMeta meta : mobList) {
  140.                         for (CustomEntityType entity : values()) {
  141.                             if (entity.getCustomClass().equals(meta.b)) {
  142.                                 meta.b = entity.getNMSClass();
  143.                             }
  144.                         }
  145.                     }
  146.                 } catch (Exception e) {
  147.                     e.printStackTrace();
  148.                 }
  149.             }
  150.         }
  151.     }
  152.  
  153.     /**
  154.      * A convenience method.
  155.      * @param clazz The class.
  156.      * @param f The string representation of the private static field.
  157.      * @return The object found
  158.      * @throws Exception if unable to get the object.
  159.      */
  160.     private static Object getPrivateStatic(Class clazz, String f) throws Exception {
  161.         Field field = clazz.getDeclaredField(f);
  162.         field.setAccessible(true);
  163.         return field.get(null);
  164.     }
  165.  
  166.     private static void a(Class paramClass, String paramString, int paramInt) {
  167.         try {
  168.             ((Map) getPrivateStatic(EntityTypes.class, "c")).put(paramString, paramClass);
  169.             ((Map) getPrivateStatic(EntityTypes.class, "d")).put(paramClass, paramString);
  170.             ((Map) getPrivateStatic(EntityTypes.class, "e")).put(Integer.valueOf(paramInt), paramClass);
  171.             ((Map) getPrivateStatic(EntityTypes.class, "f")).put(paramClass, Integer.valueOf(paramInt));
  172.             ((Map) getPrivateStatic(EntityTypes.class, "g")).put(paramString, Integer.valueOf(paramInt));
  173.         } catch (Exception exc) {
  174.           // Unable to register the new class.
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement