diff --git a/doc/en/scripting/builtins/libinventory.md b/doc/en/scripting/builtins/libinventory.md index 344840f1..ccb7be20 100644 --- a/doc/en/scripting/builtins/libinventory.md +++ b/doc/en/scripting/builtins/libinventory.md @@ -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 +) ``` diff --git a/doc/ru/scripting/builtins/libinventory.md b/doc/ru/scripting/builtins/libinventory.md index 01ecc5cc..7f3cf26e 100644 --- a/doc/ru/scripting/builtins/libinventory.md +++ b/doc/ru/scripting/builtins/libinventory.md @@ -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 +) ``` diff --git a/res/layouts/inventory.xml.lua b/res/layouts/inventory.xml.lua index 589e5e44..9f836223 100644 --- a/res/layouts/inventory.xml.lua +++ b/res/layouts/inventory.xml.lua @@ -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 diff --git a/src/logic/scripting/lua/libs/libinventory.cpp b/src/logic/scripting/lua/libs/libinventory.cpp index a705c48b..bcdc9056 100644 --- a/src/logic/scripting/lua/libs/libinventory.cpp +++ b/src/logic/scripting/lua/libs/libinventory.cpp @@ -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}, {"set", lua::wrap}, {"size", lua::wrap}, {"add", lua::wrap}, {"move", lua::wrap}, + {"move_range", lua::wrap}, {"get_block", lua::wrap}, {"bind_block", lua::wrap}, {"unbind_block", lua::wrap},