GenuineSounds

Forge Compatibility Layer

May 30th, 2012
202
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. package net.minecraft.src;
  2.  
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.lang.reflect.Method;
  5.  
  6. public class GenuineForgeCompat {
  7.  
  8.     public static boolean init;
  9.     public static boolean forgeInstalled;
  10.  
  11.     public static Class minecraftForge;
  12.     public static Class achievementPage;
  13.  
  14.     public static Method ladder;
  15.     public static Method loot;
  16.     public static Method achieve;
  17.  
  18.     public static boolean forgeLadderHook(Block block, World world, int x, int y, int z) {
  19.         if (!init)
  20.             load();
  21.         if (block == null || !forgeInstalled)
  22.             return false;
  23.         try {
  24.             return (Boolean) ladder.invoke(block, world, x, y, z);
  25.         }
  26.         catch (IllegalArgumentException e) { }
  27.         catch (IllegalAccessException e) { }
  28.         catch (InvocationTargetException e) { }
  29.         return false;
  30.     }
  31.  
  32.     public static void forgeAddDungeonLoot(ItemStack item, float rarity) {
  33.         if (!init)
  34.             load();
  35.         try {
  36.             loot.invoke(null, item, rarity);
  37.         }
  38.         catch (IllegalArgumentException e) { }
  39.         catch (IllegalAccessException e) { }
  40.         catch (InvocationTargetException e) { }
  41.     }
  42.  
  43.     public static void forgeAddAchievementPage(String name, Achievement... achievements) {
  44.         if (!init)
  45.             load();
  46.         try {
  47.             Object page = achievementPage.getConstructor(String.class, Achievement[].class).newInstance(name, achievements);
  48.             achieve.invoke(null, page);
  49.         }
  50.         catch (SecurityException e) { }
  51.         catch (NoSuchMethodException e) { }
  52.         catch (IllegalArgumentException e) { }
  53.         catch (InstantiationException e) { }
  54.         catch (IllegalAccessException e) { }
  55.         catch (InvocationTargetException e) { }
  56.     }
  57.  
  58.     public static void load() {
  59.         if (init)
  60.             return;
  61.         forgeInstalled = ModLoader.isModLoaded("mod_MinecraftForge") || ModLoader.isModLoaded("net.minecraft.src.mod_MinecraftForge");
  62.         if (forgeInstalled) {
  63.             minecraftForge = defineClass("forge.MinecraftForge", "net.minecraft.src.forge.MinecraftForge");
  64.             achievementPage = defineClass("forge.AchievementPage", "net.minecraft.src.forge.AchievementPage");
  65.             if (minecraftForge != null) {
  66.                 ladder = defineMethod(Block.class, "isLadder", World.class, int.class, int.class, int.class);
  67.                 loot = defineMethod(minecraftForge, "addDungeonLoot", ItemStack.class, float.class);
  68.                 achieve = defineMethod(minecraftForge, "registerAchievementPage", achievementPage);
  69.             }
  70.         }
  71.         init = true;
  72.     }
  73.  
  74.     private static Class defineClass(String... classNames) {
  75.         for (String name : classNames) {
  76.             try {
  77.                 return Class.forName(name);
  78.             }
  79.             catch (ClassNotFoundException e) { }
  80.         }
  81.         return null;
  82.     }
  83.  
  84.     private static Method defineMethod(Class class1, String methodName, Class... args) {
  85.         try {
  86.             return class1.getDeclaredMethod(methodName, args);
  87.         }
  88.         catch (SecurityException e) { }
  89.         catch (NoSuchMethodException e) { }
  90.         return null;
  91.     }
  92. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment