cf-lib/cf-lib/data/Item.lua
Simon Brodtmann 9878331df5 Add :merge
2025-05-31 22:55:18 +02:00

60 lines
No EOL
1.4 KiB
Lua

--- Utility class for a single item
--- @class Item
local Item = {}
--- Pass an item name or an item table to get a Item object
--- @param value string|table The name of the item or the item table
function Item:new(value)
local name
local item
if type(value) == "string" then
name = value
item = data.raw.item[name]
if not item then
log("Item not found: " .. name)
return nil
end
elseif type(value) == "table" then
name = value.name
item = value
end
local obj = {
prototype = item
}
setmetatable(obj, self)
self.__index = self
return obj
end
--- Applies the item to the game
function Item:apply()
data:extend({ self.prototype })
end
--- Assigns data to the item
--- Shorthand for table.assign(item.prototype, data)
--- @param data table The data to assign
function Item:assign(data)
table.assign(self.prototype, data)
return self
end
--- Deeply merges data with the item
--- Shorthand for table.merge(item.prototype, data)
--- @param data table The data to merge
function Item:merge(data)
table.merge(self.prototype, data)
return self
end
--- Sets the weight of the item calculated from given count per rocket.
--- @param count number The amount of items that fit into a rocket
function Item:itemsPerRocket(count)
self.prototype.weight = (1000 / count) * kg
return self
end
return Item