Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ltd.genuine.common;
- import java.lang.reflect.Field;
- import java.lang.reflect.Modifier;
- import java.util.Arrays;
- import net.minecraft.src.ComponentStrongholdChestCorridor;
- import net.minecraft.src.ComponentStrongholdLibrary;
- import net.minecraft.src.ComponentStrongholdRoomCrossing;
- import net.minecraft.src.ComponentVillageHouse2;
- import net.minecraft.src.StructureMineshaftPieces;
- import net.minecraft.src.StructurePieceTreasure;
- public final class Tasks {
- public static Object getPrivateValue(Class clazz, Object instance, String... fieldNames) {
- SecurityException e1 = null;
- NoSuchFieldException e2 = null;
- IllegalArgumentException e3 = null;
- IllegalAccessException e4 = null;
- for (String fieldName : fieldNames) {
- try {
- Field field = clazz.getDeclaredField(fieldName);
- field.setAccessible(true);
- Field modifiersField = Field.class.getDeclaredField("modifiers");
- modifiersField.setAccessible(true);
- Object value = field.get(instance);
- return value;
- }
- catch (SecurityException e) {
- e1 = e;
- }
- catch (NoSuchFieldException e) {
- e2 = e;
- }
- catch (IllegalArgumentException e) {
- e3 = e;
- }
- catch (IllegalAccessException e) {
- e4 = e;
- }
- }
- if (e1 != null)
- e1.printStackTrace();
- if (e2 != null)
- e2.printStackTrace();
- if (e3 != null)
- e3.printStackTrace();
- if (e4 != null)
- e4.printStackTrace();
- return null;
- }
- /**
- * Method for turning seconds into a string hh:mm:ss
- *
- * @param secsIn
- * @return
- */
- public static String secondsToTimeString(double secsIn) {
- int hours = (int) (secsIn / 3600);
- int remainder = (int) (secsIn % 3600);
- int minutes = remainder / 60;
- int seconds = remainder % 60;
- String hoursString = hours <= 0 ? "" : (hours < 10 ? "0" : "") + hours + ":";
- String minutesString = hours <= 0 && minutes <= 0 ? "" : (minutes < 10 ? "0" : "") + minutes + ":";
- String secondsString = (seconds < 10 ? "0" : "") + seconds;
- return hoursString + minutesString + secondsString;
- }
- /**
- * @param loot
- * Input {@link StructurePieceTreasure} to add to loot tables.
- */
- public static void registerChestLoot(StructurePieceTreasure... loot) {
- reflectInjectArray(ComponentStrongholdChestCorridor.class, loot, "chestLoot", "field_40013_a", "a");
- reflectInjectArray(ComponentStrongholdLibrary.class, loot, "chestLoot", "field_35056_b", "b");
- reflectInjectArray(ComponentStrongholdRoomCrossing.class, loot, "chestLoot", "field_35061_c", "c");
- reflectInjectArray(ComponentVillageHouse2.class, loot, "chestLoot", "field_46002_a", "a");
- reflectInjectArray(StructureMineshaftPieces.class, loot, "chestLoot", "lootArray", "a");
- }
- public static <T> int reflectInjectArray(Class class1, T[] newValue, String... fieldNames) {
- for (String fieldName : fieldNames) {
- try {
- Field field = class1.getDeclaredField(fieldName);
- field.setAccessible(true);
- Field modifiersField = Field.class.getDeclaredField("modifiers");
- modifiersField.setAccessible(true);
- modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
- T[] oldValue = (T[]) field.get(null);
- if (field != null) {
- field.set(null, toArrayCopy(oldValue, newValue));
- return oldValue.length;
- }
- }
- catch (SecurityException e) {}
- catch (NoSuchFieldException e) {}
- catch (IllegalArgumentException e) {}
- catch (IllegalAccessException e) {}
- }
- return 0;
- }
- /**
- * Method for adding an object to the same object type array.
- * @param array
- * The Object {@link Array} to have objects added to.
- * @param objects
- * One or more objects to add to array.
- * @return The array with all objects added.
- */
- public static <T> T[] toArrayCopy(T[] array, T... objects) {
- for (T object : objects) {
- if (object == null)
- continue;
- array = Arrays.copyOf(array, array.length + 1);
- array[array.length - 1] = object;
- }
- return array;
- }
- }
Advertisement