z_mirror/EditorExtensions

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

Caleb HeuerRemove debug logging92b9acf

trunk
2.3 KiB82 linesraw
1--- @param pipe LuaEntity
2--- @param combinator LuaEntity
3local function copy_from_pipe_to_combinator(pipe, combinator)
4  local filter = pipe.get_infinity_pipe_filter()
5  if not filter then
6    return
7  end
8  local cb = combinator.get_or_create_control_behavior() --[[@as LuaConstantCombinatorControlBehavior]]
9  --- @type LuaLogisticSection?
10  local section
11  for _, sec in pairs(cb.sections) do
12    if sec.group == "" then
13      section = sec
14      break
15    end
16  end
17  if not section then
18    section = cb.add_section()
19  end
20  if not section then
21    return -- When will this ever happen?
22  end
23  section.set_slot(1, {
24    value = {
25      comparator = "=",
26      type = "fluid",
27      name = filter.name --[[@as string]],
28      quality = "normal",
29    },
30    min = 1,
31  })
32end
33
34--- @param combinator LuaEntity
35--- @param pipe LuaEntity
36local function copy_from_combinator_to_pipe(combinator, pipe)
37  local cb = combinator.get_control_behavior() --[[@as LuaConstantCombinatorControlBehavior?]]
38  if not cb then
39    return
40  end
41  local cb = combinator.get_control_behavior() --[[@as LuaConstantCombinatorControlBehavior?]]
42  if not cb then
43    return
44  end
45  local section = cb.get_section(1)
46  if not section then
47    return
48  end
49  local filter = section.filters[1]
50  if not filter then
51    return
52  end
53  local value = filter.value
54  -- XXX: `filter` will not always have a `type` field.
55  if value and prototypes.fluid[value.name] then
56    pipe.set_infinity_pipe_filter({ type = "fluid", name = value.name, percentage = 1 })
57  else
58    pipe.set_infinity_pipe_filter(nil)
59  end
60end
61
62--- @param e EventData.on_entity_settings_pasted
63local function on_entity_settings_pasted(e)
64  local source, destination = e.source, e.destination
65  if not source.valid or not destination.valid then
66    return
67  end
68  local source_is_pipe, destination_is_pipe = source.type == "infinity-pipe", destination.type == "infinity-pipe"
69  if source_is_pipe and destination.name == "constant-combinator" then
70    copy_from_pipe_to_combinator(source, destination)
71  elseif source.name == "constant-combinator" and destination_is_pipe then
72    copy_from_combinator_to_pipe(source, destination)
73  end
74end
75
76local infinity_pipe = {}
77
78infinity_pipe.events = {
79  [defines.events.on_entity_settings_pasted] = on_entity_settings_pasted,
80}
81
82return infinity_pipe