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
No known key found for this signature in database
GPG Key ID: 1688DA83D222D0B5
6 changed files with 88 additions and 23 deletions

59
elepower_fapi/bucket.lua Normal 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

@ -33,6 +33,7 @@ function elefluid.get_buffer_data(pos, buffer)
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
@ -40,8 +41,9 @@ function elefluid.get_buffer_data(pos, buffer)
fluid = ffluid fluid = ffluid
amount = fluidcount amount = fluidcount
accepts = true
capacity = fcapacity capacity = fcapacity
accepts = true
drainable = true
end end
return { return {
@ -49,6 +51,7 @@ function elefluid.get_buffer_data(pos, buffer)
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

@ -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")

@ -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

@ -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"},

@ -131,11 +131,13 @@ ele.register_machine("elepower_farming:tree_processor", {
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