Skip to content

Commit

Permalink
Add OnNpcVehicleDataUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
soccermitchy committed Dec 20, 2016
1 parent ccef5a1 commit bb19a9c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions gtaserver.core/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ private void HandleClientIncomingData(Client client, NetIncomingMessage msg)
var pedPosData = Util.DeserializeBinary<PedData>(msg.ReadBytes(len));
if (pedPosData != null)
{
var pedPluginResult = GameEvents.PedDataUpdate(client, pedPosData);
if (!pedPluginResult.ContinueServerProc) return;
pedPosData = pedPluginResult.Data;

pedPosData.Id = client.NetConnection.RemoteUniqueIdentifier;
pedPosData.Name = client.DisplayName;
pedPosData.Latency = client.Latency;
Expand All @@ -457,8 +461,13 @@ private void HandleClientIncomingData(Client client, NetIncomingMessage msg)
{
var len = msg.ReadInt32();
var vehData = Util.DeserializeBinary<VehicleData>(msg.ReadBytes(len));

if (vehData != null)
{
var pluginVehData = GameEvents.NpcVehicleDataUpdate(client, vehData);
if (!pluginVehData.ContinueServerProc) return;
vehData = pluginVehData.Data;

vehData.Id = client.NetConnection.RemoteUniqueIdentifier;
SendToAll(vehData, PacketType.NpcVehPositionData, false, client);
}
Expand Down
28 changes: 28 additions & 0 deletions gtaserver.core/PluginAPI/Events/GameEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,33 @@ public static PluginResponse<PedData> PedDataUpdate(Client c, PedData p)
}
return result;
}

/// <summary>
/// Called on every NPC vehicle update/creation
/// </summary>
public static List<Func<Client, VehicleData, PluginResponse<VehicleData>>> OnNpcVehicleDataUpdate
= new List<Func<Client, VehicleData, PluginResponse<VehicleData>>>();
/// <summary>
/// Internal method. Triggers OnNpcVehicleDataUpdate
/// </summary>
/// <param name="c">Client who sent the update</param>
/// <param name="v">PedData object</param>
/// <returns>A PluginResponse, with the ability to rewrite the received data.</returns>
public static PluginResponse<VehicleData> NpcVehicleDataUpdate(Client c, VehicleData v)
{
var result = new PluginResponse<VehicleData>()
{
ContinuePluginProc = true,
ContinueServerProc = true,
Data = v
};
foreach (var f in OnNpcVehicleDataUpdate)
{
result = f(c, v);
if (!result.ContinuePluginProc) return result;
v = result.Data;
}
return result;
}
}
}

0 comments on commit bb19a9c

Please sign in to comment.