Skip to content

Commit 40dd03e

Browse files
authoredSep 4, 2017
Implement minetest.register_can_bypass_userlimit (#6369)
* Implement minetest.register_on_userlimit_check This new callback permits to bypass the max_users parameter with new mods condition, based on player name or IP Only one mod needs to permit it. Move core part for builtin privileges checks to builtin
1 parent c05228f commit 40dd03e

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed
 

‎builtin/game/privileges.lua

+5
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ core.register_privilege("debug", {
8686
description = "Allows enabling various debug options that may affect gameplay",
8787
give_to_singleplayer = false,
8888
})
89+
90+
core.register_can_bypass_userlimit(function(name, ip)
91+
local privs = core.get_player_privs(name)
92+
return privs["server"] or privs["ban"] or privs["privs"] or privs["password"]
93+
end)

‎builtin/game/register.lua

+1
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ core.registered_on_item_eats, core.register_on_item_eat = make_registration()
581581
core.registered_on_punchplayers, core.register_on_punchplayer = make_registration()
582582
core.registered_on_priv_grant, core.register_on_priv_grant = make_registration()
583583
core.registered_on_priv_revoke, core.register_on_priv_revoke = make_registration()
584+
core.registered_can_bypass_userlimit, core.register_can_bypass_userlimit = make_registration()
584585

585586
--
586587
-- Compatibility for on_mapgen_init()

‎doc/lua_api.txt

+3
Original file line numberDiff line numberDiff line change
@@ -2470,6 +2470,9 @@ Call these functions only at load time!
24702470
* Called when `revoker` revokes the priv `priv` from `name`.
24712471
* Note that the callback will be called twice if it's done by a player, once with revoker being the player name,
24722472
and again with revoker being nil.
2473+
* `minetest.register_can_bypass_userlimit(function(name, ip))`
2474+
* Called when `name` user connects with `ip`.
2475+
* Return `true` to by pass the player limit
24732476

24742477
### Other registration functions
24752478
* `minetest.register_chatcommand(cmd, chatcommand definition)`

‎src/network/serverpackethandler.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,10 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
209209
<< addr_s << " (peer_id=" << pkt->getPeerId() << ")" << std::endl;
210210

211211
// Enforce user limit.
212-
// Don't enforce for users that have some admin right
212+
// Don't enforce for users that have some admin right or mod permits it.
213213
if (m_clients.isUserLimitReached() &&
214-
!checkPriv(playername, "server") &&
215-
!checkPriv(playername, "ban") &&
216-
!checkPriv(playername, "privs") &&
217-
!checkPriv(playername, "password") &&
218-
playername != g_settings->get("name")) {
214+
playername != g_settings->get("name") &&
215+
!m_script->can_bypass_userlimit(playername, addr_s)) {
219216
actionstream << "Server: " << playername << " tried to join from "
220217
<< addr_s << ", but there" << " are already max_users="
221218
<< g_settings->getU16("max_users") << " players." << std::endl;

‎src/script/cpp_api/s_player.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121
#include "cpp_api/s_internal.h"
2222
#include "common/c_converter.h"
2323
#include "common/c_content.h"
24+
#include "debug.h"
2425
#include "util/string.h"
2526

2627
void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
@@ -123,6 +124,20 @@ bool ScriptApiPlayer::on_prejoinplayer(
123124
return false;
124125
}
125126

127+
bool ScriptApiPlayer::can_bypass_userlimit(const std::string &name, const std::string &ip)
128+
{
129+
SCRIPTAPI_PRECHECKHEADER
130+
131+
// Get core.registered_on_prejoinplayers
132+
lua_getglobal(L, "core");
133+
lua_getfield(L, -1, "registered_can_bypass_userlimit");
134+
lua_pushstring(L, name.c_str());
135+
lua_pushstring(L, ip.c_str());
136+
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
137+
FATAL_ERROR_IF(!lua_isboolean(L, -1), "on_user_limitcheck must return a boolean");
138+
return lua_toboolean(L, -1);
139+
}
140+
126141
void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
127142
{
128143
SCRIPTAPI_PRECHECKHEADER

‎src/script/cpp_api/s_player.h

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ScriptApiPlayer : virtual public ScriptApiBase
3535
bool on_respawnplayer(ServerActiveObject *player);
3636
bool on_prejoinplayer(const std::string &name, const std::string &ip,
3737
std::string *reason);
38+
bool can_bypass_userlimit(const std::string &name, const std::string &ip);
3839
void on_joinplayer(ServerActiveObject *player);
3940
void on_leaveplayer(ServerActiveObject *player, bool timeout);
4041
void on_cheat(ServerActiveObject *player, const std::string &cheat_type);

0 commit comments

Comments
 (0)
Please sign in to comment.