Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.onlymc.OnlyMZ;
- import java.lang.reflect.Field;
- import java.util.List;
- import java.util.Map;
- import net.minecraft.world.biome.BiomeGenBase;//import net.minecraft.server.v1_7_R4.BiomeBase;
- import net.minecraft.world.biome.BiomeGenBase.SpawnListEntry;//import net.minecraft.server.v1_7_R4.BiomeMeta;
- import net.minecraft.entity.EntityLiving;//import net.minecraft.server.v1_7_R4.EntityInsentient;
- import net.minecraft.entity.EntityList;//import net.minecraft.server.v1_7_R4.EntityTypes;
- import net.minecraft.entity.monster.EntityZombie;//import net.minecraft.server.v1_7_R4.EntityZombie;
- import org.bukkit.entity.EntityType;
- public enum CustomEntityType {
- ZOMBIE("Zombie", 54, EntityType.ZOMBIE, EntityZombie.class, CustomEntityZombie.class);
- private String name;
- private int id;
- private EntityType entityType;
- private Class<? extends EntityLiving> nmsClass;
- private Class<? extends EntityLiving> customClass;
- private CustomEntityType(String name, int id, EntityType entityType,
- Class<? extends EntityLiving> nmsClass,
- Class<? extends EntityLiving> customClass) {
- this.name = name;
- this.id = id;
- this.entityType = entityType;
- this.nmsClass = nmsClass;
- this.customClass = customClass;
- }
- public String getName() {
- return name;
- }
- public int getID() {
- return id;
- }
- public EntityType getEntityType() {
- return entityType;
- }
- public Class<? extends EntityLiving> getNMSClass() {
- return nmsClass;
- }
- public Class<? extends EntityLiving> getCustomClass() {
- return customClass;
- }
- /**
- * Register our entities.
- */
- public static void registerEntities() {
- for (CustomEntityType entity : values())
- a(entity.getCustomClass(), entity.getName(), entity.getID());
- // BiomeGenBase#biomes became private.
- BiomeGenBase[] biomes;
- try {
- biomes = (BiomeGenBase[]) getPrivateStatic(BiomeGenBase.class, "field_76773_a");//biomes - biomeList - aD
- } catch (Exception exc) {
- // Unable to fetch.
- return;
- }
- for (BiomeGenBase BiomeGenBase : biomes) {
- if (BiomeGenBase == null)
- break;
- // This changed names from J, K, L and M.
- for (String field : new String[] { "field_76761_J", "field_76762_K", "field_76755_L", "field_82914_M" })// as at au av
- try {
- Field list = BiomeGenBase.class.getDeclaredField(field);
- list.setAccessible(true);
- @SuppressWarnings("unchecked")
- List<SpawnListEntry> mobList = (List<SpawnListEntry>) list
- .get(BiomeGenBase);
- // Write in our custom class.
- for (SpawnListEntry meta : mobList)
- for (CustomEntityType entity : values())
- if (entity.getNMSClass().equals(meta.entityClass))//entityClass - b
- meta.entityClass = entity.getCustomClass();//entityClass - b
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * Unregister our entities to prevent memory leaks. Call on disable.
- */
- @SuppressWarnings("rawtypes")
- public static void unregisterEntities() {
- for (CustomEntityType entity : values()) {
- // Remove our class references.
- try {
- ((Map) getPrivateStatic(EntityList.class, "field_75626_c")).remove(entity
- .getCustomClass());//d
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- ((Map) getPrivateStatic(EntityList.class, "field_75624_e")).remove(entity
- .getCustomClass());//f
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- for (CustomEntityType entity : values())
- try {
- // Unregister each entity by writing the NMS back in place of
- // the custom class.
- a(entity.getNMSClass(), entity.getName(), entity.getID());
- } catch (Exception e) {
- e.printStackTrace();
- }
- // Biomes#biomes was made private so use reflection to get it.
- BiomeGenBase[] biomes;
- try {
- biomes = (BiomeGenBase[]) getPrivateStatic(BiomeGenBase.class, "field_76773_a");//biomes - biomeList - aD
- } catch (Exception exc) {
- // Unable to fetch.
- return;
- }
- for (BiomeGenBase BiomeGenBase : biomes) {
- if (BiomeGenBase == null)
- break;
- // The list fields changed names but update the meta regardless.
- for (String field : new String[] { "field_76761_J", "field_76762_K", "field_76755_L", "field_82914_M" })// as at au av
- try {
- Field list = BiomeGenBase.class.getDeclaredField(field);
- list.setAccessible(true);
- @SuppressWarnings("unchecked")
- List<SpawnListEntry> mobList = (List<SpawnListEntry>) list
- .get(BiomeGenBase);
- // Make sure the NMS class is written back over our custom
- // class.
- for (SpawnListEntry meta : mobList)
- for (CustomEntityType entity : values())
- if (entity.getCustomClass().equals(meta.entityClass))//entityClass - b
- meta.entityClass = entity.getNMSClass();//entityClass - b
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * A convenience method.
- *
- * @param clazz
- * The class.
- * @param f
- * The string representation of the private static field.
- * @return The object found
- * @throws Exception
- * if unable to get the object.
- */
- @SuppressWarnings("rawtypes")
- private static Object getPrivateStatic(Class clazz, String f) throws Exception {
- //try {
- Field field = clazz.getDeclaredField(f);
- field.setAccessible(true);
- return field.get(null);
- /*} catch (NoSuchFieldException e) {
- // Throw a more helpful exception.
- Bukkit.getLogger().warning("NoSuchFieldException...");
- Bukkit.getLogger().warning(
- "Could not find field named '" + f + "' in class '" + clazz +
- "'. All fields: " + Arrays.asList(clazz.getDeclaredFields()));
- } catch (NoClassDefFoundError e) {
- Bukkit.getLogger().warning("NoClassDefFoundError...");
- Bukkit.getLogger().warning(
- "Could not find field named '" + f + "' in class '" + clazz +
- "'. All fields: " + Arrays.asList(clazz.getDeclaredFields()));
- } catch (Exception e) {
- Bukkit.getLogger().warning("Exception...");
- Bukkit.getLogger().warning(
- "Could not find field named '" + f + "' in class '" + clazz +
- "'. All fields: " + Arrays.asList(clazz.getDeclaredFields()));
- }
- return null;*/
- }
- /*
- * Since 1.7.2 added a check in their entity registration, simply bypass it
- * and write to the maps ourself.
- */
- @SuppressWarnings({ "unchecked", "rawtypes" })
- private static void a(Class paramClass, String paramString, int paramInt) {
- try {
- ((Map) getPrivateStatic(EntityList.class, "field_75625_b")).put(paramString,
- paramClass);//c
- ((Map) getPrivateStatic(EntityList.class, "field_75626_c")).put(paramClass,
- paramString);//d
- ((Map) getPrivateStatic(EntityList.class, "field_75623_d")).put(
- Integer.valueOf(paramInt), paramClass);//e
- ((Map) getPrivateStatic(EntityList.class, "field_75624_e")).put(paramClass,
- Integer.valueOf(paramInt));//f
- ((Map) getPrivateStatic(EntityList.class, "field_75622_f")).put(paramString,
- Integer.valueOf(paramInt));//g
- /*Bukkit.getLogger().info("- - - - -");
- Bukkit.getLogger().info("Field c...");
- Iterator i = ((Map) getPrivateStatic(EntityList.class, "c")).entrySet().iterator();
- while (i.hasNext()) {
- Entry e = (Entry) i.next();
- Bukkit.getLogger().info("§r "+e.getKey()+" -> "+e.getValue());
- }
- Bukkit.getLogger().info("Field d...");
- Iterator iq = ((Map) getPrivateStatic(EntityList.class, "d")).entrySet().iterator();
- while (iq.hasNext()) {
- Entry e = (Entry) iq.next();
- Bukkit.getLogger().info("§r "+e.getKey()+" -> "+e.getValue());
- }
- Bukkit.getLogger().info("Field e...");
- Iterator iw = ((Map) getPrivateStatic(EntityList.class, "e")).entrySet().iterator();
- while (iw.hasNext()) {
- Entry e = (Entry) iw.next();
- Bukkit.getLogger().info("§r "+e.getKey()+" -> "+e.getValue());
- }
- Bukkit.getLogger().info("Field f...");
- Iterator ie = ((Map) getPrivateStatic(EntityList.class, "f")).entrySet().iterator();
- while (ie.hasNext()) {
- Entry e = (Entry) ie.next();
- Bukkit.getLogger().info("§r "+e.getKey()+" -> "+e.getValue());
- }
- Bukkit.getLogger().info("Field g...");
- Iterator ir = ((Map) getPrivateStatic(EntityList.class, "g")).entrySet().iterator();
- while (ir.hasNext()) {
- Entry e = (Entry) ir.next();
- Bukkit.getLogger().info("§r "+e.getKey()+" -> "+e.getValue());
- }
- Bukkit.getLogger().info("- - - - -");*/
- } catch (Exception exc) {
- // Unable to register the new class.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment