GenuineSounds

Forge Hook

Jun 11th, 2012
225
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1.  
  2. /**
  3.  * returns true if this entity is by a ladder, false otherwise
  4.  */
  5. public boolean isOnLadder() {
  6.     int x = MathHelper.floor_double(this.posX);
  7.     int y = MathHelper.floor_double(this.boundingBox.minY);
  8.     int z = MathHelper.floor_double(this.posZ);
  9.     int blockId = this.worldObj.getBlockId(x, y, z);
  10.     if (ForgeLayer.isForgeInstalled())
  11.         if (ForgeLayer.isLadder(Block.blocksList[blockId], this.worldObj, x, y, z))
  12.             return true;
  13.     return Block.blocksList[blockId] != null && Block.blocksList[blockId] == Block.ladder;
  14. }
  15.  
  16.  
  17. // ----------------------------------------------------------------------------------------------------
  18.  
  19. package ltd.genuine.forge;
  20.  
  21. import java.lang.reflect.InvocationTargetException;
  22. import java.lang.reflect.Method;
  23.  
  24. import net.minecraft.src.Achievement;
  25. import net.minecraft.src.Block;
  26. import net.minecraft.src.ItemStack;
  27. import net.minecraft.src.ModLoader;
  28. import net.minecraft.src.World;
  29.  
  30. public class ForgeLayer {
  31.  
  32.     static boolean init;
  33.  
  34.     static boolean forgeInstalled;
  35.  
  36.     static Class minecraftForge;
  37.  
  38.     static Class achievementPage;
  39.  
  40.     static Method isLadder;
  41.  
  42.     static Method addDungeonLoot;
  43.  
  44.     static Method registerAchievementPage;
  45.  
  46.     public static boolean isLadder(Block block, World world, int x, int y, int z) {
  47.         if (!init)
  48.             init();
  49.         if (block == null || !forgeInstalled)
  50.             return false;
  51.         try {
  52.             return (Boolean) isLadder.invoke(block, world, x, y, z);
  53.         }
  54.         catch (IllegalArgumentException e) {
  55.         }
  56.         catch (IllegalAccessException e) {
  57.         }
  58.         catch (InvocationTargetException e) {
  59.         }
  60.         return false;
  61.     }
  62.  
  63.     public static void addDungeonLoot(ItemStack item, float rarity) {
  64.         if (!init)
  65.             init();
  66.         if (!forgeInstalled)
  67.             return;
  68.         try {
  69.             addDungeonLoot.invoke(null, item, rarity);
  70.         }
  71.         catch (IllegalArgumentException e) {
  72.         }
  73.         catch (IllegalAccessException e) {
  74.         }
  75.         catch (InvocationTargetException e) {
  76.         }
  77.     }
  78.  
  79.     public static void registerAchievementPage(String name, Achievement... achievements) {
  80.         if (!init)
  81.             init();
  82.         if (!forgeInstalled)
  83.             return;
  84.         try {
  85.             Object page = achievementPage.getConstructor(String.class, Achievement[].class).newInstance(name, achievements);
  86.             registerAchievementPage.invoke(null, page);
  87.         }
  88.         catch (SecurityException e) {
  89.         }
  90.         catch (NoSuchMethodException e) {
  91.         }
  92.         catch (IllegalArgumentException e) {
  93.         }
  94.         catch (InstantiationException e) {
  95.         }
  96.         catch (IllegalAccessException e) {
  97.         }
  98.         catch (InvocationTargetException e) {
  99.         }
  100.     }
  101.  
  102.     private static void init() {
  103.         if (init)
  104.             return;
  105.         forgeInstalled = ModLoader.isModLoaded("mod_MinecraftForge") || ModLoader.isModLoaded("net.minecraft.src.mod_MinecraftForge");
  106.         if (forgeInstalled) {
  107.             minecraftForge = findClass("forge.MinecraftForge", "net.minecraft.src.forge.MinecraftForge");
  108.             achievementPage = findClass("forge.AchievementPage", "net.minecraft.src.forge.AchievementPage");
  109.             if (minecraftForge != null) {
  110.                 isLadder = findMethod(Block.class, "isLadder", World.class, int.class, int.class, int.class);
  111.                 addDungeonLoot = findMethod(minecraftForge, "addDungeonLoot", ItemStack.class, float.class);
  112.                 registerAchievementPage = findMethod(minecraftForge, "registerAchievementPage", achievementPage);
  113.             }
  114.         }
  115.         System.out.println("[Genuine] Forge compatibility layer was loaded and Forge is " + (forgeInstalled ? "installed." : "not installed."));
  116.         init = true;
  117.     }
  118.  
  119.     public static boolean isForgeInstalled() {
  120.         if (!init)
  121.             init();
  122.         return forgeInstalled;
  123.     }
  124.  
  125.     private static Class findClass(String... classNames) {
  126.         for (String name : classNames) {
  127.             try {
  128.                 return Class.forName(name);
  129.             }
  130.             catch (ClassNotFoundException e) {
  131.             }
  132.         }
  133.         return null;
  134.     }
  135.  
  136.     private static Method findMethod(Class clazz, String methodName, Class... args) {
  137.         try {
  138.             return clazz.getDeclaredMethod(methodName, args);
  139.         }
  140.         catch (SecurityException e) {
  141.         }
  142.         catch (NoSuchMethodException e) {
  143.         }
  144.         return null;
  145.     }
  146. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment