README and code
parent
f813c4eca6
commit
0f4906d149
|
@ -0,0 +1,59 @@
|
|||
LuaCmd Minetest Mod
|
||||
===================
|
||||
|
||||
This mod adds the useful debugging command /lua which executes its parameter
|
||||
string as a Lua chunk, as if it were being run in the body of a function inside
|
||||
some mod file. Errors are reported to the player via in-game chat, as is
|
||||
anything sent to the print() function. For example, entering the command:
|
||||
|
||||
]/lua print("Hello self");
|
||||
|
||||
will result in the following output in the in-game chat console:
|
||||
|
||||
issued command: /lua print("Hello self");
|
||||
Hello self
|
||||
|
||||
whereas entering the (erroneous) command:
|
||||
|
||||
]/lua ^
|
||||
|
||||
will result in the following output:
|
||||
|
||||
issued command: /lua ^
|
||||
Server -!- ERROR: [string "/lua command"]:1: unexpected symbol near '^'
|
||||
|
||||
A slightly more useful example might be the command (note: though shown here
|
||||
with line wrapping, it must actually be done on a single line):
|
||||
|
||||
/lua for _, player in ipairs(minetest.get_connected_players()) do
|
||||
print(player:get_player_name()); end
|
||||
|
||||
Any player executing this command must have the new "lua" permission, as it is
|
||||
about the most dangerous thing you could allow a player to do. Also not
|
||||
recommended for public servers. Use at your own risk!
|
||||
|
||||
Required Minetest Version: (tested in 0.4.9)
|
||||
|
||||
Dependencies: (none)
|
||||
|
||||
Commands: /lua <luaStatement>
|
||||
|
||||
Privileges: lua
|
||||
|
||||
Git Repo: https://github.com/prestidigitator/minetest-mod-luacmd
|
||||
|
||||
Change History
|
||||
--------------
|
||||
|
||||
Version 1.0
|
||||
|
||||
* Released 2014-07-04
|
||||
* First working version
|
||||
|
||||
Copyright and Licensing
|
||||
-----------------------
|
||||
|
||||
All contents are the original creations of the mod author (prestidigitator).
|
||||
|
||||
Author: prestidigitator (as registered on forum.minetest.net)
|
||||
License: WTFPL (all content)
|
|
@ -0,0 +1,46 @@
|
|||
local function copyTable(t)
|
||||
if type(t) ~= "table" then return t; end
|
||||
local tc = {};
|
||||
for k, v in pairs(t) do
|
||||
tc[k] = v;
|
||||
end
|
||||
return tc;
|
||||
end
|
||||
|
||||
minetest.register_privilege(
|
||||
"lua",
|
||||
{
|
||||
description = "Allows use of the /lua chat command for debugging.",
|
||||
give_to_singleplayer = false
|
||||
});
|
||||
|
||||
minetest.register_chatcommand(
|
||||
"lua",
|
||||
{
|
||||
params = "<luaStatement>",
|
||||
description = "Executes a lua statement (chunk), for debugging.",
|
||||
privs = { lua = true },
|
||||
func =
|
||||
function(name, param)
|
||||
local cmdFunc, success, errMsg;
|
||||
|
||||
cmdFunc, errMsg = loadstring(param, "/lua command");
|
||||
if not cmdFunc then
|
||||
minetest.chat_send_player(name, "ERROR: "..errMsg);
|
||||
return;
|
||||
end
|
||||
|
||||
local env = copyTable(getfenv(0));
|
||||
env.print =
|
||||
function(...)
|
||||
minetest.chat_send_player(name, table.concat({...}), false);
|
||||
end;
|
||||
|
||||
setfenv(cmdFunc, env);
|
||||
|
||||
success, errMsg = pcall(cmdFunc);
|
||||
if not success then
|
||||
minetest.chat_send_player(name, "ERROR: "..errMsg);
|
||||
end
|
||||
end
|
||||
});
|
Loading…
Reference in New Issue