From b7db6fba12691dee11ab8f83357ef7f0f6b0d93c Mon Sep 17 00:00:00 2001 From: PurkkaKoodari Date: Thu, 6 Jun 2019 01:32:32 +0300 Subject: [PATCH] SPIGOT-2000: Picking up items to shield slot working inconsistently when inventory is full --- nms-patches/EntityItem.patch | 3 +++ nms-patches/PlayerInventory.patch | 6 ++++++ .../inventory/PlayerInventoryTest.java | 17 +++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/nms-patches/EntityItem.patch b/nms-patches/EntityItem.patch index 38f79d4cba..5c30c97fa6 100644 --- a/nms-patches/EntityItem.patch +++ b/nms-patches/EntityItem.patch @@ -127,6 +127,9 @@ + + // Possibly < 0; fix here so we do not have to modify code below + this.pickupDelay = 0; ++ } else if (this.pickupDelay == 0) { ++ // ensure that the code below isn't triggered if canHold says we can't pick the items up ++ this.pickupDelay = -1; + } + // CraftBukkit end + diff --git a/nms-patches/PlayerInventory.patch b/nms-patches/PlayerInventory.patch index 0b90c81a58..b5c6b2b7ec 100644 --- a/nms-patches/PlayerInventory.patch +++ b/nms-patches/PlayerInventory.patch @@ -85,6 +85,12 @@ + } + if (remains <= 0) return itemstack.getCount(); + } ++ ItemStack offhandItemStack = this.getItem(this.items.size() + this.armor.size()); ++ if (this.a(offhandItemStack, itemstack)) { ++ remains -= (offhandItemStack.getMaxStackSize() < this.getMaxStackSize() ? offhandItemStack.getMaxStackSize() : this.getMaxStackSize()) - offhandItemStack.getCount(); ++ } ++ if (remains <= 0) return itemstack.getCount(); ++ + return itemstack.getCount() - remains; + } + // CraftBukkit end diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/PlayerInventoryTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/PlayerInventoryTest.java index 806f879579..6f56e56afb 100644 --- a/src/test/java/org/bukkit/craftbukkit/inventory/PlayerInventoryTest.java +++ b/src/test/java/org/bukkit/craftbukkit/inventory/PlayerInventoryTest.java @@ -56,5 +56,22 @@ public class PlayerInventoryTest extends AbstractTestingBase { assertEquals(1, inventory.canHold(itemStack1Coal)); assertEquals(2, inventory.canHold(itemStack32Coal)); assertEquals(2, inventory.canHold(itemStack64Coal)); + + // free space for 32 items in non-empty off-hand slot + inventory.setItem(inventory.items.size() - 1, itemStackApple); + inventory.setItem(inventory.items.size() - 2, itemStackApple); + inventory.setItem(inventory.items.size() + inventory.armor.size(), itemStack32Coal); + + assertEquals(1, inventory.canHold(itemStack1Coal)); + assertEquals(32, inventory.canHold(itemStack32Coal)); + assertEquals(32, inventory.canHold(itemStack64Coal)); + + // free space for 1 item in non-empty off-hand slot and another slot + inventory.setItem(inventory.items.size() - 1, itemStack63Coal); + inventory.setItem(inventory.items.size() + inventory.armor.size(), itemStack63Coal); + + assertEquals(1, inventory.canHold(itemStack1Coal)); + assertEquals(2, inventory.canHold(itemStack32Coal)); + assertEquals(2, inventory.canHold(itemStack64Coal)); } }