Skip to content

Commit e6a9e60

Browse files
nybble41paramat
authored andcommittedJun 21, 2017
Inventory: Make addItem for empty ItemStacks respect max stack size
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.
1 parent 16938ad commit e6a9e60

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed
 

‎src/inventory.cpp

+18-3
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,17 @@ ItemStack ItemStack::addItem(const ItemStack &newitem_,
267267
// If this is an empty item, it's an easy job.
268268
else if(empty())
269269
{
270+
const u16 stackMax = getStackMax(itemdef);
271+
270272
*this = newitem;
271-
newitem.clear();
273+
274+
// If the item fits fully, delete it
275+
if (count <= stackMax) {
276+
newitem.clear();
277+
} else { // Else the item does not fit fully. Return the rest.
278+
count = stackMax;
279+
newitem.remove(count);
280+
}
272281
}
273282
// If item name or metadata differs, bail out
274283
else if (name != newitem.name
@@ -308,7 +317,14 @@ bool ItemStack::itemFits(const ItemStack &newitem_,
308317
// If this is an empty item, it's an easy job.
309318
else if(empty())
310319
{
311-
newitem.clear();
320+
const u16 stackMax = getStackMax(itemdef);
321+
322+
// If the item fits fully, delete it
323+
if (newitem.count <= stackMax) {
324+
newitem.clear();
325+
} else { // Else the item does not fit fully. Return the rest.
326+
newitem.remove(stackMax);
327+
}
312328
}
313329
// If item name or metadata differs, bail out
314330
else if (name != newitem.name
@@ -322,7 +338,6 @@ bool ItemStack::itemFits(const ItemStack &newitem_,
322338
newitem.clear();
323339
}
324340
// Else the item does not fit fully. Return the rest.
325-
// the rest.
326341
else
327342
{
328343
u16 freespace = freeSpace(itemdef);

0 commit comments

Comments
 (0)
Please sign in to comment.