Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvRef: Add convenience method add_list for bulk transfer of items #8466

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/script/common/c_content.cpp
Expand Up @@ -1276,19 +1276,25 @@ void push_tool_capabilities(lua_State *L,
}

/******************************************************************************/
void push_inventory_list(lua_State *L, Inventory *inv, const char *name)
void push_inventory_list_raw(lua_State *L, InventoryList *invlist)
{
InventoryList *invlist = inv->getList(name);
if(invlist == NULL){
lua_pushnil(L);
return;
}
std::vector<ItemStack> items;
for(u32 i=0; i<invlist->getSize(); i++)
for (u32 i = 0; i < invlist->getSize(); i++)
items.push_back(invlist->getItem(i));

push_items(L, items);
}

/******************************************************************************/
void push_inventory_list(lua_State *L, Inventory *inv, const char *name)
{
InventoryList *invlist = inv->getList(name);
if (!invlist)
lua_pushnil(L);
else
push_inventory_list_raw(L, invlist);
}

/******************************************************************************/
void read_inventory_list(lua_State *L, int tableindex,
Inventory *inv, const char *name, Server* srv, int forcesize)
Expand Down
3 changes: 3 additions & 0 deletions src/script/common/c_content.h
Expand Up @@ -53,6 +53,7 @@ struct ObjectProperties;
struct SimpleSoundSpec;
struct ServerSoundParams;
class Inventory;
class InventoryList;
struct NodeBox;
struct ContentFeatures;
struct TileDef;
Expand Down Expand Up @@ -112,6 +113,8 @@ void read_object_properties (lua_State *L, int index,
void push_object_properties (lua_State *L,
ObjectProperties *prop);

void push_inventory_list_raw (lua_State *L,
InventoryList *invlist);
void push_inventory_list (lua_State *L,
Inventory *inv,
const char *name);
Expand Down
31 changes: 31 additions & 0 deletions src/script/lua_api/l_inventory.cpp
Expand Up @@ -241,6 +241,36 @@ int InvRef::l_set_list(lua_State *L)
return 0;
}

// add_list(self, listname, list)
int InvRef::l_add_list(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
Inventory *inv = getinv(L, ref);
if (!inv)
return 0;

const char *dst_listname = luaL_checkstring(L, 2);
const char *src_listname = luaL_checkstring(L, 3);

InventoryList *dst_list = inv->getList(dst_listname);
InventoryList *src_list = inv->getList(src_listname);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getList may return nullptr is that list does not exist. Check for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.


// Create new list to store leftovers
InventoryList *leftover_list = new InventoryList("leftover",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need it as pointer?
InventoryList leftover_list("leftover", src_list->getSize(), getServer(L)->getItemDefManager());
EDIT: and push_inventory_list_raw(L, &leftover_list);

src_list->getSize(), getServer(L)->getItemDefManager());

for (u32 i = 0; i < src_list->getSize(); i++) {
ItemStack leftover = dst_list->addItem(src_list->getItem(i));
if (!leftover.empty())
leftover_list->addItem(leftover);
}

push_inventory_list_raw(L, leftover_list);
delete leftover_list;
return 1;
}

// get_lists(self) -> list of InventoryLists
int InvRef::l_get_lists(lua_State *L)
{
Expand Down Expand Up @@ -472,6 +502,7 @@ const luaL_Reg InvRef::methods[] = {
luamethod(InvRef, set_stack),
luamethod(InvRef, get_list),
luamethod(InvRef, set_list),
luamethod(InvRef, add_list),
luamethod(InvRef, get_lists),
luamethod(InvRef, set_lists),
luamethod(InvRef, add_item),
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_inventory.h
Expand Up @@ -78,6 +78,9 @@ class InvRef : public ModApiBase {
// set_list(self, listname, list)
static int l_set_list(lua_State *L);

// add_list(self, listname, list)
static int l_add_list(lua_State *L);

// get_lists(self) -> list of InventoryLists
static int l_get_lists(lua_State *L);

Expand Down