Add buffer_to_string API function

This commit is contained in:
Evert Prants 2018-06-21 12:38:16 +03:00
parent 0260ee789e
commit 69b7e1c410
No known key found for this signature in database
GPG Key ID: 1688DA83D222D0B5
3 changed files with 22 additions and 14 deletions

@ -137,3 +137,16 @@ function fluid_lib.take_from_buffer(pos, buffer, count)
return bfdata.fluid, take_count return bfdata.fluid, take_count
end end
function fluid_lib.buffer_to_string(buffer)
if not buffer then return "" end
local amount = fluid_lib.comma_value(buffer.amount)
local capacity = fluid_lib.comma_value(buffer.capacity)
local description = "Empty"
if buffer.fluid ~= "" then
description = fluid_lib.cleanse_node_description(buffer.fluid)
end
return ("%s (%s / %s %s)"):format(description, amount, capacity, fluid_lib.unit)
end

@ -38,4 +38,9 @@ function fluid_lib.cleanse_node_description(node)
return no_source return no_source
end end
function fluid_lib.comma_value(n) -- credit http://richard.warburton.it
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
dofile(modpath.."/buffer.lua") dofile(modpath.."/buffer.lua")

@ -12,17 +12,12 @@ local function preserve_metadata(pos, oldnode, oldmeta, drops)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
local ndef = minetest.registered_nodes[node.name] local ndef = minetest.registered_nodes[node.name]
local fluid_desc = "Empty"
if buffer.fluid ~= "" then
fluid_desc = fluid_lib.cleanse_node_description(buffer.fluid)
end
for i,stack in pairs(drops) do for i,stack in pairs(drops) do
local stack_meta = stack:get_meta() local stack_meta = stack:get_meta()
stack_meta:set_int("fluid_storage", buffer.amount) stack_meta:set_int("fluid_storage", buffer.amount)
stack_meta:set_string("fluid", buffer.fluid) stack_meta:set_string("fluid", buffer.fluid)
stack_meta:set_string("description", ("%s\nContains: %s (%d/%d %s)"):format(ndef.description, stack_meta:set_string("description", ("%s\nContents: %s (%d/%d %s)"):format(ndef.description,
fluid_desc, buffer.amount, buffer.capacity, fluid_lib.unit)) fluid_lib.buffer_to_string(buffer)))
drops[i] = stack drops[i] = stack
end end
@ -78,14 +73,9 @@ local function tank_on_timer(pos, elapsed)
meta:set_string("buffer_fluid", "") meta:set_string("buffer_fluid", "")
end end
local fluid_desc = "Empty"
if buffer.fluid ~= "" then
fluid_desc = fluid_lib.cleanse_node_description(buffer.fluid)
end
-- Update infotext -- Update infotext
meta:set_string("infotext", ("%s\nContains: %s (%d/%d %s)"):format(ndef.description, fluid_desc, meta:set_string("infotext", ("%s\nContents: %s"):format(ndef.description,
buffer.amount, buffer.capacity, fluid_lib.unit)) fluid_lib.buffer_to_string(buffer)))
local param2 = math.min(percentile * 63, 63) local param2 = math.min(percentile * 63, 63)