hud.get_block_inventory and inventory.move

This commit is contained in:
MihailRis 2024-04-03 20:50:26 +03:00
parent cbdc151373
commit 0dc3ab50a2
7 changed files with 79 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -564,3 +564,10 @@ void Hud::setPause(bool pause) {
Player* Hud::getPlayer() const {
return player;
}
std::shared_ptr<Inventory> Hud::getBlockInventory() {
if (blockUI == nullptr) {
return nullptr;
}
return blockUI->getInventory();
}

View File

@ -155,6 +155,8 @@ public:
void remove(std::shared_ptr<gui::UINode> node);
Player* getPlayer() const;
std::shared_ptr<Inventory> getBlockInventory();
};
#endif // SRC_HUD_H_

View File

@ -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<l_hud_open_inventory>},
{"close_inventory", lua_wrap_errors<l_hud_close_inventory>},
{"open_block", lua_wrap_errors<l_hud_open_block>},
{"open_permanent", lua_wrap_errors<l_hud_open_permanent>},
{"show_overlay", lua_wrap_errors<l_hud_show_overlay>},
{"get_block_inventory", lua_wrap_errors<l_hud_get_block_inventory>},
{"close", lua_wrap_errors<l_hud_close>},
{"pause", lua_wrap_errors<l_hud_pause>},
{"resume", lua_wrap_errors<l_hud_resume>},

View File

@ -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<l_inventory_get>},
{"set", lua_wrap_errors<l_inventory_set>},
{"size", lua_wrap_errors<l_inventory_size>},
{"add", lua_wrap_errors<l_inventory_add>},
{"move", lua_wrap_errors<l_inventory_move>},
{"get_block", lua_wrap_errors<l_inventory_get_block>},
{"bind_block", lua_wrap_errors<l_inventory_bind_block>},
{"unbind_block", lua_wrap_errors<l_inventory_unbind_block>},