From 6628fe049967c7e4f74246545d2e866dff69a295 Mon Sep 17 00:00:00 2001 From: Adubbz Date: Sun, 9 Feb 2014 15:17:55 +1100 Subject: [PATCH] Fixed #184 --- .../biomesoplenty/common/core/BOPPotions.java | 49 +++++-------------- .../common/helpers/BOPReflectionHelper.java | 39 ++++++--------- 2 files changed, 27 insertions(+), 61 deletions(-) diff --git a/src/main/java/biomesoplenty/common/core/BOPPotions.java b/src/main/java/biomesoplenty/common/core/BOPPotions.java index 0dc376cb8..edb336e76 100644 --- a/src/main/java/biomesoplenty/common/core/BOPPotions.java +++ b/src/main/java/biomesoplenty/common/core/BOPPotions.java @@ -8,6 +8,7 @@ import net.minecraft.potion.Potion; import org.apache.logging.log4j.Level; import biomesoplenty.api.BOPPotionHelper; +import biomesoplenty.common.helpers.BOPReflectionHelper; import biomesoplenty.common.potions.PotionParalysis; import biomesoplenty.common.potions.PotionPossession; import cpw.mods.fml.common.FMLCommonHandler; @@ -22,49 +23,23 @@ public class BOPPotions extendPotionsArray(); intializePotions(); } + + private static void extendPotionsArray() + { + FMLCommonHandler.instance().getFMLLogger().log(Level.INFO, "[BiomesOPlenty] Extending Potions Array."); + potionOffset = Potion.potionTypes.length; + + Potion[] potionTypes = new Potion[potionOffset + MAXNEWPOTIONS]; + System.arraycopy(Potion.potionTypes, 0, potionTypes, 0, potionOffset); + + BOPReflectionHelper.setPrivateFinalValue(Potion.class, null, potionTypes, "potionTypes", "field_76425_a"); + } private static void intializePotions() { registerPotion(new PotionParalysis(getNextID(), true, 16767262).setPotionName("potion.paralysis")); registerPotion(new PotionPossession(getNextID(), true, 1280).setPotionName("potion.possession")); } - - private static void extendPotionsArray() - { - FMLCommonHandler.instance().getFMLLogger().log(Level.INFO, "[BiomesOPlenty] Extending Potions Array."); - potionOffset = Potion.potionTypes.length; - - Potion[] potionTypes = new Potion[potionOffset + MAXNEWPOTIONS]; - System.arraycopy(Potion.potionTypes, 0, potionTypes, 0, potionOffset); - - Field field = null; - Field[] fields = Potion.class.getDeclaredFields(); - - for (Field f : fields) - { - if (f.getName().equals("potionTypes") || f.getName().equals("potionTypes")) - { - field = f; - break; - } - } - - try - { - field.setAccessible(true); - - Field modfield = Field.class.getDeclaredField("modifiers"); - modfield.setAccessible(true); - modfield.setInt(field, field.getModifiers() & ~Modifier.FINAL); - - field.set(null, potionTypes); - } - catch (Exception e) - { - System.err.println("[BiomesOPlenty] Severe error, please report this to the mod author:"); - System.err.println(e); - } - } public static int getNextID() { diff --git a/src/main/java/biomesoplenty/common/helpers/BOPReflectionHelper.java b/src/main/java/biomesoplenty/common/helpers/BOPReflectionHelper.java index 883061902..43202489f 100755 --- a/src/main/java/biomesoplenty/common/helpers/BOPReflectionHelper.java +++ b/src/main/java/biomesoplenty/common/helpers/BOPReflectionHelper.java @@ -35,30 +35,21 @@ public class BOPReflectionHelper } } - public static void setPrivateFinalValue(Class classToAccess, T instance, E value, String fieldName, String obfFieldName) + public static void setPrivateFinalValue(Class classToAccess, T instance, E value, String... fieldNames) { - Field field = null; - - try - { - if (isDeobfuscated) - { - field = ReflectionHelper.findField(classToAccess, fieldName); - } - else - { - field = ReflectionHelper.findField(classToAccess, ObfuscationReflectionHelper.remapFieldNames(classToAccess.getName(), obfFieldName)); - } - - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); - - field.set(instance, value); - } - catch (Exception e) - { - e.printStackTrace(); - } + Field field = ReflectionHelper.findField(classToAccess, ObfuscationReflectionHelper.remapFieldNames(classToAccess.getName(), fieldNames)); + + try + { + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + + field.set(instance, value); + } + catch (Exception e) + { + e.printStackTrace(); + } } }