From 6835ecafdbcca79a071dfd412c6bd8220000baa9 Mon Sep 17 00:00:00 2001 From: games647 Date: Sun, 11 Jun 2017 09:31:06 +0200 Subject: [PATCH] Provide backwards compatible getStorageContent method for 1.8 (Fixes #11, #12) --- .../sainttx/auctions/util/AuctionUtil.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sainttx/auctions/util/AuctionUtil.java b/src/main/java/com/sainttx/auctions/util/AuctionUtil.java index 777afce..e037d93 100644 --- a/src/main/java/com/sainttx/auctions/util/AuctionUtil.java +++ b/src/main/java/com/sainttx/auctions/util/AuctionUtil.java @@ -20,6 +20,8 @@ package com.sainttx.auctions.util; +import java.util.Arrays; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -29,6 +31,22 @@ */ public class AuctionUtil { + //main content + hotbar + private static final int INVENTORY_MAX_SIZE = 27 + 9; + private static final boolean storageMethodAvailable; + + static { + boolean available = false; + try { + Bukkit.createInventory(null, 10).getStorageContents(); + available = true; + } catch (NoSuchMethodError noMethodEx) { + available = false; + } + + storageMethodAvailable = available; + } + /** * Gets the amount of slots available for a particular item * @@ -44,8 +62,15 @@ public static int getFreeSlots(Inventory inv, ItemStack base) { throw new IllegalArgumentException("base item cannot be null"); } + ItemStack[] storageContents; + if (storageMethodAvailable) { + storageContents = inv.getStorageContents(); + } else { + storageContents = Arrays.copyOfRange(inv.getContents(), 0, INVENTORY_MAX_SIZE); + } + int totalFree = 0; - for (ItemStack is : inv.getStorageContents()) { + for (ItemStack is : storageContents) { if (is == null || is.getType() == Material.AIR) { totalFree += base.getMaxStackSize(); } else if (is.isSimilar(base)) {