mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-01 17:38:41 +00:00
InvRef: deduplicate code
This commit is contained in:
parent
5419345dff
commit
8caf922df6
1 changed files with 35 additions and 36 deletions
|
@ -96,38 +96,37 @@ int InvRef::l_set_size(lua_State *L)
|
||||||
NO_MAP_LOCK_REQUIRED;
|
NO_MAP_LOCK_REQUIRED;
|
||||||
InvRef *ref = checkObject<InvRef>(L, 1);
|
InvRef *ref = checkObject<InvRef>(L, 1);
|
||||||
const char *listname = luaL_checkstring(L, 2);
|
const char *listname = luaL_checkstring(L, 2);
|
||||||
|
Inventory *inv;
|
||||||
|
InventoryList *list;
|
||||||
|
|
||||||
int newsize = luaL_checknumber(L, 3);
|
int newsize = luaL_checknumber(L, 3);
|
||||||
if (newsize < 0) {
|
if (newsize < 0)
|
||||||
lua_pushboolean(L, false);
|
goto fail;
|
||||||
return 1;
|
|
||||||
|
inv = getinv(L, ref);
|
||||||
|
if (!inv)
|
||||||
|
goto fail;
|
||||||
|
|
||||||
|
if (newsize == 0) {
|
||||||
|
inv->deleteList(listname);
|
||||||
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
Inventory *inv = getinv(L, ref);
|
list = inv->getList(listname);
|
||||||
if(inv == NULL){
|
if (list) {
|
||||||
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);
|
list->setSize(newsize);
|
||||||
} else {
|
} else {
|
||||||
list = inv->addList(listname, newsize);
|
list = inv->addList(listname, newsize);
|
||||||
if (!list)
|
if (!list)
|
||||||
{
|
goto fail;
|
||||||
lua_pushboolean(L, false);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
done:
|
||||||
reportInventoryChange(L, ref);
|
reportInventoryChange(L, ref);
|
||||||
lua_pushboolean(L, true);
|
lua_pushboolean(L, true);
|
||||||
return 1;
|
return 1;
|
||||||
|
fail:
|
||||||
|
lua_pushboolean(L, false);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// set_width(self, listname, size)
|
// set_width(self, listname, size)
|
||||||
|
@ -136,28 +135,28 @@ int InvRef::l_set_width(lua_State *L)
|
||||||
NO_MAP_LOCK_REQUIRED;
|
NO_MAP_LOCK_REQUIRED;
|
||||||
InvRef *ref = checkObject<InvRef>(L, 1);
|
InvRef *ref = checkObject<InvRef>(L, 1);
|
||||||
const char *listname = luaL_checkstring(L, 2);
|
const char *listname = luaL_checkstring(L, 2);
|
||||||
|
Inventory *inv;
|
||||||
|
InventoryList *list;
|
||||||
|
|
||||||
int newwidth = luaL_checknumber(L, 3);
|
int newwidth = luaL_checknumber(L, 3);
|
||||||
if (newwidth < 0) {
|
if (newwidth < 0)
|
||||||
lua_pushboolean(L, false);
|
goto fail;
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
Inventory *inv = getinv(L, ref);
|
inv = getinv(L, ref);
|
||||||
if(inv == NULL){
|
if (!inv)
|
||||||
lua_pushboolean(L, false);
|
goto fail;
|
||||||
return 1;
|
|
||||||
}
|
list = inv->getList(listname);
|
||||||
InventoryList *list = inv->getList(listname);
|
if (!list)
|
||||||
if(list){
|
goto fail;
|
||||||
list->setWidth(newwidth);
|
|
||||||
} else {
|
list->setWidth(newwidth);
|
||||||
lua_pushboolean(L, false);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
reportInventoryChange(L, ref);
|
reportInventoryChange(L, ref);
|
||||||
lua_pushboolean(L, true);
|
lua_pushboolean(L, true);
|
||||||
return 1;
|
return 1;
|
||||||
|
fail:
|
||||||
|
lua_pushboolean(L, false);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_stack(self, listname, i) -> itemstack
|
// get_stack(self, listname, i) -> itemstack
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue