]> git.somenet.org - factorio/some-luaconsole.git/blob - control.lua
Add a check for admin status. But you should not use this mod in multiplayer
[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     cmd = player.gui.left.some_luaconsole.input.text:gsub('game%.player%.', 'game.players['..player.index..'].')
9
10     f, err = loadstring(cmd)
11     if not f then
12         cmd = 'game.players['..player.index..'].print('..cmd..')'
13         f, err = loadstring(cmd)
14     end
15
16     _, err = pcall(f)
17     if err then
18         player.print(cmd)
19         player.print(err:sub(1, err:find('\n')))
20     end
21 end
22
23
24 function toggleGui (player)
25     if player.gui.left.some_luaconsole then
26         player.gui.left.some_luaconsole.destroy()
27     else
28         frame = player.gui.left.add{type = 'frame',
29             name = 'some_luaconsole',
30             direction = 'vertical',
31             caption = {'some_luaconsole.title'}
32         }
33
34         frame.add{type = 'label', caption = {'some_luaconsole.inputlabel'}}
35         input = frame.add{type = 'text-box',
36             name = 'input',
37             style='some_luaconsole_input_textbox'
38         }
39         input.word_wrap = true
40         input.focus()
41
42         horizontal_flow = frame.add{type='flow', direction='horizontal'}
43         horizontal_flow.add{type = 'button',
44             name = 'some_luaconsole_close',
45             style='back_button',
46             caption = {'some_luaconsole.close'},
47             tooltip = {'some_luaconsole.close_tooltip'}
48         }
49         horizontal_flow.add{type = 'button',
50             name = 'some_luaconsole_exec',
51             style='confirm_button',
52             caption = {'some_luaconsole.exec'},
53             tooltip = {'some_luaconsole.exec_tooltip'}
54         }
55
56         if not player.admin then
57             player.gui.left.some_luaconsole.input.text = 'You are not an admin. You may not use this mod. :('
58             player.gui.left.some_luaconsole.input.enabled = false
59         end
60     end
61 end
62
63
64
65 script.on_event(defines.events.on_gui_click, function(event)
66     if event.element.name == 'some_luaconsole_exec' then
67         exec_command(game.players[event.player_index])
68     elseif event.element.name == 'some_luaconsole_close' then
69         toggleGui(game.players[event.player_index])
70     end
71 end)
72
73
74 script.on_event('some_luaconsole_toggle', function(event)
75     toggleGui(game.players[event.player_index])
76 end)