Skip to content

Commit

Permalink
refactor(server): code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Doxoh committed Jan 9, 2024
1 parent 1279cae commit 7b47b8d
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 185 deletions.
117 changes: 117 additions & 0 deletions api/AltV.Net.Async/Elements/Entities/AsyncPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using AltV.Net.Data;
using AltV.Net.Elements.Args;
using AltV.Net.Elements.Entities;
using AltV.Net.Enums;
using AltV.Net.Native;
using AltV.Net.Shared.Elements.Entities;
using AltV.Net.Shared.Utils;
Expand Down Expand Up @@ -615,6 +617,15 @@ public void SetWeather(uint weather)
}
}

public void SetWeather(WeatherType weatherType)
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player)) return;
Player.SetWeather(weatherType);
}
}

public void GiveWeapon(uint weapon, int ammo, bool selectWeapon)
{
lock (Player)
Expand All @@ -624,6 +635,15 @@ public void GiveWeapon(uint weapon, int ammo, bool selectWeapon)
}
}

public void GiveWeapon(WeaponModel weaponModel, int ammo, bool selectWeapon)
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player)) return;
Player.GiveWeapon(weaponModel, ammo, selectWeapon);
}
}

public bool RemoveWeapon(uint weapon)
{
lock (Player)
Expand All @@ -633,6 +653,15 @@ public bool RemoveWeapon(uint weapon)
}
}

public bool RemoveWeapon(WeaponModel weaponModel)
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player)) return false;
return Player.RemoveWeapon(weaponModel);
}
}

public void RemoveAllWeapons(bool removeAllAmmo)
{
lock (Player)
Expand All @@ -651,6 +680,15 @@ public bool HasWeapon(uint weapon)
}
}

public bool HasWeapon(WeaponModel weapon)
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player)) return default;
return Player.HasWeapon(weapon);
}
}

public void Kick(string reason)
{
lock (Player)
Expand Down Expand Up @@ -760,6 +798,15 @@ public void AddWeaponComponent(uint weapon, uint weaponComponent)
}
}

public void AddWeaponComponent(WeaponModel weaponModel, uint weaponComponent)
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player)) return;
Player.AddWeaponComponent(weaponModel, weaponComponent);
}
}

public void RemoveWeaponComponent(uint weapon, uint weaponComponent)
{
lock (Player)
Expand All @@ -769,6 +816,15 @@ public void RemoveWeaponComponent(uint weapon, uint weaponComponent)
}
}

public void RemoveWeaponComponent(WeaponModel weaponModel, uint weaponComponent)
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player)) return;
Player.RemoveWeaponComponent(weaponModel, weaponComponent);
}
}

public bool HasWeaponComponent(uint weapon, uint weaponComponent)
{
lock (Player)
Expand All @@ -778,6 +834,15 @@ public bool HasWeaponComponent(uint weapon, uint weaponComponent)
}
}

public bool HasWeaponComponent(WeaponModel weapon, uint weaponComponent)
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player)) return false;
return Player.HasWeaponComponent(weapon, weaponComponent);
}
}

public void GetCurrentWeaponComponents(out uint[] weaponComponents)
{
lock (Player)
Expand Down Expand Up @@ -813,6 +878,15 @@ public void SetWeaponTintIndex(uint weapon, byte tintIndex)
}
}

public void SetWeaponTintIndex(WeaponModel weaponModel, byte tintIndex)
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player)) return;
Player.SetWeaponTintIndex(weaponModel, tintIndex);
}
}

public byte GetWeaponTintIndex(uint weapon)
{
lock (Player)
Expand All @@ -822,6 +896,15 @@ public byte GetWeaponTintIndex(uint weapon)
}
}

public byte GetWeaponTintIndex(WeaponModel weapon)
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player)) return default;
return Player.GetWeaponTintIndex(weapon);
}
}

public byte GetCurrentWeaponTintIndex()
{
lock (Player)
Expand Down Expand Up @@ -1275,6 +1358,31 @@ public void GetLocalMetaData(string key, out MValueConst value)
}
}

public bool GetLocalMetaData<T>(string key, out T result)
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player))
{
result = default;
return false;
}
return Player.GetLocalMetaData(key, out result);
}
}

public void SetLocalMetaData(string key, object value)
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player))
{
return;
}
Player.SetLocalMetaData(key, value);
}
}

public void SetLocalMetaData(string key, in MValueConst value)
{
lock (Player)
Expand Down Expand Up @@ -1571,6 +1679,15 @@ public string BloodDamage
}
}

public Vector3 GetForwardVector()
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsOrCachedNullable(Player)) return default;
return Player.GetForwardVector();
}
}

[Obsolete("Use new async API instead")]
public IPlayer ToAsync(IAsyncContext asyncContext)
{
Expand Down
27 changes: 27 additions & 0 deletions api/AltV.Net.Async/Elements/Entities/AsyncWorldObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,32 @@ public AsyncWorldObject(IWorldObject worldObject, IAsyncContext asyncContext) :
{
WorldObject = worldObject;
}

public void SetPosition((float X, float Y, float Z) position)
{
lock (WorldObject)
{
if (!AsyncContext.CheckIfExistsOrCachedNullable(WorldObject)) return;
WorldObject.SetPosition(position);
}
}

public void SetPosition(float x, float y, float z)
{
lock (WorldObject)
{
if (!AsyncContext.CheckIfExistsOrCachedNullable(WorldObject)) return;
WorldObject.SetPosition(x, y, z);
}
}

public (float X, float Y, float Z) GetPosition()
{
lock (WorldObject)
{
if (!AsyncContext.CheckIfExistsOrCachedNullable(WorldObject)) return default;
return WorldObject.GetPosition();
}
}
}
}
2 changes: 2 additions & 0 deletions api/AltV.Net/Elements/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public void SetStreamSyncedMetaData(Dictionary<string, object> metaData)

public void SetStreamSyncedMetaData(string key, in MValueConst value)
{
CheckIfEntityExists();
unsafe
{
var stringPtr = MemoryUtils.StringToHGlobalUtf8(key);
Expand All @@ -155,6 +156,7 @@ public void SetStreamSyncedMetaData(string key, in MValueConst value)

public void GetStreamSyncedMetaData(string key, out MValueConst value)
{
CheckIfEntityExistsOrCached();
unsafe
{
var stringPtr = MemoryUtils.StringToHGlobalUtf8(key);
Expand Down
Loading

0 comments on commit 7b47b8d

Please sign in to comment.