Skip to content

Commit f2fd443

Browse files
authoredJun 30, 2021
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.
1 parent 72927b7 commit f2fd443

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed
 

Diff for: ‎src/inventory.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -938,19 +938,16 @@ void Inventory::deSerialize(std::istream &is)
938938
InventoryList * Inventory::addList(const std::string &name, u32 size)
939939
{
940940
setModified();
941+
942+
// Remove existing lists
941943
s32 i = getListIndex(name);
942-
if(i != -1)
943-
{
944-
if(m_lists[i]->getSize() != size)
945-
{
946-
delete m_lists[i];
947-
m_lists[i] = new InventoryList(name, size, m_itemdef);
948-
m_lists[i]->setModified();
949-
}
944+
if (i != -1) {
945+
delete m_lists[i];
946+
m_lists[i] = new InventoryList(name, size, m_itemdef);
947+
m_lists[i]->setModified();
950948
return m_lists[i];
951949
}
952950

953-
954951
//don't create list with invalid name
955952
if (name.find(' ') != std::string::npos)
956953
return nullptr;

Diff for: ‎src/inventory.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class Inventory
298298
void serialize(std::ostream &os, bool incremental = false) const;
299299
void deSerialize(std::istream &is);
300300

301-
// Adds a new list or clears and resizes an existing one
301+
// Creates a new list if none exists or truncates existing lists
302302
InventoryList * addList(const std::string &name, u32 size);
303303
InventoryList * getList(const std::string &name);
304304
const InventoryList * getList(const std::string &name) const;

Diff for: ‎src/script/common/c_content.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1359,9 +1359,9 @@ void read_inventory_list(lua_State *L, int tableindex,
13591359

13601360
// Get Lua-specified items to insert into the list
13611361
std::vector<ItemStack> items = read_items(L, tableindex,srv);
1362-
size_t listsize = (forcesize > 0) ? forcesize : items.size();
1362+
size_t listsize = (forcesize >= 0) ? forcesize : items.size();
13631363

1364-
// Create or clear list
1364+
// Create or resize/clear list
13651365
InventoryList *invlist = inv->addList(name, listsize);
13661366
if (!invlist) {
13671367
luaL_error(L, "inventory list: cannot create list named '%s'", name);

0 commit comments

Comments
 (0)
Please sign in to comment.