Skip to content

Commit 7b47b8d

Browse files
committed
refactor(server): code cleanup
1 parent 1279cae commit 7b47b8d

File tree

7 files changed

+279
-185
lines changed

7 files changed

+279
-185
lines changed

api/AltV.Net.Async/Elements/Entities/AsyncPlayer.cs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
3+
using System.Numerics;
34
using System.Runtime.InteropServices;
45
using System.Threading.Tasks;
56
using AltV.Net.Data;
67
using AltV.Net.Elements.Args;
78
using AltV.Net.Elements.Entities;
9+
using AltV.Net.Enums;
810
using AltV.Net.Native;
911
using AltV.Net.Shared.Elements.Entities;
1012
using AltV.Net.Shared.Utils;
@@ -615,6 +617,15 @@ public void SetWeather(uint weather)
615617
}
616618
}
617619

620+
public void SetWeather(WeatherType weatherType)
621+
{
622+
lock (Player)
623+
{
624+
if (!AsyncContext.CheckIfExistsNullable(Player)) return;
625+
Player.SetWeather(weatherType);
626+
}
627+
}
628+
618629
public void GiveWeapon(uint weapon, int ammo, bool selectWeapon)
619630
{
620631
lock (Player)
@@ -624,6 +635,15 @@ public void GiveWeapon(uint weapon, int ammo, bool selectWeapon)
624635
}
625636
}
626637

638+
public void GiveWeapon(WeaponModel weaponModel, int ammo, bool selectWeapon)
639+
{
640+
lock (Player)
641+
{
642+
if (!AsyncContext.CheckIfExistsNullable(Player)) return;
643+
Player.GiveWeapon(weaponModel, ammo, selectWeapon);
644+
}
645+
}
646+
627647
public bool RemoveWeapon(uint weapon)
628648
{
629649
lock (Player)
@@ -633,6 +653,15 @@ public bool RemoveWeapon(uint weapon)
633653
}
634654
}
635655

656+
public bool RemoveWeapon(WeaponModel weaponModel)
657+
{
658+
lock (Player)
659+
{
660+
if (!AsyncContext.CheckIfExistsNullable(Player)) return false;
661+
return Player.RemoveWeapon(weaponModel);
662+
}
663+
}
664+
636665
public void RemoveAllWeapons(bool removeAllAmmo)
637666
{
638667
lock (Player)
@@ -651,6 +680,15 @@ public bool HasWeapon(uint weapon)
651680
}
652681
}
653682

683+
public bool HasWeapon(WeaponModel weapon)
684+
{
685+
lock (Player)
686+
{
687+
if (!AsyncContext.CheckIfExistsNullable(Player)) return default;
688+
return Player.HasWeapon(weapon);
689+
}
690+
}
691+
654692
public void Kick(string reason)
655693
{
656694
lock (Player)
@@ -760,6 +798,15 @@ public void AddWeaponComponent(uint weapon, uint weaponComponent)
760798
}
761799
}
762800

801+
public void AddWeaponComponent(WeaponModel weaponModel, uint weaponComponent)
802+
{
803+
lock (Player)
804+
{
805+
if (!AsyncContext.CheckIfExistsNullable(Player)) return;
806+
Player.AddWeaponComponent(weaponModel, weaponComponent);
807+
}
808+
}
809+
763810
public void RemoveWeaponComponent(uint weapon, uint weaponComponent)
764811
{
765812
lock (Player)
@@ -769,6 +816,15 @@ public void RemoveWeaponComponent(uint weapon, uint weaponComponent)
769816
}
770817
}
771818

819+
public void RemoveWeaponComponent(WeaponModel weaponModel, uint weaponComponent)
820+
{
821+
lock (Player)
822+
{
823+
if (!AsyncContext.CheckIfExistsNullable(Player)) return;
824+
Player.RemoveWeaponComponent(weaponModel, weaponComponent);
825+
}
826+
}
827+
772828
public bool HasWeaponComponent(uint weapon, uint weaponComponent)
773829
{
774830
lock (Player)
@@ -778,6 +834,15 @@ public bool HasWeaponComponent(uint weapon, uint weaponComponent)
778834
}
779835
}
780836

837+
public bool HasWeaponComponent(WeaponModel weapon, uint weaponComponent)
838+
{
839+
lock (Player)
840+
{
841+
if (!AsyncContext.CheckIfExistsNullable(Player)) return false;
842+
return Player.HasWeaponComponent(weapon, weaponComponent);
843+
}
844+
}
845+
781846
public void GetCurrentWeaponComponents(out uint[] weaponComponents)
782847
{
783848
lock (Player)
@@ -813,6 +878,15 @@ public void SetWeaponTintIndex(uint weapon, byte tintIndex)
813878
}
814879
}
815880

881+
public void SetWeaponTintIndex(WeaponModel weaponModel, byte tintIndex)
882+
{
883+
lock (Player)
884+
{
885+
if (!AsyncContext.CheckIfExistsNullable(Player)) return;
886+
Player.SetWeaponTintIndex(weaponModel, tintIndex);
887+
}
888+
}
889+
816890
public byte GetWeaponTintIndex(uint weapon)
817891
{
818892
lock (Player)
@@ -822,6 +896,15 @@ public byte GetWeaponTintIndex(uint weapon)
822896
}
823897
}
824898

899+
public byte GetWeaponTintIndex(WeaponModel weapon)
900+
{
901+
lock (Player)
902+
{
903+
if (!AsyncContext.CheckIfExistsNullable(Player)) return default;
904+
return Player.GetWeaponTintIndex(weapon);
905+
}
906+
}
907+
825908
public byte GetCurrentWeaponTintIndex()
826909
{
827910
lock (Player)
@@ -1275,6 +1358,31 @@ public void GetLocalMetaData(string key, out MValueConst value)
12751358
}
12761359
}
12771360

1361+
public bool GetLocalMetaData<T>(string key, out T result)
1362+
{
1363+
lock (Player)
1364+
{
1365+
if (!AsyncContext.CheckIfExistsNullable(Player))
1366+
{
1367+
result = default;
1368+
return false;
1369+
}
1370+
return Player.GetLocalMetaData(key, out result);
1371+
}
1372+
}
1373+
1374+
public void SetLocalMetaData(string key, object value)
1375+
{
1376+
lock (Player)
1377+
{
1378+
if (!AsyncContext.CheckIfExistsNullable(Player))
1379+
{
1380+
return;
1381+
}
1382+
Player.SetLocalMetaData(key, value);
1383+
}
1384+
}
1385+
12781386
public void SetLocalMetaData(string key, in MValueConst value)
12791387
{
12801388
lock (Player)
@@ -1571,6 +1679,15 @@ public string BloodDamage
15711679
}
15721680
}
15731681

1682+
public Vector3 GetForwardVector()
1683+
{
1684+
lock (Player)
1685+
{
1686+
if (!AsyncContext.CheckIfExistsOrCachedNullable(Player)) return default;
1687+
return Player.GetForwardVector();
1688+
}
1689+
}
1690+
15741691
[Obsolete("Use new async API instead")]
15751692
public IPlayer ToAsync(IAsyncContext asyncContext)
15761693
{

api/AltV.Net.Async/Elements/Entities/AsyncWorldObject.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,32 @@ public AsyncWorldObject(IWorldObject worldObject, IAsyncContext asyncContext) :
5353
{
5454
WorldObject = worldObject;
5555
}
56+
57+
public void SetPosition((float X, float Y, float Z) position)
58+
{
59+
lock (WorldObject)
60+
{
61+
if (!AsyncContext.CheckIfExistsOrCachedNullable(WorldObject)) return;
62+
WorldObject.SetPosition(position);
63+
}
64+
}
65+
66+
public void SetPosition(float x, float y, float z)
67+
{
68+
lock (WorldObject)
69+
{
70+
if (!AsyncContext.CheckIfExistsOrCachedNullable(WorldObject)) return;
71+
WorldObject.SetPosition(x, y, z);
72+
}
73+
}
74+
75+
public (float X, float Y, float Z) GetPosition()
76+
{
77+
lock (WorldObject)
78+
{
79+
if (!AsyncContext.CheckIfExistsOrCachedNullable(WorldObject)) return default;
80+
return WorldObject.GetPosition();
81+
}
82+
}
5683
}
5784
}

api/AltV.Net/Elements/Entities/Entity.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public void SetStreamSyncedMetaData(Dictionary<string, object> metaData)
145145

146146
public void SetStreamSyncedMetaData(string key, in MValueConst value)
147147
{
148+
CheckIfEntityExists();
148149
unsafe
149150
{
150151
var stringPtr = MemoryUtils.StringToHGlobalUtf8(key);
@@ -155,6 +156,7 @@ public void SetStreamSyncedMetaData(string key, in MValueConst value)
155156

156157
public void GetStreamSyncedMetaData(string key, out MValueConst value)
157158
{
159+
CheckIfEntityExistsOrCached();
158160
unsafe
159161
{
160162
var stringPtr = MemoryUtils.StringToHGlobalUtf8(key);

0 commit comments

Comments
 (0)