90 lines
3.5 KiB
Lua
90 lines
3.5 KiB
Lua
|
-- 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 <http://www.gnu.org/licenses/>.
|
||
|
|
||
|
-- \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
|