From 068e546a63dfd9468a436d0e711d8f38b86c3f10 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 9 Mar 2026 17:51:56 +0300 Subject: [PATCH] update libinventory.cpp --- src/logic/scripting/lua/libs/libinventory.cpp | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/logic/scripting/lua/libs/libinventory.cpp b/src/logic/scripting/lua/libs/libinventory.cpp index 7f43ae88b..3ec9d2ca3 100644 --- a/src/logic/scripting/lua/libs/libinventory.cpp +++ b/src/logic/scripting/lua/libs/libinventory.cpp @@ -8,14 +8,28 @@ using namespace scripting; namespace { + Level& require_level() { + if (level == nullptr) { + throw std::runtime_error("world is not open"); + } + return *level; + } + + const Content& require_content() { + if (content == nullptr) { + throw std::runtime_error("content is not initialized"); + } + return *content; + } + void validate_itemid(itemid_t id) { - if (id >= indices->items.count()) { + if (id >= require_content().getIndices()->items.count()) { throw std::runtime_error("invalid item id"); } } Inventory& get_inventory(int64_t id) { - auto inv = level->inventories->get(id); + auto inv = require_level().inventories->get(id); if (inv == nullptr) { throw std::runtime_error("inventory not found: " + std::to_string(id)); } @@ -23,7 +37,7 @@ namespace { } Inventory& get_inventory(int64_t id, int arg) { - auto inv = level->inventories->get(id); + auto inv = require_level().inventories->get(id); if (inv == nullptr) { throw std::runtime_error( "inventory not found: " + std::to_string(id) + " argument " + @@ -137,7 +151,7 @@ static int l_unbind_block(lua::State* L) { static int l_create(lua::State* L) { auto invsize = lua::tointeger(L, 1); - auto inv = level->inventories->create(invsize); + auto inv = require_level().inventories->create(invsize); if (inv == nullptr) { return lua::pushinteger(L, 0); } @@ -146,13 +160,13 @@ static int l_create(lua::State* L) { static int l_remove(lua::State* L) { auto invid = lua::tointeger(L, 1); - level->inventories->remove(invid); + require_level().inventories->remove(invid); return 0; } static int l_clone(lua::State* L) { auto id = lua::tointeger(L, 1); - auto clone = level->inventories->clone(id); + auto clone = require_level().inventories->clone(id); if (clone == nullptr) { return lua::pushinteger(L, 0); }