2 -- suck in items on top of hopper
3 minetest.register_abm({
4 label = "Hopper suction",
5 nodenames = {"hopper:hopper", "hopper:hopper_side"},
8 action = function(pos, node, active_object_count, active_object_count_wider)
9 if active_object_count_wider == 0 then
13 local inv = minetest.get_meta(pos):get_inventory()
16 for _,object in pairs(minetest.get_objects_inside_radius(pos, 1)) do
17 if not object:is_player()
18 and object:get_luaentity()
19 and object:get_luaentity().name == "__builtin:item"
21 and inv:room_for_item("main",
22 ItemStack(object:get_luaentity().itemstring)) then
24 posob = object:getpos()
26 if math.abs(posob.x - pos.x) <= 0.5
27 and posob.y - pos.y <= 0.85
28 and posob.y - pos.y >= 0.3 then
31 ItemStack(object:get_luaentity().itemstring))
33 object:get_luaentity().itemstring = ""
41 -- Used to convert side hopper facing into source and destination relative coordinates
42 -- This was tedious to populate and test
44 [0]={["src"]={x=0, y=1, z=0},["dst"]={x=-1, y=0, z=0}},
45 [1]={["src"]={x=0, y=1, z=0},["dst"]={x=0, y=0, z=1}},
46 [2]={["src"]={x=0, y=1, z=0},["dst"]={x=1, y=0, z=0}},
47 [3]={["src"]={x=0, y=1, z=0},["dst"]={x=0, y=0, z=-1}},
48 [4]={["src"]={x=0, y=0, z=1},["dst"]={x=-1, y=0, z=0}},
49 [5]={["src"]={x=0, y=0, z=1},["dst"]={x=0, y=-1, z=0}},
50 [6]={["src"]={x=0, y=0, z=1},["dst"]={x=1, y=0, z=0}},
51 [7]={["src"]={x=0, y=0, z=1},["dst"]={x=0, y=1, z=0}},
52 [8]={["src"]={x=0, y=0, z=-1},["dst"]={x=-1, y=0, z=0}},
53 [9]={["src"]={x=0, y=0, z=-1},["dst"]={x=0, y=1, z=0}},
54 [10]={["src"]={x=0, y=0, z=-1},["dst"]={x=1, y=0, z=0}},
55 [11]={["src"]={x=0, y=0, z=-1},["dst"]={x=0, y=-1, z=0}},
56 [12]={["src"]={x=1, y=0, z=0},["dst"]={x=0, y=1, z=0}},
57 [13]={["src"]={x=1, y=0, z=0},["dst"]={x=0, y=0, z=1}},
58 [14]={["src"]={x=1, y=0, z=0},["dst"]={x=0, y=-1, z=0}},
59 [15]={["src"]={x=1, y=0, z=0},["dst"]={x=0, y=0, z=-1}},
60 [16]={["src"]={x=-1, y=0, z=0},["dst"]={x=0, y=-1, z=0}},
61 [17]={["src"]={x=-1, y=0, z=0},["dst"]={x=0, y=0, z=1}},
62 [18]={["src"]={x=-1, y=0, z=0},["dst"]={x=0, y=1, z=0}},
63 [19]={["src"]={x=-1, y=0, z=0},["dst"]={x=0, y=0, z=-1}},
64 [20]={["src"]={x=0, y=-1, z=0},["dst"]={x=1, y=0, z=0}},
65 [21]={["src"]={x=0, y=-1, z=0},["dst"]={x=0, y=0, z=1}},
66 [22]={["src"]={x=0, y=-1, z=0},["dst"]={x=-1, y=0, z=0}},
67 [23]={["src"]={x=0, y=-1, z=0},["dst"]={x=0, y=0, z=-1}},
70 local bottomdir = function(facedir)
71 return ({[0]={x=0, y=-1, z=0},
76 {x=0, y=1, z=0}})[math.floor(facedir/4)]
80 minetest.register_abm({
81 label = "Hopper transfer",
82 nodenames = {"hopper:hopper", "hopper:hopper_side"},
83 neighbors = hopper.neighbors,
88 action = function(pos, node, active_object_count, active_object_count_wider)
89 local source_pos, destination_pos, destination_dir
90 if node.name == "hopper:hopper_side" then
91 source_pos = vector.add(pos, directions[node.param2].src)
92 destination_dir = directions[node.param2].dst
93 destination_pos = vector.add(pos, destination_dir)
95 destination_dir = bottomdir(node.param2)
96 source_pos = vector.subtract(pos, destination_dir)
97 destination_pos = vector.add(pos, destination_dir)
100 local output_direction
101 if destination_dir.y == 0 then
102 output_direction = "horizontal"
105 local source_node = minetest.get_node(source_pos)
106 local destination_node = minetest.get_node(destination_pos)
108 local registered_source_inventories = hopper.get_registered_inventories_for(source_node.name)
109 if registered_source_inventories ~= nil then
110 hopper.take_item_from(pos, source_pos, source_node, registered_source_inventories["top"])
113 local registered_destination_inventories = hopper.get_registered_inventories_for(destination_node.name)
114 if registered_destination_inventories ~= nil then
115 if output_direction == "horizontal" then
116 hopper.send_item_to(pos, destination_pos, destination_node, registered_destination_inventories["side"])
118 hopper.send_item_to(pos, destination_pos, destination_node, registered_destination_inventories["bottom"])
121 hopper.send_item_to(pos, destination_pos, destination_node) -- for handling ejection