Modify mcl_util.select_stack to allow specifying the number of items that will be moved, modify hopper on_try_push functions to specify only 1 item will be moved at a time, general cleanup of touched code (reduce indent - 1 place, break filter functions out of function call parameter - 4 places)

This commit is contained in:
teknomunk 2024-02-12 12:03:55 +00:00
parent 1d8fc7abac
commit bdcd89e1bf
6 changed files with 67 additions and 45 deletions

View File

@ -257,12 +257,21 @@ end
---@param dst_inventory InvRef Destination inventory to push to ---@param dst_inventory InvRef Destination inventory to push to
---@param dst_list string Name of destination inventory list to push to ---@param dst_list string Name of destination inventory list to push to
---@param condition? fun(stack: ItemStack) Condition which items are allowed to be transfered. ---@param condition? fun(stack: ItemStack) Condition which items are allowed to be transfered.
---@param count? integer Number of items to try to transfer at once
---@return integer Item stack number to be transfered ---@return integer Item stack number to be transfered
function mcl_util.select_stack(src_inventory, src_list, dst_inventory, dst_list, condition) function mcl_util.select_stack(src_inventory, src_list, dst_inventory, dst_list, condition, count)
local src_size = src_inventory:get_size(src_list) local src_size = src_inventory:get_size(src_list)
local stack local stack
for i = 1, src_size do for i = 1, src_size do
stack = src_inventory:get_stack(src_list, i) stack = src_inventory:get_stack(src_list, i)
-- Allow for partial stack movement
if count then
local new_stack = ItemStack(stack)
new_stack:set_count(count)
stack = new_stack
end
if not stack:is_empty() and dst_inventory:room_for_item(dst_list, stack) and ((condition == nil or condition(stack))) then if not stack:is_empty() and dst_inventory:room_for_item(dst_list, stack) and ((condition == nil or condition(stack))) then
return i return i
end end
@ -280,9 +289,13 @@ end
-- Returns true on success and false on failure -- Returns true on success and false on failure
-- Possible failures: No item in source slot, destination inventory full -- Possible failures: No item in source slot, destination inventory full
function mcl_util.move_item(source_inventory, source_list, source_stack_id, destination_inventory, destination_list) function mcl_util.move_item(source_inventory, source_list, source_stack_id, destination_inventory, destination_list)
if not source_inventory:is_empty(source_list) then -- Can't move items we don't have
if source_inventory:is_empty(source_list) then return false end
-- Can't move from an empty stack
local stack = source_inventory:get_stack(source_list, source_stack_id) local stack = source_inventory:get_stack(source_list, source_stack_id)
if not stack:is_empty() then if stack:is_empty() then return false end
local new_stack = ItemStack(stack) local new_stack = ItemStack(stack)
new_stack:set_count(1) new_stack:set_count(1)
if not destination_inventory:room_for_item(destination_list, new_stack) then if not destination_inventory:room_for_item(destination_list, new_stack) then
@ -292,9 +305,6 @@ function mcl_util.move_item(source_inventory, source_list, source_stack_id, dest
source_inventory:set_stack(source_list, source_stack_id, stack) source_inventory:set_stack(source_list, source_stack_id, stack)
destination_inventory:add_item(destination_list, new_stack) destination_inventory:add_item(destination_list, new_stack)
return true return true
end
end
return false
end end
--- Try pushing item from hopper inventory to destination inventory --- Try pushing item from hopper inventory to destination inventory
@ -314,25 +324,23 @@ function mcl_util.hopper_push(pos, dst_pos)
local dst_list = 'main' local dst_list = 'main'
local dst_inv, stack_id local dst_inv, stack_id
-- Find a inventory stack in the destination
if dst_def._mcl_hoppers_on_try_push then if dst_def._mcl_hoppers_on_try_push then
dst_inv, dst_list, stack_id = dst_def._mcl_hoppers_on_try_push(dst_pos, pos, hop_inv, hop_list) dst_inv, dst_list, stack_id = dst_def._mcl_hoppers_on_try_push(dst_pos, pos, hop_inv, hop_list)
else else
local dst_meta = minetest.get_meta(dst_pos) local dst_meta = minetest.get_meta(dst_pos)
dst_inv = dst_meta:get_inventory() dst_inv = dst_meta:get_inventory()
stack_id = mcl_util.select_stack(hop_inv, hop_list, dst_inv, dst_list) stack_id = mcl_util.select_stack(hop_inv, hop_list, dst_inv, dst_list, nil, 1)
end end
if not stack_id then return false end
if stack_id ~= nil then -- Move the item
local ok = mcl_util.move_item(hop_inv, hop_list, stack_id, dst_inv, dst_list) local ok = mcl_util.move_item(hop_inv, hop_list, stack_id, dst_inv, dst_list)
if dst_def._mcl_hoppers_on_after_push then if dst_def._mcl_hoppers_on_after_push then
dst_def._mcl_hoppers_on_after_push(dst_pos) dst_def._mcl_hoppers_on_after_push(dst_pos)
end end
if ok then
return true
end
end
return false return ok
end end
-- Try pulling from source inventory to hopper inventory -- Try pulling from source inventory to hopper inventory
@ -357,7 +365,7 @@ function mcl_util.hopper_pull(pos, src_pos)
else else
local src_meta = minetest.get_meta(src_pos) local src_meta = minetest.get_meta(src_pos)
src_inv = src_meta:get_inventory() src_inv = src_meta:get_inventory()
stack_id = mcl_util.select_stack(src_inv, src_list, hop_inv, hop_list) stack_id = mcl_util.select_stack(src_inv, src_list, hop_inv, hop_list, nil, 1)
end end
if stack_id ~= nil then if stack_id ~= nil then

View File

@ -475,8 +475,11 @@ minetest.register_node("mcl_books:bookshelf", {
_mcl_hoppers_on_try_push = function(pos, hop_pos, hop_inv, hop_list) _mcl_hoppers_on_try_push = function(pos, hop_pos, hop_inv, hop_list)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
return inv, "main", mcl_util.select_stack(hop_inv, hop_list, inv, "main", local filter = function(stack)
function(stack) return minetest.get_item_group(stack:get_name(), "book") == 1 or stack:get_name() == "mcl_enchanting:book_enchanted" end) return minetest.get_item_group(stack:get_name(), "book") == 1 or
stack:get_name() == "mcl_enchanting:book_enchanted"
end
return inv, "main", mcl_util.select_stack(hop_inv, hop_list, inv, "main", filter, 1)
end, end,
}) })

View File

@ -379,16 +379,27 @@ end
local function hoppers_on_try_push(pos, hop_pos, hop_inv, hop_list) local function hoppers_on_try_push(pos, hop_pos, hop_inv, hop_list)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
if math.abs(pos.y - hop_pos.y) > math.abs(pos.x - hop_pos.x) and math.abs(pos.y - hop_pos.y) > math.abs(pos.z - hop_pos.z) then
return inv, "input", mcl_util.select_stack(hop_inv, hop_list, inv, "input", if math.abs(pos.y - hop_pos.y) > math.abs(pos.x - hop_pos.x) and math.abs(pos.y - hop_pos.y) > math.abs(pos.z - hop_pos.z)
function(stack) return minetest.get_item_group(stack:get_name(), "brewitem") == 1 and minetest.get_item_group(stack:get_name(), "bottle") == 0 end) then
local filter = function(stack)
return minetest.get_item_group(stack:get_name(), "brewitem") == 1 and
minetest.get_item_group(stack:get_name(), "bottle") == 0
end
return inv, "input", mcl_util.select_stack(hop_inv, hop_list, inv, "input", filter, 1)
else else
local stack = mcl_util.select_stack(hop_inv, hop_list, inv, "fuel", function(stack) return stack:get_name() == "mcl_mobitems:blaze_powder" end) local filter = function(stack)
return stack:get_name() == "mcl_mobitems:blaze_powder"
end
local stack = mcl_util.select_stack(hop_inv, hop_list, inv, "fuel", filter, 1 )
if stack then if stack then
return inv, "fuel", stack return inv, "fuel", stack
else else
return inv, "stand", mcl_util.select_stack(hop_inv, hop_list, inv, "stand", local filter = function(stack)
function(stack) return minetest.get_item_group(stack:get_name(), "bottle") == 1 end) return minetest.get_item_group(stack:get_name(), "bottle") == 1
end
return inv, "stand", mcl_util.select_stack(hop_inv, hop_list, inv, "stand", filter, 1)
end end
end end
end end

View File

@ -760,7 +760,7 @@ local function register_chest(basename, desc, longdesc, usagehelp, tt_help, tile
_mcl_hoppers_on_try_push = function(pos, hop_pos, hop_inv, hop_list) _mcl_hoppers_on_try_push = function(pos, hop_pos, hop_inv, hop_list)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
local stack_id = mcl_util.select_stack(hop_inv, hop_list, inv, "main") local stack_id = mcl_util.select_stack(hop_inv, hop_list, inv, "main", null, 1)
if stack_id ~= nil then if stack_id ~= nil then
return inv, "main", stack_id return inv, "main", stack_id
end end
@ -768,7 +768,7 @@ local function register_chest(basename, desc, longdesc, usagehelp, tt_help, tile
local pos_other = mcl_util.get_double_container_neighbor_pos(pos, node.param2, "left") local pos_other = mcl_util.get_double_container_neighbor_pos(pos, node.param2, "left")
local meta_other = minetest.get_meta(pos_other) local meta_other = minetest.get_meta(pos_other)
local inv_other = meta_other:get_inventory() local inv_other = meta_other:get_inventory()
stack_id = mcl_util.select_stack(hop_inv, hop_list, inv_other, "main") stack_id = mcl_util.select_stack(hop_inv, hop_list, inv_other, "main", null, 1)
return inv_other, "main", stack_id return inv_other, "main", stack_id
end, end,
}) })
@ -955,13 +955,13 @@ local function register_chest(basename, desc, longdesc, usagehelp, tt_help, tile
local pos_other = mcl_util.get_double_container_neighbor_pos(pos, node.param2, "right") local pos_other = mcl_util.get_double_container_neighbor_pos(pos, node.param2, "right")
local meta_other = minetest.get_meta(pos_other) local meta_other = minetest.get_meta(pos_other)
local inv_other = meta_other:get_inventory() local inv_other = meta_other:get_inventory()
local stack_id = mcl_util.select_stack(hop_inv, hop_list, inv_other, "main") local stack_id = mcl_util.select_stack(hop_inv, hop_list, inv_other, "main", null, 1)
if stack_id ~= nil then if stack_id ~= nil then
return inv_other, "main", stack_id return inv_other, "main", stack_id
end end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
stack_id = mcl_util.select_stack(hop_inv, hop_list, inv, "main") stack_id = mcl_util.select_stack(hop_inv, hop_list, inv, "main", null, 1)
return inv, "main", stack_id return inv, "main", stack_id
end, end,
}) })
@ -1522,7 +1522,7 @@ for color, desc in pairs(boxtypes) do
_mcl_hoppers_on_try_push = function(pos, hop_pos, hop_inv, hop_list) _mcl_hoppers_on_try_push = function(pos, hop_pos, hop_inv, hop_list)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
return inv, "main", mcl_util.select_stack(hop_inv, hop_list, inv, "main", mcl_chests.is_not_shulker_box) return inv, "main", mcl_util.select_stack(hop_inv, hop_list, inv, "main", mcl_chests.is_not_shulker_box, 1)
end, end,
}) })

View File

@ -238,7 +238,7 @@ minetest.register_node("mcl_composters:composter", {
_mcl_hoppers_on_try_push = function(pos, hop_pos, hop_inv, hop_list) _mcl_hoppers_on_try_push = function(pos, hop_pos, hop_inv, hop_list)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
return inv, "src", mcl_util.select_stack(hop_inv, hop_list, inv, "src", hopper_push_condition) return inv, "src", mcl_util.select_stack(hop_inv, hop_list, inv, "src", hopper_push_condition, 1)
end, end,
_mcl_hoppers_on_after_push = function(pos) _mcl_hoppers_on_after_push = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
@ -293,7 +293,7 @@ local function register_filled_composter(level)
_mcl_hoppers_on_try_push = function(pos, hop_pos, hop_inv, hop_list) _mcl_hoppers_on_try_push = function(pos, hop_pos, hop_inv, hop_list)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
return inv, "src", mcl_util.select_stack(hop_inv, hop_list, inv, "src", hopper_push_condition) return inv, "src", mcl_util.select_stack(hop_inv, hop_list, inv, "src", hopper_push_condition, 1)
end, end,
_mcl_hoppers_on_after_push = function(pos) _mcl_hoppers_on_after_push = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)

View File

@ -466,9 +466,9 @@ function mcl_furnaces.hoppers_on_try_push(pos, hop_pos, hop_inv, hop_list)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
if math.abs(pos.y - hop_pos.y) > math.abs(pos.x - hop_pos.x) and math.abs(pos.y - hop_pos.y) > math.abs(pos.z - hop_pos.z) then if math.abs(pos.y - hop_pos.y) > math.abs(pos.x - hop_pos.x) and math.abs(pos.y - hop_pos.y) > math.abs(pos.z - hop_pos.z) then
return inv, "src", mcl_util.select_stack(hop_inv, hop_list, inv, "src") return inv, "src", mcl_util.select_stack(hop_inv, hop_list, inv, "src", nil, 1)
else else
return inv, "fuel", mcl_util.select_stack(hop_inv, hop_list, inv, "fuel", mcl_util.is_fuel) return inv, "fuel", mcl_util.select_stack(hop_inv, hop_list, inv, "fuel", mcl_util.is_fuel, 1)
end end
end end