[Mod] LuaCmd [luacmd]

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

[Mod] LuaCmd [luacmd]

by prestidigitator » Fri Jul 04, 2014 10:58

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.
 

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

Re: [Mod] LuaCmd [luacmd]

by Jordach » Fri Jul 04, 2014 11:59

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
Location: Netherlands
GitHub: bas080
IRC: bas080
In-game: bas080

Re: [Mod] LuaCmd [luacmd]

by Bas080 » Fri Jul 04, 2014 15:15

Handy tool! Keeping this one!

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

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

Re: [Mod] LuaCmd [luacmd]

by Calinou » Fri Jul 04, 2014 16:20

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

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

Re: [Mod] LuaCmd [luacmd]

by prestidigitator » Fri Jul 04, 2014 21:26

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.
 

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

Re: [Mod] LuaCmd [luacmd]

by prestidigitator » Fri Jul 04, 2014 21:30

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

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

Re: [Mod] LuaCmd [luacmd]

by prestidigitator » Sat Jul 05, 2014 00:19

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.
 

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

Re: [Mod] LuaCmd [luacmd]

by prestidigitator » Sat Jul 05, 2014 00:37

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
Location: Netherlands
GitHub: bas080
IRC: bas080
In-game: bas080

Re: [Mod] LuaCmd [luacmd]

by Bas080 » Sat Feb 07, 2015 02:29

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: 3243
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy

Re: [Mod] LuaCmd [luacmd]

by Wuzzy » Sat Feb 07, 2015 06:49

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
Location: Online
GitHub: jogag
IRC: jogag
In-game: jogag

Re: [Mod] LuaCmd [luacmd]

by jogag » Wed Aug 26, 2015 18:07

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: 2684
Joined: Thu Nov 01, 2012 12:46

Re: [Mod] LuaCmd [luacmd]

by Hybrid Dog » Fri Aug 28, 2015 11:14

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
Location: Online
GitHub: jogag
IRC: jogag
In-game: jogag

Re: [Mod] LuaCmd [luacmd]

by jogag » Fri Aug 28, 2015 20:52

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: 2684
Joined: Thu Nov 01, 2012 12:46

Re: [Mod] LuaCmd [luacmd]

by Hybrid Dog » Fri Aug 28, 2015 23:15

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

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


Return to Mod Releases



Who is online

Users browsing this forum: Bing [Bot] and 10 guests