]> git.lizzy.rs Git - minetest.git/commitdiff
Inventory: Make addList() consistent (#11382)
authorSmallJoker <SmallJoker@users.noreply.github.com>
Wed, 30 Jun 2021 18:39:38 +0000 (20:39 +0200)
committerGitHub <noreply@github.com>
Wed, 30 Jun 2021 18:39:38 +0000 (20:39 +0200)
Fixes list clearing for inv:set_list() using same size, since 2db6b07.
addList() now clears the list in all cases. Use setSize() to resize without clearing.

src/inventory.cpp
src/inventory.h
src/script/common/c_content.cpp

index fc1aaf371aa3d5dafb39865b3b3c0015a1c5d16d..b3bed623a9bb5ccacd2bc2cfe340556b1fac0f07 100644 (file)
@@ -938,19 +938,16 @@ void Inventory::deSerialize(std::istream &is)
 InventoryList * Inventory::addList(const std::string &name, u32 size)
 {
        setModified();
+
+       // Remove existing lists
        s32 i = getListIndex(name);
-       if(i != -1)
-       {
-               if(m_lists[i]->getSize() != size)
-               {
-                       delete m_lists[i];
-                       m_lists[i] = new InventoryList(name, size, m_itemdef);
-                       m_lists[i]->setModified();
-               }
+       if (i != -1) {
+               delete m_lists[i];
+               m_lists[i] = new InventoryList(name, size, m_itemdef);
+               m_lists[i]->setModified();
                return m_lists[i];
        }
 
-
        //don't create list with invalid name
        if (name.find(' ') != std::string::npos)
                return nullptr;
index fbf995fab3af3727461322ba52077177907b35f5..6c84f5fd12ac7f734cfed949397ae7efbf56561d 100644 (file)
@@ -298,7 +298,7 @@ class Inventory
        void serialize(std::ostream &os, bool incremental = false) const;
        void deSerialize(std::istream &is);
 
-       // Adds a new list or clears and resizes an existing one
+       // Creates a new list if none exists or truncates existing lists
        InventoryList * addList(const std::string &name, u32 size);
        InventoryList * getList(const std::string &name);
        const InventoryList * getList(const std::string &name) const;
index f8cc4092735f491adf74a180def3fa27df880abb..a0b45982a3de27e944cb76b5827d2f4dc2b821cd 100644 (file)
@@ -1359,9 +1359,9 @@ void read_inventory_list(lua_State *L, int tableindex,
 
        // Get Lua-specified items to insert into the list
        std::vector<ItemStack> items = read_items(L, tableindex,srv);
-       size_t listsize = (forcesize > 0) ? forcesize : items.size();
+       size_t listsize = (forcesize >= 0) ? forcesize : items.size();
 
-       // Create or clear list
+       // Create or resize/clear list
        InventoryList *invlist = inv->addList(name, listsize);
        if (!invlist) {
                luaL_error(L, "inventory list: cannot create list named '%s'", name);