Make industrialtest.api.addPower accept negative power amount

This commit is contained in:
mrkubax10 2023-03-21 13:43:17 +01:00
parent f82d4730eb
commit f74f36aeb3

15
api.lua
View File

@ -19,6 +19,10 @@ local S=minetest.get_translator("industrialtest")
industrialtest.api={}
industrialtest.api.maceratorRecipes={}
local function clamp(num,min,max)
return math.max(math.min(num,max),min)
end
-- \brief Adds power storage to metadata
-- \param capacity How much EU item/node can store
-- \param flow How much EU can flow in or out item/node per industrialtest.updateDelay
@ -93,13 +97,10 @@ end
industrialtest.api.addPower=function(meta,amount)
local powerAmount=meta:get_int("industrialtest.powerAmount")
local powerCapacity=meta:get_int("industrialtest.powerCapacity")
if powerAmount+amount>powerCapacity then
local addedAmount=powerCapacity-powerAmount
meta:set_int("industrialtest.powerAmount",powerCapacity)
return addedAmount
end
meta:set_int("industrialtest.powerAmount",powerAmount+amount)
return amount
local prevPowerAmount=powerAmount
powerAmount=clamp(powerAmount+amount,0,powerCapacity)
meta:set_int("industrialtest.powerAmount",powerAmount)
return powerAmount-prevPowerAmount
end
industrialtest.api.addPowerToItem=function(itemstack,amount)
local meta=itemstack:get_meta()