add function inventory.move_range

This commit is contained in:
MihailRis 2024-11-26 09:32:53 +03:00
parent f6ab0de5af
commit 0ba0584c5d
4 changed files with 52 additions and 2 deletions

View File

@ -55,8 +55,21 @@ inventory.create(size: int) -> int
-- Create inventory copy. Returns the created copy ID.
inventory.clone(invid: int) -> int
-- Move item from slotA of invA to slotB of invB.
-- Move an 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.
-- The move may be incomplete if the available slot has no enough stack space.
inventory.move(invA: int, slotA: int, invB: int, slotB: int)
-- Moves an item from slotA of inventory invA to a suitable slot(s)
-- in the specified range of inventory invB.
-- invA may be the same as invB.
-- The move may be incomplete if the available slots are filled.
inventory.move(
invA: int,
slotA: int,
invB: int,
rangeBegin: int,
[optional] rangeEnd: int
)
```

View File

@ -66,5 +66,20 @@ inventory.clone(invid: int) -> int
-- Перемещает предмет из slotA инвентаря invA в slotB инвентаря invB.
-- invA и invB могут указывать на один инвентарь.
-- slotB будет выбран автоматически, если не указывать явно.
-- Перемещение может быть неполным, если стек слота заполнится.
inventory.move(invA: int, slotA: int, invB: int, slotB: int)
-- Перемещает предмет из slotA инвентаря invA в подходящий слот, находящийся в
-- указанном отрезке инвентаря invB.
-- invA и invB могут указывать на один инвентарь.
-- rangeBegin - начало отрезка.
-- rangeEnd - конец отрезка.
-- Перемещение может быть неполным, если доступные слоты будут заполнены.
inventory.move_range(
invA: int,
slotA: int,
invB: int,
rangeBegin: int,
[опционально] rangeEnd: int
)
```

View File

@ -4,7 +4,9 @@ function inventory_share_func(invid, slotid)
inventory.move(invid, slotid, blockinv)
elseif rules.get("allow-content-access") then
inventory.set(invid, slotid, 0, 0)
elseif slotid < 10 then
inventory.move_range(invid, slotid, invid, 10)
else
inventory.move(invid, slotid, invid)
inventory.move_range(invid, slotid, invid, 0, 9)
end
end

View File

@ -156,12 +156,32 @@ static int l_inventory_move(lua::State* L) {
return 0;
}
static int l_inventory_move_range(lua::State* L) {
auto invAid = lua::tointeger(L, 1);
auto slotAid = lua::tointeger(L, 2);
auto invA = get_inventory(invAid, 1);
validate_slotid(slotAid, invA.get());
auto invBid = lua::tointeger(L, 3);
auto slotBegin = lua::isnoneornil(L, 4) ? -1 : lua::tointeger(L, 4);
auto slotEnd = lua::isnoneornil(L, 5) ? -1 : lua::tointeger(L, 5);
auto invB = get_inventory(invBid, 3);
auto& slot = invA->getSlot(slotAid);
if (slotBegin == -1) {
invB->move(slot, content->getIndices());
} else {
invB->move(slot, content->getIndices(), slotBegin, slotEnd);
}
return 0;
}
const luaL_Reg inventorylib[] = {
{"get", lua::wrap<l_inventory_get>},
{"set", lua::wrap<l_inventory_set>},
{"size", lua::wrap<l_inventory_size>},
{"add", lua::wrap<l_inventory_add>},
{"move", lua::wrap<l_inventory_move>},
{"move_range", lua::wrap<l_inventory_move_range>},
{"get_block", lua::wrap<l_inventory_get_block>},
{"bind_block", lua::wrap<l_inventory_bind_block>},
{"unbind_block", lua::wrap<l_inventory_unbind_block>},