This commit is contained in:
Adubbz 2014-02-09 15:17:55 +11:00
parent 6232506dce
commit 6628fe0499
2 changed files with 27 additions and 61 deletions

View File

@ -8,6 +8,7 @@ import net.minecraft.potion.Potion;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import biomesoplenty.api.BOPPotionHelper; import biomesoplenty.api.BOPPotionHelper;
import biomesoplenty.common.helpers.BOPReflectionHelper;
import biomesoplenty.common.potions.PotionParalysis; import biomesoplenty.common.potions.PotionParalysis;
import biomesoplenty.common.potions.PotionPossession; import biomesoplenty.common.potions.PotionPossession;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
@ -22,49 +23,23 @@ public class BOPPotions
extendPotionsArray(); extendPotionsArray();
intializePotions(); 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() private static void intializePotions()
{ {
registerPotion(new PotionParalysis(getNextID(), true, 16767262).setPotionName("potion.paralysis")); registerPotion(new PotionParalysis(getNextID(), true, 16767262).setPotionName("potion.paralysis"));
registerPotion(new PotionPossession(getNextID(), true, 1280).setPotionName("potion.possession")); 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() public static int getNextID()
{ {

View File

@ -35,30 +35,21 @@ public class BOPReflectionHelper
} }
} }
public static <T, E> void setPrivateFinalValue(Class <? super T > classToAccess, T instance, E value, String fieldName, String obfFieldName) public static <T, E> void setPrivateFinalValue(Class <? super T > classToAccess, T instance, E value, String... fieldNames)
{ {
Field field = null; Field field = ReflectionHelper.findField(classToAccess, ObfuscationReflectionHelper.remapFieldNames(classToAccess.getName(), fieldNames));
try try
{ {
if (isDeobfuscated) Field modifiersField = Field.class.getDeclaredField("modifiers");
{ modifiersField.setAccessible(true);
field = ReflectionHelper.findField(classToAccess, fieldName); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
}
else field.set(instance, value);
{ }
field = ReflectionHelper.findField(classToAccess, ObfuscationReflectionHelper.remapFieldNames(classToAccess.getName(), obfFieldName)); catch (Exception e)
} {
e.printStackTrace();
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();
}
} }
} }