Skip to content

Commit

Permalink
Merge pull request #3 from carmineos/development
Browse files Browse the repository at this point in the history
Fixed sync issue on vehicle changed
  • Loading branch information
carmineos authored May 31, 2020
2 parents ebf5fca + a085366 commit 07dc652
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
27 changes: 19 additions & 8 deletions LightTrail/TrailScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace LightTrail
{
class TrailScript : BaseScript
{
public const string DecorName = "_trail_mode";
public const string DecorName = "light_trail_mode";

TrailVehicle localVehicle;
Dictionary<int, TrailVehicle> remoteVehicles = new Dictionary<int, TrailVehicle>();
Expand All @@ -31,32 +31,43 @@ public TrailScript()
{
if (args.Count < 1)
{
Debug.WriteLine($"LightTrail: Missing argument off|on|brake");
Debug.WriteLine($"Missing argument off|on|brake");
return;
}

if (!Enum.TryParse<TrailMode>(args[0], true, out TrailMode trailMode))
{
Debug.WriteLine($"LightTrail: Error parsing {args[0]}");
Debug.WriteLine($"Invalid parameter {args[0]}, accepted values are: [off|on|brake]");
return;
}

if(localVehicle == null)
{
Debug.WriteLine($"No local vehicle found");
return;
}

Debug.WriteLine($"LightTrail: Switched trail mode to {trailMode}");
await localVehicle.SetTrailModeAsync(trailMode);
DecorSetInt(localVehicle.PlayerVehicle, DecorName, (int)trailMode);

Debug.WriteLine($"Switched light trail mode to {trailMode}");

if(DoesEntityExist(localVehicle.PlayerVehicle))
DecorSetInt(localVehicle.PlayerVehicle, DecorName, (int)trailMode);

}), false);
RegisterCommand("trail_print", new Action<int, dynamic>(async (source, args) =>

RegisterCommand("trail_print", new Action<int, dynamic>((source, args) =>
{
if(localVehicle != null)
Debug.WriteLine(localVehicle.ToString());

foreach (var vehicle in remoteVehicles)
{
Debug.WriteLine(vehicle.ToString());
Debug.WriteLine(vehicle.Value.ToString());
}

}), false);

Tick += Update;
}

Expand All @@ -70,7 +81,7 @@ void UpdateRemotePlayers()

foreach (var player in remotePlayers)
{
if (player == GetPlayerIndex())
if (player == PlayerId())
continue;

if(!remoteVehicles.ContainsKey(player))
Expand Down
18 changes: 9 additions & 9 deletions LightTrail/TrailVehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ public int PlayerVehicle
if (m_playerVehicle == value)
return;

if(DoesEntityExist(m_playerVehicle))
if (DoesEntityExist(m_playerVehicle))
DecorRemove(m_playerVehicle, TrailScript.DecorName);

m_playerVehicle = value;

if (DoesEntityExist(m_playerVehicle))
DecorSetInt(m_playerVehicle, TrailScript.DecorName, (int)m_trailMode);
}
}

Expand Down Expand Up @@ -218,29 +215,32 @@ public async Task GetPlayerVehicle()

if (!IsPedInAnyVehicle(playerPed, false))
{
await SetTrailModeAsync(TrailMode.Off);
PlayerVehicle = -1;
await StopAll();
}

int vehicle = GetVehiclePedIsIn(playerPed, false);

if (GetPedInVehicleSeat(vehicle, -1) != playerPed || IsEntityDead(vehicle))
{
await SetTrailModeAsync(TrailMode.Off);
PlayerVehicle = -1;
await StopAll();
}

if (vehicle != m_playerVehicle)
{
await StopAll();
await SetTrailModeAsync(TrailMode.Off);
PlayerVehicle = vehicle;
await SetupTrailMode(m_trailMode);
}
}

public override string ToString()
{
return $"PlayerIndex: {PlayerIndex}, Vehicle: {m_playerVehicle}, TrailMode: {m_trailMode}";
string decor = string.Empty;
if (DoesEntityExist(m_playerVehicle) && DecorExistOn(m_playerVehicle, TrailScript.DecorName))
decor = $"{(TrailMode)DecorGetInt(m_playerVehicle, TrailScript.DecorName)}";

return $"PlayerIndex: {PlayerIndex}, Vehicle: {m_playerVehicle}, TrailMode: {m_trailMode}, Decor: {decor}";
}
}
}

0 comments on commit 07dc652

Please sign in to comment.