From 443585a02950716ef4e39510a5036538ba5319c5 Mon Sep 17 00:00:00 2001 From: AvarianKnight Date: Mon, 10 Jul 2023 23:01:27 -0500 Subject: [PATCH] feat(server): add DOES_PLAYER_EXIST native this should replace the need to use GetPlayerPing and other natives which rely on implementation details --- .../src/PlayerScriptFunctions.cpp | 5 +++ ext/native-decls/DoesPlayerExist.md | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 ext/native-decls/DoesPlayerExist.md diff --git a/code/components/citizen-server-impl/src/PlayerScriptFunctions.cpp b/code/components/citizen-server-impl/src/PlayerScriptFunctions.cpp index 042c436895..29d99a2bea 100644 --- a/code/components/citizen-server-impl/src/PlayerScriptFunctions.cpp +++ b/code/components/citizen-server-impl/src/PlayerScriptFunctions.cpp @@ -30,6 +30,11 @@ static void CreatePlayerCommands() return client->GetName().c_str(); })); + fx::ScriptEngine::RegisterNativeHandler("DOES_PLAYER_EXIST", MakeClientFunction([](fx::ScriptContext& context, const fx::ClientSharedPtr& client) + { + return true; + })); + fx::ScriptEngine::RegisterNativeHandler("GET_PLAYER_GUID", MakeClientFunction([](fx::ScriptContext& context, const fx::ClientSharedPtr& client) { return client->GetGuid().c_str(); diff --git a/ext/native-decls/DoesPlayerExist.md b/ext/native-decls/DoesPlayerExist.md new file mode 100644 index 0000000000..3777f516d5 --- /dev/null +++ b/ext/native-decls/DoesPlayerExist.md @@ -0,0 +1,39 @@ +--- +ns: CFX +apiset: server +--- +## DOES_PLAYER_EXIST + +```c +BOOL DOES_PLAYER_EXIST(char* playerSrc); +``` + +Returns whether or not the player exists + +## Return value +True of the player exists, false otherwise + +## Examples + +```lua +local deferralMessages = { "Isn't this just magical!", "We can defer all day!", "You'll get in eventually", "You're totally not going to sit here forever", "The Fruit Tree is a lie" } +AddEventHandler("playerConnecting", function(name, setKickReason, deferrals) + local source = source + deferrals.defer() + + Wait(0) + + + local messageIndex = 0 + + repeat + Wait(2000) + if messageIndex >= #deferralMessages then + deferrals.done() + else + messageIndex = messageIndex + 1 + end + deferrals.update(deferralMessages[messageIndex]) + until not DoesPlayerExist(source) +end) +```