Skip to content

Commit cf3985c

Browse files
authored
Add Lua function to get a player's role (#366)
Adds `MP.GetPlayerRole(player_id)` to the Lua API to get a player's role ("USER", "EA", "MDEV", "STAFF", "ET") by their player id. Currently you can only get someone's role in onPlayerAuth from the parameters and in onVehicleSpawned and onVehicleEdited from the packet data, but not in onPlayerJoin for example without storing it.
2 parents b04c506 + 9a0270c commit cf3985c

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

include/TLuaEngine.h

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ class TLuaEngine : public std::enable_shared_from_this<TLuaEngine>, IThreaded {
263263
sol::table Lua_TriggerGlobalEvent(const std::string& EventName, sol::variadic_args EventArgs);
264264
sol::table Lua_TriggerLocalEvent(const std::string& EventName, sol::variadic_args EventArgs);
265265
sol::table Lua_GetPlayerIdentifiers(int ID);
266+
std::variant<std::string, sol::nil_t> Lua_GetPlayerRole(int ID);
266267
sol::table Lua_GetPlayers();
267268
std::string Lua_GetPlayerName(int ID);
268269
sol::table Lua_GetPlayerVehicles(int ID);

src/TLuaEngine.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,16 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayerIdentifiers(int ID) {
595595
}
596596
}
597597

598+
std::variant<std::string, sol::nil_t> TLuaEngine::StateThreadData::Lua_GetPlayerRole(int ID) {
599+
auto MaybeClient = GetClient(mEngine->Server(), ID);
600+
if (MaybeClient) {
601+
return MaybeClient.value().lock()->GetRoles();
602+
} else {
603+
return sol::nil;
604+
}
605+
}
606+
607+
598608
sol::table TLuaEngine::StateThreadData::Lua_GetPlayers() {
599609
sol::table Result = mStateView.create_table();
600610
mEngine->Server().ForEachClient([&](std::weak_ptr<TClient> Client) -> bool {
@@ -892,6 +902,9 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
892902
MPTable.set_function("GetPlayerIdentifiers", [&](int ID) -> sol::table {
893903
return Lua_GetPlayerIdentifiers(ID);
894904
});
905+
MPTable.set_function("GetPlayerRole", [&](int ID) -> std::variant<std::string, sol::nil_t> {
906+
return Lua_GetPlayerRole(ID);
907+
});
895908
MPTable.set_function("Sleep", &LuaAPI::MP::Sleep);
896909
// const std::string& EventName, size_t IntervalMS, int strategy
897910
MPTable.set_function("CreateEventTimer", [&](sol::variadic_args Args) {

0 commit comments

Comments
 (0)