-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathforcedel.lua
More file actions
292 lines (247 loc) · 8.05 KB
/
forcedel.lua
File metadata and controls
292 lines (247 loc) · 8.05 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
-- Carl Frank Otto III
-- carlotto81@gmail.com
-- GitHub: https://github.com/M45-Science/SoftMod
-- License: MPL 2.0
local FORCEDEL_BUTTON_NAME = "m45_force_delete_toggle"
local FORCEDEL_SPRITE = "file/img/buttons/force-delete-64.png"
local FORCEDEL_CONFIRM_WINDOW = "m45_force_delete_confirm_window"
local FORCEDEL_CONFIRM_CLOSE = "m45_force_delete_confirm_close"
local FORCEDEL_CONFIRM_CONFIRM = "m45_force_delete_confirm_confirm"
local FORCEDEL_CONFIRM_CANCEL = "m45_force_delete_confirm_cancel"
local function is_armed(player)
return storage and storage.PData and storage.PData[player.index] and storage.PData[player.index].force_delete_armed
end
local function set_armed(player, armed)
if not (player and player.valid) then
return
end
STORAGE_EnsureGlobal()
STORAGE_MakePlayerStorage(player)
storage.PData[player.index].force_delete_armed = armed and true or false
end
local function clear_pending(player)
if not (player and player.valid) then
return
end
if not (storage and storage.PData and storage.PData[player.index]) then
return
end
storage.PData[player.index].force_delete_pending_entity = nil
storage.PData[player.index].force_delete_pending_name = nil
storage.PData[player.index].force_delete_pending_gps = nil
end
local function set_pending(player, entity)
if not (player and player.valid and entity and entity.valid) then
return
end
STORAGE_EnsureGlobal()
STORAGE_MakePlayerStorage(player)
storage.PData[player.index].force_delete_pending_entity = entity
storage.PData[player.index].force_delete_pending_name = entity.name
storage.PData[player.index].force_delete_pending_gps = UTIL_GPSObj(entity)
end
local function destroy_confirm_window(player)
if not (player and player.valid and player.gui and player.gui.screen) then
return
end
if player.gui.screen[FORCEDEL_CONFIRM_WINDOW] then
player.gui.screen[FORCEDEL_CONFIRM_WINDOW].destroy()
end
end
local function open_confirm_window(player, entity)
if not (player and player.valid and player.admin) then
return
end
if not (entity and entity.valid) then
return
end
if not (player.gui and player.gui.screen) then
return
end
destroy_confirm_window(player)
set_pending(player, entity)
local pdata = storage and storage.PData and storage.PData[player.index]
local name = (pdata and pdata.force_delete_pending_name) or entity.name
local gps = (pdata and pdata.force_delete_pending_gps) or UTIL_GPSObj(entity)
local main_flow_def = {
type = "frame",
name = FORCEDEL_CONFIRM_WINDOW,
direction = "vertical"
}
local main_flow = player.gui.screen.add(main_flow_def)
main_flow.style.horizontal_align = "center"
main_flow.style.vertical_align = "center"
main_flow.style.minimal_width = 420
main_flow.style.padding = 4
local titlebar = main_flow.add {
type = "flow",
direction = "horizontal"
}
titlebar.drag_target = main_flow
titlebar.style.horizontal_align = "center"
titlebar.style.horizontally_stretchable = true
titlebar.add {
type = "label",
style = "frame_title",
caption = "Force Delete"
}
local pusher = titlebar.add {
type = "empty-widget",
style = "draggable_space_header"
}
pusher.style.vertically_stretchable = true
pusher.style.horizontally_stretchable = true
pusher.drag_target = main_flow
titlebar.add {
type = "sprite-button",
name = FORCEDEL_CONFIRM_CLOSE,
sprite = "utility/close",
style = "frame_action_button",
tooltip = "Close"
}
local body = main_flow.add {
type = "flow",
direction = "vertical"
}
body.style.minimal_width = 420
body.style.horizontal_align = "center"
body.add {
type = "label",
caption = "Delete this entity?"
}
body.add {
type = "label",
caption = name .. " " .. gps
}
local warn = body.add {
type = "label",
caption = "This cannot be undone."
}
warn.style.font_color = { r = 1, g = 0.3, b = 0.3 }
local buttons = main_flow.add {
type = "flow",
direction = "horizontal"
}
buttons.style.horizontally_stretchable = true
buttons.style.horizontal_align = "center"
buttons.style.top_padding = 6
buttons.add {
type = "button",
name = FORCEDEL_CONFIRM_CONFIRM,
caption = "Confirm",
style = "red_button",
tooltip = "Force-delete this entity"
}
buttons.add {
type = "button",
name = FORCEDEL_CONFIRM_CANCEL,
caption = "Cancel",
tooltip = "Cancel force-delete"
}
main_flow.force_auto_center()
end
local function draw_button(player)
if not (player and player.valid and player.gui and player.gui.top) then
return
end
if not player.admin then
if player.gui.top[FORCEDEL_BUTTON_NAME] then
player.gui.top[FORCEDEL_BUTTON_NAME].destroy()
end
return
end
local button = player.gui.top[FORCEDEL_BUTTON_NAME]
if not button then
button = player.gui.top.add {
type = "sprite-button",
name = FORCEDEL_BUTTON_NAME,
sprite = FORCEDEL_SPRITE,
tooltip = "Force delete (admin): arm, click entity, confirm"
}
button.style.size = { 64, 64 }
end
-- Use toggled state for the orange highlight (matches other top-bar tool buttons).
button.toggled = is_armed(player) and true or false
end
function FORCEDEL_MakeButton(player)
draw_button(player)
end
function FORCEDEL_Clicks(event)
if not (event and event.element and event.element.valid and event.player_index) then
return
end
local ename = event.element.name
if ename ~= FORCEDEL_BUTTON_NAME and ename ~= FORCEDEL_CONFIRM_CLOSE and ename ~= FORCEDEL_CONFIRM_CANCEL and
ename ~= FORCEDEL_CONFIRM_CONFIRM then
return
end
local player = game.players[event.player_index]
if not (player and player.valid) then
return
end
if ename == FORCEDEL_BUTTON_NAME then
if not player.admin then
return
end
local armed = is_armed(player)
destroy_confirm_window(player)
clear_pending(player)
set_armed(player, not armed)
draw_button(player)
return
end
if not player.admin then
return
end
if ename == FORCEDEL_CONFIRM_CLOSE or ename == FORCEDEL_CONFIRM_CANCEL then
destroy_confirm_window(player)
clear_pending(player)
set_armed(player, false)
draw_button(player)
return
end
if ename == FORCEDEL_CONFIRM_CONFIRM then
STORAGE_EnsureGlobal()
STORAGE_MakePlayerStorage(player)
local entity = storage.PData[player.index].force_delete_pending_entity
destroy_confirm_window(player)
clear_pending(player)
if not (entity and entity.valid) then
set_armed(player, false)
draw_button(player)
UTIL_SmartPrint(player, "[FORCE-DELETE] Target no longer exists.")
return
end
local gps = UTIL_GPSObj(entity)
local name = entity.name
set_armed(player, false)
draw_button(player)
entity.destroy { raise_destroy = true }
UTIL_SmartPrint(player, "[FORCE-DELETE] " .. name .. " " .. gps)
return
end
end
function FORCEDEL_OnGuiOpened(event)
if not (event and event.player_index) then
return
end
local player = game.players[event.player_index]
if not (player and player.valid and player.admin) then
return
end
if not is_armed(player) then
return
end
if event.gui_type ~= defines.gui_type.entity then
return
end
local entity = event.entity
if entity and entity.valid then
if player.opened == entity then
player.opened = nil
end
set_armed(player, false)
draw_button(player)
open_confirm_window(player, entity)
end
end