local LOG = give_initial_enderchest.log local enable_bags = give_initial_enderchest.bags_enable ---@param n bagSlot ---@return (mt.ItemStack|string)[]? local function getSlot(n) local items = give_initial_enderchest.bags_items[n] if items == nil then LOG("error", ("slot %s is out of range!"):format(n)) end return items end ---@param player mt.PlayerObjectRef ---@return nil local function bags_give_fallback(player) for i = 1, 4 do if give_initial_enderchest.bags_type[i] then give_initial_enderchest._give_common(player, "main", give_initial_enderchest.bags_items[i]) end end end ---@param player mt.PlayerObjectRef ---@return nil local function bags_give(player) if not enable_bags then -- safeguard LOG("error", "bags_give: bags are disabled!") return end local bags_inv_name = player:get_player_name() .. "_bags" local bags_inv = core.get_inventory({ type = "detached", name = bags_inv_name }) for i = 1, 4 do local bag_item = give_initial_enderchest.bags_type[i] if bag_item then local list = "bag" .. i if bags_inv:is_empty(list) then bag_item = ItemStack(bag_item) local left = bags_inv:add_item(list, bag_item) -- Manually trigger callback to create player inventory lists -- https://github.com/luanti-org/luanti/issues/8390 if left:get_count() == 0 then core.detached_inventories[bags_inv_name].on_put(bags_inv, list, 1, bag_item, player) end end give_initial_enderchest._give_common(player, "bag" .. i .. "contents", give_initial_enderchest.bags_items[i]) end end end ---@param slot bagSlot ---@param stack mt.ItemStack|string ---@return nil function give_initial_enderchest.bags_add(slot, stack) local items = getSlot(slot) if items == nil then return end table.insert(items, stack) end ---@param slot bagSlot ---@param csv string ---@return nil function give_initial_enderchest.bags_add_from_csv(slot, csv) local items = getSlot(slot) if items == nil then return end for _, stack in ipairs(csv:split(",")) do give_initial_enderchest.bags_add(slot, stack) end end ---@param slot bagSlot ---@return (mt.ItemStack|string)[] function give_initial_enderchest.bags_get_list(slot) return getSlot(slot) end ---@param slot bagSlot ---@param list (mt.ItemStack|string)[] ---@return nil function give_initial_enderchest.bags_set_list(slot, list) if give_initial_enderchest.bags_items[slot] == nil then LOG("error", ("slot %s is out of range!"):format(slot)) end give_initial_enderchest.bags_items[slot] = list end ---@param slot bagSlot ---@return nil function give_initial_enderchest.bags_clear(slot) give_initial_enderchest.bags_set_list(slot, {}) end -- TODO: check if unified_inventory version is too old if core.get_modpath("unified_inventory") and core.settings:get_bool("unified_inventory_bags", true) then give_initial_enderchest.bags_give = bags_give else LOG("warning", "Initial bags are enabled but unified_inventory is not found. Using main inv instead") give_initial_enderchest.bags_give = bags_give_fallback end if enable_bags then for i = 1, 4 do local type = core.settings:get("give_initial_enderchest_bag" .. i) or "none" if type == "bag_small" or type == "bag_medium" or type == "bag_large" then give_initial_enderchest.bags_type[i] = "unified_inventory:" .. type end local csv = core.settings:get("give_initial_enderchest_bag" .. i .. "_items") or "" give_initial_enderchest.bags_add_from_csv(i, csv) end end