z_mirror/EditorExtensions

git clone https://git.raiguard.me/z_mirror/EditorExtensions

Caleb HeuerImprove handler naming consistencye2c82ce

hidden-items
2.7 KiB90 linesraw
1local util = require("__EditorExtensions__/scripts/util")
2
3local aggregate_chest_names = {
4  ["ee-aggregate-chest"] = "ee-aggregate-chest",
5  ["ee-aggregate-chest-passive-provider"] = "ee-aggregate-chest-passive-provider",
6}
7
8--- Set the filters for the given aggregate chest and removes the bar if there is one
9--- @param entity LuaEntity
10local function set_filters(entity)
11  entity.remove_unfiltered_items = true
12  entity.infinity_container_filters = global.aggregate_filters
13  entity.get_inventory(defines.inventory.chest).set_bar()
14end
15
16-- Update the filters of all existing aggregate chests
17local function update_all_filters()
18  for _, surface in pairs(game.surfaces) do
19    for _, entity in pairs(surface.find_entities_filtered({ name = aggregate_chest_names })) do
20      set_filters(entity)
21    end
22  end
23end
24
25-- Retrieve each item prototype and its stack size
26local function build_filter_cache()
27  local include_hidden = settings.global["ee-aggregate-include-hidden"].value
28  --- @type InfinityInventoryFilter[]
29  local filters = {}
30  local i = 0
31  for name, prototype in pairs(game.item_prototypes) do
32    if prototype.type ~= "mining-tool" and include_hidden or not prototype.has_flag("hidden") then
33      i = i + 1
34      filters[i] = { name = name, count = prototype.stack_size, mode = "exactly", index = i }
35    end
36  end
37  global.aggregate_filters = filters
38end
39
40--- @param e BuiltEvent
41local function on_entity_built(e)
42  local entity = e.created_entity or e.entity or e.destination
43  if not entity or not entity.valid or not aggregate_chest_names[entity.name] then
44    return
45  end
46  set_filters(entity)
47end
48
49--- @param e EventData.on_player_setup_blueprint
50local function on_player_setup_blueprint(e)
51  local blueprint = util.get_blueprint(e)
52  if not blueprint then
53    return
54  end
55
56  local entities = blueprint.get_blueprint_entities()
57  if not entities then
58    return
59  end
60  local set = false
61  for _, entity in pairs(entities) do
62    if aggregate_chest_names[entity.name] then
63      set = true
64      entity.infinity_settings.filters = nil --- @diagnostic disable-line
65    end
66  end
67  if set then
68    blueprint.set_blueprint_entities(entities)
69  end
70end
71
72local aggregate_chest = {}
73
74aggregate_chest.on_init = build_filter_cache
75
76aggregate_chest.on_configuration_changed = function()
77  build_filter_cache()
78  update_all_filters()
79end
80
81aggregate_chest.events = {
82  [defines.events.on_built_entity] = on_entity_built,
83  [defines.events.on_entity_cloned] = on_entity_built,
84  [defines.events.on_player_setup_blueprint] = on_player_setup_blueprint,
85  [defines.events.on_robot_built_entity] = on_entity_built,
86  [defines.events.script_raised_built] = on_entity_built,
87  [defines.events.script_raised_revive] = on_entity_built,
88}
89
90return aggregate_chest