From 71275aedf4a95f9f2eebb8a3b7aebfe05ee93c11 Mon Sep 17 00:00:00 2001 From: TheNormalnij Date: Sat, 25 May 2019 14:59:26 +0300 Subject: [PATCH 1/5] Fix #948: cancelEvent works onElementChange --- .../logic/CStaticFunctionDefinitions.cpp | 61 +++++++++++++------ 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index 0c88b579358..60d14914ce2 100644 --- a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -1580,11 +1580,17 @@ bool CStaticFunctionDefinitions::SetElementModel(CElement* pElement, unsigned sh return false; if (!CPlayerManager::IsValidPlayerModel(usModel)) return false; + unsigned short usOldModel = pPed->GetModel(); // Get the old model CLuaArguments Arguments; - Arguments.PushNumber(pPed->GetModel()); // Get the old model + Arguments.PushNumber(usOldModel); pPed->SetModel(usModel); // Set the new model Arguments.PushNumber(usModel); // Get the new model - pPed->CallEvent("onElementModelChange", Arguments); + if (!pPed->CallEvent("onElementModelChange", Arguments)) + { + // Change canceled + pPed->SetModel(usOldModel); + return false; + } break; } case CElement::VEHICLE: @@ -1594,28 +1600,37 @@ bool CStaticFunctionDefinitions::SetElementModel(CElement* pElement, unsigned sh return false; if (!CVehicleManager::IsValidModel(usModel)) return false; + unsigned short usOldModel = pVehicle->GetModel(); // Get the old model CLuaArguments Arguments; - Arguments.PushNumber(pVehicle->GetModel()); // Get the old model + Arguments.PushNumber(usOldModel); pVehicle->SetModel(usModel); // Set the new model Arguments.PushNumber(usModel); // Get the new model - pVehicle->CallEvent("onElementModelChange", Arguments); - - // Check for any passengers above the max seat list - unsigned char ucMaxPassengers = pVehicle->GetMaxPassengers(); - CLogger::DebugPrintf("Max passengers = %u\n", ucMaxPassengers); - unsigned char i = 0; - for (; i < MAX_VEHICLE_SEATS; i++) - { - // Got a player in this seat and is it bigger than the supported amount - // of seats in this new vehicle - CPed* pPed = pVehicle->GetOccupant(i); - if (pPed && IS_PLAYER(pPed) && (i > ucMaxPassengers)) + if (!pVehicle->CallEvent("onElementModelChange", Arguments)) + { + // Change canceled + pVehicle->SetModel(usOldModel); + return false; + } + else + { + // Check for any passengers above the max seat list + unsigned char ucMaxPassengers = pVehicle->GetMaxPassengers(); + CLogger::DebugPrintf("Max passengers = %u\n", ucMaxPassengers); + unsigned char i = 0; + for (; i < MAX_VEHICLE_SEATS; i++) { - // Throw him out - // TODO: Maybe relocate him in the future. Find a free seat if available and put him in it. - RemovePedFromVehicle(pPed); + // Got a player in this seat and is it bigger than the supported amount + // of seats in this new vehicle + CPed* pPed = pVehicle->GetOccupant(i); + if (pPed && IS_PLAYER(pPed) && (i > ucMaxPassengers)) + { + // Throw him out + // TODO: Maybe relocate him in the future. Find a free seat if available and put him in it. + RemovePedFromVehicle(pPed); + } } } + break; } case CElement::OBJECT: @@ -1625,11 +1640,17 @@ bool CStaticFunctionDefinitions::SetElementModel(CElement* pElement, unsigned sh return false; if (!CObjectManager::IsValidModel(usModel)) return false; + unsigned short usOldModel = pObject->GetModel(); // Get the old model CLuaArguments Arguments; - Arguments.PushNumber(pObject->GetModel()); // Get the old model + Arguments.PushNumber(usOldModel); // Get the old model pObject->SetModel(usModel); // Set the new model Arguments.PushNumber(usModel); // Get the new model - pObject->CallEvent("onElementModelChange", Arguments); + if (!pObject->CallEvent("onElementModelChange", Arguments)) + { + // Change canceled + pObject->SetModel(usOldModel); + return false; + } break; } default: From c156290a8880c05a8bbce417d89d15b7b1b949d4 Mon Sep 17 00:00:00 2001 From: TheNormalnij Date: Sat, 25 May 2019 15:14:05 +0300 Subject: [PATCH 2/5] remove unnecessary else --- .../logic/CStaticFunctionDefinitions.cpp | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index 60d14914ce2..9ba103f1887 100644 --- a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -1611,23 +1611,21 @@ bool CStaticFunctionDefinitions::SetElementModel(CElement* pElement, unsigned sh pVehicle->SetModel(usOldModel); return false; } - else + + // Check for any passengers above the max seat list + unsigned char ucMaxPassengers = pVehicle->GetMaxPassengers(); + CLogger::DebugPrintf("Max passengers = %u\n", ucMaxPassengers); + unsigned char i = 0; + for (; i < MAX_VEHICLE_SEATS; i++) { - // Check for any passengers above the max seat list - unsigned char ucMaxPassengers = pVehicle->GetMaxPassengers(); - CLogger::DebugPrintf("Max passengers = %u\n", ucMaxPassengers); - unsigned char i = 0; - for (; i < MAX_VEHICLE_SEATS; i++) + // Got a player in this seat and is it bigger than the supported amount + // of seats in this new vehicle + CPed* pPed = pVehicle->GetOccupant(i); + if (pPed && IS_PLAYER(pPed) && (i > ucMaxPassengers)) { - // Got a player in this seat and is it bigger than the supported amount - // of seats in this new vehicle - CPed* pPed = pVehicle->GetOccupant(i); - if (pPed && IS_PLAYER(pPed) && (i > ucMaxPassengers)) - { - // Throw him out - // TODO: Maybe relocate him in the future. Find a free seat if available and put him in it. - RemovePedFromVehicle(pPed); - } + // Throw him out + // TODO: Maybe relocate him in the future. Find a free seat if available and put him in it. + RemovePedFromVehicle(pPed); } } From cdd617817d9535102c1cc919bbafa690bb62575a Mon Sep 17 00:00:00 2001 From: TheNormalnij Date: Sat, 25 May 2019 15:23:09 +0300 Subject: [PATCH 3/5] remove old comment --- Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index 9ba103f1887..f195d7c7fcb 100644 --- a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -1640,7 +1640,7 @@ bool CStaticFunctionDefinitions::SetElementModel(CElement* pElement, unsigned sh return false; unsigned short usOldModel = pObject->GetModel(); // Get the old model CLuaArguments Arguments; - Arguments.PushNumber(usOldModel); // Get the old model + Arguments.PushNumber(usOldModel); pObject->SetModel(usModel); // Set the new model Arguments.PushNumber(usModel); // Get the new model if (!pObject->CallEvent("onElementModelChange", Arguments)) From dc2d606050594224079dd2dd6a4a9bbf13f8f237 Mon Sep 17 00:00:00 2001 From: TheNormalnij Date: Sat, 25 May 2019 17:41:19 +0300 Subject: [PATCH 4/5] Check for another call to setElementModel --- .../logic/CStaticFunctionDefinitions.cpp | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index f195d7c7fcb..3abd34b20d8 100644 --- a/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -1585,7 +1585,12 @@ bool CStaticFunctionDefinitions::SetElementModel(CElement* pElement, unsigned sh Arguments.PushNumber(usOldModel); pPed->SetModel(usModel); // Set the new model Arguments.PushNumber(usModel); // Get the new model - if (!pPed->CallEvent("onElementModelChange", Arguments)) + bool bContinue = pPed->CallEvent("onElementModelChange", Arguments); + // Check for another call to setElementModel + if (usModel != pPed->GetModel()) + return false; + + if (!bContinue) { // Change canceled pPed->SetModel(usOldModel); @@ -1605,7 +1610,12 @@ bool CStaticFunctionDefinitions::SetElementModel(CElement* pElement, unsigned sh Arguments.PushNumber(usOldModel); pVehicle->SetModel(usModel); // Set the new model Arguments.PushNumber(usModel); // Get the new model - if (!pVehicle->CallEvent("onElementModelChange", Arguments)) + bool bContinue = pVehicle->CallEvent("onElementModelChange", Arguments); + // Check for another call to setElementModel + if (usModel != pVehicle->GetModel()) + return false; + + if (!bContinue) { // Change canceled pVehicle->SetModel(usOldModel); @@ -1643,7 +1653,12 @@ bool CStaticFunctionDefinitions::SetElementModel(CElement* pElement, unsigned sh Arguments.PushNumber(usOldModel); pObject->SetModel(usModel); // Set the new model Arguments.PushNumber(usModel); // Get the new model - if (!pObject->CallEvent("onElementModelChange", Arguments)) + bool bContinue = pObject->CallEvent("onElementModelChange", Arguments); + // Check for another call to setElementModel + if (usModel != pObject->GetModel()) + return false; + + if (!bContinue) { // Change canceled pObject->SetModel(usOldModel); From bebce448573c8bf6e20c7bb3ee363c84beca6dd8 Mon Sep 17 00:00:00 2001 From: Uladzislau Nikalayevich Date: Sat, 24 Oct 2020 08:37:58 +0300 Subject: [PATCH 5/5] Allow cancel onClientElementModelChange --- .../logic/CStaticFunctionDefinitions.cpp | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index 9cdb305b358..e67f4afd7c9 100644 --- a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -1461,7 +1461,19 @@ bool CStaticFunctionDefinitions::SetElementModel(CClientEntity& Entity, unsigned CLuaArguments Arguments; Arguments.PushNumber(usCurrentModel); Arguments.PushNumber(usModel); - Ped.CallEvent("onClientElementModelChange", Arguments, true); + bool bContinue = Ped.CallEvent("onClientElementModelChange", Arguments, true); + + // Check for another call to setElementModel + if (usModel != Ped.GetModel()) + return false; + + if (!bContinue) + { + // Change canceled + Ped.SetModel(usCurrentModel); + return false; + } + break; } case CCLIENTVEHICLE: @@ -1480,7 +1492,19 @@ bool CStaticFunctionDefinitions::SetElementModel(CClientEntity& Entity, unsigned CLuaArguments Arguments; Arguments.PushNumber(usCurrentModel); Arguments.PushNumber(usModel); - Vehicle.CallEvent("onClientElementModelChange", Arguments, true); + bool bContinue = Vehicle.CallEvent("onClientElementModelChange", Arguments, true); + + // Check for another call to setElementModel + if (usModel != Vehicle.GetModel()) + return false; + + if (!bContinue) + { + // Change canceled + Vehicle.SetModelBlocking(usCurrentModel, 255, 255); + return false; + } + break; } case CCLIENTOBJECT: @@ -1500,7 +1524,19 @@ bool CStaticFunctionDefinitions::SetElementModel(CClientEntity& Entity, unsigned CLuaArguments Arguments; Arguments.PushNumber(usCurrentModel); Arguments.PushNumber(usModel); - Object.CallEvent("onClientElementModelChange", Arguments, true); + bool bContinue = Object.CallEvent("onClientElementModelChange", Arguments, true); + + // Check for another call to setElementModel + if (usModel != Object.GetModel()) + return false; + + if (!bContinue) + { + // Change canceled + Object.SetModel(usCurrentModel); + return false; + } + break; } case CCLIENTPROJECTILE: