Skip to content

Commit 33de69a

Browse files
kaezaShadowNinja
authored andcommittedDec 12, 2013
Add 'on_prejoinplayer' callback
1 parent 76036ab commit 33de69a

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed
 

‎builtin/misc_register.lua

+1
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ minetest.registered_on_generateds, minetest.register_on_generated = make_registr
380380
minetest.registered_on_newplayers, minetest.register_on_newplayer = make_registration()
381381
minetest.registered_on_dieplayers, minetest.register_on_dieplayer = make_registration()
382382
minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make_registration()
383+
minetest.registered_on_prejoinplayers, minetest.register_on_prejoinplayer = make_registration()
383384
minetest.registered_on_joinplayers, minetest.register_on_joinplayer = make_registration()
384385
minetest.registered_on_leaveplayers, minetest.register_on_leaveplayer = make_registration()
385386
minetest.registered_on_player_receive_fields, minetest.register_on_player_receive_fields = make_registration_reverse()

‎doc/lua_api.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,9 @@ minetest.register_on_respawnplayer(func(ObjectRef))
11751175
^ Called when player is to be respawned
11761176
^ Called _before_ repositioning of player occurs
11771177
^ return true in func to disable regular player placement
1178+
minetest.register_on_prejoinplayer(func(name, ip))
1179+
^ Called before a player joins the game
1180+
^ If it returns a string, the player is disconnected with that string as reason
11781181
minetest.register_on_joinplayer(func(ObjectRef))
11791182
^ Called when a player joins the game
11801183
minetest.register_on_leaveplayer(func(ObjectRef))

‎src/script/cpp_api/s_player.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1919

2020
#include "cpp_api/s_player.h"
2121
#include "cpp_api/s_internal.h"
22+
#include "util/string.h"
2223

2324
void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
2425
{
@@ -58,6 +59,23 @@ bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
5859
return positioning_handled_by_some;
5960
}
6061

62+
bool ScriptApiPlayer::on_prejoinplayer(std::string name, std::string ip, std::string &reason)
63+
{
64+
SCRIPTAPI_PRECHECKHEADER
65+
66+
// Get minetest.registered_on_prejoinplayers
67+
lua_getglobal(L, "minetest");
68+
lua_getfield(L, -1, "registered_on_prejoinplayers");
69+
lua_pushstring(L, name.c_str());
70+
lua_pushstring(L, ip.c_str());
71+
script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR);
72+
if (lua_isstring(L, -1)) {
73+
reason.assign(lua_tostring(L, -1));
74+
return true;
75+
}
76+
return false;
77+
}
78+
6179
void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
6280
{
6381
SCRIPTAPI_PRECHECKHEADER

‎src/script/cpp_api/s_player.h

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ScriptApiPlayer
3434
void on_newplayer(ServerActiveObject *player);
3535
void on_dieplayer(ServerActiveObject *player);
3636
bool on_respawnplayer(ServerActiveObject *player);
37+
bool on_prejoinplayer(std::string name, std::string ip, std::string &reason);
3738
void on_joinplayer(ServerActiveObject *player);
3839
void on_leaveplayer(ServerActiveObject *player);
3940
void on_cheat(ServerActiveObject *player, const std::string &cheat_type);

‎src/server.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -1970,6 +1970,19 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
19701970
return;
19711971
}
19721972

1973+
{
1974+
std::string reason;
1975+
if(m_script->on_prejoinplayer(playername, addr_s, reason))
1976+
{
1977+
actionstream<<"Server: Player with the name \""<<playername<<"\" "
1978+
<<"tried to connect from "<<addr_s<<" "
1979+
<<"but it was disallowed for the following reason: "
1980+
<<reason<<std::endl;
1981+
DenyAccess(peer_id, narrow_to_wide(reason.c_str()));
1982+
return;
1983+
}
1984+
}
1985+
19731986
infostream<<"Server: New connection: \""<<playername<<"\" from "
19741987
<<addr_s<<" (peer_id="<<peer_id<<")"<<std::endl;
19751988

0 commit comments

Comments
 (0)
Please sign in to comment.