diff --git a/src/items/Inventories.cpp b/src/items/Inventories.cpp index 3bf3e706..53167093 100644 --- a/src/items/Inventories.cpp +++ b/src/items/Inventories.cpp @@ -44,6 +44,16 @@ std::shared_ptr Inventories::get(int64_t id) { return found->second; } +std::shared_ptr Inventories::clone(int64_t id) { + auto original = get(id); + if (original == nullptr) + return nullptr; + auto origptr = reinterpret_cast(original.get()); + auto clone = std::make_shared(*origptr); + clone->setId(level.getWorld()->getNextInventoryId()); + return clone; +} + const inventories_map& Inventories::getMap() const { return map; } diff --git a/src/items/Inventories.h b/src/items/Inventories.h index 40e3f2ee..b31933eb 100644 --- a/src/items/Inventories.h +++ b/src/items/Inventories.h @@ -36,6 +36,8 @@ public: /* Get inventory by id (works with both real and virtual)*/ std::shared_ptr get(int64_t id); + std::shared_ptr clone(int64_t id); + const inventories_map& getMap() const; }; diff --git a/src/items/Inventory.h b/src/items/Inventory.h index 6185f07d..68226598 100644 --- a/src/items/Inventory.h +++ b/src/items/Inventory.h @@ -40,6 +40,10 @@ public: static void convert(dynamic::Map* data, const ContentLUT* lut); + inline void setId(int64_t id) { + this->id = id; + } + inline int64_t getId() const { return id; } diff --git a/src/logic/scripting/lua/libinventory.cpp b/src/logic/scripting/lua/libinventory.cpp index 69d2e4c8..8e657d6a 100644 --- a/src/logic/scripting/lua/libinventory.cpp +++ b/src/logic/scripting/lua/libinventory.cpp @@ -101,3 +101,14 @@ int l_inventory_unbind_block(lua_State* L) { scripting::blocks->unbindInventory(x, y, z); 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; +} diff --git a/src/logic/scripting/lua/libinventory.h b/src/logic/scripting/lua/libinventory.h index b6510e6f..feca2942 100644 --- a/src/logic/scripting/lua/libinventory.h +++ b/src/logic/scripting/lua/libinventory.h @@ -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_bind_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 [] = { {"get", lua_wrap_errors}, @@ -19,6 +20,7 @@ static const luaL_Reg inventorylib [] = { {"get_block", lua_wrap_errors}, {"bind_block", lua_wrap_errors}, {"unbind_block", lua_wrap_errors}, + {"clone", lua_wrap_errors}, {NULL, NULL} };