Modernize client code (#6250)
[oweals/minetest.git] / src / inventory.cpp
index 5ce82737a940ae52d402940e7b10bf81839962f0..4da380a249b06f58700b3b895b1080c63093c851 100644 (file)
@@ -254,11 +254,8 @@ std::string ItemStack::getItemString() const
 }
 
 
-ItemStack ItemStack::addItem(const ItemStack &newitem_,
-               IItemDefManager *itemdef)
+ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef)
 {
-       ItemStack newitem = newitem_;
-
        // If the item is empty or the position invalid, bail out
        if(newitem.empty())
        {
@@ -267,8 +264,17 @@ ItemStack ItemStack::addItem(const ItemStack &newitem_,
        // If this is an empty item, it's an easy job.
        else if(empty())
        {
+               const u16 stackMax = newitem.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
@@ -294,11 +300,10 @@ ItemStack ItemStack::addItem(const ItemStack &newitem_,
        return newitem;
 }
 
-bool ItemStack::itemFits(const ItemStack &newitem_,
+bool ItemStack::itemFits(ItemStack newitem,
                ItemStack *restitem,
                IItemDefManager *itemdef) const
 {
-       ItemStack newitem = newitem_;
 
        // If the item is empty or the position invalid, bail out
        if(newitem.empty())
@@ -308,7 +313,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 = newitem.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 +334,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);