Extract fluid API functions to separate file
This commit is contained in:
parent
cb41e76742
commit
0363a2ba26
74
api.lua
74
api.lua
@ -60,80 +60,6 @@ industrialtest.api.afterToolUse=function(itemstack)
|
||||
itemstack:set_wear(65535-uses/def.tool_capabilities.uses*65535)
|
||||
end
|
||||
|
||||
-- \brief Check if itemstack contains fluid storage
|
||||
-- \param itemstack ItemStack
|
||||
-- \returns bool
|
||||
industrialtest.api.itemHasFluidStorage=function(itemstack)
|
||||
local values={"industrialtest.fluidAmount","industrialtest.fluidCapacity"}
|
||||
local meta=itemstack:get_meta()
|
||||
for _,value in ipairs(values) do
|
||||
if not meta:contains(value) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- \brief Updates itemstack description and wear depending on contained fluid
|
||||
-- \param itemstack ItemStack
|
||||
-- \returns nil
|
||||
industrialtest.api.updateItemFluidText=function(itemstack)
|
||||
local meta=itemstack:get_meta()
|
||||
local def=itemstack:get_definition()
|
||||
meta:set_string("description",S("@1\n@2 / @3 mB",def.description,meta:get_int("industrialtest.fluidAmount"),meta:get_int("industrialtest.fluidCapacity")))
|
||||
itemstack:set_wear(65535-meta:get_int("industrialtest.fluidAmount")/meta:get_int("industrialtest.fluidCapacity")*65534)
|
||||
end
|
||||
|
||||
-- \brief Prepares itemstack containing fluid storage
|
||||
-- \param itemstack ItemStack
|
||||
-- \returns bool
|
||||
industrialtest.api.prepareFluidStorageItem=function(itemstack)
|
||||
local meta=itemstack:get_meta()
|
||||
local def=itemstack:get_definition()
|
||||
if industrialtest.api.itemHasFluidStorage(itemstack) or not def.groups or not def.groups._industrialtest_fluidStorage or not def._industrialtest_fluidCapacity then
|
||||
return false
|
||||
end
|
||||
meta:set_int("industrialtest.fluidAmount",0)
|
||||
meta:set_int("industrialtest.fluidCapacity",def._industrialtest_fluidCapacity)
|
||||
industrialtest.api.updateItemFluidText(itemstack)
|
||||
return true
|
||||
end
|
||||
|
||||
-- \brief Adds fluid amount to item fluid storage
|
||||
-- \param itemstack ItemStack
|
||||
-- \param amount number
|
||||
-- \returns number
|
||||
industrialtest.api.addFluidToItem=function(itemstack,amount)
|
||||
local meta=itemstack:get_meta()
|
||||
if not industrialtest.api.itemHasFluidStorage(itemstack) then
|
||||
return 0
|
||||
end
|
||||
local fluidAmount=meta:get_int("industrialtest.fluidAmount")
|
||||
local fluidCapacity=meta:get_int("industrialtest.fluidCapacity")
|
||||
local prevFluidAmount=fluidAmount
|
||||
fluidAmount=industrialtest.internal.clamp(fluidAmount+amount,0,fluidCapacity)
|
||||
meta:set_int("industrialtest.fluidAmount",fluidAmount)
|
||||
industrialtest.api.updateItemFluidText(itemstack)
|
||||
return fluidAmount-prevFluidAmount
|
||||
end
|
||||
|
||||
-- \brief Adds fluid to destination itemstack while subtracting it from source itemstack's metadata
|
||||
-- \param srcItemstack ItemStack
|
||||
-- \param itemstack ItemStack
|
||||
-- \param amount number
|
||||
-- \returns number
|
||||
industrialtest.api.transferFluidToItem=function(srcItemstack,itemstack,amount)
|
||||
local meta=srcItemstack:get_meta()
|
||||
local flow=math.min(meta:get_int("industrialtest.fluidAmount"),amount)
|
||||
if flow==0 then
|
||||
return 0
|
||||
end
|
||||
local actualFlow=industrialtest.api.addFluidToItem(itemstack,flow)
|
||||
meta:set_int("industrialtest.fluidAmount",meta:get_int("industrialtest.fluidAmount")-actualFlow)
|
||||
industrialtest.api.updateItemFluidText(srcItemstack)
|
||||
return actualFlow
|
||||
end
|
||||
|
||||
-- \brief Transfers power from source node to it's network, if sides is set then power will be only transfered to network connected to that sides
|
||||
-- \param pos Vector with position of source node
|
||||
-- \param (optional) sides table with Vectors
|
||||
|
89
api/fluid.lua
Normal file
89
api/fluid.lua
Normal file
@ -0,0 +1,89 @@
|
||||
-- IndustrialTest
|
||||
-- Copyright (C) 2024 mrkubax10
|
||||
|
||||
-- This program is free software: you can redistribute it and/or modify
|
||||
-- it under the terms of the GNU General Public License as published by
|
||||
-- the Free Software Foundation, either version 3 of the License, or
|
||||
-- (at your option) any later version.
|
||||
|
||||
-- This program is distributed in the hope that it will be useful,
|
||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
-- GNU General Public License for more details.
|
||||
|
||||
-- You should have received a copy of the GNU General Public License
|
||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
-- \brief Prepares itemstack containing fluid storage
|
||||
-- \param itemstack ItemStack
|
||||
-- \returns bool
|
||||
function industrialtest.api.prepareFluidStorageItem(itemstack)
|
||||
local meta=itemstack:get_meta()
|
||||
local def=itemstack:get_definition()
|
||||
if industrialtest.api.itemHasFluidStorage(itemstack) or not def.groups or not def.groups._industrialtest_fluidStorage or not def._industrialtest_fluidCapacity then
|
||||
return false
|
||||
end
|
||||
meta:set_int("industrialtest.fluidAmount",0)
|
||||
meta:set_int("industrialtest.fluidCapacity",def._industrialtest_fluidCapacity)
|
||||
industrialtest.api.updateItemFluidText(itemstack)
|
||||
return true
|
||||
end
|
||||
|
||||
-- \brief Check if itemstack contains fluid storage
|
||||
-- \param itemstack ItemStack
|
||||
-- \returns bool
|
||||
function industrialtest.api.itemHasFluidStorage(itemstack)
|
||||
local values={"industrialtest.fluidAmount","industrialtest.fluidCapacity"}
|
||||
local meta=itemstack:get_meta()
|
||||
for _,value in ipairs(values) do
|
||||
if not meta:contains(value) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- \brief Updates itemstack description and wear depending on contained fluid
|
||||
-- \param itemstack ItemStack
|
||||
-- \returns nil
|
||||
function industrialtest.api.updateItemFluidText(itemstack)
|
||||
local meta=itemstack:get_meta()
|
||||
local def=itemstack:get_definition()
|
||||
meta:set_string("description",S("@1\n@2 / @3 mB",def.description,meta:get_int("industrialtest.fluidAmount"),meta:get_int("industrialtest.fluidCapacity")))
|
||||
itemstack:set_wear(65535-meta:get_int("industrialtest.fluidAmount")/meta:get_int("industrialtest.fluidCapacity")*65534)
|
||||
end
|
||||
|
||||
-- \brief Adds fluid amount to item fluid storage
|
||||
-- \param itemstack ItemStack
|
||||
-- \param amount number
|
||||
-- \returns number
|
||||
function industrialtest.api.addFluidToItem(itemstack,amount)
|
||||
local meta=itemstack:get_meta()
|
||||
if not industrialtest.api.itemHasFluidStorage(itemstack) then
|
||||
return 0
|
||||
end
|
||||
local fluidAmount=meta:get_int("industrialtest.fluidAmount")
|
||||
local fluidCapacity=meta:get_int("industrialtest.fluidCapacity")
|
||||
local prevFluidAmount=fluidAmount
|
||||
fluidAmount=industrialtest.internal.clamp(fluidAmount+amount,0,fluidCapacity)
|
||||
meta:set_int("industrialtest.fluidAmount",fluidAmount)
|
||||
industrialtest.api.updateItemFluidText(itemstack)
|
||||
return fluidAmount-prevFluidAmount
|
||||
end
|
||||
|
||||
-- \brief Adds fluid to destination itemstack while subtracting it from source itemstack's metadata
|
||||
-- \param srcItemstack ItemStack
|
||||
-- \param itemstack ItemStack
|
||||
-- \param amount number
|
||||
-- \returns number
|
||||
function industrialtest.api.transferFluidToItem(srcItemstack,itemstack,amount)
|
||||
local meta=srcItemstack:get_meta()
|
||||
local flow=math.min(meta:get_int("industrialtest.fluidAmount"),amount)
|
||||
if flow==0 then
|
||||
return 0
|
||||
end
|
||||
local actualFlow=industrialtest.api.addFluidToItem(itemstack,flow)
|
||||
meta:set_int("industrialtest.fluidAmount",meta:get_int("industrialtest.fluidAmount")-actualFlow)
|
||||
industrialtest.api.updateItemFluidText(srcItemstack)
|
||||
return actualFlow
|
||||
end
|
Loading…
Reference in New Issue
Block a user