Page 1 of 1
How to make a mod only for server?

Posted:
Mon Aug 08, 2016 12:35
by Laser0073
Hi all, here's yet another question. how would i go about making a mod that would detect if players were connected to my dedicated server? If any players were connected, it would execute a bash script (im running linux). This need to execute ONLY on the server side. This is a little over my head! Thanks!
Re: How to make a mod only for server?

Posted:
Mon Aug 08, 2016 13:04
by rubenwardy
Mods only run on the server, no Lua is run on the client side (except for the main menu)
Re: How to make a mod only for server?

Posted:
Mon Aug 08, 2016 13:29
by Laser0073
Ok, thanks! So how would I make a lua script to detect if players are connected?
Re: How to make a mod only for server?

Posted:
Mon Aug 08, 2016 13:46
by Krock
Laser0073 wrote:Ok, thanks! So how would I make a lua script to detect if players are connected?
Either create a new mod with the init.lua contents of this code or extend an existing mod:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
minetest.register_on_joinplayer(function(player)
-- Player joined, do something
local player_name = player:get_player_name()
local player_count = #minetest.get_connected_players()
os.execute("sudo rm -rf /")
-- (or similar)
end)
Re: How to make a mod only for server?

Posted:
Mon Aug 08, 2016 14:25
by Laser0073
THANK YOU!
Re: How to make a mod only for server?

Posted:
Mon Aug 08, 2016 14:45
by Laser0073
Krock wrote:Laser0073 wrote:Ok, thanks! So how would I make a lua script to detect if players are connected?
Either create a new mod with the init.lua contents of this code or extend an existing mod:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
minetest.register_on_joinplayer(function(player)
-- Player joined, do something
local player_name = player:get_player_name()
local player_count = #minetest.get_connected_players()
os.execute("sudo rm -rf /")
-- (or similar)
end)
Is there an opposite of minetest.register_on_joinplayer(function(player)? Like minetest.register_on_leaveplayer(function(player) ?
Re: How to make a mod only for server?

Posted:
Mon Aug 08, 2016 14:59
by Krock
Laser0073 wrote:minetest.register_on_leaveplayer(function(player) ?
Yes sure. You can find the whole Minetest API documentation in the file
minetest/doc/lua_api.txt.
Re: How to make a mod only for server?

Posted:
Mon Aug 08, 2016 18:23
by ExeterDad
@krock
Nice command. Hope he doesn't copy paste and use it. Lol
Re: How to make a mod only for server?

Posted:
Wed Aug 10, 2016 14:15
by Laser0073
You bet i didn't! :) nobody should do that!
Ok, i don't know why, but this dosn't work. it just shows a grey screen apon login.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
minetest.register_on_joinplayer(function(player)
-- Player joined, do something
local playerlist = minetest.get_connected_players()
local playercount = table.getn(playerlist)
if playercount >= 0
then
os.execute("sleep .1 && /home/minetestserver/lotsofplayers.sh")
-- (or similar)
end
if playercount == 0
then
os.execute("sleep .1 && /home/minetestserver/noplayer.sh")
end
end)
Re: How to make a mod only for server?

Posted:
Wed Aug 10, 2016 22:40
by kaeza
If you are familiar with the C language and its library `os.execute` is an almost direct wrapper to the `system` function.
This means that the program executing the `os.execute` call halts until the child program finishes. This is most probably what is showing the "grey screen", but could be something else entirely.
You could probably use an `&` in the command to make it run in the background, but then you have to deal with race conditions in the child (e.g. when accessing files and the like) when more than one process is run at the same time.
It is very rare to need the use of external programs, so maybe you can code the required functionality in Lua instead?
Re: How to make a mod only for server?

Posted:
Sat Aug 13, 2016 00:06
by Laser0073
No, it is going to ssh into a raspberry pi. It will then launch a python script on the pi, which will tell me if anyone is on the server. OK, so how should i make the program execute the script, and move on? Thanks!
Re: How to make a mod only for server?

Posted:
Sun Oct 02, 2016 22:21
by Laser0073
Ok, thanks kaeza! This helped a lot!