]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/inventory.cpp
Clean up rollback
[dragonfireclient.git] / src / inventory.cpp
index fda11e40fde5e5904bc5cb42ae4d1b0f104af1c2..3fa31965c5bf65ae28c51b8eb4bc76da4cf61002 100644 (file)
@@ -1,6 +1,6 @@
 /*
-Minetest-c55
-Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
@@ -175,7 +175,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
                // Convert old materials
                if(material <= 0xff)
                        material = content_translate_from_19_to_internal(material);
-               if(material > MAX_CONTENT)
+               if(material > 0xfff)
                        throw SerializationError("Too large material number");
                // Convert old id to name
                NameIdMapping legacy_nimap;
@@ -183,7 +183,8 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
                legacy_nimap.getName(material, name);
                if(name == "")
                        name = "unknown_block";
-               name = itemdef->getAlias(name);
+               if (itemdef)
+                       name = itemdef->getAlias(name);
                count = materialcount;
        }
        else if(name == "MaterialItem2")
@@ -194,7 +195,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
                is>>material;
                u16 materialcount;
                is>>materialcount;
-               if(material > MAX_CONTENT)
+               if(material > 0xfff)
                        throw SerializationError("Too large material number");
                // Convert old id to name
                NameIdMapping legacy_nimap;
@@ -202,7 +203,8 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
                legacy_nimap.getName(material, name);
                if(name == "")
                        name = "unknown_block";
-               name = itemdef->getAlias(name);
+               if (itemdef)
+                       name = itemdef->getAlias(name);
                count = materialcount;
        }
        else if(name == "node" || name == "NodeItem" || name == "MaterialItem3"
@@ -223,7 +225,8 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
                        name = fnd.next(" ");
                }
                fnd.skip_over(" ");
-               name = itemdef->getAlias(name);
+               if (itemdef)
+                       name = itemdef->getAlias(name);
                count = stoi(trim(fnd.next("")));
                if(count == 0)
                        count = 1;
@@ -252,7 +255,8 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
                count = 1;
                // Then read wear
                fnd.skip_over(" ");
-               name = itemdef->getAlias(name);
+               if (itemdef)
+                       name = itemdef->getAlias(name);
                wear = stoi(trim(fnd.next("")));
        }
        else
@@ -262,7 +266,8 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
                        // The real thing
 
                        // Apply item aliases
-                       name = itemdef->getAlias(name);
+                       if (itemdef)
+                               name = itemdef->getAlias(name);
 
                        // Read the count
                        std::string count_str;
@@ -294,9 +299,9 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
                } while(false);
        }
 
-       if(name.empty() || count == 0)
+       if (name.empty() || count == 0)
                clear();
-       else if(itemdef->get(name).type == ITEM_TOOL)
+       else if (itemdef && itemdef->get(name).type == ITEM_TOOL)
                count = 1;
 }
 
@@ -308,12 +313,12 @@ void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef)
 
 std::string ItemStack::getItemString() const
 {
-       // Get item string
        std::ostringstream os(std::ios::binary);
        serialize(os);
        return os.str();
 }
 
+
 ItemStack ItemStack::addItem(const ItemStack &newitem_,
                IItemDefManager *itemdef)
 {
@@ -542,10 +547,6 @@ void InventoryList::deSerialize(std::istream &is)
                                throw SerializationError("too many items");
                        m_items[item_i++].clear();
                }
-               else
-               {
-                       throw SerializationError("Unknown inventory identifier");
-               }
        }
 }
 
@@ -566,6 +567,26 @@ InventoryList & InventoryList::operator = (const InventoryList &other)
        return *this;
 }
 
+bool InventoryList::operator == (const InventoryList &other) const
+{
+       if(m_size != other.m_size)
+               return false;
+       if(m_width != other.m_width)
+               return false;
+       if(m_name != other.m_name)
+               return false;
+       for(u32 i=0; i<m_items.size(); i++)
+       {
+               ItemStack s1 = m_items[i];
+               ItemStack s2 = other.m_items[i];
+               if(s1.name != s2.name || s1.wear!= s2.wear || s1.count != s2.count ||
+                               s1.metadata != s2.metadata)
+                       return false;
+       }
+
+       return true;
+}
+
 const std::string &InventoryList::getName() const
 {
        return m_name;
@@ -815,6 +836,7 @@ Inventory::~Inventory()
 
 void Inventory::clear()
 {
+       m_dirty = true;
        for(u32 i=0; i<m_lists.size(); i++)
        {
                delete m_lists[i];
@@ -824,6 +846,7 @@ void Inventory::clear()
 
 void Inventory::clearContents()
 {
+       m_dirty = true;
        for(u32 i=0; i<m_lists.size(); i++)
        {
                InventoryList *list = m_lists[i];
@@ -836,12 +859,14 @@ void Inventory::clearContents()
 
 Inventory::Inventory(IItemDefManager *itemdef)
 {
+       m_dirty = false;
        m_itemdef = itemdef;
 }
 
 Inventory::Inventory(const Inventory &other)
 {
        *this = other;
+       m_dirty = false;
 }
 
 Inventory & Inventory::operator = (const Inventory &other)
@@ -849,6 +874,7 @@ Inventory & Inventory::operator = (const Inventory &other)
        // Gracefully handle self assignment
        if(this != &other)
        {
+               m_dirty = true;
                clear();
                m_itemdef = other.m_itemdef;
                for(u32 i=0; i<other.m_lists.size(); i++)
@@ -859,6 +885,19 @@ Inventory & Inventory::operator = (const Inventory &other)
        return *this;
 }
 
+bool Inventory::operator == (const Inventory &other) const
+{
+       if(m_lists.size() != other.m_lists.size())
+               return false;
+
+       for(u32 i=0; i<m_lists.size(); i++)
+       {
+               if(*m_lists[i] != *other.m_lists[i])
+                       return false;
+       }
+       return true;
+}
+
 void Inventory::serialize(std::ostream &os) const
 {
        for(u32 i=0; i<m_lists.size(); i++)
@@ -909,13 +948,14 @@ void Inventory::deSerialize(std::istream &is)
                }
                else
                {
-                       throw SerializationError("Unknown inventory identifier");
+                       throw SerializationError("invalid inventory specifier: " + name);
                }
        }
 }
 
 InventoryList * Inventory::addList(const std::string &name, u32 size)
 {
+       m_dirty = true;
        s32 i = getListIndex(name);
        if(i != -1)
        {
@@ -928,6 +968,9 @@ InventoryList * Inventory::addList(const std::string &name, u32 size)
        }
        else
        {
+               //don't create list with invalid name
+               if (name.find(" ") != std::string::npos) return NULL;
+
                InventoryList *list = new InventoryList(name, size, m_itemdef);
                m_lists.push_back(list);
                return list;
@@ -958,6 +1001,7 @@ bool Inventory::deleteList(const std::string &name)
        s32 i = getListIndex(name);
        if(i == -1)
                return false;
+       m_dirty = true;
        delete m_lists[i];
        m_lists.erase(m_lists.begin() + i);
        return true;