diff --git a/src/main/java/org/maxgamer/quickshop/Util/Util.java b/src/main/java/org/maxgamer/quickshop/Util/Util.java index e3628ba43..c938938e2 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/Util.java +++ b/src/main/java/org/maxgamer/quickshop/Util/Util.java @@ -38,6 +38,7 @@ public class Util { private static HashSet transparent = new HashSet(); private static Map> restrictedPrices = new HashMap>(); private static QuickShop plugin; + private static Method storageContents; public static void initialize() { tools.clear(); @@ -221,6 +222,16 @@ public class Util { } } } + + try { + storageContents = Inventory.class.getMethod("getStorageContents"); + } catch (Exception e) { + try { + storageContents = Inventory.class.getMethod("getContents"); + } catch (Exception e1) { + throw new RuntimeException(e1); + } + } } /** Return an entry with min and max prices, but null if there isn't a price restriction */ @@ -941,19 +952,18 @@ public class Util { */ public static int countSpace(Inventory inv, ItemStack item) { int space = 0; - ItemStack[] contents; - try { // Bukkit API MC 1.9+ : Get actual storage slots (not armor...) - Method storageContents = Inventory.class.getMethod("getStorageContents"); - contents = (ItemStack[])storageContents.invoke(inv); - } catch (Exception e) { - contents = inv.getContents(); // Bukkit API MC 1.8- fallback: Get all slots - } - for (ItemStack iStack : contents) { - if (iStack == null || iStack.getType() == Material.AIR) { - space += item.getMaxStackSize(); - } else if (matches(item, iStack)) { - space += item.getMaxStackSize() - iStack.getAmount(); + + try { + ItemStack[] contents = (ItemStack[])storageContents.invoke(inv); + for (ItemStack iStack : contents) { + if (iStack == null || iStack.getType() == Material.AIR) { + space += item.getMaxStackSize(); + } else if (matches(item, iStack)) { + space += item.getMaxStackSize() - iStack.getAmount(); + } } + } catch (Exception e) { + throw new RuntimeException(e); } return space; }