Changes in ReflectionHelper

This commit is contained in:
Zixxl 2015-07-06 17:42:39 +02:00
parent 17d9b78930
commit 6bf0797054
13 changed files with 115 additions and 143 deletions

View file

@ -5,16 +5,47 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class ReflectionHelper {
public static <T> T getInstance(Constructor<T> constructor, Object... arguments) {
try {
return constructor.newInstance(arguments);
} catch (Exception e) { LogHelper.logError("Exception creating instance of " + constructor.getClass().getName(), e); }
return null;
}
public static <T> T getInstance(Class<T> clazz, Object... arguments) {
Class[] classArray = new Class[arguments.length];
for(int i = 0; i < arguments.length; i++) {
classArray[i] = arguments[i].getClass();
}
Constructor<T> constructor = getConstructor(clazz, classArray);
return getInstance(constructor, arguments);
}
public static Constructor getConstructor(String string, Class... types) {
try {
Class clazz = Class.forName(string);
Constructor constructor = clazz.getDeclaredConstructor(types);
constructor.setAccessible(true);
return constructor;
} catch (Exception ex) {}
} catch (Exception ex) { LogHelper.logError("Exception creating instance of " + string, ex); }
return null;
}
public static <T> Constructor<T> getConstructor(Class<T> clazz, Class... types) {
try {
Constructor<T> constructor = clazz.getDeclaredConstructor(types);
constructor.setAccessible(true);
return constructor;
} catch (Exception ex) { LogHelper.logError("Exception getting constructore of " + clazz.getName(), ex); }
return null;
}
public static <T> T getObject(Object o, String... fieldName) {
Class cls = o.getClass();

View file

@ -2,9 +2,9 @@ package modtweaker2.mods.factorization;
import static modtweaker2.helpers.ReflectionHelper.getStaticObject;
import java.lang.reflect.Constructor;
import java.util.List;
import modtweaker2.helpers.ReflectionHelper;
import net.minecraft.item.ItemStack;
public class FactorizationHelper {
@ -22,36 +22,30 @@ public class FactorizationHelper {
private FactorizationHelper() {}
@SuppressWarnings("unchecked")
public static Object getLaceratorRecipe(ItemStack input, ItemStack output, float probability) {
try {
Class clazz = Class.forName("factorization.oreprocessing.TileEntityGrinder$GrinderRecipe");
Constructor constructor = clazz.getDeclaredConstructor(Object.class, ItemStack.class, float.class);
constructor.setAccessible(true);
return constructor.newInstance(input, output, probability);
} catch (Exception e) {
throw new NullPointerException("Failed to instantiate Lacerator Recipe");
}
return ReflectionHelper.getInstance(ReflectionHelper.getConstructor("factorization.oreprocessing.TileEntityGrinder$GrinderRecipe", Object.class, ItemStack.class, float.class),
input,
output,
probability);
}
@SuppressWarnings("unchecked")
public static Object getSlagFurnaceRecipe(ItemStack input, float chance1, ItemStack output1, float chance2, ItemStack output2) {
try {
Class clazz = Class.forName("factorization.oreprocessing.TileEntitySlagFurnace$SmeltingResult");
Constructor constructor = clazz.getDeclaredConstructor(ItemStack.class, float.class, ItemStack.class, float.class, ItemStack.class);
constructor.setAccessible(true);
return constructor.newInstance(input, chance1, output1, chance2, output2);
} catch (Exception e) {
throw new NullPointerException("Failed to instantiate SmeltingResult");
}
return ReflectionHelper.getInstance(ReflectionHelper.getConstructor("factorization.oreprocessing.TileEntitySlagFurnace$SmeltingResult", ItemStack.class, float.class, ItemStack.class, float.class, ItemStack.class),
input,
chance1,
output1,
chance2,
output2);
}
@SuppressWarnings("unchecked")
public static Object getCrystallizerRecipe(ItemStack input, ItemStack output, ItemStack solution, float output_count) {
try {
Class clazz = Class.forName("factorization.oreprocessing.TileEntityCrystallizer$CrystalRecipe");
Constructor constructor = clazz.getDeclaredConstructor(ItemStack.class, ItemStack.class, float.class, ItemStack.class);
constructor.setAccessible(true);
return constructor.newInstance(input, output, output_count, solution);
} catch (Exception e) {
throw new NullPointerException("Failed to instantiate CrystalRecipe");
}
return ReflectionHelper.getInstance(ReflectionHelper.getConstructor("factorization.oreprocessing.TileEntityCrystallizer$CrystalRecipe", ItemStack.class, ItemStack.class, float.class, ItemStack.class),
input,
output,
output_count,
solution);
}
}

View file

@ -1,51 +1,30 @@
package modtweaker2.mods.forestry;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import modtweaker2.helpers.ReflectionHelper;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import forestry.factory.gadgets.MachineCarpenter;
public class ForestryHelper {
@SuppressWarnings("unchecked")
public static void addCarpenterRecipeBox(ItemStack box) {
Class clazz;
try {
clazz = Class.forName("forestry.factory.gadgets.MachineCarpenter$RecipeManager");
Field field_box = clazz.getDeclaredField("boxes");
field_box.setAccessible(true);
ArrayList<ItemStack> recipeBoxes = (ArrayList<ItemStack>) field_box.get(clazz);
recipeBoxes.add(box);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
ArrayList<ItemStack> recipeBoxes = (ArrayList<ItemStack>) ReflectionHelper.getStaticObject(MachineCarpenter.RecipeManager.class, "boxes");
if(recipeBoxes != null) {
recipeBoxes.add(box);
}
}
@SuppressWarnings("unchecked")
public static void addCarpenterRecipeFluids(Fluid newFluid) {
Class clazz;
try {
clazz = Class.forName("forestry.factory.gadgets.MachineCarpenter$RecipeManager");
Field field_recipeFluids = clazz.getDeclaredField("recipeFluids");
field_recipeFluids.setAccessible(true);
HashSet<Fluid> recipeFluids = (HashSet<Fluid>) field_recipeFluids.get(clazz);
recipeFluids.add(newFluid);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
HashSet<Fluid> recipeFluids = (HashSet<Fluid>) ReflectionHelper.getStaticObject(MachineCarpenter.RecipeManager.class, "recipeFluids");
if(recipeFluids != null) {
recipeFluids.add(newFluid);
}
}
}

View file

@ -1,12 +1,15 @@
package modtweaker2.mods.railcraft;
import java.lang.reflect.Constructor;
import java.util.List;
import mods.railcraft.api.crafting.IBlastFurnaceRecipe;
import mods.railcraft.api.crafting.ICokeOvenRecipe;
import mods.railcraft.api.crafting.IRockCrusherRecipe;
import mods.railcraft.api.crafting.RailcraftCraftingManager;
import mods.railcraft.common.util.crafting.BlastFurnaceCraftingManager.BlastFurnaceRecipe;
import mods.railcraft.common.util.crafting.CokeOvenCraftingManager.CokeOvenRecipe;
import mods.railcraft.common.util.crafting.RockCrusherCraftingManager.CrusherRecipe;
import modtweaker2.helpers.ReflectionHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraftforge.fluids.FluidStack;
@ -28,35 +31,28 @@ public class RailcraftHelper {
private RailcraftHelper() {}
public static IBlastFurnaceRecipe getBlastFurnaceRecipe(ItemStack input, boolean matchDamage, boolean matchNBT, int cookTime, ItemStack output) {
try {
Class clazz = Class.forName("mods.railcraft.common.util.crafting.BlastFurnaceCraftingManager$BlastFurnaceRecipe");
Constructor constructor = clazz.getDeclaredConstructor(ItemStack.class, boolean.class, boolean.class, int.class, ItemStack.class);
constructor.setAccessible(true);
return (IBlastFurnaceRecipe) constructor.newInstance(input, matchDamage, matchNBT, cookTime, output);
} catch (Exception e) {
throw new NullPointerException("Failed to instantiate BlastFurnaceRecipe");
}
return ReflectionHelper.getInstance(ReflectionHelper.getConstructor(BlastFurnaceRecipe.class,ItemStack.class, boolean.class, boolean.class, int.class, ItemStack.class),
input,
matchDamage,
matchNBT,
cookTime,
output);
}
public static ICokeOvenRecipe getCokeOvenRecipe(ItemStack input, boolean matchDamage, boolean matchNBT, ItemStack output, FluidStack fluidOutput, int cookTime) {
try {
Class clazz = Class.forName("mods.railcraft.common.util.crafting.CokeOvenCraftingManager$CokeOvenRecipe");
Constructor constructor = clazz.getDeclaredConstructor(ItemStack.class, boolean.class, boolean.class, ItemStack.class, FluidStack.class, int.class);
constructor.setAccessible(true);
return (ICokeOvenRecipe) constructor.newInstance(input, matchDamage, matchNBT, output, fluidOutput, cookTime);
} catch (Exception e) {
throw new NullPointerException("Failed to instantiate CokeOvenRecipe");
}
return ReflectionHelper.getInstance(ReflectionHelper.getConstructor(CokeOvenRecipe.class, ItemStack.class, boolean.class, boolean.class, ItemStack.class, FluidStack.class, int.class),
input,
matchDamage,
matchNBT,
output,
fluidOutput,
cookTime);
}
public static IRockCrusherRecipe getRockCrusherRecipe(ItemStack stack, boolean matchDamage, boolean matchNBT) {
try {
Class clazz = Class.forName("mods.railcraft.common.util.crafting.RockCrusherCraftingManager$CrusherRecipe");
Constructor constructor = clazz.getDeclaredConstructor(ItemStack.class, boolean.class, boolean.class);
constructor.setAccessible(true);
return (IRockCrusherRecipe) constructor.newInstance(stack, matchDamage, matchNBT);
} catch (Exception e) {
throw new NullPointerException("Failed to instantiate CrusherRecipe");
}
return ReflectionHelper.getInstance(ReflectionHelper.getConstructor(CrusherRecipe.class, ItemStack.class, boolean.class, boolean.class),
stack,
matchDamage,
matchNBT);
}
}

View file

@ -1,13 +1,12 @@
package modtweaker2.mods.tconstruct;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import mantle.utils.ItemMetaWrapper;
import modtweaker2.helpers.ReflectionHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
@ -18,12 +17,11 @@ import tconstruct.library.crafting.CastingRecipe;
import tconstruct.library.crafting.DryingRackRecipes.DryingRecipe;
import tconstruct.library.crafting.ModifyBuilder;
import tconstruct.library.crafting.PatternBuilder.ItemKey;
import tconstruct.library.crafting.Smeltery;
import tconstruct.library.modifier.ItemModifier;
import tconstruct.library.tools.ToolMaterial;
public class TConstructHelper {
public static HashMap<String, Integer> mappings = new HashMap();
public static HashMap<String, Integer> mappings = new HashMap<String, Integer>();
public static List<AlloyMix> alloys = null;
public static ArrayList<CastingRecipe> basinCasting = null;
public static ArrayList<CastingRecipe> tableCasting = null;
@ -43,13 +41,13 @@ public class TConstructHelper {
basinCasting = TConstructRegistry.getBasinCasting().getCastingRecipes();
tableCasting = TConstructRegistry.getTableCasting().getCastingRecipes();
modifiers = ModifyBuilder.instance.itemModifiers;
modifiers_clone = new ArrayList(modifiers);
modifiers_clone = new ArrayList<ItemModifier>(modifiers);
for (Map.Entry<Integer, ToolMaterial> entry : TConstructRegistry.toolMaterials.entrySet()) {
mappings.put(entry.getValue().materialName, entry.getKey());
}
fuelList = getFuelList();
fuelList = ReflectionHelper.getObject(tconstruct.library.crafting.Smeltery.instance, "smelteryFuels");
} catch (Exception e) {}
}
@ -60,31 +58,13 @@ public class TConstructHelper {
return -1;
} else return mappings.get(material);
}
public static Map<Fluid, Integer[]> getFuelList() {
Smeltery smelteryInstance = tconstruct.library.crafting.Smeltery.instance;
try {
Field smelteryFuels = Smeltery.class.getDeclaredField("smelteryFuels");
smelteryFuels.setAccessible(true);
return (Map<Fluid, Integer[]>)smelteryFuels.get(smelteryInstance);
} catch (Exception e) {
e.printStackTrace();
throw new NullPointerException("Failed to instantiate smeltery fuel map");
}
}
//Returns a Drying Recipe, using reflection as the constructor is not visible
public static DryingRecipe getDryingRecipe(ItemStack input, int time, ItemStack output) {
try {
Constructor constructor = DryingRecipe.class.getDeclaredConstructor(ItemStack.class, int.class, ItemStack.class);
constructor.setAccessible(true);
return (DryingRecipe) constructor.newInstance(input, time, output);
} catch (Exception e) {
e.printStackTrace();
throw new NullPointerException("Failed to instantiate DryingRecipe");
}
return ReflectionHelper.getInstance(ReflectionHelper.getConstructor(DryingRecipe.class, ItemStack.class, int.class, ItemStack.class),
input,
time,
output);
}
public static String getMaterialFromID(int id) {
@ -92,13 +72,10 @@ public class TConstructHelper {
}
public static ItemKey getItemKey(Item item, int damage, int value, String material) {
try {
Constructor constructor = ItemKey.class.getDeclaredConstructor(ItemStack.class, int.class, int.class, String.class);
constructor.setAccessible(true);
return (ItemKey) constructor.newInstance(item, damage, value, material);
} catch (Exception e) {
e.printStackTrace();
throw new NullPointerException("Failed to instantiate ItemKey");
}
return ReflectionHelper.getInstance(ReflectionHelper.getConstructor(ItemKey.class, ItemStack.class, int.class, int.class, String.class),
item,
damage,
value,
material);
}
}

View file

@ -35,7 +35,6 @@ public class Modifiers {
}
}
//Searches through the modifiers and removes the first valid entry
private static class Remove extends BaseListRemoval<ItemModifier>{
public Remove(List<ItemModifier> recipes) {
super(Modifiers.name, TConstructHelper.modifiers, recipes);

View file

@ -14,8 +14,6 @@ import cofh.thermalexpansion.util.crafting.SawmillManager.RecipeSawmill;
import cofh.thermalexpansion.util.crafting.SmelterManager.RecipeSmelter;
import cofh.thermalexpansion.util.crafting.TransposerManager.RecipeTransposer;
@SuppressWarnings("unchecked")
public class ThermalHelper {
public static Constructor<RecipeCrucible> crucibleRecipe;
public static Constructor<RecipeFurnace> furanceRecipe;
@ -26,20 +24,12 @@ public class ThermalHelper {
static {
try {
crucibleRecipe = getConstructor("cofh.thermalexpansion.util.crafting.CrucibleManager$RecipeCrucible", ItemStack.class, FluidStack.class, int.class);
furanceRecipe = getConstructor("cofh.thermalexpansion.util.crafting.FurnaceManager$RecipeFurnace", ItemStack.class, ItemStack.class, int.class);
pulverizerRecipe = getConstructor("cofh.thermalexpansion.util.crafting.PulverizerManager$RecipePulverizer", ItemStack.class, ItemStack.class, ItemStack.class, int.class, int.class);
sawmillRecipe = getConstructor("cofh.thermalexpansion.util.crafting.SawmillManager$RecipeSawmill", ItemStack.class, ItemStack.class, ItemStack.class, int.class, int.class);
smelterRecipe = getConstructor("cofh.thermalexpansion.util.crafting.SmelterManager$RecipeSmelter", ItemStack.class, ItemStack.class, ItemStack.class, ItemStack.class, int.class, int.class);
transposerRecipe = getConstructor("cofh.thermalexpansion.util.crafting.TransposerManager$RecipeTransposer", ItemStack.class, ItemStack.class, FluidStack.class, int.class, int.class);
crucibleRecipe = getConstructor(RecipeCrucible.class, ItemStack.class, FluidStack.class, int.class);
furanceRecipe = getConstructor(RecipeFurnace.class, ItemStack.class, ItemStack.class, int.class);
pulverizerRecipe = getConstructor(RecipePulverizer.class, ItemStack.class, ItemStack.class, ItemStack.class, int.class, int.class);
sawmillRecipe = getConstructor(RecipeSawmill.class, ItemStack.class, ItemStack.class, ItemStack.class, int.class, int.class);
smelterRecipe = getConstructor(RecipeSmelter.class, ItemStack.class, ItemStack.class, ItemStack.class, ItemStack.class, int.class, int.class);
transposerRecipe = getConstructor(RecipeTransposer.class, ItemStack.class, ItemStack.class, FluidStack.class, int.class, int.class);
} catch (Exception e) { LogHelper.logError("Exception getting constructor for Thermal Expansion recipes!", e); }
}
public static <T> T getTERecipe(Constructor<T> constructor, Object... items) {
try {
return constructor.newInstance(items);
} catch (Exception e) { LogHelper.logError("Exception creating instance of Thermal Expansion recipe!", e); }
return null;
}
}

View file

@ -14,6 +14,7 @@ import minetweaker.api.item.IItemStack;
import minetweaker.api.liquid.ILiquidStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.helpers.LogHelper;
import modtweaker2.helpers.ReflectionHelper;
import modtweaker2.mods.thermalexpansion.ThermalHelper;
import modtweaker2.utils.BaseListAddition;
import modtweaker2.utils.BaseListRemoval;
@ -39,7 +40,7 @@ public class Crucible {
return;
}
MineTweakerAPI.apply(new Add(ThermalHelper.getTERecipe(ThermalHelper.crucibleRecipe, toStack(input), toFluid(output), energy)));
MineTweakerAPI.apply(new Add(ReflectionHelper.getInstance(ThermalHelper.crucibleRecipe, toStack(input), toFluid(output), energy)));
}
private static class Add extends BaseListAddition<RecipeCrucible> {

View file

@ -13,6 +13,7 @@ import minetweaker.api.item.IIngredient;
import minetweaker.api.item.IItemStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.helpers.LogHelper;
import modtweaker2.helpers.ReflectionHelper;
import modtweaker2.mods.thermalexpansion.ThermalHelper;
import modtweaker2.utils.BaseListAddition;
import modtweaker2.utils.BaseListRemoval;
@ -40,7 +41,7 @@ public class Furnace {
return;
}
MineTweakerAPI.apply(new Add(ThermalHelper.getTERecipe(ThermalHelper.furanceRecipe, toStack(input), toStack(output), energy)));
MineTweakerAPI.apply(new Add(ReflectionHelper.getInstance(ThermalHelper.furanceRecipe, toStack(input), toStack(output), energy)));
}
private static class Add extends BaseListAddition<RecipeFurnace> {

View file

@ -12,6 +12,7 @@ import minetweaker.api.item.IIngredient;
import minetweaker.api.item.IItemStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.helpers.LogHelper;
import modtweaker2.helpers.ReflectionHelper;
import modtweaker2.mods.thermalexpansion.ThermalHelper;
import modtweaker2.utils.BaseListAddition;
import modtweaker2.utils.BaseListRemoval;
@ -44,7 +45,7 @@ public class Pulverizer {
return;
}
MineTweakerAPI.apply(new Add(ThermalHelper.getTERecipe(ThermalHelper.pulverizerRecipe, toStack(input), toStack(output), toStack(secondary), secondaryChance, energy)));
MineTweakerAPI.apply(new Add(ReflectionHelper.getInstance(ThermalHelper.pulverizerRecipe, toStack(input), toStack(output), toStack(secondary), secondaryChance, energy)));
}
private static class Add extends BaseListAddition<RecipePulverizer> {

View file

@ -12,6 +12,7 @@ import minetweaker.api.item.IIngredient;
import minetweaker.api.item.IItemStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.helpers.LogHelper;
import modtweaker2.helpers.ReflectionHelper;
import modtweaker2.mods.thermalexpansion.ThermalHelper;
import modtweaker2.utils.BaseListAddition;
import modtweaker2.utils.BaseListRemoval;
@ -44,7 +45,7 @@ public class Sawmill {
return;
}
MineTweakerAPI.apply(new Add(ThermalHelper.getTERecipe(ThermalHelper.sawmillRecipe, toStack(input), toStack(output), toStack(secondary), secondaryChance, energy)));
MineTweakerAPI.apply(new Add(ReflectionHelper.getInstance(ThermalHelper.sawmillRecipe, toStack(input), toStack(output), toStack(secondary), secondaryChance, energy)));
}
private static class Add extends BaseListAddition<RecipeSawmill> {

View file

@ -12,6 +12,7 @@ import minetweaker.api.item.IIngredient;
import minetweaker.api.item.IItemStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.helpers.LogHelper;
import modtweaker2.helpers.ReflectionHelper;
import modtweaker2.mods.thermalexpansion.ThermalHelper;
import modtweaker2.utils.BaseListAddition;
import modtweaker2.utils.BaseListRemoval;
@ -44,7 +45,7 @@ public class Smelter {
return;
}
MineTweakerAPI.apply(new Add(ThermalHelper.getTERecipe(ThermalHelper.smelterRecipe, toStack(input), toStack(input2), toStack(output), toStack(output2), chance, energy)));
MineTweakerAPI.apply(new Add(ReflectionHelper.getInstance(ThermalHelper.smelterRecipe, toStack(input), toStack(input2), toStack(output), toStack(output2), chance, energy)));
}
private static class Add extends BaseListAddition<RecipeSmelter> {

View file

@ -16,6 +16,7 @@ import minetweaker.api.item.IngredientAny;
import minetweaker.api.liquid.ILiquidStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.helpers.LogHelper;
import modtweaker2.helpers.ReflectionHelper;
import modtweaker2.mods.thermalexpansion.ThermalHelper;
import modtweaker2.utils.BaseListAddition;
import modtweaker2.utils.BaseListRemoval;
@ -44,7 +45,7 @@ public class Transposer {
return;
}
MineTweakerAPI.apply(new Add(ThermalHelper.getTERecipe(ThermalHelper.transposerRecipe, toStack(input), toStack(output), toFluid(liquid), energy, 100), RecipeType.Fill));
MineTweakerAPI.apply(new Add(ReflectionHelper.getInstance(ThermalHelper.transposerRecipe, toStack(input), toStack(output), toFluid(liquid), energy, 100), RecipeType.Fill));
}
@ZenMethod
@ -59,7 +60,7 @@ public class Transposer {
return;
}
MineTweakerAPI.apply(new Add(ThermalHelper.getTERecipe(ThermalHelper.transposerRecipe, toStack(input), toStack(output), toFluid(liquid), energy, 100), RecipeType.Extract));
MineTweakerAPI.apply(new Add(ReflectionHelper.getInstance(ThermalHelper.transposerRecipe, toStack(input), toStack(output), toFluid(liquid), energy, 100), RecipeType.Extract));
}
private static class Add extends BaseListAddition<RecipeTransposer> {