Added simple API to expand hopper usage

This commit is contained in:
TenPlus1 2017-01-26 16:34:15 +00:00
parent 014da42d6f
commit 7cddb3a124
3 changed files with 138 additions and 62 deletions

@ -17,5 +17,6 @@ Change log:
- 0.9 - Added support for Wine mod's wine barrels - 0.9 - Added support for Wine mod's wine barrels
- 1.0 - New furances do not work properly with hoppers so old reverted to abm furnaces - 1.0 - New furances do not work properly with hoppers so old reverted to abm furnaces
- 1.1 - Hoppers now work with new node timer Furnaces. Reduced Abm's and tidied code. - 1.1 - Hoppers now work with new node timer Furnaces. Reduced Abm's and tidied code.
- 1.2 - Added simple API so that hoppers can work with other containers.
Lucky Blocks: 2 Lucky Blocks: 2

34
api.txt Normal file

@ -0,0 +1,34 @@
Hopper API
----------
This API is kept simple by adding a single command which allows mods to add
containers like chests and furnaces to the hopper check list.
Command Usage
-------------
Make sure any mods using this function has 'hopper' in the depends.txt file.
hopper:add_container({ {"where_from", "node_name", "inventory_name"} })
'where_from' is a string telling the api that items are coming from either
the 'top', going to the 'bottom' or coming from the 'side'.
'node_name" is the name of the container itself (e.g. "default:chest")
'inventory_name' is the name of the container inventory that is affected.
e.g.
hopper:add_container({
{"top", "default:furnace", "dst"}, -- take cooked items into hopper
{"bottom", "default:furnace", "src"}, -- insert items to be cooked from hopper
{"side", "default:furnace", "fuel"}, -- replenish furnace fuel from hopper
})
We already have support for the wine barrel inside of the Wine mod and protected
chests inside of Protector Redo, as well as default chests, furnaces and hoppers
themselves.

165
init.lua

@ -1,4 +1,60 @@
-- define global
hopper = {}
-- default containers ( from position [into hopper], from node, into node inventory )
local containers = {
{"top", "hopper:hopper", "main"},
{"bottom", "hopper:hopper", "main"},
{"side", "hopper:hopper", "main"},
{"side", "hopper:hopper_side", "main"},
{"top", "default:chest", "main"},
{"bottom", "default:chest", "main"},
{"side", "default:chest", "main"},
{"top", "default:furnace", "dst"},
{"bottom", "default:furnace", "src"},
{"side", "default:furnace", "fuel"},
{"top", "default:furnace_active", "dst"},
{"bottom", "default:furnace_active", "src"},
{"side", "default:furnace_active", "fuel"},
}
-- global function to add new containers
function hopper:add_container(list)
for n = 1, #list do
table.insert(containers, list[n])
end
end
-- protector redo mod support
if minetest.get_modpath("protector") then
hopper:add_container({
{"top", "protector:chest", "main"},
{"bottom", "protector:chest", "main"},
{"side", "protector:chest", "main"},
})
end
-- wine mod support
if minetest.get_modpath("wine") then
hopper:add_container({
{"top", "wine:wine_barrel", "dst"},
{"bottom", "wine:wine_barrel", "src"},
{"side", "wine:wine_barrel", "src"},
})
end
-- formspec -- formspec
local function get_hopper_formspec(pos) local function get_hopper_formspec(pos)
@ -17,6 +73,7 @@ local function get_hopper_formspec(pos)
return formspec return formspec
end end
-- hopper -- hopper
minetest.register_node("hopper:hopper", { minetest.register_node("hopper:hopper", {
description = "Hopper", description = "Hopper",
@ -43,7 +100,8 @@ minetest.register_node("hopper:hopper", {
on_construct = function(pos) on_construct = function(pos)
local inv = minetest.get_meta(pos):get_inventory() local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("main", 4*4) inv:set_size("main", 4*4)
end, end,
@ -119,6 +177,7 @@ minetest.register_node("hopper:hopper", {
on_rotate = screwdriver.disallow, on_rotate = screwdriver.disallow,
}) })
-- side hopper -- side hopper
minetest.register_node("hopper:hopper_side", { minetest.register_node("hopper:hopper_side", {
description = "Side Hopper", description = "Side Hopper",
@ -150,7 +209,8 @@ minetest.register_node("hopper:hopper_side", {
on_construct = function(pos) on_construct = function(pos)
local inv = minetest.get_meta(pos):get_inventory() local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("main", 4*4) inv:set_size("main", 4*4)
end, end,
@ -196,6 +256,7 @@ minetest.register_node("hopper:hopper_side", {
on_rotate = screwdriver.rotate_simple, on_rotate = screwdriver.rotate_simple,
}) })
-- suck in items on top of hopper -- suck in items on top of hopper
minetest.register_abm({ minetest.register_abm({
@ -236,8 +297,9 @@ minetest.register_abm({
end, end,
}) })
-- transfer function -- transfer function
local transfer = function(src, srcpos, dst, dstpos, name) local transfer = function(src, srcpos, dst, dstpos)
-- source inventory -- source inventory
local inv = minetest.get_meta(srcpos):get_inventory() local inv = minetest.get_meta(srcpos):get_inventory()
@ -275,28 +337,23 @@ local transfer = function(src, srcpos, dst, dstpos, name)
return return
end end
end end
end end
-- hopper workings -- hopper workings
minetest.register_abm({ minetest.register_abm({
label = "Hopper transfer", label = "Hopper transfer",
nodenames = {"hopper:hopper", "hopper:hopper_side"}, nodenames = {"hopper:hopper", "hopper:hopper_side"},
neighbors = {
"default:chest","default:chest_locked","protector:chest",
"hopper:hopper","hopper:hopper_side","default:furnace",
"default:furnace_active", "wine:wine_barrel"
},
interval = 1.0, interval = 1.0,
chance = 1, chance = 1,
catch_up = false, catch_up = false,
action = function(pos, node) action = function(pos, node)
local front = {} local front
-- if side hopper check which way it's facing -- if side hopper check which way spout is facing
if node.name == "hopper:hopper_side" then if node.name == "hopper:hopper_side" then
local face = minetest.get_node(pos).param2 local face = minetest.get_node(pos).param2
@ -320,68 +377,52 @@ minetest.register_abm({
front = {x = pos.x, y = pos.y - 1, z = pos.z} front = {x = pos.x, y = pos.y - 1, z = pos.z}
end end
-- what is above hopper and on other end of funnel -- get node above hopper
local a = minetest.get_node({x = pos.x, y = pos.y + 1, z = pos.z}).name local top = minetest.get_node({x = pos.x, y = pos.y + 1, z = pos.z}).name
local b = minetest.get_node(front).name
-- funnel input -- get node at other end of spout
local out = minetest.get_node(front).name
if a == "default:chest" local where, nod, inv, def
or a == "default:chest_locked"
or a == "protector:chest"
or a == "hopper:hopper"
or a == "hopper:hopper_side" then
transfer("main", { -- do for loop here for api check
x = pos.x, for n = 1, #containers do
y = pos.y + 1,
z = pos.z
}, "main", pos)
elseif a == "default:furnace" where = containers[n][1]
or a == "default:furnace_active" nod = containers[n][2]
or a == "wine:wine_barrel" then inv = containers[n][3]
transfer("dst", { -- take from top node into hopper
x = pos.x, if where == "top" and top == nod
y = pos.y + 1, and (node.name == "hopper:hopper" or node.name == "hopper:hopper_side") then
z = pos.z --print ("-- top")
}, "main", pos) transfer(inv, {x = pos.x, y = pos.y + 1, z = pos.z}, "main", pos)
end minetest.get_node_timer(
{x = pos.x, y = pos.y + 1, z = pos.z}):start(0.5)
return
-- spout output -- take from hopper into bottom node
elseif where == "bottom" and out == nod
and node.name == "hopper:hopper" then
--print ("-- bot")
transfer("main", pos, inv, front)
minetest.get_node_timer(front):start(0.5)
return
if b == "default:chest" -- take from side hopper into node
or b == "default:chest_locked" elseif where == "side" and out == nod
or b == "protector:chest" and node.name == "hopper:hopper_side" then
or b == "hopper:hopper" --print ("-- sid")
or b == "hopper:hopper_side" then transfer("main", pos, inv, front)
minetest.get_node_timer(front):start(0.5)
return
transfer("main", pos, "main", front)
elseif b == "default:furnace"
or b == "default:furnace_active" then
if node.name == "hopper:hopper" then
-- hopper above to furnace source below
transfer("main", pos, "src", front)
else
-- hopper to furnace fuel beside
transfer("main", pos, "fuel", front)
end end
-- re-start furnace timer
minetest.get_node_timer(front):start(0.5)--(1.0)
elseif b == "wine:wine_barrel" then
-- hopper to wine source beside
transfer("main", pos, "src", front)
end end
end, end,
}) })
-- hopper recipe -- hopper recipe
minetest.register_craft({ minetest.register_craft({
output = "hopper:hopper", output = "hopper:hopper",
@ -391,9 +432,8 @@ minetest.register_craft({
}, },
}) })
-- add lucky blocks
-- Hopper mod -- add lucky blocks
if minetest.get_modpath("lucky_block") then if minetest.get_modpath("lucky_block") then
lucky_block:add_blocks({ lucky_block:add_blocks({
@ -402,4 +442,5 @@ if minetest.get_modpath("lucky_block") then
}) })
end end
print ("[MOD] Hopper loaded") print ("[MOD] Hopper loaded")