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

View File

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

View File

@ -1,4 +1,4 @@
name = elepower_fapi
description = Elepower Fluid Transfer API
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" },
}
minetest.register_node("elepower_farming:tree_extractor", {
minetest.register_node("elepower_farming:tree_extractor", elefluid.add_bucket_handler({
description = "Tree Fluid Extractor",
groups = {fluid_container = 1, oddly_breakable_by_hand = 1, cracky = 1},
tiles = {
@ -23,7 +23,7 @@ minetest.register_node("elepower_farming:tree_extractor", {
}
},
paramtype2 = "facedir"
})
}))
minetest.register_abm({
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},
fluid_buffers = {
tree = {
capacity = 8000,
accepts = {"group:tree_fluid"}
capacity = 8000,
accepts = {"group:tree_fluid"},
drainable = false,
},
water = {
capacity = 8000,
accepts = {"default:water_source"}
capacity = 8000,
accepts = {"default:water_source"},
drainable = false,
},
output = {
capacity = 8000