Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sorter and eject #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions abms.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ minetest.register_abm({
destination_pos = vector.add(pos, destination_dir)
end

local output_direction
local output_direction = "bottom"
if destination_dir.y == 0 then
output_direction = "horizontal"
output_direction = "side"
end

local source_node = minetest.get_node(source_pos)
Expand All @@ -112,13 +112,11 @@ minetest.register_abm({

local registered_destination_inventories = hopper.get_registered(destination_node.name)
if registered_destination_inventories ~= nil then
if output_direction == "horizontal" then
hopper.send_item_to(pos, destination_pos, destination_node, registered_destination_inventories["side"])
else
hopper.send_item_to(pos, destination_pos, destination_node, registered_destination_inventories["bottom"])
if not hopper.send_item_to(pos, destination_pos, destination_node, registered_destination_inventories[output_direction]) then
hopper.eject_item(pos, destination_pos)
end
else
hopper.send_item_to(pos, destination_pos, destination_node) -- for handling ejection
hopper.eject_item(pos, destination_pos)
end
end,
})
12 changes: 5 additions & 7 deletions nodes/chute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,19 @@ minetest.register_node("hopper:chute", {
local node = minetest.get_node(pos)
local dir = minetest.facedir_to_dir(node.param2)
local destination_pos = vector.add(pos, dir)
local output_direction
local output_direction = "bottom"
if dir.y == 0 then
output_direction = "horizontal"
output_direction = "side"
end

local destination_node = minetest.get_node(destination_pos)
local registered_inventories = hopper.get_registered(destination_node.name)
if registered_inventories ~= nil then
if output_direction == "horizontal" then
hopper.send_item_to(pos, destination_pos, destination_node, registered_inventories["side"])
else
hopper.send_item_to(pos, destination_pos, destination_node, registered_inventories["bottom"])
if not hopper.send_item_to(pos, destination_pos, destination_node, registered_inventories[output_direction]) then
hopper.eject_item(pos, destination_pos)
end
else
hopper.send_item_to(pos, destination_pos, destination_node)
hopper.eject_item(pos, destination_pos)
end

if not inv:is_empty("main") then
Expand Down
12 changes: 7 additions & 5 deletions nodes/sorter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,23 @@ minetest.register_node("hopper:sorter", {
local filter_output_direction = (dir.y == 0) and "side" or "bottom"

--- returns success? = true/false
local function try_send_item(output_dir, dst_pos)
local function try_send_item(output_dir, dst_pos, filter_items_map)
local dst_node = minetest.get_node(dst_pos)
local registered_inventories = hopper.get_registered(dst_node.name)

if registered_inventories ~= nil then
return hopper.send_item_to(pos, dst_pos, dst_node, registered_inventories[output_dir], filter_items)
return hopper.send_item_to(pos, dst_pos, dst_node, registered_inventories[output_dir], filter_items_map)
end

return hopper.send_item_to(pos, dst_pos, dst_node, nil, filter_items)
return hopper.eject_item(pos, dst_pos)
end

if not try_send_item(filter_output_direction, filter_destination_pos) then
if not try_send_item(filter_output_direction, filter_destination_pos, filter_items) then
-- weren't able to put something in the filter destination, for whatever reason.
-- Now we can start moving stuff forward to the default.
try_send_item(default_output_direction, default_destination_pos)
if not try_send_item(default_output_direction, default_destination_pos) then
hopper.eject_item(pos, default_destination_pos)
end
end

if not inv:is_empty("main") then
Expand Down
45 changes: 34 additions & 11 deletions utility.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,25 +129,39 @@ 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 function send_item_to_air(hopper_inv, target_pos)
local item
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
item = stack:get_name()
if item ~= "" then
stack_num = i
break
end
end
if not stack_num then
return false

local eject_node = minetest.get_node(target_pos)
if (eject_node["name"] ~= "air") then
minetest.log("verbose", "hopper.send_item_to_air: eject direction not air ("..eject_node["name"].." at "..target_pos:to_string().."). Looking for alternate.")
local air_pos
local radius = 1
while not air_pos and radius <= 5 do
air_pos = minetest.find_node_near(target_pos, radius, {"air"})
radius = radius + 1
end
if not air_pos then
minetest.log("warning", "hopper.send_item_to_air: could not find an air node nearby")
return
end
target_pos = air_pos
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
hopper.log_inventory("hopper ejecting "..item.." to "..target_pos:to_string())
end

local function send_item_to_inv(hopper_inv, target_pos, filtered_items, placer, target_inv_info, target_def)
Expand Down Expand Up @@ -204,10 +218,19 @@ hopper.send_item_to = function(hopper_pos, target_pos, target_node, target_inv_i
return send_item_to_inv(hopper_inv, target_pos, filtered_items, placer, target_inv_info, target_def)
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

hopper.eject_item = function(hopper_pos, target_pos)
if hopper.config.eject_button_enabled then

local hopper_meta = minetest.get_meta(hopper_pos)
if hopper_meta:get_string("eject") == "true" then
local hopper_inv = hopper_meta:get_inventory()
if hopper_inv:is_empty("main") then
return
end
send_item_to_air(hopper_inv, target_pos)
end
end
end