Add utility function for accessing hidden object constructors with reflection

This commit is contained in:
Cheeserolls 2015-05-22 23:10:14 +01:00
parent 43c2d300fb
commit ee22af02e0
3 changed files with 39 additions and 40 deletions

View file

@ -11,7 +11,6 @@ package biomesoplenty.common.init;
import static biomesoplenty.api.item.BOPItems.*; import static biomesoplenty.api.item.BOPItems.*;
import static biomesoplenty.api.item.BOPItemHelper.*; import static biomesoplenty.api.item.BOPItemHelper.*;
import java.lang.reflect.Constructor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -30,6 +29,7 @@ import net.minecraftforge.fml.relauncher.Side;
import biomesoplenty.api.block.BOPBlocks; import biomesoplenty.api.block.BOPBlocks;
import biomesoplenty.common.command.BOPCommand; import biomesoplenty.common.command.BOPCommand;
import biomesoplenty.common.item.*; import biomesoplenty.common.item.*;
import biomesoplenty.common.util.BOPReflectionHelper;
import biomesoplenty.common.util.inventory.CreativeTabBOP; import biomesoplenty.common.util.inventory.CreativeTabBOP;
import biomesoplenty.core.BiomesOPlenty; import biomesoplenty.core.BiomesOPlenty;
@ -141,30 +141,10 @@ public class ModItems
// no repair item for amethyst tool - they can't be repaired // no repair item for amethyst tool - they can't be repaired
// ItemAxe and ItemPickaxe have protected constructors - use reflection to construct // ItemAxe and ItemPickaxe have protected constructors - use reflection to construct
// TODO use utility functions for this mud_axe = registerItem(BOPReflectionHelper.construct(ItemAxe.class, mud_tool_material), "mud_axe");
Constructor<ItemAxe> axeConstructor; mud_pickaxe = registerItem(BOPReflectionHelper.construct(ItemPickaxe.class, mud_tool_material), "mud_pickaxe");
try amethyst_axe = registerItem(BOPReflectionHelper.construct(ItemAxe.class, amethyst_tool_material), "amethyst_axe");
{ amethyst_pickaxe = registerItem(BOPReflectionHelper.construct(ItemPickaxe.class, amethyst_tool_material), "amethyst_pickaxe");
axeConstructor = ItemAxe.class.getDeclaredConstructor(ToolMaterial.class);
axeConstructor.setAccessible(true);
mud_axe = registerItem(axeConstructor.newInstance(mud_tool_material), "mud_axe");
amethyst_axe = registerItem(axeConstructor.newInstance(amethyst_tool_material), "amethyst_axe");
}
catch (Exception e)
{
e.printStackTrace();
}
try
{
Constructor<ItemPickaxe> pickaxeConstructor = ItemPickaxe.class.getDeclaredConstructor(ToolMaterial.class);
pickaxeConstructor.setAccessible(true);
mud_pickaxe = registerItem(pickaxeConstructor.newInstance(mud_tool_material), "mud_pickaxe");
amethyst_pickaxe = registerItem(pickaxeConstructor.newInstance(amethyst_tool_material), "amethyst_pickaxe");
}
catch (Exception e)
{
e.printStackTrace();
}
// the other tools have public constructors, so we create instances in the normal way // the other tools have public constructors, so we create instances in the normal way
mud_hoe = registerItem(new ItemHoe(mud_tool_material), "mud_hoe"); mud_hoe = registerItem(new ItemHoe(mud_tool_material), "mud_hoe");

View file

@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright 2014, the Biomes O' Plenty Team
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/
package biomesoplenty.common.util;
import java.lang.reflect.Constructor;
public class BOPReflectionHelper
{
// Construct an instance of the given class using the given parameters in the constructor
// Works on constructors which aren't usually accessible
public static <T> T construct(Class<T> clazz, Object... args)
{
try {
Class<?>[] argClasses = new Class<?>[args.length];
for (int i = 0; i < args.length; i++)
{
argClasses[i] = args[i].getClass();
}
Constructor<T> constructor = clazz.getDeclaredConstructor(argClasses);
constructor.setAccessible(true);
return constructor.newInstance(args);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View file

@ -1,15 +0,0 @@
/*******************************************************************************
* Copyright 2014, the Biomes O' Plenty Team
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/
package biomesoplenty.common.util;
public class ReflectionHelper
{
// Various fields used in Reflection. These should be checked with every
// update.
}