Page 1 of 1

[Mod] LuaCmd [luacmd]

Posted: Fri Jul 04, 2014 10:58
by prestidigitator
Note (2014-07-05): Any updates I make to this mod will be announced here, but documentation changes for such updates will be made on its wiki page at http://wiki.minetest.net/LuaCmd

----

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:

Code: Select all

/lua print("Hello self");
will result in the following output in the in-game chat console:

Code: Select all

issued command: /lua print("Hello self");
Hello self
whereas entering the (erroneous) command:

Code: Select all

/lua ^
will result in the following output:

Code: Select all

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):

Code: Select all

/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: unknown (>=0.4.9 definitely OK)

Dependencies: (none)

Commands:
  • /lua <luaStatement>
  • /luaclear
Privileges: lua

Git Repo: https://github.com/prestidigitator/minetest-mod-luacmd

Change History

Version 1.0
  • Released 2014-07-04
  • First working version
Version 1.1
  • Release 2014-07-04
  • Variable 'me': reference to current player object
  • Variable 'myname': current player's name
  • Variable 'here': current player's position as a printable vector
  • Made the print(...) function a little more useful: converts args to strings
Try the following commands:

Code: Select all

/lua me:set_physics_override({ jump = 10.0 });
/lua print(myname);
/lua print(here);
Version 1.2
  • Release 2014-07-04
  • Keeps "global" variables set by commands in a player-local context.
  • Prevents setting of special variables (e.g. me, myname, here, print).
  • Added /luaclear command to clear the player-local context.
To set true globals visible to mods and other players, use "_G.var = ...". Can also be used to get any globals hidden by specials and player-local variables.

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)

Re: [Mod] LuaCmd [luacmd]

Posted: Fri Jul 04, 2014 11:59
by Jordach
This is like Command Blocks only better.

Re: [Mod] LuaCmd [luacmd]

Posted: Fri Jul 04, 2014 15:15
by Bas080
Handy tool! Keeping this one!

Edit: For debugging some builtin commands might be handy. Printing out tables f.e.

Re: [Mod] LuaCmd [luacmd]

Posted: Fri Jul 04, 2014 16:20
by Calinou
Good idea. Does this let you use os. commands to know server time and manipulate files remotely?

Re: [Mod] LuaCmd [luacmd]

Posted: Fri Jul 04, 2014 21:26
by prestidigitator
Calinou wrote:Does this let you use os. commands to know server time and manipulate files remotely?
Sure thing. Anything you could do from inside a mod. I added the extra variables and print behavior for convenience. They don't affect the global environment; only that seen while running this command. I actually mainly intended it for debugging my own mods, because for example inserting some temporary debugging code and restarting the server to see what happened was getting to be a major pain. This is essentially a debugger, built into the running game.

Re: [Mod] LuaCmd [luacmd]

Posted: Fri Jul 04, 2014 21:30
by prestidigitator
Bas080 wrote:Handy tool! Keeping this one!

Edit: For debugging some builtin commands might be handy. Printing out tables f.e.
True. You should be able to use minetest.serialize() to print things out, but it might be nice to have a few more convenient tools too. I was also thinking of keeping variables around between commands, independently for each player while they are logged in (probably with another command for clearing them). Currently any "global" variable you set will be lost between different commands, unless you store it in an existing global table somewhere (e.g. myModTable.myVariable = "keep this").

Re: [Mod] LuaCmd [luacmd]

Posted: Sat Jul 05, 2014 00:19
by prestidigitator
Released version 1.2. Keeps player-local context, so variables can be kept between commands, and are not visible to other players who can use the /lua command. This way you don't necessarily have to do everything in one line of text. Example:

Code: Select all

/lua players = minetest.get_connected_players();
/lua print(type(players));
table
/lua for _, p in ipairs(players) do print(p:get_player_name()); end
prestidigitator
/luaclear
/lua print(type(players));
nil
Note that the player-local context is cleared when the player logs out. You can also set global variables using the _G global table. For example, John and Fred might issue the following commands and get the indicated results:

Code: Select all

John: /lua message = "Hello self";
John: /lua print(message);
Hello self
Fred: /lua print(message);
Fred: /lua print(type(message));
nil
John: /lua message = nil;
John /lua _G.message = "Hello anyone";
John /lua print(message);
Hello anyone
Fred: /lua print(message);
Hello anyone
Of course, any globals added through _G can change/pollute the global namespace of mods, too, so use with caution.

Re: [Mod] LuaCmd [luacmd]

Posted: Sat Jul 05, 2014 00:37
by prestidigitator
By the way, the following global helper functions are described in lua_api.txt, but not (yet?) on the wiki. (EDIT: Added to wiki.)
lua_api.txt wrote: dump2(obj, name="_", dumped={})
^ Return object serialized as a string, handles reference loops

dump(obj, dumped={})
^ Return object serialized as a string

math.hypot(x, y)
^ Get the hypotenuse of a triangle with legs x and y.
Usefull for distance calculation.

string:split(separator)
^ eg. string:split("a,b", ",") == {"a","b"}

string:trim()
^ eg. string.trim("\n \t\tfoo bar\t ") == "foo bar"
So to do that nice dumping of tables you can actually do one of:

Code: Select all

/lua print(dump(myTable));
/lua print(dump2(myTable));
though for some reason the second one doesn't seem to produce any output. Defect, maybe.

Re: [Mod] LuaCmd [luacmd]

Posted: Sat Feb 07, 2015 02:29
by Bas080
Just wanted to let you know that I use this tool with much pleasure. This makes my testing experience much more awesome. <3

Re: [Mod] LuaCmd [luacmd]

Posted: Sat Feb 07, 2015 06:49
by Wuzzy
Oh, me, too.
I use this mod frequently when testing certain functions or new mods, and it certainly makes some tasks much faster and less painful.

Re: [Mod] LuaCmd [luacmd]

Posted: Wed Aug 26, 2015 18:07
by jogag
This mod is unnecessary if you have worldedit. Worldedit provides a //lua command, but only the main server admin can use that command (in singleplayer, you are the main admin).
So this can be a mod which adds this command to secondary admins on a server.

Re: [Mod] LuaCmd [luacmd]

Posted: Fri Aug 28, 2015 11:14
by Hybrid Dog
jogag wrote:This mod is unnecessary if you have worldedit. Worldedit provides a //lua command, but only the main server admin can use that command (in singleplayer, you are the main admin).
So this can be a mod which adds this command to secondary admins on a server.
as far as l know everyone with server and worldedit priv can use the //lua command, e.g. to install a new mod as a worldmod

Re: [Mod] LuaCmd [luacmd]

Posted: Fri Aug 28, 2015 20:52
by jogag
Hybrid Dog wrote:as far as l know everyone with server and worldedit priv can use the //lua command, e.g. to install a new mod as a worldmod
Wrong. I seen worldedit code and if you have worldedit and server privs but you are not the main admin you can't do any lua command.
(The main admin is set by the "name" setting in minetest.conf, see here)
This is checked in the code of the command, so the worldedit.lua API function can be used by any mod.

Example:
  • On most VanessaE servers, VanessaE (VanessaEzekowitz) is the main admin, and cheapie and ShadowNinja are secondary admins.
  • The "name" setting of the servers is set then to "VanessaEzekowitz".
  • VanessaE can use the //lua command,
  • but cheapie, ShadowNinja and other admins will be blocked.
  • In singleplayer, the "name" setting (both client and local server) is forced to "singleplayer". So you are the main admin and you can use the //lua command as well.

Re: [Mod] LuaCmd [luacmd]

Posted: Fri Aug 28, 2015 23:15
by Hybrid Dog
thanks for informing me, maybe it's a long time ago when l read the code
https://github.com/Uberi/Minetest-World ... 54798d88eb