Skip to content

Commit dad547a

Browse files
committed
Fixed #29
1 parent b4298d2 commit dad547a

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

TShop/Commands/CommandBuyVehicle.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Tavstal.TShop.Components;
1414
using Tavstal.TShop.Models;
1515
using Tavstal.TShop.Utils.Helpers;
16+
using UnityEngine;
1617

1718
namespace Tavstal.TShop.Commands
1819
{
@@ -95,10 +96,7 @@ protected override async Task<bool> ExecutionRequested(IRocketPlayer caller, str
9596
await TShop.EconomyProvider.WithdrawAsync(callerPlayer.CSteamID, cost);
9697
await MainThreadDispatcher.RunOnMainThreadAsync(() =>
9798
{
98-
InteractableVehicle vehicle = VehicleManager.spawnLockedVehicleForPlayerV2(id,
99-
callerPlayer.Position + UnturnedHelper.GetVehicleSpawnModifier(), callerPlayer.Player.transform.rotation,
100-
callerPlayer.Player);
101-
99+
InteractableVehicle vehicle = UnturnedHelper.SpawnOwnedVehicle(id, callerPlayer);
102100
if (!item.VehicleColor.IsNullOrEmpty())
103101
vehicle.ServerSetPaintColor(item.GetVehicleColor());
104102
});

TShop/Utils/Handlers/UnturnedEventHandler.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,7 @@ private static void Event_OnButtonClick(Player player, string button)
382382
await TShop.EconomyProvider.WithdrawAsync(uPlayer.CSteamID, cost);
383383
await MainThreadDispatcher.RunOnMainThreadAsync(() =>
384384
{
385-
InteractableVehicle vehicle = VehicleManager.spawnLockedVehicleForPlayerV2(prod.Key.UnturnedId,
386-
uPlayer.Position + UnturnedHelper.GetVehicleSpawnModifier(),
387-
uPlayer.Player.transform.rotation, uPlayer.Player);
385+
InteractableVehicle vehicle = UnturnedHelper.SpawnOwnedVehicle(prod.Key.UnturnedId, uPlayer);;
388386

389387
if (!prod.Key.VehicleColor.IsNullOrEmpty())
390388
vehicle.ServerSetPaintColor(prod.Key.GetVehicleColor());

TShop/Utils/Helpers/UnturnedHelper.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Newtonsoft.Json.Linq;
33
using System;
44
using System.Linq;
5+
using Rocket.Unturned.Player;
6+
using SDG.Unturned;
57
using Tavstal.TShop.Models;
68
using UnityEngine;
79

@@ -99,7 +101,7 @@ public static string GetVehiclesInJson()
99101
/// <exception cref="Exception">
100102
/// Logs an error if there is an issue retrieving the vehicle spawn modifier.
101103
/// </exception>
102-
public static Vector3 GetVehicleSpawnModifier()
104+
private static Vector3 GetVehicleSpawnModifier()
103105
{
104106
try
105107
{
@@ -110,7 +112,23 @@ public static Vector3 GetVehicleSpawnModifier()
110112
TShop.Logger.LogError("Failed to get vehicle spawn modifier. Using default value...");
111113
}
112114

113-
return new Vector3(0, 5, 0);
115+
return new Vector3(0, 5, 5);
116+
}
117+
118+
/// <summary>
119+
/// Spawns a vehicle owned by the specified player at the player's current position,
120+
/// rotated to match the direction the player is looking.
121+
/// </summary>
122+
/// <param name="id">The ID of the vehicle to spawn.</param>
123+
/// <param name="owner">The player who will own the spawned vehicle.</param>
124+
/// <returns>
125+
/// The spawned <see cref="InteractableVehicle"/> instance.
126+
/// </returns>
127+
public static InteractableVehicle SpawnOwnedVehicle(ushort id, UnturnedPlayer owner)
128+
{
129+
Quaternion playerRotation = Quaternion.LookRotation(owner.Player.look.aim.forward);
130+
Vector3 spawnPosition = owner.Position + (playerRotation * GetVehicleSpawnModifier());
131+
return VehicleManager.spawnLockedVehicleForPlayerV2(id, spawnPosition, owner.Player.transform.rotation, owner.Player);
114132
}
115133
}
116134
}

0 commit comments

Comments
 (0)