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