2025-04-24 18:26:46 +00:00

78 lines
2.3 KiB
Lua

---@alias bagSlot 1|2|3|4
local tag = core.get_current_modname()
local modpath = core.get_modpath(tag)
tag = "[" .. tag .. "] "
---@diagnostic disable-next-line:lowercase-global
give_initial_enderchest = {
enable = core.settings:get_bool("give_initial_enderchest", false),
items = {}, ---@type (string|mt.ItemStack)[]
targetlist = "enderchest",
bags_enable = core.settings:get_bool("give_initial_enderchest_bags", false),
bags_type = {}, ---@type { [bagSlot]: string }
bags_items = { ---@type { [bagSlot]: (string|mt.ItemStack)[] }
{}, {}, {}, {}
},
}
local enable_enderchest = give_initial_enderchest.enable
local enable_bags = give_initial_enderchest.bags_enable
---@param level "none"|"error"|"warning"|"action"|"info"|"verbose"
---@param text string
---@return nil
function give_initial_enderchest.log(level, text)
core.log(level, tag .. text)
end
local LOG = give_initial_enderchest.log
---@param player mt.PlayerObjectRef
---@param invlist string
---@param items (string|mt.ItemStack)[]
---@return nil
function give_initial_enderchest._give_common(player, invlist, items)
LOG("action", "Populating " .. invlist .. " inv. list of player " ..
player:get_player_name() .. " with items.")
local inv = player:get_inventory()
if inv:get_size(invlist) == 0 then
LOG("warning", "Inv. list " .. invlist ..
" not found! This might be a bug. Using main inventory.")
invlist = "main"
end
for _, stack in ipairs(items) do
local left = inv:add_item(invlist, ItemStack(stack))
if left:get_count() > 0 then
LOG("warning", invlist .. " is full! Unable to add " ..
left:to_string())
end
end
end
local new_players = {} ---@type { [string]: boolean? }
dofile(modpath .. "/enderchest.lua")
dofile(modpath .. "/bags.lua")
if enable_enderchest or enable_bags then
-- enderchest gets configured later in joinplayer callback
-- so do unified_inventory bags
core.register_on_newplayer(function(player)
new_players[player:get_player_name()] = true
end)
core.register_on_joinplayer(function(player)
local name = player:get_player_name()
if new_players[name] then
new_players[name] = nil
if enable_enderchest then
give_initial_enderchest.give(player)
end
if enable_bags then
give_initial_enderchest.bags_give(player)
end
end
end)
end