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