From 3041d0982fd0eac178481b01ae9df74fcc428516 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Fri, 8 Mar 2024 08:56:56 +0100 Subject: [PATCH] Deduplicate machine charging from power storage item code --- machines/canning_machine.lua | 19 ++---------------- machines/common.lua | 39 +++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/machines/canning_machine.lua b/machines/canning_machine.lua index 03c4f32..93a3a6c 100644 --- a/machines/canning_machine.lua +++ b/machines/canning_machine.lua @@ -85,15 +85,7 @@ canningMachine.onTimer=function(pos,elapsed,meta,inv) local powerStorageSlot=inv:get_stack("powerStorage",1) local targetMeta=targetSlot:get_meta() - if not powerStorageSlot:is_empty() then - local stackMeta=powerStorageSlot:get_meta() - if industrialtest.api.transferPower(stackMeta,meta,stackMeta:get_int("industrialtest.powerFlow"))>0 then - shouldUpdateFormspec=true - shouldRerunTimer=true - industrialtest.api.updateItemPowerText(powerStorageSlot) - inv:set_stack("powerStorage",1,powerStorageSlot) - end - end + shouldRerunTimer,shouldUpdateFormspec=industrialtest.internal.chargeFromPowerStorageItem(meta,inv) local def=fuelSlot:get_definition() if not fuelSlot:is_empty() and not targetSlot:is_empty() and meta:get_int("industrialtest.powerAmount")>=canningMachine.opPower and (not def._industrialtest_emptyVariant or leftoverSlot:item_fits(ItemStack(def._industrialtest_emptyVariant))) and @@ -170,14 +162,7 @@ canningMachine.activeOnTimer=function(pos,elapsed,meta,inv) local targetSlot=inv:get_stack("target",1) local powerStorageSlot=inv:get_stack("powerStorage",1) - if not powerStorageSlot:is_empty() then - local stackMeta=powerStorageSlot:get_meta() - if industrialtest.api.transferPower(stackMeta,meta,stackMeta:get_int("industrialtest.powerFlow"))>0 then - shouldUpdateFormspec=true - industrialtest.api.updateItemPowerText(powerStorageSlot) - inv:set_stack("powerStorage",1,powerStorageSlot) - end - end + shouldRerunTimer,shouldUpdateFormspec=industrialtest.internal.chargeFromPowerStorageItem(meta,inv) if fuelSlot:is_empty() or targetSlot:is_empty() or meta:get_int("industrialtest.powerAmount")=canningMachine.opPower then diff --git a/machines/common.lua b/machines/common.lua index 31cd2d3..1b77888 100644 --- a/machines/common.lua +++ b/machines/common.lua @@ -49,6 +49,22 @@ industrialtest.internal.allowMoveToUpgradeSlot=function(pos,toIndex,stack) return stack:get_count() end +industrialtest.internal.chargeFromPowerStorageItem=function(meta,inv) + local shouldRerunTimer=false + local shouldUpdateFormspec=false + local powerStorageSlot=inv:get_stack("powerStorage",1) + if not powerStorageSlot:is_empty() then + local stackMeta=powerStorageSlot:get_meta() + if industrialtest.api.transferPower(stackMeta,meta,stackMeta:get_int("industrialtest.powerFlow"))>0 then + shouldUpdateFormspec=true + shouldRerunTimer=stackMeta:get_int("industrialtest.powerAmount")>0 and not industrialtest.api.isFullyCharged(meta) + industrialtest.api.updateItemPowerText(powerStorageSlot) + inv:set_stack("powerStorage",1,powerStorageSlot) + end + end + return shouldRerunTimer,shouldUpdateFormspec +end + machine.getFormspec=function(pos,config) local formspec if industrialtest.mtgAvailable then @@ -561,15 +577,9 @@ simpleElectricItemProcessor.onTimer=function(pos,elapsed,meta,inv,config) local shouldUpdateFormspec=false local shouldRerunTimer=false local requiredPower=elapsed*config.opPower*industrialtest.api.getMachineSpeed(meta) - if powerStorageSlot:get_count()>0 then - local stackMeta=powerStorageSlot:get_meta() - if industrialtest.api.transferPower(stackMeta,meta,stackMeta:get_int("industrialtest.powerFlow"))>0 then - shouldUpdateFormspec=true - shouldRerunTimer=true - industrialtest.api.updateItemPowerText(powerStorageSlot) - inv:set_stack("powerStorage",1,powerStorageSlot) - end - end + + shouldRerunTimer,shouldUpdateFormspec=industrialtest.internal.chargeFromPowerStorageItem(meta,inv) + if srcSlot:get_count()>0 and meta:get_int("industrialtest.powerAmount")>=requiredPower then local output=craftResultProxy(config.method,srcSlot) if output.time>0 and inv:room_for_item("dst",output.item) then @@ -658,15 +668,8 @@ simpleElectricItemProcessor.activeOnTimer=function(pos,elapsed,meta,inv,config) local shouldRerunTimer=false local requiredPower=elapsed*config.opPower*industrialtest.api.getMachineSpeed(meta) - if powerStorageSlot:get_count()>0 then - local stackMeta=powerStorageSlot:get_meta() - if industrialtest.api.transferPower(stackMeta,meta,stackMeta:get_int("industrialtest.powerFlow"))>0 then - shouldUpdateFormspec=true - shouldRerunTimer=true - industrialtest.api.updateItemPowerText(powerStorageSlot) - inv:set_stack("powerStorage",1,powerStorageSlot) - end - end + shouldRerunTimer,shouldUpdateFormspec=industrialtest.internal.chargeFromPowerStorageItem(meta,inv) + if srcSlot:get_count()>0 and meta:get_float("maxSrcTime")<=0 and meta:get_int("industrialtest.powerAmount")>=requiredPower then local output=craftResultProxy(config.method,srcSlot) if output.time>0 and inv:room_for_item("dst",output.item) then