mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
update libinventory.cpp
This commit is contained in:
parent
68ef974096
commit
068e546a63
1 changed files with 20 additions and 6 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue