z_mirror/BlueprintTools

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

Caleb HeuerUpdate to Factorio 2.1bf73278

trunk
7.0 KiB226 linesraw
1local gui = require("gui")
2
3local buttons_gui = require("scripts.gui.buttons")
4local import_string_gui = require("scripts.gui.import-string")
5local library_shortcuts_gui = require("scripts.gui.library-shortcuts")
6local migrations = require("scripts.migrations")
7local player_data = require("scripts.player-data")
8local util = require("scripts.util")
9
10local planner_pipette = require("scripts.processors.planner-pipette")
11local set_tiles_gui = require("scripts.gui.set-tiles")
12local swap_wire_colors = require("scripts.processors.swap-wire-colors")
13
14-- -----------------------------------------------------------------------------
15-- EVENT HANDLERS
16
17-- BOOTSTRAP
18
19script.on_init(function()
20  storage.players = {}
21  planner_pipette.cache_downgrades()
22
23  for i, player in pairs(game.players) do
24    player_data.init(i)
25    player_data.refresh(player, storage.players[i])
26  end
27end)
28
29script.on_configuration_changed(function(e)
30  planner_pipette.cache_downgrades()
31
32  local old_version = e.mod_changes[script.mod_name] and e.mod_changes[script.mod_name].old_version
33  if not old_version then
34    return
35  end
36
37  for version, migration in pairs(migrations) do
38    if helpers.compare_versions(version, old_version) > 0 then
39      migration()
40    end
41  end
42
43  for i, player in pairs(game.players) do
44    local player_table = storage.players[i]
45    player_data.refresh(player, player_table)
46  end
47end)
48
49-- CUSTOM INPUTS
50
51script.on_event("bpt-import-string", function(e)
52  local player = game.get_player(e.player_index)
53  import_string_gui.build(player)
54end)
55
56script.on_event("bpt-swap-wire-colors", function(e)
57  swap_wire_colors(game.get_player(e.player_index))
58end)
59
60script.on_event("bpt-set-tiles", function(e)
61  local player = game.get_player(e.player_index)
62  if not player then
63    return
64  end
65  local player_table = storage.players[e.player_index]
66  local cursor_stack = player.cursor_stack
67  if cursor_stack and cursor_stack.valid_for_read then
68    local blueprint = util.get_blueprint(cursor_stack)
69    if blueprint then
70      set_tiles_gui.build(player, player_table)
71    end
72  end
73end)
74
75script.on_event("bpt-configure", function(e)
76  local player = game.get_player(e.player_index)
77  if not player then
78    return
79  end
80  local cursor_stack = player.cursor_stack
81  if cursor_stack and cursor_stack.valid_for_read then
82    local blueprint = util.get_blueprint(cursor_stack)
83    if blueprint or (cursor_stack.is_upgrade_item or cursor_stack.is_deconstruction_item) then
84      player.opened = cursor_stack
85    end
86  end
87end)
88
89script.on_event("bpt-linked-confirm-gui", function(e)
90  local player = game.get_player(e.player_index)
91  if not player then
92    return
93  end
94  local player_table = storage.players[e.player_index]
95  local gui_data = player_table.guis.set_tiles
96  if gui_data and gui_data.refs.confirm_button.enabled then
97    set_tiles_gui.handle_action(e, { action = "confirm" })
98    player.play_sound({ path = "utility/confirm" })
99  end
100end)
101
102script.on_event("bpt-linked-clear-cursor", function(e)
103  local player = game.get_player(e.player_index)
104  if not player then
105    return
106  end
107  local player_table = storage.players[e.player_index]
108  local cursor_stack = player.cursor_stack
109  if player_table.flags.holding_temporary_item and cursor_stack.valid_for_read then
110    local item_type = cursor_stack.type
111    if item_type == "blueprint" and not cursor_stack.is_blueprint_setup() then
112      cursor_stack.clear()
113    elseif item_type == "blueprint-book" or item_type == "upgrade-item" or item_type == "deconstruction-item" then
114      cursor_stack.clear()
115    end
116  end
117end)
118
119-- GUI
120
121gui.hook_events(function(e)
122  local player = game.get_player(e.player_index)
123  if not player then
124    return
125  end
126  local player_table = storage.players[e.player_index]
127  local action = gui.read_action(e)
128
129  if action then
130    if action.gui == "buttons" then
131      if action.action == "swap_wire_colors" then
132        swap_wire_colors(player)
133      elseif action.action == "set_tiles" then
134        set_tiles_gui.build(player, player_table)
135      elseif action.action == "configure" then
136        player.opened = player.cursor_stack
137      end
138    elseif action.gui == "set_tiles" then
139      set_tiles_gui.handle_action(e, action)
140    elseif action.gui == "library_shortcuts" then
141      library_shortcuts_gui.handle_action(player, player_table, action)
142    elseif action.gui == "import_string" then
143      import_string_gui.handle_action(player, action)
144    end
145  end
146end)
147
148-- PLAYER
149
150script.on_event(defines.events.on_player_created, function(e)
151  player_data.init(e.player_index)
152  player_data.refresh(game.get_player(e.player_index), storage.players[e.player_index])
153end)
154
155script.on_event(defines.events.on_player_removed, function(e)
156  storage.players[e.player_index] = nil
157end)
158
159script.on_event(defines.events.on_player_cursor_stack_changed, function(e)
160  local player = game.get_player(e.player_index)
161  if not player then
162    return
163  end
164  local player_table = storage.players[e.player_index]
165  local cursor_stack = player.cursor_stack
166
167  if player_table.flags.setting_temporary_item then
168    player_table.flags.setting_temporary_item = false
169  elseif player_table.flags.holding_temporary_item then
170    player_table.flags.holding_temporary_item = false
171  end
172
173  local blueprint = util.get_blueprint(cursor_stack)
174  local blueprint_buttons_shown = player_table.flags.blueprint_buttons_shown
175
176  if blueprint and not blueprint_buttons_shown then
177    buttons_gui.show(player_table, "blueprint")
178  elseif not blueprint and blueprint_buttons_shown then
179    buttons_gui.hide(player_table, "blueprint")
180  end
181
182  if cursor_stack.is_upgrade_item then
183    buttons_gui.show(player_table, "upgrade_planner")
184  elseif player_table.flags.upgrade_planner_buttons_shown then
185    buttons_gui.hide(player_table, "upgrade_planner")
186  end
187
188  if cursor_stack.is_deconstruction_item then
189    buttons_gui.show(player_table, "deconstruction_planner")
190  elseif player_table.flags.deconstruction_planner_buttons_shown then
191    buttons_gui.hide(player_table, "deconstruction_planner")
192  end
193
194  -- if the set tiles GUI is open and the cursor stack changes in any way, close it
195  if player_table.guis.set_tiles then
196    set_tiles_gui.destroy(player_table)
197  end
198end)
199
200-- SETTINGS
201
202script.on_event(defines.events.on_runtime_mod_setting_changed, function(e)
203  if string.sub(e.setting, 1, 4) == "bpt-" then
204    local player = game.get_player(e.player_index)
205    if not player then
206      return
207    end
208    local player_table = storage.players[e.player_index]
209    player_data.update_settings(player, player_table)
210    if e.setting == "bpt-show-library-shortcuts" then
211      library_shortcuts_gui.refresh(player, player_table)
212    else
213      buttons_gui.refresh(player, player_table)
214      local flags = player_table.flags
215      if flags.blueprint_buttons_shown then
216        buttons_gui.show(player_table, "blueprint")
217      end
218      if flags.upgrade_planner_buttons_shown then
219        buttons_gui.show(player_table, "upgrade_planner")
220      end
221      if flags.deconstruction_planner_buttons_shown then
222        buttons_gui.show(player_table, "deconstruction_planner")
223      end
224    end
225  end
226end)