From 24df17fde2c1d82349fd53b5d2748a5ed43d1d19 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Thu, 22 Sep 2016 11:57:49 -0400 Subject: [PATCH] Restore compatibility with 1.8 Fixes #9 --- .../Acrobot/Breeze/Utils/InventoryUtil.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java b/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java index 4379a3f..7dd43ae 100644 --- a/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java +++ b/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java @@ -12,6 +12,21 @@ import org.bukkit.inventory.ItemStack; * @author Acrobot */ public class InventoryUtil { + private static Boolean legacyContents = null; + + private static ItemStack[] getStorageContents(Inventory inventory) { + if (legacyContents == null) { + try { + inventory.getStorageContents(); + legacyContents = false; + } catch (NoSuchMethodError e) { + legacyContents = true; + } + } + + return legacyContents ? inventory.getContents() : inventory.getStorageContents(); + } + /** * Returns the amount of the item inside the inventory * @@ -49,7 +64,7 @@ public class InventoryUtil { * @return Is the inventory empty? */ public static boolean isEmpty(Inventory inventory) { - for (ItemStack stack : inventory.getStorageContents()) { + for (ItemStack stack : getStorageContents(inventory)) { if (!MaterialUtil.isEmpty(stack)) { return false; } @@ -89,7 +104,7 @@ public class InventoryUtil { return true; } - for (ItemStack iStack : inventory.getStorageContents()) { + for (ItemStack iStack : getStorageContents(inventory)) { if (left <= 0) { return true; } @@ -155,7 +170,7 @@ public class InventoryUtil { // Don't use the armor slots or extra slots private static int effectiveSize(Inventory inventory) { - return inventory.getStorageContents().length; + return getStorageContents(inventory).length; } /**