InvRef: deduplicate code

This commit is contained in:
SmallJoker 2025-02-01 10:08:51 +01:00 committed by SmallJoker
parent 5419345dff
commit 8caf922df6

View file

@ -96,38 +96,37 @@ int InvRef::l_set_size(lua_State *L)
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
Inventory *inv;
InventoryList *list;
int newsize = luaL_checknumber(L, 3);
if (newsize < 0) {
lua_pushboolean(L, false);
return 1;
if (newsize < 0)
goto fail;
inv = getinv(L, ref);
if (!inv)
goto fail;
if (newsize == 0) {
inv->deleteList(listname);
goto done;
}
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 = inv->getList(listname);
if (list) {
list->setSize(newsize);
} else {
list = inv->addList(listname, newsize);
if (!list)
{
lua_pushboolean(L, false);
return 1;
}
goto fail;
}
done:
reportInventoryChange(L, ref);
lua_pushboolean(L, true);
return 1;
fail:
lua_pushboolean(L, false);
return 1;
}
// set_width(self, listname, size)
@ -136,28 +135,28 @@ int InvRef::l_set_width(lua_State *L)
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
Inventory *inv;
InventoryList *list;
int newwidth = luaL_checknumber(L, 3);
if (newwidth < 0) {
lua_pushboolean(L, false);
return 1;
}
if (newwidth < 0)
goto fail;
Inventory *inv = getinv(L, ref);
if(inv == NULL){
lua_pushboolean(L, false);
return 1;
}
InventoryList *list = inv->getList(listname);
if(list){
list->setWidth(newwidth);
} else {
lua_pushboolean(L, false);
return 1;
}
inv = getinv(L, ref);
if (!inv)
goto fail;
list = inv->getList(listname);
if (!list)
goto fail;
list->setWidth(newwidth);
reportInventoryChange(L, ref);
lua_pushboolean(L, true);
return 1;
fail:
lua_pushboolean(L, false);
return 1;
}
// get_stack(self, listname, i) -> itemstack