GenuineSounds

Adding Loot

Jun 19th, 2012
196
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. package ltd.genuine.common;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Modifier;
  5. import java.util.Arrays;
  6. import net.minecraft.src.ComponentStrongholdChestCorridor;
  7. import net.minecraft.src.ComponentStrongholdLibrary;
  8. import net.minecraft.src.ComponentStrongholdRoomCrossing;
  9. import net.minecraft.src.ComponentVillageHouse2;
  10. import net.minecraft.src.StructureMineshaftPieces;
  11. import net.minecraft.src.StructurePieceTreasure;
  12.  
  13. public final class Tasks {
  14.  
  15.     public static Object getPrivateValue(Class clazz, Object instance, String... fieldNames) {
  16.         SecurityException e1 = null;
  17.         NoSuchFieldException e2 = null;
  18.         IllegalArgumentException e3 = null;
  19.         IllegalAccessException e4 = null;
  20.         for (String fieldName : fieldNames) {
  21.             try {
  22.                 Field field = clazz.getDeclaredField(fieldName);
  23.                 field.setAccessible(true);
  24.                 Field modifiersField = Field.class.getDeclaredField("modifiers");
  25.                 modifiersField.setAccessible(true);
  26.                 Object value = field.get(instance);
  27.                 return value;
  28.             }
  29.             catch (SecurityException e) {
  30.                 e1 = e;
  31.             }
  32.             catch (NoSuchFieldException e) {
  33.                 e2 = e;
  34.             }
  35.             catch (IllegalArgumentException e) {
  36.                 e3 = e;
  37.             }
  38.             catch (IllegalAccessException e) {
  39.                 e4 = e;
  40.             }
  41.         }
  42.         if (e1 != null)
  43.             e1.printStackTrace();
  44.         if (e2 != null)
  45.             e2.printStackTrace();
  46.         if (e3 != null)
  47.             e3.printStackTrace();
  48.         if (e4 != null)
  49.             e4.printStackTrace();
  50.         return null;
  51.     }
  52.  
  53.     /**
  54.      * Method for turning seconds into a string hh:mm:ss
  55.      *
  56.      * @param secsIn
  57.      * @return
  58.      */
  59.     public static String secondsToTimeString(double secsIn) {
  60.         int hours = (int) (secsIn / 3600);
  61.         int remainder = (int) (secsIn % 3600);
  62.         int minutes = remainder / 60;
  63.         int seconds = remainder % 60;
  64.         String hoursString = hours <= 0 ? "" : (hours < 10 ? "0" : "") + hours + ":";
  65.         String minutesString = hours <= 0 && minutes <= 0 ? "" : (minutes < 10 ? "0" : "") + minutes + ":";
  66.         String secondsString = (seconds < 10 ? "0" : "") + seconds;
  67.         return hoursString + minutesString + secondsString;
  68.     }
  69.  
  70.     /**
  71.      * @param loot
  72.      *            Input {@link StructurePieceTreasure} to add to loot tables.
  73.      */
  74.     public static void registerChestLoot(StructurePieceTreasure... loot) {
  75.         reflectInjectArray(ComponentStrongholdChestCorridor.class, loot, "chestLoot", "field_40013_a", "a");
  76.         reflectInjectArray(ComponentStrongholdLibrary.class, loot, "chestLoot", "field_35056_b", "b");
  77.         reflectInjectArray(ComponentStrongholdRoomCrossing.class, loot, "chestLoot", "field_35061_c", "c");
  78.         reflectInjectArray(ComponentVillageHouse2.class, loot, "chestLoot", "field_46002_a", "a");
  79.         reflectInjectArray(StructureMineshaftPieces.class, loot, "chestLoot", "lootArray", "a");
  80.     }
  81.  
  82.     public static <T> int reflectInjectArray(Class class1, T[] newValue, String... fieldNames) {
  83.         for (String fieldName : fieldNames) {
  84.             try {
  85.                 Field field = class1.getDeclaredField(fieldName);
  86.                 field.setAccessible(true);
  87.                 Field modifiersField = Field.class.getDeclaredField("modifiers");
  88.                 modifiersField.setAccessible(true);
  89.                 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
  90.                 T[] oldValue = (T[]) field.get(null);
  91.                 if (field != null) {
  92.                     field.set(null, toArrayCopy(oldValue, newValue));
  93.                     return oldValue.length;
  94.                 }
  95.             }
  96.             catch (SecurityException e) {}
  97.             catch (NoSuchFieldException e) {}
  98.             catch (IllegalArgumentException e) {}
  99.             catch (IllegalAccessException e) {}
  100.         }
  101.         return 0;
  102.     }
  103.  
  104.     /**
  105.      * Method for adding an object to the same object type array.
  106.      * @param array
  107.      *            The Object {@link Array} to have objects added to.
  108.      * @param objects
  109.      *            One or more objects to add to array.
  110.      * @return The array with all objects added.
  111.      */
  112.     public static <T> T[] toArrayCopy(T[] array, T... objects) {
  113.         for (T object : objects) {
  114.             if (object == null)
  115.                 continue;
  116.             array = Arrays.copyOf(array, array.length + 1);
  117.             array[array.length - 1] = object;
  118.         }
  119.         return array;
  120.     }
  121. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment