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 @@ -1591,6 +1591,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
54 changes: 42 additions & 12 deletions Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,36 @@ bool CStaticFunctionDefinitions::DestroyElement(CElement* pElement)

// We can't destroy the root or a player/remote client/console
int iType = pElement->GetType();
if (pElement == m_pMapManager->GetRootElement() || iType == CElement::PLAYER || iType == CElement::CONSOLE ||
if (pElement == m_pMapManager->GetRootElement() || iType == CElement::PLAYER || iType == CElement::CONSOLE ||
g_pGame->GetResourceManager()->IsAResourceElement(pElement))
{
return false;
}

if (iType == CElement::TEAM) { // Its team trigger onPlayerTeamChange for each player in the team
CTeam* pTeam = static_cast<CTeam*>(pElement);

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

for (auto iter = iterBegin; iter != iterEnd; ++iter)
{
CPlayer* player = *iter;
arguments.PushElement(pTeam); // Return team element as oldteam
arguments.PushNil(); // No new team return nil
player->CallEvent("onPlayerTeamChange", arguments);
arguments.DeleteArguments();
}
}

// 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 @@ -9204,21 +9221,34 @@ bool CStaticFunctionDefinitions::SetPlayerTeam(CPlayer* pPlayer, CTeam* pTeam)
{
assert(pPlayer);

CTeam* currentTeam = pPlayer->GetTeam();
// If its a different team
if (pTeam != pPlayer->GetTeam())
if (pTeam == currentTeam)
return false;

// Call the Event
CLuaArguments Arguments;
if (currentTeam)
{
// Change his team
pPlayer->SetTeam(pTeam, true);
Arguments.PushElement(currentTeam);
}
else
{
Arguments.PushNil(); // No oldTeam return nil
}
Arguments.PushElement(pTeam);
if (!pPlayer->CallEvent("onPlayerTeamChange", Arguments))
return false; // Event cancelled, 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));
// Change his team
pPlayer->SetTeam(pTeam, true);

return true;
}
// 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 false;
return true;
}

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