aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSmallJoker <SmallJoker@users.noreply.github.com>2021-06-30 20:39:38 +0200
committerGitHub <noreply@github.com>2021-06-30 20:39:38 +0200
commitf2fd4432625ee5cf0380bdd006cd1f15d053b12f (patch)
treec86099e50c53442900ea034342f9e50f9a87c603 /src
parent72927b73ca857acd4dc5a40a96be220d090436f3 (diff)
downloadminetest-f2fd4432625ee5cf0380bdd006cd1f15d053b12f.tar.gz
minetest-f2fd4432625ee5cf0380bdd006cd1f15d053b12f.tar.bz2
minetest-f2fd4432625ee5cf0380bdd006cd1f15d053b12f.zip
Inventory: Make addList() consistent (#11382)
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.
Diffstat (limited to 'src')
-rw-r--r--src/inventory.cpp15
-rw-r--r--src/inventory.h2
-rw-r--r--src/script/common/c_content.cpp4
3 files changed, 9 insertions, 12 deletions
diff --git a/src/inventory.cpp b/src/inventory.cpp
index fc1aaf371..b3bed623a 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -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;
diff --git a/src/inventory.h b/src/inventory.h
index fbf995fab..6c84f5fd1 100644
--- a/src/inventory.h
+++ b/src/inventory.h
@@ -298,7 +298,7 @@ public:
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;
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp
index f8cc40927..a0b45982a 100644
--- a/src/script/common/c_content.cpp
+++ b/src/script/common/c_content.cpp
@@ -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);