z_mirror/EditorExtensions

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

Caleb HeuerFix reassigning blueprints not working596431c

trunk
2.8 KiB98 linesraw
1local aggregate_chest_names = {
2  ["ee-aggregate-chest"] = "ee-aggregate-chest",
3  ["ee-aggregate-chest-passive-provider"] = "ee-aggregate-chest-passive-provider",
4}
5
6--- @type table<string, InfinityInventoryFilter[]>
7local filters = {}
8do
9  local include_hidden = settings.global["ee-aggregate-include-hidden"].value --[[@as boolean]]
10  local i = 0
11  for quality_name in pairs(prototypes.quality) do
12    local this_filters = {}
13    for item_name, prototype in pairs(prototypes.item) do
14      if string.find(item_name, "^parameter%-") then
15        goto continue
16      end
17      if prototype.hidden and not include_hidden then
18        goto continue
19      end
20      i = i + 1
21      this_filters[i] = {
22        name = item_name,
23        quality = quality_name,
24        count = prototype.stack_size,
25        mode = "exactly",
26        index = i,
27      }
28      ::continue::
29    end
30    filters[quality_name] = this_filters
31  end
32end
33--- Set the filters for the given aggregate chest and removes the bar if there is one
34--- @param entity LuaEntity
35local function set_filters(entity)
36  entity.remove_unfiltered_items = true
37  entity.infinity_container_filters = filters[entity.quality.name]
38  entity.get_inventory(defines.inventory.chest).set_bar()
39end
40
41-- Update the filters of all existing aggregate chests
42local function update_all_filters()
43  for _, surface in pairs(game.surfaces) do
44    for _, entity in pairs(surface.find_entities_filtered({ name = aggregate_chest_names })) do
45      set_filters(entity)
46    end
47  end
48end
49
50--- @param e BuiltEvent
51local function on_entity_built(e)
52  local entity = e.entity or e.destination
53  if not entity or not entity.valid or not aggregate_chest_names[entity.name] then
54    return
55  end
56  set_filters(entity)
57end
58
59--- @param e EventData.on_player_setup_blueprint
60local function on_player_setup_blueprint(e)
61  local blueprint = e.stack or e.record
62  if not blueprint then
63    return
64  end
65
66  local entities = blueprint.get_blueprint_entities()
67  if not entities then
68    return
69  end
70  local set = false
71  for _, entity in pairs(entities) do
72    if aggregate_chest_names[entity.name] then
73      set = true
74      entity.infinity_settings.filters = nil --- @diagnostic disable-line
75    end
76  end
77  if set then
78    blueprint.set_blueprint_entities(entities)
79  end
80end
81
82local aggregate_chest = {}
83
84aggregate_chest.on_configuration_changed = function(_)
85  update_all_filters()
86end
87
88aggregate_chest.events = {
89  [defines.events.on_built_entity] = on_entity_built,
90  [defines.events.on_entity_cloned] = on_entity_built,
91  [defines.events.on_player_setup_blueprint] = on_player_setup_blueprint,
92  [defines.events.on_robot_built_entity] = on_entity_built,
93  [defines.events.on_space_platform_built_entity] = on_entity_built,
94  [defines.events.script_raised_built] = on_entity_built,
95  [defines.events.script_raised_revive] = on_entity_built,
96}
97
98return aggregate_chest