]> git.somenet.org - factorio/some-luaconsole.git/blob - control.lua
RELEASE 2.0.1 - Global storage should work again + Language file update
[factorio/some-luaconsole.git] / control.lua
1 function exec_command(player)
2     if not player.admin then
3         player.print('You are not an admin. You may not use this mod. :(')
4         return
5     end
6
7     local f, err, cmd
8
9     if player.gui.screen.some_luaconsole then
10         storage.cmd = player.gui.screen.some_luaconsole.input.text
11     end
12
13     cmd = storage.cmd or ""
14     cmd = cmd:gsub('game%.player([^s])', 'game.players['..player.index..']%1')
15
16     f, err = loadstring(cmd)
17     if not f then
18         cmd = 'game.players['..player.index..'].print('..cmd..')'
19         f, err = loadstring(cmd)
20     end
21
22     _, err = pcall(f)
23     if err then
24         player.print(cmd)
25         player.print(err:sub(1, err:find('\n')))
26     end
27 end
28
29
30 function toggleGui (player)
31     if player.gui.screen.some_luaconsole then
32         storage.cmd = player.gui.screen.some_luaconsole.input.text
33         player.gui.screen.some_luaconsole.destroy()
34     else
35         frame = player.gui.screen.add{type = 'frame',
36             name = 'some_luaconsole',
37             direction = 'vertical',
38             caption = {'some_luaconsole.title'}
39         }
40
41         frame.add{type = 'label', caption = {'some_luaconsole.inputlabel'}}
42         input = frame.add{type = 'text-box',
43             name = 'input',
44             style='some_luaconsole_input_textbox'
45         }
46         input.word_wrap = true
47         input.style.maximal_height = (player.display_resolution.height/player.display_scale*0.6)
48         input.text = storage.cmd or ""
49
50         horizontal_flow = frame.add{type='flow', direction='horizontal'}
51         horizontal_flow.add{type = 'button',
52             name = 'some_luaconsole_close',
53             style='back_button',
54             caption = {'some_luaconsole.close'},
55             tooltip = {'some_luaconsole.close_tooltip'}
56         }
57         horizontal_flow.add{type = 'button',
58             name = 'some_luaconsole_exec',
59             style='confirm_button',
60             caption = {'some_luaconsole.exec'},
61             tooltip = {'some_luaconsole.exec_tooltip'}
62         }
63
64         if not player.admin then
65             player.gui.screen.some_luaconsole.input.text = 'You are not an admin. You may not use this mod. :('
66             player.gui.screen.some_luaconsole.input.enabled = false
67         end
68         frame.force_auto_center()
69     end
70 end
71
72
73
74 script.on_event(defines.events.on_gui_click, function(event)
75     if event.element.name == 'some_luaconsole_exec' then
76         exec_command(game.players[event.player_index])
77     elseif event.element.name == 'some_luaconsole_close' then
78         toggleGui(game.players[event.player_index])
79     end
80 end)
81
82
83 script.on_event('some_luaconsole_toggle', function(event)
84     toggleGui(game.players[event.player_index])
85 end)
86 script.on_event('some_luaconsole_exec', function(event)
87     exec_command(game.players[event.player_index])
88 end)