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

onPlayerTeamChange Event #3370

Merged
merged 13 commits into from
May 23, 2024
1 change: 1 addition & 0 deletions Server/mods/deathmatch/logic/CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,7 @@ void CGame::AddBuiltInEvents()
m_Events.AddEvent("onPlayerProjectileCreation", "weaponType, posX, posY, posZ, force, target, rotX, rotY, rotZ, velX, velY, velZ", nullptr, false);
m_Events.AddEvent("onPlayerDetonateSatchels", "", nullptr, false);
m_Events.AddEvent("onPlayerTriggerEventThreshold", "", nullptr, false);
m_Events.AddEvent("onPlayerTeamChange", "oldTeam, newTeam", nullptr, false);

// Ped events
m_Events.AddEvent("onPedVehicleEnter", "vehicle, seat, jacked", NULL, false);
Expand Down
52 changes: 39 additions & 13 deletions Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,30 @@ bool CStaticFunctionDefinitions::DestroyElement(CElement* pElement)
{
return false;
}
// Its team trigger onPlayerTeamChange for each player in the team
else if (iType == CElement::TEAM)
{
esmail9900 marked this conversation as resolved.
Show resolved Hide resolved
CTeam* pTeam = static_cast<CTeam*>(pElement);

auto iterBegin = pTeam->PlayersBegin();
auto iterEnd = pTeam->PlayersEnd();

for (auto iter = iterBegin; iter != iterEnd; ++iter){
esmail9900 marked this conversation as resolved.
Show resolved Hide resolved
CPlayer* player = *iter;
CLuaArguments arguments;
esmail9900 marked this conversation as resolved.
Show resolved Hide resolved
arguments.PushNil(); // Current team going to delete so return nil
esmail9900 marked this conversation as resolved.
Show resolved Hide resolved
arguments.PushNil(); // No new team return nil
player->CallEvent("onPlayerTeamChange", arguments);
}
}

// Tell everyone to destroy it if this is not a per-player entity
if (IS_PERPLAYER_ENTITY(pElement))
{
// Unsync it (will destroy it for those that know about it)
CPerPlayerEntity* pEntity = static_cast<CPerPlayerEntity*>(pElement);
pEntity->Sync(false);
}
}

// Tell everyone to destroy it
CEntityRemovePacket Packet;
Expand Down Expand Up @@ -9203,22 +9219,32 @@ bool CStaticFunctionDefinitions::SetTeamColor(CTeam* pTeam, unsigned char ucRed,
bool CStaticFunctionDefinitions::SetPlayerTeam(CPlayer* pPlayer, CTeam* pTeam)
{
assert(pPlayer);

CTeam* currentTeam = pPlayer->GetTeam();
esmail9900 marked this conversation as resolved.
Show resolved Hide resolved
// If its a different team
if (pTeam != pPlayer->GetTeam())
{
// Change his team
pPlayer->SetTeam(pTeam, true);
if (pTeam == currentTeam)
return false;

// Tell everyone his new team
CBitStream BitStream;
BitStream.pBitStream->Write(pTeam ? pTeam->GetID() : INVALID_ELEMENT_ID);
m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pPlayer, SET_PLAYER_TEAM, *BitStream.pBitStream));
// Call the Event
CLuaArguments Arguments;
if (currentTeam){
Arguments.PushElement(currentTeam);
} else {
Arguments.PushNil(); // No oldTeam return nil
}
Arguments.PushElement(pTeam);
if (!pPlayer->CallEvent("onPlayerTeamChange", Arguments))
// Event cancelled, return false
return false;

return true;
}
// Change his team
pPlayer->SetTeam(pTeam, true);

return false;
// Tell everyone his new team
CBitStream BitStream;
BitStream.pBitStream->Write(pTeam ? pTeam->GetID() : INVALID_ELEMENT_ID);
m_pPlayerManager->BroadcastOnlyJoined(CElementRPCPacket(pPlayer, SET_PLAYER_TEAM, *BitStream.pBitStream));

return true;
}

bool CStaticFunctionDefinitions::SetTeamFriendlyFire(CTeam* pTeam, bool bFriendlyFire)
Expand Down