diff options
author | SmallJoker <SmallJoker@users.noreply.github.com> | 2021-06-20 17:21:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-20 17:21:50 +0200 |
commit | 2db6b07de1e45c56f6135363939dbb3781336514 (patch) | |
tree | 58ca691aee214dad63af5359a3b59c0e743189d0 | |
parent | b10091be9b6b6c74a170b9444f856f83dd3fe952 (diff) | |
download | minetest-2db6b07de1e45c56f6135363939dbb3781336514.tar.gz minetest-2db6b07de1e45c56f6135363939dbb3781336514.tar.bz2 minetest-2db6b07de1e45c56f6135363939dbb3781336514.zip |
Inventory: show error on invalid list names (#11368)
-rw-r--r-- | src/inventory.h | 1 | ||||
-rw-r--r-- | src/script/common/c_content.cpp | 26 |
2 files changed, 15 insertions, 12 deletions
diff --git a/src/inventory.h b/src/inventory.h index f36bc57cf..fbf995fab 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -298,6 +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 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 52baeae9d..f8cc40927 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1350,26 +1350,28 @@ void read_inventory_list(lua_State *L, int tableindex, { if(tableindex < 0) tableindex = lua_gettop(L) + 1 + tableindex; + // If nil, delete list if(lua_isnil(L, tableindex)){ inv->deleteList(name); return; } - // Otherwise set list + + // Get Lua-specified items to insert into the list std::vector<ItemStack> items = read_items(L, tableindex,srv); - int listsize = (forcesize != -1) ? forcesize : items.size(); + size_t listsize = (forcesize > 0) ? forcesize : items.size(); + + // Create or clear list InventoryList *invlist = inv->addList(name, listsize); - int index = 0; - for(std::vector<ItemStack>::const_iterator - i = items.begin(); i != items.end(); ++i){ - if(forcesize != -1 && index == forcesize) - break; - invlist->changeItem(index, *i); - index++; + if (!invlist) { + luaL_error(L, "inventory list: cannot create list named '%s'", name); + return; } - while(forcesize != -1 && index < forcesize){ - invlist->deleteItem(index); - index++; + + for (size_t i = 0; i < items.size(); ++i) { + if (i == listsize) + break; // Truncate provided list of items + invlist->changeItem(i, items[i]); } } |