Skip to content

Commit

Permalink
Inventory: Make addItem for empty ItemStacks respect max stack size
Browse files Browse the repository at this point in the history
When adding items to an empty ItemStack, limit the number of items taken
based on the maximum stack size in the item description.
Likewise, when checking whether items will fit into an empty ItemStack,
only absorb as many items as are allowed in a single stack and return the rest.
  • Loading branch information
nybble41 authored and paramat committed Jun 21, 2017
1 parent 16938ad commit e6a9e60
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/inventory.cpp
Expand Up @@ -267,8 +267,17 @@ ItemStack ItemStack::addItem(const ItemStack &newitem_,
// If this is an empty item, it's an easy job.
else if(empty())
{
const u16 stackMax = getStackMax(itemdef);

*this = newitem;
newitem.clear();

// If the item fits fully, delete it
if (count <= stackMax) {
newitem.clear();
} else { // Else the item does not fit fully. Return the rest.
count = stackMax;
newitem.remove(count);
}
}
// If item name or metadata differs, bail out
else if (name != newitem.name
Expand Down Expand Up @@ -308,7 +317,14 @@ bool ItemStack::itemFits(const ItemStack &newitem_,
// If this is an empty item, it's an easy job.
else if(empty())
{
newitem.clear();
const u16 stackMax = getStackMax(itemdef);

// If the item fits fully, delete it
if (newitem.count <= stackMax) {
newitem.clear();
} else { // Else the item does not fit fully. Return the rest.
newitem.remove(stackMax);
}
}
// If item name or metadata differs, bail out
else if (name != newitem.name
Expand All @@ -322,7 +338,6 @@ bool ItemStack::itemFits(const ItemStack &newitem_,
newitem.clear();
}
// Else the item does not fit fully. Return the rest.
// the rest.
else
{
u16 freespace = freeSpace(itemdef);
Expand Down

0 comments on commit e6a9e60

Please sign in to comment.