Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(state/scripting): fixes player natives trying to reference non-existent player entity #2756

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

#include <se/Security.h>

#include <MakeClientFunction.h>

#include <state/ServerGameState.h>

#include <MakePlayerEntityFunction.h>

static void CreatePlayerCommands();

static InitFunction initFunction([]()
Expand All @@ -32,81 +32,29 @@ static InitFunction initFunction([]()

static void CreatePlayerCommands()
{
fx::ScriptEngine::RegisterNativeHandler("GET_PLAYER_WANTED_LEVEL", MakeClientFunction([](fx::ScriptContext& context, const fx::ClientSharedPtr& client)
fx::ScriptEngine::RegisterNativeHandler("GET_PLAYER_WANTED_LEVEL", MakePlayerEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
// get the current resource manager
auto resourceManager = fx::ResourceManager::GetCurrent();

// get the owning server instance
auto instance = resourceManager->GetComponent<fx::ServerInstanceBaseRef>()->Get();

// get the server's game state
auto gameState = instance->GetComponent<fx::ServerGameState>();

try
{
auto entity = gameState->GetEntity(fx::AnyCast<uint32_t>(client->GetData("playerEntity")));

auto node = entity->syncTree->GetPlayerWantedAndLOS();
auto node = entity->syncTree->GetPlayerWantedAndLOS();

return node ? node->wantedLevel : 0;
}
catch (std::bad_any_cast&)
{
return 0;
}
return node ? node->wantedLevel : 0;
}));

fx::ScriptEngine::RegisterNativeHandler("IS_PLAYER_EVADING_WANTED_LEVEL", MakeClientFunction([](fx::ScriptContext& context, const fx::ClientSharedPtr& client)
fx::ScriptEngine::RegisterNativeHandler("IS_PLAYER_EVADING_WANTED_LEVEL", MakePlayerEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
// get the current resource manager
auto resourceManager = fx::ResourceManager::GetCurrent();

// get the owning server instance
auto instance = resourceManager->GetComponent<fx::ServerInstanceBaseRef>()->Get();

// get the server's game state
auto gameState = instance->GetComponent<fx::ServerGameState>();

try
{
auto entity = gameState->GetEntity(fx::AnyCast<uint32_t>(client->GetData("playerEntity")));

auto node = entity->syncTree->GetPlayerWantedAndLOS();
auto node = entity->syncTree->GetPlayerWantedAndLOS();

return node ? node->isEvading : 0;
}
catch (std::bad_any_cast&)
{
return 0;
}
return node ? node->isEvading : 0;
}));

fx::ScriptEngine::RegisterNativeHandler("GET_PLAYER_TIME_IN_PURSUIT", MakeClientFunction([](fx::ScriptContext& context, const fx::ClientSharedPtr& client)
fx::ScriptEngine::RegisterNativeHandler("GET_PLAYER_TIME_IN_PURSUIT", MakePlayerEntityFunction([](fx::ScriptContext& context, const fx::sync::SyncEntityPtr& entity)
{
// get the current resource manager
auto resourceManager = fx::ResourceManager::GetCurrent();

// get the owning server instance
auto instance = resourceManager->GetComponent<fx::ServerInstanceBaseRef>()->Get();
bool prevPursuitArg = context.GetArgument<bool>(1);

// get the server's game state
auto gameState = instance->GetComponent<fx::ServerGameState>();

try
if (auto node = entity->syncTree->GetPlayerWantedAndLOS())
{
auto entity = gameState->GetEntity(fx::AnyCast<uint32_t>(client->GetData("playerEntity")));

auto node = entity->syncTree->GetPlayerWantedAndLOS();
bool prevPursuitArg = context.GetArgument<bool>(1);

if (node)
return prevPursuitArg ? node->timeInPrevPursuit : node->timeInPursuit;
else
return -1;
return prevPursuitArg ? node->timeInPrevPursuit : node->timeInPursuit;
}
catch (std::bad_any_cast&) {
return -1;
}
}));

return -1;
}, -1));
}
Loading