mirror of
https://github.com/minetest-mods/hopper.git
synced 2024-11-19 22:03:47 +01:00
Fix regression with throwing items out of the hopper (#29)
Co-authored-by: SmallJoker <SmallJoker@users.noreply.github.com>
This commit is contained in:
parent
18f37112d4
commit
0baf36e7d9
104
utility.lua
104
utility.lua
@ -129,6 +129,63 @@ hopper.take_item_from = function(hopper_pos, target_pos, target_node, target_inv
|
||||
end
|
||||
end
|
||||
|
||||
local function send_item_to_air(hopper_inv, target_pos, filtered_items)
|
||||
local stack
|
||||
local stack_num
|
||||
for i = 1, hopper_inv:get_size("main") do
|
||||
stack = hopper_inv:get_stack("main", i)
|
||||
local item = stack:get_name()
|
||||
if item ~= "" and (filtered_items == nil or filtered_items[item]) then
|
||||
stack_num = i
|
||||
break
|
||||
end
|
||||
end
|
||||
if not stack_num then
|
||||
return false
|
||||
end
|
||||
|
||||
local stack_to_put = stack:take_item(1)
|
||||
minetest.add_item(target_pos, stack_to_put)
|
||||
hopper_inv:set_stack("main", stack_num, stack)
|
||||
return true
|
||||
end
|
||||
|
||||
local function send_item_to_inv(hopper_inv, target_pos, filtered_items, placer, target_inv_info, target_def)
|
||||
local target_inv_name = target_inv_info.inventory_name
|
||||
local target_inv = get_container_inventory(target_pos, target_inv_info)
|
||||
if not target_inv then
|
||||
return false
|
||||
end
|
||||
|
||||
local stack
|
||||
local stack_num
|
||||
for i = 1, hopper_inv:get_size("main") do
|
||||
stack = hopper_inv:get_stack("main", i)
|
||||
local item = stack:get_name()
|
||||
if item ~= "" and (filtered_items == nil or filtered_items[item])
|
||||
and target_inv:room_for_item(target_inv_name, item) then
|
||||
stack_num = i
|
||||
break
|
||||
end
|
||||
end
|
||||
if not stack_num then
|
||||
return false
|
||||
end
|
||||
|
||||
local stack_to_put = stack:take_item(1)
|
||||
if target_def.allow_metadata_inventory_put and placer -- backwards compatibility, older versions of this mod didn't record who placed the hopper
|
||||
and target_def.allow_metadata_inventory_put(target_pos, target_inv_name, stack_num, stack_to_put, placer) < 0 then
|
||||
return false
|
||||
end
|
||||
|
||||
hopper_inv:set_stack("main", stack_num, stack)
|
||||
target_inv:add_item(target_inv_name, stack_to_put)
|
||||
if target_def.on_metadata_inventory_put ~= nil and placer ~= nil then
|
||||
target_def.on_metadata_inventory_put(target_pos, target_inv_name, stack_num, stack_to_put, placer)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- Used to put items from the hopper inventory into the target block
|
||||
hopper.send_item_to = function(hopper_pos, target_pos, target_node, target_inv_info, filtered_items)
|
||||
local target_def = minetest.registered_nodes[target_node.name]
|
||||
@ -137,53 +194,20 @@ hopper.send_item_to = function(hopper_pos, target_pos, target_node, target_inv_i
|
||||
end
|
||||
|
||||
local hopper_meta = minetest.get_meta(hopper_pos)
|
||||
local eject_item = hopper.config.eject_button_enabled and hopper_meta:get_string("eject") == "true" and target_def.buildable_to
|
||||
|
||||
if not eject_item and not target_inv_info then
|
||||
return false
|
||||
end
|
||||
|
||||
--hopper inventory
|
||||
local hopper_inv = hopper_meta:get_inventory()
|
||||
if hopper_inv:is_empty("main") == true then
|
||||
return false
|
||||
end
|
||||
local hopper_inv_size = hopper_inv:get_size("main")
|
||||
local placer = get_placer(hopper_meta:get_string("placer"))
|
||||
|
||||
--source inventory
|
||||
local target_inv_name = target_inv_info.inventory_name
|
||||
local target_inv = get_container_inventory(target_pos, target_inv_info)
|
||||
if not target_inv then
|
||||
return false
|
||||
if target_inv_info then
|
||||
local placer = get_placer(hopper_meta:get_string("placer"))
|
||||
return send_item_to_inv(hopper_inv, target_pos, filtered_items, placer, target_inv_info, target_def)
|
||||
end
|
||||
|
||||
for i = 1,hopper_inv_size do
|
||||
local stack = hopper_inv:get_stack("main", i)
|
||||
local item = stack:get_name()
|
||||
if item ~= "" and (filtered_items == nil or filtered_items[item]) then
|
||||
if target_inv_name then
|
||||
if target_inv:room_for_item(target_inv_name, item) then
|
||||
local stack_to_put = stack:take_item(1)
|
||||
if target_def.allow_metadata_inventory_put == nil
|
||||
or placer == nil -- backwards compatibility, older versions of this mod didn't record who placed the hopper
|
||||
or target_def.allow_metadata_inventory_put(target_pos, target_inv_name, i, stack_to_put, placer) > 0 then
|
||||
hopper_inv:set_stack("main", i, stack)
|
||||
--add to target node
|
||||
target_inv:add_item(target_inv_name, stack_to_put)
|
||||
if target_def.on_metadata_inventory_put ~= nil and placer ~= nil then
|
||||
target_def.on_metadata_inventory_put(target_pos, target_inv_name, i, stack_to_put, placer)
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
elseif eject_item then
|
||||
local stack_to_put = stack:take_item(1)
|
||||
minetest.add_item(target_pos, stack_to_put)
|
||||
hopper_inv:set_stack("main", i, stack)
|
||||
return true
|
||||
end
|
||||
end
|
||||
if hopper.config.eject_button_enabled and target_def.buildable_to
|
||||
and hopper_meta:get_string("eject") == "true" then
|
||||
return send_item_to_air(hopper_inv, target_pos, filtered_items)
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user