Advertisement
riking

Unsafe_InspectVillagerTrades

Sep 2nd, 2013
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. import java.lang.reflect.Field;
  2. import java.lang.reflect.Method;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.bukkit.entity.Villager;
  7. import org.bukkit.inventory.ItemStack;
  8.  
  9. import com.google.common.collect.Lists;
  10.  
  11. /**
  12.  * FIXME https://github.com/Bukkit/Bukkit/pull/921
  13.  *
  14.  * @author riking
  15.  */
  16. class Unsafe_InspectVillagerTrades
  17. {
  18.     public static List<ItemStack> getTradeResults(Villager vil) throws Exception, Error
  19.     {
  20.         Object internalVillager = vil.getClass().getDeclaredMethod("getHandle", (Class<?>) null).invoke(vil, (Object[]) null);
  21.         Class<?> internalEntityHumanClass = Class.forName("net.minecraft.server.v1_6_R2.EntityHuman");
  22.         ArrayList<?> merchantRecipeList = (ArrayList<?>) internalVillager.getClass().getDeclaredMethod("getOffers", internalEntityHumanClass).invoke(internalVillager, new Object[] {null});
  23.  
  24.         if (merchantRecipeList.size() == 0) return Lists.newArrayList();
  25.         List<ItemStack> ret = Lists.newArrayListWithCapacity(merchantRecipeList.size());
  26.  
  27.         Field itemField = merchantRecipeList.get(0).getClass().getField("sellingItem");
  28.         Class<?> internalItemClass = Class.forName("net.minecraft.server.v1_6_R2.ItemStack");
  29.         Method getStackMethod = Class.forName("org.bukkit.craftbukkit.v1_6_R2.inventory.CraftItemStack").getDeclaredMethod("asBukkitCopy", internalItemClass);
  30.         for (Object o : merchantRecipeList)
  31.         {
  32.             Object internalItemStack = itemField.get(o);
  33.             if (!internalItemStack.getClass().equals(internalItemClass)) continue;
  34.             Object bukkitItemStack = getStackMethod.invoke(null, internalItemStack);
  35.             if (!ItemStack.class.isAssignableFrom(bukkitItemStack.getClass()))
  36.             {
  37.                 new RuntimeException("asBukkitCopy() did not return a Bukkit ItemStack..? Investigate.").printStackTrace();
  38.                 continue;
  39.             }
  40.             ret.add((ItemStack) bukkitItemStack);
  41.         }
  42.         return ret;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement