Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ index 6d3e3ec045d5b15a435f7217369968b33e082724..b7a3758af337270737041f84d10eb437
int i = this.inventory.findSlotMatchingCraftingIngredient(item, item1);
if (i == -1) {
diff --git a/net/minecraft/world/entity/player/Inventory.java b/net/minecraft/world/entity/player/Inventory.java
index 76eda3bd8c513c7e42ceedf1aa605fb6df8b7019..41e59f3739945ca7f6ab710c993b5c0f15fcd529 100644
index ae5bce250e5942de8cb08c397efe1b012afdd2a2..e48a80b3b5b6d0098833cd4e0ae3fef75b442ff2 100644
--- a/net/minecraft/world/entity/player/Inventory.java
+++ b/net/minecraft/world/entity/player/Inventory.java
@@ -252,12 +252,12 @@ public class Inventory implements Container, Nameable {
@@ -264,12 +264,12 @@ public class Inventory implements Container, Nameable {
return !stack.isDamaged() && !stack.isEnchanted() && !stack.has(DataComponents.CUSTOM_NAME);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@

public Inventory(Player player, EntityEquipment equipment) {
this.player = player;
@@ -93,10 +_,39 @@
@@ -93,10 +_,51 @@

private boolean hasRemainingSpaceForItem(ItemStack destination, ItemStack origin) {
return !destination.isEmpty()
Expand All @@ -96,24 +96,36 @@
+ // CraftBukkit start - Watch method above! :D
+ public int canHold(ItemStack itemStack) {
+ int remains = itemStack.getCount();
+ int maxStackSize = Math.min(this.getMaxStackSize(), itemStack.getMaxStackSize());
+
+ for (int slot = 0; slot < this.items.size(); ++slot) {
+ ItemStack itemInSlot = this.getItem(slot);
+ if (itemInSlot.isEmpty()) {
+ return itemStack.getCount();
+ }
+ remains -= maxStackSize;
+ } else if (this.hasRemainingSpaceForItem(itemInSlot, itemStack)) {
+ int currentItemMaxStack = Math.min(this.getMaxStackSize(), itemInSlot.getMaxStackSize());
+ int space = currentItemMaxStack - itemInSlot.getCount();
+
+ if (this.hasRemainingSpaceForItem(itemInSlot, itemStack)) {
+ remains -= (itemInSlot.getMaxStackSize() < this.getMaxStackSize() ? itemInSlot.getMaxStackSize() : this.getMaxStackSize()) - itemInSlot.getCount();
+ if (space > 0) {
+ remains -= space;
+ }
+ }
+
+ if (remains <= 0) {
+ return itemStack.getCount();
+ }
+ }
+
+ ItemStack itemInOffhand = this.equipment.get(EquipmentSlot.OFFHAND);
+ if (this.hasRemainingSpaceForItem(itemInOffhand, itemStack)) {
+ remains -= (itemInOffhand.getMaxStackSize() < this.getMaxStackSize() ? itemInOffhand.getMaxStackSize() : this.getMaxStackSize()) - itemInOffhand.getCount();
+ int offHandMaxStack = Math.min(this.getMaxStackSize(), itemInOffhand.getMaxStackSize());
+ int space = offHandMaxStack - itemInOffhand.getCount();
+
+ if (space > 0) {
+ remains -= space;
+ }
+ }
+
+ if (remains <= 0) {
+ return itemStack.getCount();
+ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void testCanHold() throws Exception {
ItemStack itemStack32Coal = new ItemStack(Items.COAL, 32);
ItemStack itemStack63Coal = new ItemStack(Items.COAL, 63);
ItemStack itemStack64Coal = new ItemStack(Items.COAL, 64);
ItemStack itemStack80Coal = new ItemStack(Items.COAL, 80); // Overstacked item

// keep one slot empty
Inventory inventory = new Inventory(null, new PlayerEquipment(null));
Expand All @@ -30,13 +31,15 @@ public void testCanHold() throws Exception {
assertEquals(1, inventory.canHold(itemStack1Coal));
assertEquals(32, inventory.canHold(itemStack32Coal));
assertEquals(64, inventory.canHold(itemStack64Coal));
assertEquals(64, inventory.canHold(itemStack80Coal)); // Should only fit 64 items in 1 slot

// no free space with a stack of the item to check in the inventory
inventory.setItem(inventory.getNonEquipmentItems().size() - 1, itemStack64Coal);

assertEquals(0, inventory.canHold(itemStack1Coal));
assertEquals(0, inventory.canHold(itemStack32Coal));
assertEquals(0, inventory.canHold(itemStack64Coal));
assertEquals(0, inventory.canHold(itemStack80Coal));

// no free space without a stack of the item to check in the inventory
inventory.setItem(inventory.getNonEquipmentItems().size() - 1, itemStackApple);
Expand All @@ -51,6 +54,7 @@ public void testCanHold() throws Exception {
assertEquals(1, inventory.canHold(itemStack1Coal));
assertEquals(32, inventory.canHold(itemStack32Coal));
assertEquals(32, inventory.canHold(itemStack64Coal));
assertEquals(32, inventory.canHold(itemStack80Coal));

// free space for 1 item in two slots
inventory.setItem(inventory.getNonEquipmentItems().size() - 1, itemStack63Coal);
Expand All @@ -76,5 +80,13 @@ public void testCanHold() throws Exception {
assertEquals(1, inventory.canHold(itemStack1Coal));
assertEquals(2, inventory.canHold(itemStack32Coal));
assertEquals(2, inventory.canHold(itemStack64Coal));

// two empty slots
inventory.setItem(inventory.getNonEquipmentItems().size() - 1, ItemStack.EMPTY);
inventory.setItem(inventory.getNonEquipmentItems().size() - 2, ItemStack.EMPTY);
inventory.setItem(inventory.getNonEquipmentItems().size() + inventory.getArmorContents().size(), ItemStack.EMPTY);

assertEquals(80, inventory.canHold(itemStack80Coal));
assertEquals(128, inventory.canHold(new ItemStack(Items.COAL, 130)));
}
}
Loading