|
| 1 | +-- Allows running Eluna lua from ingame chat and from server console |
| 2 | +-- This command is intended for development purposes |
| 3 | +-- Example usage: |
| 4 | +-- .eluna pp(GetLuaEngine(), "Code ran by:", plr and plr:GetName() or "console") |
| 5 | + |
| 6 | +-- pp is defined to be a function to send a message to everyone on server and to console for easy printing |
| 7 | +-- plr is the player using the command or nil if command from console |
| 8 | +-- sel is the selection of plr or nil if nothing selected |
| 9 | + |
| 10 | +-- errors in the lua code executed are printed to command invoker only (console or player), they are not logged |
| 11 | +-- errors in timed events made with the code and such delayed actions are reported by normal means to the error logs and console if so defined in Eluna settings. |
| 12 | + |
| 13 | +local runcmd = "eluna" |
| 14 | +local function RunCommand(event, player, cmd) |
| 15 | + if (cmd:lower():find("^"..runcmd.." .+")) then |
| 16 | + -- Here you can define some environment variables for the code |
| 17 | + -- I defined plr to be the player or nil |
| 18 | + -- sel to be the current selection of the player or nil |
| 19 | + -- pp to print the passed arguments to everyone and to console |
| 20 | + local env = [[ |
| 21 | + local plr = ... |
| 22 | + local sel = sel |
| 23 | + local pp = function(...) |
| 24 | + local t = {...} |
| 25 | + for i = 1, select("#", ...) do t[i] = tostring(t[i]) end |
| 26 | + local msg = table.concat(t, " ") |
| 27 | + SendWorldMessage(msg) |
| 28 | + print(msg) |
| 29 | + end |
| 30 | + if (plr) then |
| 31 | + sel = plr:GetSelection() |
| 32 | + end |
| 33 | + ]] |
| 34 | + local code = env..cmd:sub(#runcmd+2) |
| 35 | + local func, err = load(code, "."..runcmd) |
| 36 | + if (func) then |
| 37 | + local res |
| 38 | + res, err = pcall(func, player) |
| 39 | + if (res) then |
| 40 | + return false |
| 41 | + end |
| 42 | + end |
| 43 | + if (not player) then |
| 44 | + print(err) |
| 45 | + else |
| 46 | + player:SendBroadcastMessage(err) |
| 47 | + end |
| 48 | + return false |
| 49 | + end |
| 50 | +end |
| 51 | + |
| 52 | +RegisterPlayerEvent(42, RunCommand) |
0 commit comments