aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2014-01-04 19:07:30 -0500
committerShadowNinja <shadowninja@minetest.net>2014-01-11 13:52:26 -0500
commitbd8ddf1a526c57648028bbee192d92e1289219ac (patch)
tree15e578b02176d579e2d1f9a35110e3f10e85d94c /src/script/lua_api
parent6f6f289db4a14045b1053b065e9bbac3d3cb0f33 (diff)
downloadminetest-bd8ddf1a526c57648028bbee192d92e1289219ac.tar.gz
minetest-bd8ddf1a526c57648028bbee192d92e1289219ac.tar.bz2
minetest-bd8ddf1a526c57648028bbee192d92e1289219ac.zip
Add InvRef::get/set_lists()
Diffstat (limited to 'src/script/lua_api')
-rw-r--r--src/script/lua_api/l_inventory.cpp48
-rw-r--r--src/script/lua_api/l_inventory.h6
2 files changed, 54 insertions, 0 deletions
diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp
index d783cf60f..aef011da3 100644
--- a/src/script/lua_api/l_inventory.cpp
+++ b/src/script/lua_api/l_inventory.cpp
@@ -241,6 +241,52 @@ int InvRef::l_set_list(lua_State *L)
return 0;
}
+// get_lists(self) -> list of InventoryLists
+int InvRef::l_get_lists(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ InvRef *ref = checkobject(L, 1);
+ Inventory *inv = getinv(L, ref);
+ if (!inv) {
+ return 0;
+ }
+ std::vector<const InventoryList*> lists = inv->getLists();
+ std::vector<const InventoryList*>::iterator iter = lists.begin();
+ lua_createtable(L, 0, lists.size());
+ for (; iter != lists.end(); iter++) {
+ const char* name = (*iter)->getName().c_str();
+ lua_pushstring(L, name);
+ push_inventory_list(L, inv, name);
+ lua_rawset(L, -3);
+ }
+ return 1;
+}
+
+// set_lists(self, lists)
+int InvRef::l_set_lists(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ InvRef *ref = checkobject(L, 1);
+ Inventory *inv = getinv(L, ref);
+ if (!inv) {
+ return 0;
+ }
+ lua_pushnil(L);
+ while (lua_next(L, 2)) {
+ const char* listname = lua_tostring(L, -2);
+ InventoryList *list = inv->getList(listname);
+ if (list) {
+ read_inventory_list(L, -1, inv, listname,
+ getServer(L), list->getSize());
+ } else {
+ read_inventory_list(L, -1, inv, listname,
+ getServer(L));
+ }
+ lua_pop(L, 1);
+ }
+ return 0;
+}
+
// add_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
// Returns the leftover stack
int InvRef::l_add_item(lua_State *L)
@@ -426,6 +472,8 @@ const luaL_reg InvRef::methods[] = {
luamethod(InvRef, set_stack),
luamethod(InvRef, get_list),
luamethod(InvRef, set_list),
+ luamethod(InvRef, get_lists),
+ luamethod(InvRef, set_lists),
luamethod(InvRef, add_item),
luamethod(InvRef, room_for_item),
luamethod(InvRef, contains_item),
diff --git a/src/script/lua_api/l_inventory.h b/src/script/lua_api/l_inventory.h
index ed3249e5f..03b241034 100644
--- a/src/script/lua_api/l_inventory.h
+++ b/src/script/lua_api/l_inventory.h
@@ -79,6 +79,12 @@ private:
// set_list(self, listname, list)
static int l_set_list(lua_State *L);
+ // get_lists(self) -> list of InventoryLists
+ static int l_get_lists(lua_State *L);
+
+ // set_lists(self, lists)
+ static int l_set_lists(lua_State *L);
+
// add_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
// Returns the leftover stack
static int l_add_item(lua_State *L);