From e69d16aeb006bcc9d28449a508ac571811cc0c10 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Thu, 11 Jan 2024 19:48:09 +0100 Subject: [PATCH] Add simple item fluid storage API --- api.lua | 74 +++++++++++++++++++++++++++++++++++++++++++++++ craftitems.lua | 11 +++---- tools/jetpack.lua | 12 +++++--- 3 files changed, 88 insertions(+), 9 deletions(-) diff --git a/api.lua b/api.lua index c3d082e..8c67fff 100644 --- a/api.lua +++ b/api.lua @@ -151,6 +151,79 @@ industrialtest.api.afterToolUse=function(itemstack) meta:set_int("industrialtest.uses",uses) 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",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",100) + 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 metadata +-- \param srcMeta MetaDataRef +-- \param itemstack ItemStack +-- \param amount number +-- \returns number +industrialtest.api.transferFluidToItem=function(srcMeta,itemstack,amount) + local flow=math.min(srcMeta:get_int("industrialtest.fluidAmount"),amount) + if flow==0 then + return 0 + end + local actualFlow=industrialtest.api.addFluidToItem(itemstack,flow) + srcMeta:set_int("industrialtest.fluidAmount",srcMeta:get_int("industrialtest.fluidAmount")-actualFlow) + return actualFlow +end + -- \brief Checks if power storage is fully charged -- \param meta MetaDataRef which should be checked -- \returns true if power storage is fully charged, false otherwise @@ -198,6 +271,7 @@ end -- \brief Adds power to destination itemstack while subtracting it from source metadata -- \param srcMeta MetaDataRef from which take power -- \param itemstack ItemStack to which add power +-- \param amount number -- \returns How much of power was actually transferred industrialtest.api.transferPowerToItem=function(srcMeta,itemstack,amount) local currentFlow=math.min(srcMeta:get_int("industrialtest.powerAmount"),amount) diff --git a/craftitems.lua b/craftitems.lua index d76afa0..36f854b 100644 --- a/craftitems.lua +++ b/craftitems.lua @@ -690,8 +690,9 @@ minetest.register_tool("industrialtest:fuel_can",{ inventory_image="industrialtest_fuel_can.png", groups={ _industrialtest_fueled=1, - _industrialtest_emptyOnConstruct=1 - } + _industrialtest_fluidStorage=1 + }, + _industrialtest_fluidCapacity=10000 }) minetest.register_craft({ type="shaped", @@ -706,14 +707,14 @@ minetest.register_craft({ -- Item callbacks minetest.register_on_player_inventory_action(function(player,action,inventory,info) if action=="put" then - if industrialtest.api.preparePowerStorageItem(info.stack) or industrialtest.api.prepareToolItem(info.stack) then + if industrialtest.api.preparePowerStorageItem(info.stack) or industrialtest.api.prepareToolItem(info.stack) or industrialtest.api.prepareFluidStorageItem(info.stack) then inventory:set_stack(info.listname,info.index,info.stack) end end end) minetest.register_on_craft(function(itemstack) - if industrialtest.api.preparePowerStorageItem(itemstack) then + if industrialtest.api.preparePowerStorageItem(itemstack) or industrialtest.api.prepareToolItem(itemstack) then return end - industrialtest.api.prepareToolItem(itemstack) + industrialtest.api.prepareFluidStorageItem(itemstack) end) diff --git a/tools/jetpack.lua b/tools/jetpack.lua index b90e44c..b2d940d 100644 --- a/tools/jetpack.lua +++ b/tools/jetpack.lua @@ -96,10 +96,11 @@ local function onGlobalStep(player,inv,itemstack,index,def) end jetpack.tryFly=function(itemstack) - if itemstack:get_wear()>=65533 then + local meta=itemstack:get_meta() + if meta:get_int("industrialtest.fluidAmount")==0 then return false end - itemstack:set_wear(itemstack:get_wear()+2) + industrialtest.api.addFluidToItem(itemstack,-1) return true end @@ -109,9 +110,12 @@ registerJetpack({ displayName=S("Jetpack"), groups={ _industrialtest_fueled=1, - _industrialtest_emptyOnConstruct=1 + _industrialtest_fluidStorage=1 }, - tryFly=jetpack.tryFly + tryFly=jetpack.tryFly, + customKeys={ + _industrialtest_fluidCapacity=5000 + } }) minetest.register_craft({ type="shaped",