update libinventory.cpp

This commit is contained in:
MihailRis 2026-03-09 17:51:56 +03:00
parent 68ef974096
commit 068e546a63

View file

@ -8,14 +8,28 @@
using namespace scripting; using namespace scripting;
namespace { 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) { 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"); throw std::runtime_error("invalid item id");
} }
} }
Inventory& get_inventory(int64_t id) { Inventory& get_inventory(int64_t id) {
auto inv = level->inventories->get(id); auto inv = require_level().inventories->get(id);
if (inv == nullptr) { if (inv == nullptr) {
throw std::runtime_error("inventory not found: " + std::to_string(id)); throw std::runtime_error("inventory not found: " + std::to_string(id));
} }
@ -23,7 +37,7 @@ namespace {
} }
Inventory& get_inventory(int64_t id, int arg) { Inventory& get_inventory(int64_t id, int arg) {
auto inv = level->inventories->get(id); auto inv = require_level().inventories->get(id);
if (inv == nullptr) { if (inv == nullptr) {
throw std::runtime_error( throw std::runtime_error(
"inventory not found: " + std::to_string(id) + " argument " + "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) { static int l_create(lua::State* L) {
auto invsize = lua::tointeger(L, 1); auto invsize = lua::tointeger(L, 1);
auto inv = level->inventories->create(invsize); auto inv = require_level().inventories->create(invsize);
if (inv == nullptr) { if (inv == nullptr) {
return lua::pushinteger(L, 0); return lua::pushinteger(L, 0);
} }
@ -146,13 +160,13 @@ static int l_create(lua::State* L) {
static int l_remove(lua::State* L) { static int l_remove(lua::State* L) {
auto invid = lua::tointeger(L, 1); auto invid = lua::tointeger(L, 1);
level->inventories->remove(invid); require_level().inventories->remove(invid);
return 0; return 0;
} }
static int l_clone(lua::State* L) { static int l_clone(lua::State* L) {
auto id = lua::tointeger(L, 1); auto id = lua::tointeger(L, 1);
auto clone = level->inventories->clone(id); auto clone = require_level().inventories->clone(id);
if (clone == nullptr) { if (clone == nullptr) {
return lua::pushinteger(L, 0); return lua::pushinteger(L, 0);
} }