diff --git a/Essentials/src/com/earth2me/essentials/craftbukkit/FakeWorld.java b/Essentials/src/com/earth2me/essentials/craftbukkit/FakeWorld.java index 86025b449..0695d2022 100644 --- a/Essentials/src/com/earth2me/essentials/craftbukkit/FakeWorld.java +++ b/Essentials/src/com/earth2me/essentials/craftbukkit/FakeWorld.java @@ -577,6 +577,11 @@ public class FakeWorld implements World { throw new UnsupportedOperationException("Not supported yet."); } + @Override + public void playSound(Location location, String s, float v, float v1) { + throw new UnsupportedOperationException("Not supported yet."); + } + @Override public int getAmbientSpawnLimit() { throw new UnsupportedOperationException("Not supported yet."); diff --git a/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java b/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java index 4793f02aa..1bc1e64a4 100644 --- a/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java +++ b/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java @@ -2,7 +2,6 @@ package com.earth2me.essentials.craftbukkit; import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; @@ -32,25 +31,37 @@ public final class InventoryWorkaround { return -1; } - + /* + Spigot 1.9, for whatever reason, decided to merge the armor and main player inventories without providing a way + to access the main inventory. There's lots of ugly code in here to work around that. + */ private static final int USABLE_PLAYER_INV_SIZE = 36; - // Returns what it couldnt store + + private static boolean isCombinedInventory(Inventory inventory) { + return inventory instanceof PlayerInventory && inventory.getContents().length > USABLE_PLAYER_INV_SIZE; + } + + private static Inventory makeTruncatedPlayerInventory(PlayerInventory playerInventory) { + Inventory fakeInventory = Bukkit.getServer().createInventory(null, USABLE_PLAYER_INV_SIZE); + + ItemStack[] truncatedContents = new ItemStack[fakeInventory.getSize()]; + System.arraycopy(playerInventory.getContents(), 0, truncatedContents, 0, truncatedContents.length); + fakeInventory.setContents(truncatedContents); + + return fakeInventory; + } + + // Returns what it couldn't store // This will will abort if it couldn't store all items public static Map addAllItems(final Inventory inventory, final ItemStack... items) { ItemStack[] contents = inventory.getContents(); final Inventory fakeInventory; - if (inventory.getType() == InventoryType.PLAYER && contents.length > USABLE_PLAYER_INV_SIZE) { - fakeInventory = Bukkit.getServer().createInventory(null, USABLE_PLAYER_INV_SIZE); + if (isCombinedInventory(inventory)) { + fakeInventory = makeTruncatedPlayerInventory((PlayerInventory) inventory); } else { fakeInventory = Bukkit.getServer().createInventory(null, inventory.getType()); - } - try { fakeInventory.setContents(contents); - } catch (IllegalArgumentException e) { - ItemStack[] truncatedContents = new ItemStack[fakeInventory.getSize()]; - System.arraycopy(contents, 0, truncatedContents, 0, truncatedContents.length); - fakeInventory.setContents(truncatedContents); } Map overFlow = addItems(fakeInventory, items); if (overFlow.isEmpty()) { @@ -60,15 +71,24 @@ public final class InventoryWorkaround { return addItems(fakeInventory, items); } - // Returns what it couldnt store + // Returns what it couldn't store public static Map addItems(final Inventory inventory, final ItemStack... items) { - return addOversizedItems(inventory, 0, items); + return addOversizedItems(inventory, 0, items); } - // Returns what it couldnt store + // Returns what it couldn't store // Set oversizedStack to below normal stack size to disable oversized stacks public static Map addOversizedItems(final Inventory inventory, final int oversizedStacks, final ItemStack... items) { - final Map leftover = new HashMap(); + if (isCombinedInventory(inventory)) { + Inventory fakeInventory = makeTruncatedPlayerInventory((PlayerInventory) inventory); + Map overflow = addOversizedItems(fakeInventory, oversizedStacks, items); + for (int i = 0; i < fakeInventory.getContents().length; i++) { + inventory.setItem(i, fakeInventory.getContents()[i]); + } + return overflow; + } + + final Map leftover = new HashMap<>(); /* * TODO: some optimization - Create a 'firstPartial' with a 'fromIndex' - Record the lastPartial per Material -