From 0dc3ab50a23676e9a024744f2ac6b48cd6ae6243 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 3 Apr 2024 20:50:26 +0300 Subject: [PATCH] hud.get_block_inventory and inventory.move --- doc/en/8.Scripting.md | 14 ++++++++++++++ doc/ru/8.Скриптинг.md | 15 +++++++++++++++ res/layouts/inventory.xml.lua | 7 ++++++- src/frontend/hud.cpp | 7 +++++++ src/frontend/hud.h | 2 ++ src/logic/scripting/lua/libhud.cpp | 11 +++++++++++ src/logic/scripting/lua/libinventory.cpp | 24 ++++++++++++++++++++++++ 7 files changed, 79 insertions(+), 1 deletion(-) diff --git a/doc/en/8.Scripting.md b/doc/en/8.Scripting.md index 3cc2895f..39445355 100644 --- a/doc/en/8.Scripting.md +++ b/doc/en/8.Scripting.md @@ -157,6 +157,13 @@ inventory.clone(invid: int) -> int Create inventory copy. Returns the created copy ID. +```python +inventory.move(invA: int, slotA: int, invB: int, slotB: int) +``` + +Move item from slotA of invA to slotB of invB. invA may be the same as invB. +If slotB will be chosen automaticly if argument is not specified. + ## *block* library ```python @@ -333,6 +340,13 @@ hud.close(layoutid: str) Remove an element from the screen + +```python +hud.get_block_inventory() -> int +``` + +Get open block inventory ID or 0 + ## Block events ```lua diff --git a/doc/ru/8.Скриптинг.md b/doc/ru/8.Скриптинг.md index e53e7958..7aa3e930 100644 --- a/doc/ru/8.Скриптинг.md +++ b/doc/ru/8.Скриптинг.md @@ -152,6 +152,14 @@ inventory.clone(invid: int) -> int Создает копию инвентаря и возвращает id копии. Если копируемого инвентаря не существует, возвращает 0. +```python +inventory.move(invA: int, slotA: int, invB: int, slotB: int) +``` + +Перемещает предмет из slotA инвентаря invA в slotB инвентаря invB. +invA и invB могут указывать на один инвентарь. +slotB будет выбран автоматически, если не указывать явно. + ## Библиотека block ```python @@ -323,6 +331,13 @@ hud.close(layoutid: str) ``` Удаляет элемент с экрана + +```python +hud.get_block_inventory() -> int +``` + +Получить ID инвентаря открытого блока или 0 + ## События блоков ```lua diff --git a/res/layouts/inventory.xml.lua b/res/layouts/inventory.xml.lua index bc3fc5d2..c2280f15 100644 --- a/res/layouts/inventory.xml.lua +++ b/res/layouts/inventory.xml.lua @@ -1,3 +1,8 @@ function inventory_share_func(invid, slotid) - inventory.set(invid, slotid, 0, 0) + local blockinv = hud.get_block_inventory() + if blockinv ~= 0 then + inventory.move(invid, slotid, blockinv) + else + inventory.set(invid, slotid, 0, 0) + end end diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index b13de22c..d3b52ecd 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -564,3 +564,10 @@ void Hud::setPause(bool pause) { Player* Hud::getPlayer() const { return player; } + +std::shared_ptr Hud::getBlockInventory() { + if (blockUI == nullptr) { + return nullptr; + } + return blockUI->getInventory(); +} diff --git a/src/frontend/hud.h b/src/frontend/hud.h index dab28840..aaf787c1 100644 --- a/src/frontend/hud.h +++ b/src/frontend/hud.h @@ -155,6 +155,8 @@ public: void remove(std::shared_ptr node); Player* getPlayer() const; + + std::shared_ptr getBlockInventory(); }; #endif // SRC_HUD_H_ diff --git a/src/logic/scripting/lua/libhud.cpp b/src/logic/scripting/lua/libhud.cpp index 6afcfffc..555c6e69 100644 --- a/src/logic/scripting/lua/libhud.cpp +++ b/src/logic/scripting/lua/libhud.cpp @@ -109,12 +109,23 @@ static int l_hud_resume(lua_State* L) { return 0; } +static int l_hud_get_block_inventory(lua_State* L) { + auto inventory = scripting::hud->getBlockInventory(); + if (inventory == nullptr) { + lua_pushinteger(L, 0); + } else { + lua_pushinteger(L, inventory->getId()); + } + return 1; +} + const luaL_Reg hudlib [] = { {"open_inventory", lua_wrap_errors}, {"close_inventory", lua_wrap_errors}, {"open_block", lua_wrap_errors}, {"open_permanent", lua_wrap_errors}, {"show_overlay", lua_wrap_errors}, + {"get_block_inventory", lua_wrap_errors}, {"close", lua_wrap_errors}, {"pause", lua_wrap_errors}, {"resume", lua_wrap_errors}, diff --git a/src/logic/scripting/lua/libinventory.cpp b/src/logic/scripting/lua/libinventory.cpp index c2221d7c..83b3d98c 100644 --- a/src/logic/scripting/lua/libinventory.cpp +++ b/src/logic/scripting/lua/libinventory.cpp @@ -113,11 +113,35 @@ static int l_inventory_clone(lua_State* L) { return 1; } +static int l_inventory_move(lua_State* L) { + lua::luaint invAid = lua_tointeger(L, 1); + lua::luaint slotAid = lua_tointeger(L, 2); + auto invA = scripting::level->inventories->get(invAid); + if (invA == nullptr) { + luaL_error(L, "inventory A does not exists in runtime: %d", invAid); + } + + lua::luaint invBid = lua_tointeger(L, 3); + lua::luaint slotBid = lua_isnil(L, 4) ? -1 : lua_tointeger(L, 4); + auto invB = scripting::level->inventories->get(invBid); + if (invB == nullptr) { + luaL_error(L, "inventory B does not exists in runtime: %d", invBid); + } + auto& slot = invA->getSlot(slotAid); + if (slotBid == -1) { + invB->move(slot, scripting::content->getIndices()); + } else { + invB->move(slot, scripting::content->getIndices(), slotBid, slotBid+1); + } + return 0; +} + const luaL_Reg inventorylib [] = { {"get", lua_wrap_errors}, {"set", lua_wrap_errors}, {"size", lua_wrap_errors}, {"add", lua_wrap_errors}, + {"move", lua_wrap_errors}, {"get_block", lua_wrap_errors}, {"bind_block", lua_wrap_errors}, {"unbind_block", lua_wrap_errors},