From 0363a2ba26b0da53a13b690dfd0476cffe40f075 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Sun, 8 Sep 2024 22:36:33 +0200 Subject: [PATCH] Extract fluid API functions to separate file --- api.lua | 74 ------------------------------------------ api/fluid.lua | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ init.lua | 1 + 3 files changed, 90 insertions(+), 74 deletions(-) create mode 100644 api/fluid.lua diff --git a/api.lua b/api.lua index dcabe57..1c08810 100644 --- a/api.lua +++ b/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 diff --git a/api/fluid.lua b/api/fluid.lua new file mode 100644 index 0000000..f5e08c7 --- /dev/null +++ b/api/fluid.lua @@ -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 . + +-- \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 diff --git a/init.lua b/init.lua index bb190bf..ac42666 100644 --- a/init.lua +++ b/init.lua @@ -32,6 +32,7 @@ dofile(modpath.."/compatibility.lua") dofile(modpath.."/minerals.lua") dofile(modpath.."/api/common.lua") +dofile(modpath.."/api/fluid.lua") dofile(modpath.."/api/power.lua") dofile(modpath.."/api/side.lua") dofile(modpath.."/api.lua")