Guest User

Untitled

a guest
Feb 17th, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.21 KB | None | 0 0
  1. package ru.onlymc.OnlyMZ;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. import net.minecraft.world.biome.BiomeGenBase;//import net.minecraft.server.v1_7_R4.BiomeBase;
  8. import net.minecraft.world.biome.BiomeGenBase.SpawnListEntry;//import net.minecraft.server.v1_7_R4.BiomeMeta;
  9. import net.minecraft.entity.EntityLiving;//import net.minecraft.server.v1_7_R4.EntityInsentient;
  10. import net.minecraft.entity.EntityList;//import net.minecraft.server.v1_7_R4.EntityTypes;
  11. import net.minecraft.entity.monster.EntityZombie;//import net.minecraft.server.v1_7_R4.EntityZombie;
  12.  
  13. import org.bukkit.entity.EntityType;
  14.  
  15. public enum CustomEntityType {
  16.  
  17.     ZOMBIE("Zombie", 54, EntityType.ZOMBIE, EntityZombie.class, CustomEntityZombie.class);
  18.  
  19.     private String name;
  20.     private int id;
  21.     private EntityType entityType;
  22.     private Class<? extends EntityLiving> nmsClass;
  23.     private Class<? extends EntityLiving> customClass;
  24.  
  25.     private CustomEntityType(String name, int id, EntityType entityType,
  26.             Class<? extends EntityLiving> nmsClass,
  27.             Class<? extends EntityLiving> customClass) {
  28.         this.name = name;
  29.         this.id = id;
  30.         this.entityType = entityType;
  31.         this.nmsClass = nmsClass;
  32.         this.customClass = customClass;
  33.     }
  34.  
  35.     public String getName() {
  36.         return name;
  37.     }
  38.  
  39.     public int getID() {
  40.         return id;
  41.     }
  42.  
  43.     public EntityType getEntityType() {
  44.         return entityType;
  45.     }
  46.  
  47.     public Class<? extends EntityLiving> getNMSClass() {
  48.         return nmsClass;
  49.     }
  50.  
  51.     public Class<? extends EntityLiving> getCustomClass() {
  52.         return customClass;
  53.     }
  54.  
  55.     /**
  56.     * Register our entities.
  57.     */
  58.     public static void registerEntities() {
  59.         for (CustomEntityType entity : values())
  60.             a(entity.getCustomClass(), entity.getName(), entity.getID());
  61.  
  62.         // BiomeGenBase#biomes became private.
  63.         BiomeGenBase[] biomes;
  64.         try {
  65.             biomes = (BiomeGenBase[]) getPrivateStatic(BiomeGenBase.class, "field_76773_a");//biomes - biomeList - aD
  66.         } catch (Exception exc) {
  67.             // Unable to fetch.
  68.             return;
  69.         }
  70.         for (BiomeGenBase BiomeGenBase : biomes) {
  71.             if (BiomeGenBase == null)
  72.                 break;
  73.  
  74.             // This changed names from J, K, L and M.
  75.             for (String field : new String[] { "field_76761_J", "field_76762_K", "field_76755_L", "field_82914_M" })// as at au av
  76.                 try {
  77.                     Field list = BiomeGenBase.class.getDeclaredField(field);
  78.                     list.setAccessible(true);
  79.                     @SuppressWarnings("unchecked")
  80.                     List<SpawnListEntry> mobList = (List<SpawnListEntry>) list
  81.                             .get(BiomeGenBase);
  82.  
  83.                     // Write in our custom class.
  84.                     for (SpawnListEntry meta : mobList)
  85.                         for (CustomEntityType entity : values())
  86.                             if (entity.getNMSClass().equals(meta.entityClass))//entityClass - b
  87.                                 meta.entityClass = entity.getCustomClass();//entityClass - b
  88.                 } catch (Exception e) {
  89.                     e.printStackTrace();
  90.                 }
  91.         }
  92.     }
  93.  
  94.     /**
  95.     * Unregister our entities to prevent memory leaks. Call on disable.
  96.     */
  97.     @SuppressWarnings("rawtypes")
  98.     public static void unregisterEntities() {
  99.         for (CustomEntityType entity : values()) {
  100.             // Remove our class references.
  101.             try {
  102.                 ((Map) getPrivateStatic(EntityList.class, "field_75626_c")).remove(entity
  103.                         .getCustomClass());//d
  104.             } catch (Exception e) {
  105.                 e.printStackTrace();
  106.             }
  107.  
  108.             try {
  109.                 ((Map) getPrivateStatic(EntityList.class, "field_75624_e")).remove(entity
  110.                         .getCustomClass());//f
  111.             } catch (Exception e) {
  112.                 e.printStackTrace();
  113.             }
  114.         }
  115.  
  116.         for (CustomEntityType entity : values())
  117.             try {
  118.                 // Unregister each entity by writing the NMS back in place of
  119.                 // the custom class.
  120.                 a(entity.getNMSClass(), entity.getName(), entity.getID());
  121.             } catch (Exception e) {
  122.                 e.printStackTrace();
  123.             }
  124.  
  125.         // Biomes#biomes was made private so use reflection to get it.
  126.         BiomeGenBase[] biomes;
  127.         try {
  128.             biomes = (BiomeGenBase[]) getPrivateStatic(BiomeGenBase.class, "field_76773_a");//biomes - biomeList - aD
  129.         } catch (Exception exc) {
  130.             // Unable to fetch.
  131.             return;
  132.         }
  133.         for (BiomeGenBase BiomeGenBase : biomes) {
  134.             if (BiomeGenBase == null)
  135.                 break;
  136.  
  137.             // The list fields changed names but update the meta regardless.
  138.             for (String field : new String[] { "field_76761_J", "field_76762_K", "field_76755_L", "field_82914_M" })// as at au av
  139.                 try {
  140.                     Field list = BiomeGenBase.class.getDeclaredField(field);
  141.                     list.setAccessible(true);
  142.                     @SuppressWarnings("unchecked")
  143.                     List<SpawnListEntry> mobList = (List<SpawnListEntry>) list
  144.                             .get(BiomeGenBase);
  145.  
  146.                     // Make sure the NMS class is written back over our custom
  147.                     // class.
  148.                     for (SpawnListEntry meta : mobList)
  149.                         for (CustomEntityType entity : values())
  150.                             if (entity.getCustomClass().equals(meta.entityClass))//entityClass - b
  151.                                 meta.entityClass = entity.getNMSClass();//entityClass - b
  152.                 } catch (Exception e) {
  153.                     e.printStackTrace();
  154.                 }
  155.         }
  156.     }
  157.  
  158.     /**
  159.     * A convenience method.
  160.     *
  161.     * @param clazz
  162.     *            The class.
  163.     * @param f
  164.     *            The string representation of the private static field.
  165.     * @return The object found
  166.     * @throws Exception
  167.     *             if unable to get the object.
  168.     */
  169.     @SuppressWarnings("rawtypes")
  170.     private static Object getPrivateStatic(Class clazz, String f) throws Exception {
  171.         //try {
  172.             Field field = clazz.getDeclaredField(f);
  173.             field.setAccessible(true);
  174.             return field.get(null);
  175.         /*} catch (NoSuchFieldException e) {
  176.             // Throw a more helpful exception.
  177.             Bukkit.getLogger().warning("NoSuchFieldException...");
  178.             Bukkit.getLogger().warning(
  179.                 "Could not find field named '" + f + "' in class '" + clazz +
  180.                 "'.  All fields: " + Arrays.asList(clazz.getDeclaredFields()));
  181.         } catch (NoClassDefFoundError e) {
  182.             Bukkit.getLogger().warning("NoClassDefFoundError...");
  183.             Bukkit.getLogger().warning(
  184.                     "Could not find field named '" + f + "' in class '" + clazz +
  185.                     "'.  All fields: " + Arrays.asList(clazz.getDeclaredFields()));
  186.         } catch (Exception e) {
  187.             Bukkit.getLogger().warning("Exception...");
  188.             Bukkit.getLogger().warning(
  189.                     "Could not find field named '" + f + "' in class '" + clazz +
  190.                     "'.  All fields: " + Arrays.asList(clazz.getDeclaredFields()));
  191.         }
  192.         return null;*/
  193.     }
  194.  
  195.     /*
  196.     * Since 1.7.2 added a check in their entity registration, simply bypass it
  197.     * and write to the maps ourself.
  198.     */
  199.     @SuppressWarnings({ "unchecked", "rawtypes" })
  200.     private static void a(Class paramClass, String paramString, int paramInt) {
  201.         try {
  202.             ((Map) getPrivateStatic(EntityList.class, "field_75625_b")).put(paramString,
  203.                     paramClass);//c
  204.             ((Map) getPrivateStatic(EntityList.class, "field_75626_c")).put(paramClass,
  205.                     paramString);//d
  206.             ((Map) getPrivateStatic(EntityList.class, "field_75623_d")).put(
  207.                     Integer.valueOf(paramInt), paramClass);//e
  208.             ((Map) getPrivateStatic(EntityList.class, "field_75624_e")).put(paramClass,
  209.                     Integer.valueOf(paramInt));//f
  210.             ((Map) getPrivateStatic(EntityList.class, "field_75622_f")).put(paramString,
  211.                     Integer.valueOf(paramInt));//g
  212.             /*Bukkit.getLogger().info("- - - - -");
  213.             Bukkit.getLogger().info("Field c...");
  214.             Iterator i = ((Map) getPrivateStatic(EntityList.class, "c")).entrySet().iterator();
  215.             while (i.hasNext()) {
  216.                 Entry e = (Entry) i.next();
  217.                 Bukkit.getLogger().info("§r "+e.getKey()+" -> "+e.getValue());
  218.             }
  219.  
  220.             Bukkit.getLogger().info("Field d...");
  221.             Iterator iq = ((Map) getPrivateStatic(EntityList.class, "d")).entrySet().iterator();
  222.             while (iq.hasNext()) {
  223.                 Entry e = (Entry) iq.next();
  224.                 Bukkit.getLogger().info("§r "+e.getKey()+" -> "+e.getValue());
  225.             }
  226.  
  227.             Bukkit.getLogger().info("Field e...");
  228.             Iterator iw = ((Map) getPrivateStatic(EntityList.class, "e")).entrySet().iterator();
  229.             while (iw.hasNext()) {
  230.                 Entry e = (Entry) iw.next();
  231.                 Bukkit.getLogger().info("§r "+e.getKey()+" -> "+e.getValue());
  232.             }
  233.  
  234.             Bukkit.getLogger().info("Field f...");
  235.             Iterator ie = ((Map) getPrivateStatic(EntityList.class, "f")).entrySet().iterator();
  236.             while (ie.hasNext()) {
  237.                 Entry e = (Entry) ie.next();
  238.                 Bukkit.getLogger().info("§r "+e.getKey()+" -> "+e.getValue());
  239.             }
  240.  
  241.             Bukkit.getLogger().info("Field g...");
  242.             Iterator ir = ((Map) getPrivateStatic(EntityList.class, "g")).entrySet().iterator();
  243.             while (ir.hasNext()) {
  244.                 Entry e = (Entry) ir.next();
  245.                 Bukkit.getLogger().info("§r "+e.getKey()+" -> "+e.getValue());
  246.             }
  247.             Bukkit.getLogger().info("- - - - -");*/
  248.         } catch (Exception exc) {
  249.             // Unable to register the new class.
  250.         }
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment