lua: inventory.clone

This commit is contained in:
MihailRis
2024-02-19 03:51:09 +03:00
parent 60902e69ba
commit cac963bd7b
5 changed files with 29 additions and 0 deletions
+10
View File
@@ -44,6 +44,16 @@ std::shared_ptr<Inventory> Inventories::get(int64_t id) {
return found->second; return found->second;
} }
std::shared_ptr<Inventory> Inventories::clone(int64_t id) {
auto original = get(id);
if (original == nullptr)
return nullptr;
auto origptr = reinterpret_cast<const Inventory*>(original.get());
auto clone = std::make_shared<Inventory>(*origptr);
clone->setId(level.getWorld()->getNextInventoryId());
return clone;
}
const inventories_map& Inventories::getMap() const { const inventories_map& Inventories::getMap() const {
return map; return map;
} }
+2
View File
@@ -36,6 +36,8 @@ public:
/* Get inventory by id (works with both real and virtual)*/ /* Get inventory by id (works with both real and virtual)*/
std::shared_ptr<Inventory> get(int64_t id); std::shared_ptr<Inventory> get(int64_t id);
std::shared_ptr<Inventory> clone(int64_t id);
const inventories_map& getMap() const; const inventories_map& getMap() const;
}; };
+4
View File
@@ -40,6 +40,10 @@ public:
static void convert(dynamic::Map* data, const ContentLUT* lut); static void convert(dynamic::Map* data, const ContentLUT* lut);
inline void setId(int64_t id) {
this->id = id;
}
inline int64_t getId() const { inline int64_t getId() const {
return id; return id;
} }
+11
View File
@@ -101,3 +101,14 @@ int l_inventory_unbind_block(lua_State* L) {
scripting::blocks->unbindInventory(x, y, z); scripting::blocks->unbindInventory(x, y, z);
return 0; return 0;
} }
int l_inventory_clone(lua_State* L) {
lua::luaint id = lua_tointeger(L, 1);
auto clone = scripting::level->inventories->clone(id);
if (clone == nullptr) {
lua_pushinteger(L, 0);
return 1;
}
lua_pushinteger(L, clone->getId());
return 1;
}
+2
View File
@@ -10,6 +10,7 @@ extern int l_inventory_add(lua_State* L);
extern int l_inventory_get_block(lua_State* L); extern int l_inventory_get_block(lua_State* L);
extern int l_inventory_bind_block(lua_State* L); extern int l_inventory_bind_block(lua_State* L);
extern int l_inventory_unbind_block(lua_State* L); extern int l_inventory_unbind_block(lua_State* L);
extern int l_inventory_clone(lua_State* L);
static const luaL_Reg inventorylib [] = { static const luaL_Reg inventorylib [] = {
{"get", lua_wrap_errors<l_inventory_get>}, {"get", lua_wrap_errors<l_inventory_get>},
@@ -19,6 +20,7 @@ static const luaL_Reg inventorylib [] = {
{"get_block", lua_wrap_errors<l_inventory_get_block>}, {"get_block", lua_wrap_errors<l_inventory_get_block>},
{"bind_block", lua_wrap_errors<l_inventory_bind_block>}, {"bind_block", lua_wrap_errors<l_inventory_bind_block>},
{"unbind_block", lua_wrap_errors<l_inventory_unbind_block>}, {"unbind_block", lua_wrap_errors<l_inventory_unbind_block>},
{"clone", lua_wrap_errors<l_inventory_clone>},
{NULL, NULL} {NULL, NULL}
}; };