Code cleanup

This commit is contained in:
Zixxl committed 2015-07-07 12:00:17 +02:00
1 parent 3990a81390
commit 39e415b36d
5 files changed
+106 -47

No files matched your search

@@ -14,8 +14,11 @@ public class EntityMappingLogger implements ICommandFunction {
@Override
public void execute(String[] arguments, IPlayer player) {
Set<Integer> keys = EntityList.stringToIDMapping.keySet();
@SuppressWarnings("unchecked")
Set<Integer> keys = EntityList.stringToIDMapping.keySet();
System.out.println("Mob Keys: " + keys.size());
for (Integer key : keys) {
ModTweaker2.logger.info("Mob Key " + EntityList.getStringFromID(key) + " : " + key);
MineTweakerAPI.logCommand("Mob Key " + EntityList.getStringFromID(key) + " : " + key);
@@ -1,9 +1,10 @@
package modtweaker2.helpers;
import static modtweaker2.helpers.ReflectionHelper.getConstructor;
import static modtweaker2.helpers.ReflectionHelper.getFinalObject;
import static modtweaker2.helpers.ReflectionHelper.getInstance;
import static modtweaker2.helpers.ReflectionHelper.getStaticObject;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -17,7 +18,9 @@ import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
public class ForgeHelper {
@SuppressWarnings("rawtypes")
public static Map translate = null;
@SuppressWarnings("rawtypes")
public static List seeds = null;
public static HashMap<String, ChestGenHooks> loot = null;
@@ -26,23 +29,25 @@ public class ForgeHelper {
seeds = getStaticObject(ForgeHooks.class, "seedList");
loot = getStaticObject(ChestGenHooks.class, "chestInfo");
translate = getFinalObject(getStaticObject(StatCollector.class, "localizedName", "field_74839_a"), "languageList", "field_74816_c");
} catch (Exception e) {}
} catch (Exception e) { }
}
private ForgeHelper() {}
private ForgeHelper() {
}
public static Object getSeedEntry(ItemStack stack, int weight) {
try {
Class clazz = Class.forName("net.minecraftforge.common.ForgeHooks$SeedEntry");
Constructor constructor = clazz.getDeclaredConstructor(ItemStack.class, int.class);
constructor.setAccessible(true);
return constructor.newInstance(stack, weight);
} catch (Exception e) {
Object seedEntry = getInstance(getConstructor("net.minecraftforge.common.ForgeHooks$SeedEntry",
ItemStack.class, int.class), stack, weight);
if (seedEntry == null) {
throw new NullPointerException("Failed to instantiate SeedEntry");
}
return seedEntry;
}
public static boolean isLangActive(String lang) {
return FMLCommonHandler.instance().getSide() == Side.SERVER ? null : FMLClientHandler.instance().getCurrentLanguage().equals(lang);
return FMLCommonHandler.instance().getSide() == Side.SERVER ? null
: FMLClientHandler.instance().getCurrentLanguage().equals(lang);
}
}
@@ -88,7 +88,7 @@ public class InputHelper {
}
}
public static String getArrayDescription(List objects) {
public static String getArrayDescription(List<?> objects) {
StringBuilder sb = new StringBuilder();
if(objects.isEmpty()) {
@@ -209,7 +209,8 @@ public class InputHelper {
}
}
public static Object[] toShapedObjects(IIngredient[][] ingredients) {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object[] toShapedObjects(IIngredient[][] ingredients) {
if (ingredients == null)
return null;
else {
@@ -6,6 +6,12 @@ import java.lang.reflect.Modifier;
public class ReflectionHelper {
/**
* Creates a new instance with the provided constructor and arguments.
* @param constructor The constructor of the class for which a new instance should be created.
* @param arguments Array of objects to be passed as arguments to the constructor call.
* @return A new object created by calling the constructor.
*/
public static <T> T getInstance(Constructor<T> constructor, Object... arguments) {
try {
return constructor.newInstance(arguments);
@@ -14,72 +20,100 @@ public class ReflectionHelper {
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();
/**
* Obtains the constructor for the named class identified by the parameter types.
* @param className The fully qualified name of the class.
* @param argumentTypes Parameter types to identify the constructor.
* @return Constructor object representing the declared constructor for the parameter types.
*/
public static Constructor<?> getConstructor(final String className, final Class<?>... argumentTypes) {
if(className == null || argumentTypes == null) {
LogHelper.logError("The provided class name or arguments can't be null.");
return null;
}
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);
Class<?> clazz = Class.forName(className);
Constructor<?> constructor = clazz.getDeclaredConstructor(argumentTypes);
constructor.setAccessible(true);
return constructor;
} catch (Exception ex) { LogHelper.logError("Exception creating instance of " + string, ex); }
} catch (Exception ex) { LogHelper.logError("Exception getting constructore of " + className, ex); }
return null;
}
public static <T> Constructor<T> getConstructor(Class<T> clazz, Class... types) {
/**
* Obtains the constructor for the class identified by the parameter types.
* @param clazz The Class for which the constructor should be obtained.
* @param argumentTypes Parameter types to identify the constructor.
* @return Constructor object representing the declared constructor for the parameter types.
*/
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();
for (String field : fieldName) {
/**
* Returns the value of a private field for an object instance.
* @param object An object instance from which the field value is to be extracted.
* @param fieldNames A list of field names for which the value should be extracted.
* The functions returns value of the first field found.
* @return The value of the provided field name.
*/
@SuppressWarnings("unchecked")
public static <T> T getObject(Object object, String... fieldNames) {
Class<?> cls = object.getClass();
for (String field : fieldNames) {
try {
Field result = cls.getDeclaredField(field);
result.setAccessible(true);
return (T) result.get(o);
return (T) result.get(object);
} catch (Exception ex) {}
}
return null;
}
public static <T> T getFinalObject(Object o, String... fieldName) {
Class cls = o.getClass();
for (String field : fieldName) {
/**
* Returns the value of a private final field for an object instance and removes the final modifier.
* @param object An object instance from which the field value is to be extracted.
* @param fieldNames A list of field names for which the value should be extracted.
* The functions returns value of the first field found.
* @return The value of the provided field name.
*/
@SuppressWarnings("unchecked")
public static <T> T getFinalObject(Object object, String... fieldNames) {
Class<?> cls = object.getClass();
for (String field : fieldNames) {
try {
Field result = cls.getDeclaredField(field);
result.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(result, result.getModifiers() & ~Modifier.FINAL);
return (T) result.get(o);
return (T) result.get(object);
} catch (Exception ex) {}
}
return null;
}
public static <T> T getStaticObject(Class cls, String... fieldName) {
for (String field : fieldName) {
/**
* Returns the value of a private static field for a class.
* @param clazz The class for which the field value is to be extracted.
* @param fieldNames A list of field names for which the value should be extracted.
* @return The value of the provided field name.
*/
@SuppressWarnings("unchecked")
public static <T> T getStaticObject(Class<?> clazz, String... fieldNames) {
for (String field : fieldNames) {
try {
Field result = cls.getDeclaredField(field);
Field result = clazz.getDeclaredField(field);
result.setAccessible(true);
return (T) result.get(null);
} catch (Exception e) {}
@@ -87,16 +121,32 @@ public class ReflectionHelper {
return null;
}
/**
* Returns the value of a private static field for a class.
* @param className The fully qualified name of the class.
* @param fieldNames A list of field names for which the value should be extracted.
* @return The value of the provided field name.
*/
public static <T> T getStaticObject(String className, String... fieldNames) {
try {
Class<?> clazz = Class.forName(className);
return getStaticObject(clazz, fieldNames);
} catch (ClassNotFoundException e) { }
return null;
}
public static void setPrivateValue(Class cls, String field, int var) {
public static void setPrivateValue(Class<?> cls, String field, int var) {
try {
Field f = cls.getDeclaredField(field);
f.setAccessible(true);
f.setInt(null, var);
} catch (Exception e) {}
}
public static void setPrivateValue(Class cls, Object o, String field, Object var) {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void setPrivateValue( Class cls, Object o, String field, Object var) {
cpw.mods.fml.relauncher.ReflectionHelper.setPrivateValue(cls, o, var, field);
}
}
@@ -5,15 +5,15 @@ import java.util.ArrayList;
import cpw.mods.fml.common.Loader;
public class TweakerPlugin {
private static ArrayList<String> isLoaded = new ArrayList();
private static ArrayList<String> isLoaded = new ArrayList<String>();
public static void register(String mod, Class clazz) {
public static void register(String mod, Class<?> clazz) {
if (Loader.isModLoaded(mod)) {
load(mod, clazz);
}
}
public static void load(String mod, Class clazz) {
public static void load(String mod, Class<?> clazz) {
try {
clazz.newInstance();
isLoaded.add(mod);