diff --git a/api.lua b/api.lua index c5e957b..45d18f4 100644 --- a/api.lua +++ b/api.lua @@ -19,11 +19,15 @@ 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 -- \param ioConfig Input/Output configuration in following side order: -X, +X, -Y, +Y, -Z, +Z --- a - bidirectional, i - input, o - output +-- a - bidirectional, i - input, o - output -- \returns nil industrialtest.api.addPowerStorage=function(meta,capacity,flow,ioConfig) meta:set_int("industrialtest.powerCapacity",capacity) @@ -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()