forked from mspielberg/factorio-miniloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.lua
47 lines (40 loc) · 1.3 KB
/
gui.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
local circuit = require "circuit"
local event = require "lualib.event"
local ontick = require "lualib.ontick"
local util = require "lualib.util"
-- how often to poll ControlBehavior settings when a miniloader-inserter GUI is open
local POLL_INTERVAL = 15
local monitored_entities = {}
local function should_monitor_entity(entity)
return util.is_miniloader(entity) or util.is_miniloader_inserter(entity)
end
local function monitor_open_guis(_)
if not next(monitored_entities) then
ontick.unregister(monitor_open_guis)
end
for k, entity in pairs(monitored_entities) do
if entity.valid then
circuit.sync_filters(entity)
circuit.sync_behavior(entity)
else
monitored_entities[k] = nil
end
end
end
local function on_gui_opened(ev)
local entity = ev.entity
if entity and should_monitor_entity(entity) then
monitored_entities[entity.unit_number] = entity
ontick.register(monitor_open_guis, POLL_INTERVAL)
end
end
local function on_gui_closed(ev)
local entity = ev.entity
if entity and should_monitor_entity(entity) then
circuit.sync_behavior(entity)
circuit.sync_filters(entity)
monitored_entities[entity.unit_number] = nil
end
end
event.register(defines.events.on_gui_opened, on_gui_opened)
event.register(defines.events.on_gui_closed, on_gui_closed)