From ee22af02e06f970e9bd2aa895b4f5fdc06aa0d60 Mon Sep 17 00:00:00 2001 From: Cheeserolls Date: Fri, 22 May 2015 23:10:14 +0100 Subject: [PATCH] Add utility function for accessing hidden object constructors with reflection --- .../biomesoplenty/common/init/ModItems.java | 30 +++------------- .../common/util/BOPReflectionHelper.java | 34 +++++++++++++++++++ .../common/util/ReflectionHelper.java | 15 -------- 3 files changed, 39 insertions(+), 40 deletions(-) create mode 100644 src/main/java/biomesoplenty/common/util/BOPReflectionHelper.java delete mode 100644 src/main/java/biomesoplenty/common/util/ReflectionHelper.java diff --git a/src/main/java/biomesoplenty/common/init/ModItems.java b/src/main/java/biomesoplenty/common/init/ModItems.java index 105e598e3..c9e3cfc9a 100644 --- a/src/main/java/biomesoplenty/common/init/ModItems.java +++ b/src/main/java/biomesoplenty/common/init/ModItems.java @@ -11,7 +11,6 @@ package biomesoplenty.common.init; import static biomesoplenty.api.item.BOPItems.*; import static biomesoplenty.api.item.BOPItemHelper.*; -import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.List; @@ -30,6 +29,7 @@ import net.minecraftforge.fml.relauncher.Side; import biomesoplenty.api.block.BOPBlocks; import biomesoplenty.common.command.BOPCommand; import biomesoplenty.common.item.*; +import biomesoplenty.common.util.BOPReflectionHelper; import biomesoplenty.common.util.inventory.CreativeTabBOP; import biomesoplenty.core.BiomesOPlenty; @@ -141,30 +141,10 @@ public class ModItems // no repair item for amethyst tool - they can't be repaired // ItemAxe and ItemPickaxe have protected constructors - use reflection to construct - // TODO use utility functions for this - Constructor axeConstructor; - try - { - 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 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(); - } + mud_axe = registerItem(BOPReflectionHelper.construct(ItemAxe.class, mud_tool_material), "mud_axe"); + mud_pickaxe = registerItem(BOPReflectionHelper.construct(ItemPickaxe.class, mud_tool_material), "mud_pickaxe"); + amethyst_axe = registerItem(BOPReflectionHelper.construct(ItemAxe.class, amethyst_tool_material), "amethyst_axe"); + amethyst_pickaxe = registerItem(BOPReflectionHelper.construct(ItemPickaxe.class, amethyst_tool_material), "amethyst_pickaxe"); // the other tools have public constructors, so we create instances in the normal way mud_hoe = registerItem(new ItemHoe(mud_tool_material), "mud_hoe"); diff --git a/src/main/java/biomesoplenty/common/util/BOPReflectionHelper.java b/src/main/java/biomesoplenty/common/util/BOPReflectionHelper.java new file mode 100644 index 000000000..fac05dc6c --- /dev/null +++ b/src/main/java/biomesoplenty/common/util/BOPReflectionHelper.java @@ -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 construct(Class clazz, Object... args) + { + try { + Class[] argClasses = new Class[args.length]; + for (int i = 0; i < args.length; i++) + { + argClasses[i] = args[i].getClass(); + } + Constructor constructor = clazz.getDeclaredConstructor(argClasses); + constructor.setAccessible(true); + return constructor.newInstance(args); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + +} diff --git a/src/main/java/biomesoplenty/common/util/ReflectionHelper.java b/src/main/java/biomesoplenty/common/util/ReflectionHelper.java deleted file mode 100644 index cac495ccb..000000000 --- a/src/main/java/biomesoplenty/common/util/ReflectionHelper.java +++ /dev/null @@ -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. -}