[Mod] LuaCmd [luacmd]

Post Reply
User avatar
prestidigitator
Member
Posts: 640
Joined: Thu Feb 21, 2013 23:54

[Mod] LuaCmd [luacmd]

by prestidigitator » Post

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)
Last edited by prestidigitator on Sun Jul 06, 2014 07:57, edited 2 times in total.

Jordach
Member
Posts: 4523
Joined: Mon Oct 03, 2011 17:58
GitHub: Jordach
IRC: Jordach
In-game: Jordach
Location: Blender Scene

Re: [Mod] LuaCmd [luacmd]

by Jordach » Post

This is like Command Blocks only better.
viewtopic.php?f=10&t=19056 Solar Plains Dev Server

User avatar
Bas080
Member
Posts: 398
Joined: Mon May 21, 2012 15:54
GitHub: bas080
IRC: bas080
In-game: bas080
Location: Netherlands

Re: [Mod] LuaCmd [luacmd]

by Bas080 » Post

Handy tool! Keeping this one!

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

User avatar
Calinou
Moderator
Posts: 3162
Joined: Mon Aug 01, 2011 14:26
GitHub: Calinou
IRC: Calinou
In-game: Calinou
Location: Troyes, France
Contact:

Re: [Mod] LuaCmd [luacmd]

by Calinou » Post

Good idea. Does this let you use os. commands to know server time and manipulate files remotely?

User avatar
prestidigitator
Member
Posts: 640
Joined: Thu Feb 21, 2013 23:54

Re: [Mod] LuaCmd [luacmd]

by prestidigitator » Post

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.

User avatar
prestidigitator
Member
Posts: 640
Joined: Thu Feb 21, 2013 23:54

Re: [Mod] LuaCmd [luacmd]

by prestidigitator » Post

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").

User avatar
prestidigitator
Member
Posts: 640
Joined: Thu Feb 21, 2013 23:54

Re: [Mod] LuaCmd [luacmd]

by prestidigitator » Post

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.

User avatar
prestidigitator
Member
Posts: 640
Joined: Thu Feb 21, 2013 23:54

Re: [Mod] LuaCmd [luacmd]

by prestidigitator » Post

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.

User avatar
Bas080
Member
Posts: 398
Joined: Mon May 21, 2012 15:54
GitHub: bas080
IRC: bas080
In-game: bas080
Location: Netherlands

Re: [Mod] LuaCmd [luacmd]

by Bas080 » Post

Just wanted to let you know that I use this tool with much pleasure. This makes my testing experience much more awesome. <3

User avatar
Wuzzy
Member
Posts: 3933
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy

Re: [Mod] LuaCmd [luacmd]

by Wuzzy » Post

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.

User avatar
jogag
Member
Posts: 106
Joined: Wed Aug 12, 2015 18:32
GitHub: jogag
IRC: jogag
In-game: jogag
Location: Online

Re: [Mod] LuaCmd [luacmd]

by jogag » Post

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.

User avatar
Hybrid Dog
Member
Posts: 2748
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

Re: [Mod] LuaCmd [luacmd]

by Hybrid Dog » Post

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

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

User avatar
jogag
Member
Posts: 106
Joined: Wed Aug 12, 2015 18:32
GitHub: jogag
IRC: jogag
In-game: jogag
Location: Online

Re: [Mod] LuaCmd [luacmd]

by jogag » Post

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.

User avatar
Hybrid Dog
Member
Posts: 2748
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

Re: [Mod] LuaCmd [luacmd]

by Hybrid Dog » Post

thanks for informing me, maybe it's a long time ago when l read the code
https://github.com/Uberi/Minetest-World ... 54798d88eb

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests