InventoryList* InvRef::getlist(lua_State *L, InvRef *ref, const char *listname) { NO_MAP_LOCK_REQUIRED; Inventory *inv = getinv(L, ref); if(!inv) return NULL; return inv->getList(listname); }
// get_list(self, listname) -> list or nil int InvRef::l_get_list(lua_State *L) { NO_MAP_LOCK_REQUIRED; InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); Inventory *inv = getinv(L, ref); if(inv){ push_inventory_list(L, inv, listname); } else { lua_pushnil(L); } return 1; }
// set_list(self, listname, list) int InvRef::l_set_list(lua_State *L) { NO_MAP_LOCK_REQUIRED; InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); Inventory *inv = getinv(L, ref); if(inv == NULL){ return 0; } InventoryList *list = inv->getList(listname); if(list) read_inventory_list(L, 3, inv, listname, getServer(L), list->getSize()); else read_inventory_list(L, 3, inv, listname, getServer(L)); reportInventoryChange(L, ref); 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_width(self, listname, size) int InvRef::l_set_width(lua_State *L) { NO_MAP_LOCK_REQUIRED; InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); int newwidth = luaL_checknumber(L, 3); Inventory *inv = getinv(L, ref); if(inv == NULL){ return 0; } InventoryList *list = inv->getList(listname); if(list){ list->setWidth(newwidth); } else { return 0; } reportInventoryChange(L, ref); return 0; }
// set_size(self, listname, size) int InvRef::l_set_size(lua_State *L) { NO_MAP_LOCK_REQUIRED; InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); int newsize = luaL_checknumber(L, 3); if (newsize < 0) { lua_pushboolean(L, false); return 1; } Inventory *inv = getinv(L, ref); if(inv == NULL){ lua_pushboolean(L, false); return 1; } if(newsize == 0){ inv->deleteList(listname); reportInventoryChange(L, ref); lua_pushboolean(L, true); return 1; } InventoryList *list = inv->getList(listname); if(list){ list->setSize(newsize); } else { list = inv->addList(listname, newsize); if (!list) { lua_pushboolean(L, false); return 1; } } reportInventoryChange(L, ref); lua_pushboolean(L, true); 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; }
// 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; } // Make a temporary inventory in case reading fails Inventory *tempInv(inv); tempInv->clear(); Server *server = getServer(L); lua_pushnil(L); while (lua_next(L, 2)) { const char *listname = lua_tostring(L, -2); read_inventory_list(L, -1, tempInv, listname, server); lua_pop(L, 1); } inv = tempInv; return 0; }