diff options
author | Jesse McDonald <nybble41@gmail.com> | 2017-05-18 22:56:49 -0500 |
---|---|---|
committer | paramat <mat.gregory@virginmedia.com> | 2017-06-21 01:53:57 +0100 |
commit | e6a9e6066afc369f01c046de8e3a90a4b042286c (patch) | |
tree | f5eb25befff519e77a9323ed65e5bb3803e03e78 /src | |
parent | 16938adfc00b3c56a7b08a9eb97160b68902bae6 (diff) | |
download | minetest-e6a9e6066afc369f01c046de8e3a90a4b042286c.tar.gz minetest-e6a9e6066afc369f01c046de8e3a90a4b042286c.tar.bz2 minetest-e6a9e6066afc369f01c046de8e3a90a4b042286c.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/inventory.cpp | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/inventory.cpp b/src/inventory.cpp index 5ce82737a..24eebba80 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -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 @@ -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 @@ -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); |