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. :(')
7 local f, lserr, pcs, pcerr, cmd
9 if player.gui.screen.some_luaconsole then
10 storage.cmd = player.gui.screen.some_luaconsole.input.text
13 cmd = storage.cmd or ""
14 cmd = cmd:gsub('game%.player([^s])', 'game.players['..player.index..']%1')
16 f, lserr = loadstring('local ipcs,ipcr ipcs,ipcr=pcall(function() '..cmd..' end) if not ipcs then game.players['..player.index..'].print(ipcr) end')
18 -- player.print("loadstring1 failed (this may be ok)")
19 f, lserr = loadstring('game.players['..player.index..'].print('..cmd..')')
25 -- player.print("pcall failed")
26 player.print(pcerr:sub(1, pcerr:find('\n')))
29 -- player.print("syntax error2")
30 player.print(lserr:sub(1, lserr:find('\n')))
35 function toggleGui (player)
36 if player.gui.screen.some_luaconsole then
37 storage.cmd = player.gui.screen.some_luaconsole.input.text
38 player.gui.screen.some_luaconsole.destroy()
40 frame = player.gui.screen.add{type = 'frame',
41 name = 'some_luaconsole',
42 direction = 'vertical',
43 caption = {'some_luaconsole.title'}
46 frame.add{type = 'label', caption = {'some_luaconsole.inputlabel'}}
47 input = frame.add{type = 'text-box',
49 style='some_luaconsole_input_textbox'
51 input.word_wrap = true
52 input.style.maximal_height = (player.display_resolution.height/player.display_scale*0.6)
53 input.text = storage.cmd or ""
55 horizontal_flow = frame.add{type='flow', direction='horizontal'}
56 horizontal_flow.add{type = 'button',
57 name = 'some_luaconsole_close',
59 caption = {'some_luaconsole.close'},
60 tooltip = {'some_luaconsole.close_tooltip'}
62 horizontal_flow.add{type = 'button',
63 name = 'some_luaconsole_exec',
64 style='confirm_button',
65 caption = {'some_luaconsole.exec'},
66 tooltip = {'some_luaconsole.exec_tooltip'}
69 if not player.admin then
70 player.gui.screen.some_luaconsole.input.text = 'You are not an admin. You may not use this mod. :('
71 player.gui.screen.some_luaconsole.input.enabled = false
73 frame.force_auto_center()
79 script.on_event(defines.events.on_gui_click, function(event)
80 if event.element.name == 'some_luaconsole_exec' then
81 exec_command(game.players[event.player_index])
82 elseif event.element.name == 'some_luaconsole_close' then
83 toggleGui(game.players[event.player_index])
88 script.on_event('some_luaconsole_toggle', function(event)
89 toggleGui(game.players[event.player_index])
91 script.on_event('some_luaconsole_exec', function(event)
92 exec_command(game.players[event.player_index])