}
-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())
{
// 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
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())
// 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
newitem.clear();
}
// Else the item does not fit fully. Return the rest.
- // the rest.
else
{
u16 freespace = freeSpace(itemdef);
InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef):
m_name(name),
m_size(size),
- m_width(0),
m_itemdef(itemdef)
{
clearItems();
return false;
}
-bool InventoryList::containsItem(const ItemStack &item) const
+bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
{
u32 count = item.count;
if(count == 0)
{
if(count == 0)
break;
- if(i->name == item.name)
- {
- if(i->count >= count)
+ if (i->name == item.name
+ && (!match_meta || (i->metadata == item.metadata))) {
+ if (i->count >= count)
return true;
else
count -= i->count;