z_mirror/EditorExtensions

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

Caleb HeuerVersion 2.0f0478c9

hidden-items
7.7 KiB256 linesraw
1local util = require("__EditorExtensions__/scripts/util")
2
3local wagon_names = {
4  ["ee-infinity-cargo-wagon"] = true,
5  ["ee-infinity-fluid-wagon"] = true,
6}
7
8--- @param player LuaPlayer
9--- @param entity LuaEntity
10local function open_gui(player, entity)
11  player.opened = global.wagons[entity.unit_number].proxy
12end
13
14--- @param e BuiltEvent
15local function on_entity_built(e)
16  local entity = e.created_entity or e.entity or e.destination
17  if not entity or not entity.valid or not wagon_names[entity.name] then
18    return
19  end
20
21  local proxy = entity.surface.create_entity({
22    name = "ee-infinity-wagon-" .. (entity.name == "ee-infinity-cargo-wagon" and "chest" or "pipe"),
23    position = entity.position,
24    force = entity.force,
25  })
26  if not proxy then
27    return
28  end
29
30  -- Create all api lookups here to save time in on_tick()
31  local data = {
32    flip = 0,
33    proxy = proxy,
34    proxy_fluidbox = proxy.fluidbox,
35    proxy_inv = proxy.get_inventory(defines.inventory.chest),
36    wagon = entity,
37    wagon_fluidbox = entity.fluidbox,
38    wagon_inv = entity.get_inventory(defines.inventory.cargo_wagon),
39    wagon_last_position = entity.position,
40    wagon_name = entity.name,
41  }
42  global.wagons[entity.unit_number] = data
43
44  -- Apply any pre-existing filters
45  local tags = e.tags
46  if not tags or not tags.EditorExtensions then
47    return
48  end
49  local tags = tags.EditorExtensions
50  if entity.name == "ee-infinity-cargo-wagon" then
51    -- LEGACY: Before v1.9.16, we did not store the `remove unfiltered items` setting
52    if tags.filters then
53      data.proxy.infinity_container_filters = tags.filters
54      data.proxy.remove_unfiltered_items = tags.remove_unfiltered_items
55    else
56      --- @cast tags InfinityInventoryFilter[]
57      data.proxy.infinity_container_filters = tags
58    end
59  elseif entity.name == "ee-infinity-fluid-wagon" then
60    --- @cast tags InfinityPipeFilter
61    proxy.set_infinity_pipe_filter(tags)
62  end
63end
64
65--- @param e DestroyedEvent
66local function on_entity_destroyed(e)
67  local entity = e.entity
68  if not entity or not entity.valid or not wagon_names[entity.name] then
69    return
70  end
71
72  local proxy = global.wagons[entity.unit_number].proxy
73  if proxy and proxy.valid then
74    proxy.destroy()
75  end
76  global.wagons[entity.unit_number] = nil
77end
78
79--- @param e EventData.on_marked_for_deconstruction
80local function on_marked_for_deconstruction(e)
81  local entity = e.entity
82  if not entity.valid or entity.name ~= "ee-infinity-cargo-wagon" then
83    return
84  end
85  global.wagons[entity.unit_number].flip = 3
86  entity.get_inventory(defines.inventory.cargo_wagon).clear()
87end
88
89--- @param e EventData.on_cancelled_deconstruction
90local function on_cancelled_deconstruction(e)
91  local entity = e.entity
92  if not entity.valid or entity.name ~= "ee-infinity-cargo-wagon" then
93    return
94  end
95  -- Resume syncing
96  global.wagons[entity.unit_number].flip = 0
97end
98
99local function on_tick()
100  local abs = math.abs
101  for _, t in pairs(global.wagons) do
102    if t.wagon.valid and t.proxy.valid then
103      if t.wagon_name == "ee-infinity-cargo-wagon" then
104        if t.flip == 0 then
105          t.wagon_inv.clear()
106          for n, c in pairs(t.proxy_inv.get_contents()) do
107            t.wagon_inv.insert({ name = n, count = c })
108          end
109          t.flip = 1
110        elseif t.flip == 1 then
111          t.proxy_inv.clear()
112          for n, c in pairs(t.wagon_inv.get_contents()) do
113            t.proxy_inv.insert({ name = n, count = c })
114          end
115          t.flip = 0
116        end
117      elseif t.wagon_name == "ee-infinity-fluid-wagon" then
118        if t.flip == 0 then
119          local fluid = t.proxy_fluidbox[1]
120          t.wagon_fluidbox[1] = fluid
121              and fluid.amount > 0
122              and {
123                name = fluid.name,
124                amount = (abs(fluid.amount) * 250),
125                temperature = fluid.temperature,
126              }
127            or nil
128          t.flip = 1
129        elseif t.flip == 1 then
130          local fluid = t.wagon_fluidbox[1]
131          t.proxy_fluidbox[1] = fluid
132              and fluid.amount > 0
133              and {
134                name = fluid.name,
135                amount = (abs(fluid.amount) / 250),
136                temperature = fluid.temperature,
137              }
138            or nil
139          t.flip = 0
140        end
141      end
142      local position = t.wagon.position
143      local last_position = t.wagon_last_position
144      if last_position.x ~= position.x or last_position.y ~= position.y then
145        t.proxy.teleport(t.wagon.position)
146        t.wagon_last_position = last_position
147      end
148    end
149  end
150end
151
152--- @param e EventData.on_gui_opened
153local function on_gui_opened(e)
154  local player = game.get_player(e.player_index)
155  if not player then
156    return
157  end
158  local entity = e.entity
159  if not entity or not entity.valid or not wagon_names[entity.name] then
160    return
161  end
162  open_gui(player, entity)
163end
164
165--- @param e EventData.CustomInputEvent
166local function on_linked_open_gui(e)
167  local player = game.get_player(e.player_index)
168  if not player then
169    return
170  end
171  local entity = player.selected
172  if not entity or entity.name ~= "ee-infinity-fluid-wagon" then
173    return
174  end
175  if not player.can_reach_entity(entity) then
176    return
177  end
178  open_gui(player, entity)
179end
180
181--- @param e EventData.on_entity_settings_pasted
182local function on_entity_settings_pasted(e)
183  local source, destination = e.source, e.destination
184  if not source.valid or not destination.valid then
185    return
186  end
187  if not wagon_names[source.name] or not wagon_names[destination.name] then
188    return
189  end
190  if source.name ~= destination.name then
191    return
192  end
193
194  global.wagons[destination.unit_number].proxy.copy_settings(global.wagons[source.unit_number].proxy)
195end
196
197--- @param e EventData.on_player_setup_blueprint
198local function on_player_setup_blueprint(e)
199  local blueprint = util.get_blueprint(e)
200  if not blueprint then
201    return
202  end
203
204  local entities = blueprint.get_blueprint_entities()
205  if not entities then
206    return
207  end
208  for i, entity in pairs(entities) do
209    --- @cast i uint
210    if not wagon_names[entity.name] then
211      goto continue
212    end
213    local real_entity = e.surface.find_entity(entity.name, entity.position)
214    if not real_entity then
215      goto continue
216    end
217    local proxy = global.wagons[real_entity.unit_number].proxy
218    --- @type InfinityPipeFilter|InfinityInventoryFilter[]?
219    local tags = {}
220    if entity.name == "ee-infinity-cargo-wagon" then
221      tags.filters = proxy.infinity_container_filters
222      tags.remove_unfiltered_items = proxy.remove_unfiltered_items
223    else
224      tags = proxy.get_infinity_pipe_filter()
225    end
226    blueprint.set_blueprint_entity_tag(i, "EditorExtensions", tags --[[@as AnyBasic]])
227    ::continue::
228  end
229end
230
231local infinity_wagon = {}
232
233infinity_wagon.on_init = function()
234  global.wagons = {}
235end
236
237infinity_wagon.events = {
238  [defines.events.on_built_entity] = on_entity_built,
239  [defines.events.on_cancelled_deconstruction] = on_cancelled_deconstruction,
240  [defines.events.on_entity_cloned] = on_entity_built,
241  [defines.events.on_entity_died] = on_entity_destroyed,
242  [defines.events.on_entity_settings_pasted] = on_entity_settings_pasted,
243  [defines.events.on_gui_opened] = on_gui_opened,
244  [defines.events.on_marked_for_deconstruction] = on_marked_for_deconstruction,
245  [defines.events.on_player_mined_entity] = on_entity_destroyed,
246  [defines.events.on_player_setup_blueprint] = on_player_setup_blueprint,
247  [defines.events.on_robot_built_entity] = on_entity_built,
248  [defines.events.on_robot_mined_entity] = on_entity_destroyed,
249  [defines.events.on_tick] = on_tick,
250  [defines.events.script_raised_built] = on_entity_built,
251  [defines.events.script_raised_destroy] = on_entity_destroyed,
252  [defines.events.script_raised_revive] = on_entity_built,
253  ["ee-linked-open-gui"] = on_linked_open_gui,
254}
255
256return infinity_wagon