Skip to content

Commit

Permalink
onPlayerTeamChange Event (multitheftauto#3370)
Browse files Browse the repository at this point in the history
  • Loading branch information
esmail9900 authored and MegadreamsBE committed Jun 6, 2024
1 parent 2d4b040 commit d832071
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
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

0 comments on commit d832071

Please sign in to comment.