Fix EntityPickupItemEvent getRemaining / PlayerInventory#canHold

Without this change the calculation of fitting items was incorrect, so event.getRemaining() did return 0 when it should not be 0.
This commit is contained in:
Brokkonaut 2019-02-12 04:49:24 +01:00 committed by md_5
parent a2dd0e3035
commit d158f2e993
2 changed files with 64 additions and 5 deletions

View file

@ -66,7 +66,7 @@
public PlayerInventory(EntityHuman entityhuman) {
this.items = NonNullList.a(36, ItemStack.a);
this.armor = NonNullList.a(4, ItemStack.a);
@@ -42,6 +94,23 @@
@@ -42,6 +94,22 @@
return itemstack.getItem() == itemstack1.getItem() && ItemStack.equals(itemstack, itemstack1);
}
@ -77,8 +77,7 @@
+ ItemStack itemstack1 = this.getItem(i);
+ if (itemstack1.isEmpty()) return itemstack.getCount();
+
+ // Taken from firstPartial(ItemStack)
+ if (!this.a(itemstack, itemstack1)) {
+ if (this.a(itemstack1, itemstack)) { // PAIL rename isSimilarAndNotFull
+ remains -= (itemstack1.getMaxStackSize() < this.getMaxStackSize() ? itemstack1.getMaxStackSize() : this.getMaxStackSize()) - itemstack1.getCount();
+ }
+ if (remains <= 0) return itemstack.getCount();
@ -90,7 +89,7 @@
public int getFirstEmptySlotIndex() {
for (int i = 0; i < this.items.size(); ++i) {
if (((ItemStack) this.items.get(i)).isEmpty()) {
@@ -502,7 +571,7 @@
@@ -502,7 +570,7 @@
}
public int getMaxStackSize() {
@ -99,7 +98,7 @@
}
public boolean b(IBlockData iblockdata) {
@@ -554,6 +623,11 @@
@@ -554,6 +622,11 @@
}
public ItemStack getCarried() {

View file

@ -0,0 +1,60 @@
package org.bukkit.craftbukkit.inventory;
import net.minecraft.server.ItemStack;
import net.minecraft.server.Items;
import net.minecraft.server.PlayerInventory;
import org.bukkit.support.AbstractTestingBase;
import org.junit.Test;
import static org.junit.Assert.*;
public class PlayerInventoryTest extends AbstractTestingBase {
@Test
public void testCanHold() throws Exception {
ItemStack itemStackApple = new ItemStack(Items.APPLE);
ItemStack itemStack1Coal = new ItemStack(Items.COAL);
ItemStack itemStack32Coal = new ItemStack(Items.COAL, 32);
ItemStack itemStack63Coal = new ItemStack(Items.COAL, 63);
ItemStack itemStack64Coal = new ItemStack(Items.COAL, 64);
// keep one slot empty
PlayerInventory inventory = new PlayerInventory(null);
for (int i = 0; i < inventory.items.size() - 1; i++) {
inventory.setItem(i, itemStackApple);
}
// one slot empty
assertEquals(1, inventory.canHold(itemStack1Coal));
assertEquals(32, inventory.canHold(itemStack32Coal));
assertEquals(64, inventory.canHold(itemStack64Coal));
// no free space with a stack of the item to check in the inventory
inventory.setItem(inventory.items.size() - 1, itemStack64Coal);
assertEquals(0, inventory.canHold(itemStack1Coal));
assertEquals(0, inventory.canHold(itemStack32Coal));
assertEquals(0, inventory.canHold(itemStack64Coal));
// no free space without a stack of the item to check in the inventory
inventory.setItem(inventory.items.size() - 1, itemStackApple);
assertEquals(0, inventory.canHold(itemStack1Coal));
assertEquals(0, inventory.canHold(itemStack32Coal));
assertEquals(0, inventory.canHold(itemStack64Coal));
// free space for 32 items in one slot
inventory.setItem(inventory.items.size() - 1, itemStack32Coal);
assertEquals(1, inventory.canHold(itemStack1Coal));
assertEquals(32, inventory.canHold(itemStack32Coal));
assertEquals(32, inventory.canHold(itemStack64Coal));
// free space for 1 item in two slots
inventory.setItem(inventory.items.size() - 1, itemStack63Coal);
inventory.setItem(inventory.items.size() - 2, itemStack63Coal);
assertEquals(1, inventory.canHold(itemStack1Coal));
assertEquals(2, inventory.canHold(itemStack32Coal));
assertEquals(2, inventory.canHold(itemStack64Coal));
}
}