Add optional bucket support for internal buffers

This commit is contained in:
Evert Prants 2018-06-20 15:27:46 +03:00
parent 4012bf27a8
commit 8cdd2ec47f
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
6 changed files with 88 additions and 23 deletions

59
elepower_fapi/bucket.lua Normal file
View File

@ -0,0 +1,59 @@
local b_enabled = minetest.get_modpath("bucket") ~= nil
local function find_fluid(itemname)
for _,data in ipairs(bucket.liquids) do
if data.itemname and data.itemname == itemname then
return data.source
end
end
return nil
end
function elefluid.add_bucket_handler(nodedef)
if not b_enabled then return nodedef end
local orig = nodedef.on_rightclick
nodedef.on_rightclick = function (pos, node, clicker, itemstack, pointed_thing)
local bucket_name = itemstack:get_name()
local action = false
if bucket_name == "bucket:bucket_empty" then
local buffers = elefluid.get_node_buffers(pos)
for buffer in pairs(buffers) do
if elefluid.can_take_from_buffer(pos, buffer, 1000) == 1000 then
local fluid = elefluid.take_from_buffer(pos, buffer, 1000)
if bucket.liquids[fluid] then
itemstack = ItemStack(bucket.liquids[fluid].itemname)
action = true
end
break
end
end
elseif find_fluid(bucket_name) then
local fluid = find_fluid(bucket)
local buffers = elefluid.get_node_buffers(pos)
for buffer in pairs(buffers) do
if elefluid.can_insert_into_buffer(pos, buffer, fluid, 1000) == 1000 then
elefluid.insert_into_buffer(pos, buffer, fluid, 1000)
itemstack = ItemStack("bucket:bucket_empty")
action = true
break
end
end
end
-- Really stupid workaround so that the formspec closes after a bucket action was committed
if action and not orig and clicker then
minetest.close_formspec(clicker:get_player_name(), "")
end
if orig then
return orig(pos, node, clicker, itemstack, pointed_thing)
end
return itemstack
end
return nodedef
end

View File

@ -28,27 +28,30 @@ function elefluid.get_buffer_data(pos, buffer)
return nil return nil
end end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local fluid = meta:get_string(buffer .. "_fluid") local fluid = meta:get_string(buffer .. "_fluid")
local amount = meta:get_int(buffer .. "_fluid_storage") local amount = meta:get_int(buffer .. "_fluid_storage")
local capacity = buffers[buffer].capacity local capacity = buffers[buffer].capacity
local accepts = buffers[buffer].accepts local accepts = buffers[buffer].accepts
local drainable = buffers[buffer].drainable or true
-- Fluidity tanks compatibility -- Fluidity tanks compatibility
if buffer == "fluidity" then if buffer == "fluidity" then
local ffluid, fluidcount, fcapacity, fbasetank, fmod = fluidity.tanks.get_tank_at(pos) local ffluid, fluidcount, fcapacity, fbasetank, fmod = fluidity.tanks.get_tank_at(pos)
fluid = ffluid fluid = ffluid
amount = fluidcount amount = fluidcount
accepts = true capacity = fcapacity
capacity = fcapacity accepts = true
drainable = true
end end
return { return {
fluid = fluid, fluid = fluid,
amount = amount, amount = amount,
accepts = accepts, accepts = accepts,
capacity = capacity, capacity = capacity,
drainable = drainable,
} }
end end
@ -81,8 +84,8 @@ end
function elefluid.can_insert_into_buffer(pos, buffer, fluid, count) function elefluid.can_insert_into_buffer(pos, buffer, fluid, count)
local bfdata = elefluid.get_buffer_data(pos, buffer) local bfdata = elefluid.get_buffer_data(pos, buffer)
if not bfdata then return nil end if not bfdata then return 0 end
if bfdata.fluid ~= fluid and bfdata.fluid ~= "" then return nil end if bfdata.fluid ~= fluid and bfdata.fluid ~= "" then return 0 end
local can_put = 0 local can_put = 0
if bfdata.amount + count > bfdata.capacity then if bfdata.amount + count > bfdata.capacity then
@ -116,7 +119,7 @@ end
function elefluid.can_take_from_buffer(pos, buffer, count) function elefluid.can_take_from_buffer(pos, buffer, count)
local bfdata = elefluid.get_buffer_data(pos, buffer) local bfdata = elefluid.get_buffer_data(pos, buffer)
if not bfdata then return nil end if not bfdata or not bfdata.drainable then return 0 end
local amount = bfdata.amount local amount = bfdata.amount
local take_count = 0 local take_count = 0

View File

@ -13,3 +13,4 @@ dofile(modpath.."/transfer.lua")
dofile(modpath.."/transfer_node.lua") dofile(modpath.."/transfer_node.lua")
dofile(modpath.."/formspec.lua") dofile(modpath.."/formspec.lua")
dofile(modpath.."/buffer.lua") dofile(modpath.."/buffer.lua")
dofile(modpath.."/bucket.lua")

View File

@ -1,4 +1,4 @@
name = elepower_fapi name = elepower_fapi
description = Elepower Fluid Transfer API description = Elepower Fluid Transfer API
depends = elepower_papi depends = elepower_papi
optional_depends = fluidity optional_depends = fluidity,bucket

View File

@ -9,7 +9,7 @@ local fluid_table = {
["default:aspen_tree"] = { fpc = 50, fluid = "elepower_farming:resin_source" }, ["default:aspen_tree"] = { fpc = 50, fluid = "elepower_farming:resin_source" },
} }
minetest.register_node("elepower_farming:tree_extractor", { minetest.register_node("elepower_farming:tree_extractor", elefluid.add_bucket_handler({
description = "Tree Fluid Extractor", description = "Tree Fluid Extractor",
groups = {fluid_container = 1, oddly_breakable_by_hand = 1, cracky = 1}, groups = {fluid_container = 1, oddly_breakable_by_hand = 1, cracky = 1},
tiles = { tiles = {
@ -23,7 +23,7 @@ minetest.register_node("elepower_farming:tree_extractor", {
} }
}, },
paramtype2 = "facedir" paramtype2 = "facedir"
}) }))
minetest.register_abm({ minetest.register_abm({
nodenames = {"elepower_farming:tree_extractor"}, nodenames = {"elepower_farming:tree_extractor"},

View File

@ -130,12 +130,14 @@ ele.register_machine("elepower_farming:tree_processor", {
groups = {ele_user = 1, oddly_breakable_by_hand = 1, cracky = 1, fluid_container = 1}, groups = {ele_user = 1, oddly_breakable_by_hand = 1, cracky = 1, fluid_container = 1},
fluid_buffers = { fluid_buffers = {
tree = { tree = {
capacity = 8000, capacity = 8000,
accepts = {"group:tree_fluid"} accepts = {"group:tree_fluid"},
drainable = false,
}, },
water = { water = {
capacity = 8000, capacity = 8000,
accepts = {"default:water_source"} accepts = {"default:water_source"},
drainable = false,
}, },
output = { output = {
capacity = 8000 capacity = 8000