Skip to content

Commit

Permalink
feat(server): add BaseObjectCreate/BaseObjectRemove event
Browse files Browse the repository at this point in the history
  • Loading branch information
Doxoh committed Mar 31, 2024
1 parent fbd8c07 commit 8457a8d
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 5 deletions.
26 changes: 26 additions & 0 deletions api/AltV.Net.Async/AltAsync.RegisterEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ public static void RegisterEvents(object target)
ScriptFunction scriptFunction;
switch (scriptEventType)
{
case ScriptEventType.BaseObjectCreate:
{
scriptFunction = ScriptFunction.Create(eventMethodDelegate,
new[] { typeof(IBaseObject) }, isAsync: true);
if (scriptFunction == null) return;
OnBaseObjectCreate += (baseObject) =>
{
var currScriptFunction = scriptFunction.Clone();
currScriptFunction.Set(baseObject);
return currScriptFunction.CallAsync();
};
break;
}
case ScriptEventType.BaseObjectRemove:
{
scriptFunction = ScriptFunction.Create(eventMethodDelegate,
new[] { typeof(IBaseObject) }, isAsync: true);
if (scriptFunction == null) return;
OnBaseObjectRemove += (baseObject) =>
{
var currScriptFunction = scriptFunction.Clone();
currScriptFunction.Set(baseObject);
return currScriptFunction.CallAsync();
};
break;
}
case ScriptEventType.Checkpoint:
{
scriptFunction = ScriptFunction.Create(eventMethodDelegate,
Expand Down
12 changes: 12 additions & 0 deletions api/AltV.Net.Async/AltAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public static event CheckpointAsyncDelegate OnCheckpoint
remove => Core.CheckpointAsyncEventHandler.Remove(value);
}

public static event BaseObjectCreateAsyncDelegate OnBaseObjectCreate
{
add => Core.BaseObjectCreateAsyncEventHandler.Add(value);
remove => Core.BaseObjectCreateAsyncEventHandler.Remove(value);
}

public static event BaseObjectRemoveAsyncDelegate OnBaseObjectRemove
{
add => Core.BaseObjectRemoveAsyncEventHandler.Add(value);
remove => Core.BaseObjectRemoveAsyncEventHandler.Remove(value);
}

public static event PlayerConnectAsyncDelegate OnPlayerConnect
{
add => Core.PlayerConnectAsyncEventHandler.Add(value);
Expand Down
27 changes: 27 additions & 0 deletions api/AltV.Net.Async/AsyncCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ internal readonly AsyncEventHandler<PlayerChangeVehicleSeatAsyncDelegate>
internal readonly AsyncEventHandler<PlayerDisconnectAsyncDelegate> PlayerDisconnectAsyncEventHandler =
new(EventType.PLAYER_DISCONNECT);

internal readonly AsyncEventHandler<BaseObjectCreateAsyncDelegate> BaseObjectCreateAsyncEventHandler =
new();

internal readonly AsyncEventHandler<BaseObjectRemoveAsyncDelegate> BaseObjectRemoveAsyncEventHandler =
new();

internal readonly AsyncEventHandler<PlayerRemoveAsyncDelegate> PlayerRemoveAsyncEventHandler =
new();

Expand Down Expand Up @@ -357,6 +363,27 @@ await PlayerDisconnectAsyncEventHandler.CallAsync(@delegate =>
);
}

public override void OnCreateBaseObjectEvent(IBaseObject baseObject)
{
base.OnCreateBaseObjectEvent(baseObject);
if (!BaseObjectCreateAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await BaseObjectCreateAsyncEventHandler.CallAsync(@delegate =>
@delegate(baseObject));
});
}
public override void OnRemoveBaseObjectEvent(IBaseObject baseObject)
{
base.OnRemoveBaseObjectEvent(baseObject);
if (!BaseObjectRemoveAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await BaseObjectRemoveAsyncEventHandler.CallAsync(@delegate =>
@delegate(baseObject));
});
}

public override void OnPlayerRemoveEvent(IPlayer player)
{
base.OnPlayerRemoveEvent(player);
Expand Down
2 changes: 2 additions & 0 deletions api/AltV.Net.Async/Events/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public delegate Task PlayerHealAsyncDelegate(IPlayer target, ushort oldHealth, u

public delegate Task PlayerDisconnectAsyncDelegate(IPlayer player, string reason);

public delegate Task BaseObjectCreateAsyncDelegate(IBaseObject baseObject);
public delegate Task BaseObjectRemoveAsyncDelegate(IBaseObject baseObject);
public delegate Task PlayerRemoveAsyncDelegate(IPlayer player);

public delegate Task VehicleRemoveAsyncDelegate(IVehicle vehicle);
Expand Down
12 changes: 12 additions & 0 deletions api/AltV.Net/Alt.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public static event PlayerConnectDelegate OnPlayerConnect
remove => CoreImpl.PlayerConnectEventHandler.Remove(value);
}

public static event BaseObjectCreateDelegate OnBaseObjectCreate
{
add => CoreImpl.BaseObjectCreateEventHandler.Add(value);
remove => CoreImpl.BaseObjectCreateEventHandler.Remove(value);
}

public static event BaseObjectRemoveDelegate OnBaseObjectRemove
{
add => CoreImpl.BaseObjectRemoveEventHandler.Add(value);
remove => CoreImpl.BaseObjectRemoveEventHandler.Remove(value);
}

public static event PlayerConnectDeniedDelegate OnPlayerConnectDenied
{
add => CoreImpl.PlayerConnectDeniedEventHandler.Add(value);
Expand Down
24 changes: 24 additions & 0 deletions api/AltV.Net/Alt.RegisterEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ public static void RegisterEvents(object target)
ScriptFunction scriptFunction;
switch (scriptEventType)
{
case ScriptEventType.BaseObjectCreate:
{
scriptFunction = ScriptFunction.Create(eventMethodDelegate,
new[] { typeof(IBaseObject) });
if (scriptFunction == null) return;
OnBaseObjectCreate += (baseObject) =>
{
scriptFunction.Set(baseObject);
scriptFunction.Call();
};
break;
}
case ScriptEventType.BaseObjectRemove:
{
scriptFunction = ScriptFunction.Create(eventMethodDelegate,
new[] { typeof(IBaseObject) });
if (scriptFunction == null) return;
OnBaseObjectRemove += (baseObject) =>
{
scriptFunction.Set(baseObject);
scriptFunction.Call();
};
break;
}
case ScriptEventType.Checkpoint:
{
scriptFunction = ScriptFunction.Create(eventMethodDelegate,
Expand Down
55 changes: 51 additions & 4 deletions api/AltV.Net/Core.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public partial class Core
internal readonly IEventHandler<PlayerDisconnectDelegate> PlayerDisconnectEventHandler =
new HashSetEventHandler<PlayerDisconnectDelegate>(EventType.PLAYER_DISCONNECT);

internal readonly IEventHandler<BaseObjectCreateDelegate> BaseObjectCreateEventHandler =
new HashSetEventHandler<BaseObjectCreateDelegate>();

internal readonly IEventHandler<BaseObjectRemoveDelegate> BaseObjectRemoveEventHandler =
new HashSetEventHandler<BaseObjectRemoveDelegate>();

internal readonly IEventHandler<PlayerRemoveDelegate> PlayerRemoveEventHandler =
new HashSetEventHandler<PlayerRemoveDelegate>();

Expand Down Expand Up @@ -2057,14 +2063,55 @@ public virtual void OnModuleLoaded(IModule module)
{
}

public void OnCreateBaseObject(IntPtr baseObject, BaseObjectType type, uint id)
public void OnCreateBaseObject(IntPtr baseObjectPtr, BaseObjectType type, uint id)
{
var baseObject = PoolManager.GetOrCreate(this, baseObjectPtr, type, id);
OnCreateBaseObjectEvent(baseObject);
}

public virtual void OnCreateBaseObjectEvent(IBaseObject baseObject)
{
foreach (var @delegate in BaseObjectCreateEventHandler.GetEvents())
{
try
{
@delegate(baseObject);
}
catch (TargetInvocationException exception)
{
Alt.Log("exception at event:" + "OnCreateBaseObjectEvent" + ":" + exception.InnerException);
}
catch (Exception exception)
{
Alt.Log("exception at event:" + "OnCreateBaseObjectEvent" + ":" + exception);
}
}
}

public void OnRemoveBaseObject(IntPtr baseObjectPtr, BaseObjectType type)
{
PoolManager.GetOrCreate(this, baseObject, type, id);
var baseObject = PoolManager.Get(baseObjectPtr, type);
OnRemoveBaseObjectEvent(baseObject);
PoolManager.Remove(baseObjectPtr, type);
}

public void OnRemoveBaseObject(IntPtr baseObject, BaseObjectType type)
public virtual void OnRemoveBaseObjectEvent(IBaseObject baseObject)
{
PoolManager.Remove(baseObject, type);
foreach (var @delegate in BaseObjectRemoveEventHandler.GetEvents())
{
try
{
@delegate(baseObject);
}
catch (TargetInvocationException exception)
{
Alt.Log("exception at event:" + "OnRemoveBaseObjectEvent" + ":" + exception.InnerException);
}
catch (Exception exception)
{
Alt.Log("exception at event:" + "OnRemoveBaseObjectEvent" + ":" + exception);
}
}
}

public void OnPlayerRemove(IntPtr playerPointer)
Expand Down
3 changes: 3 additions & 0 deletions api/AltV.Net/Events/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public delegate void PlayerHealDelegate(IPlayer target, ushort oldHealth, ushort

public delegate void PlayerDisconnectDelegate(IPlayer player, string reason);

public delegate void BaseObjectCreateDelegate(IBaseObject baseObject);
public delegate void BaseObjectRemoveDelegate(IBaseObject baseObject);

public delegate void PlayerRemoveDelegate(IPlayer player);

public delegate void VehicleRemoveDelegate(IVehicle vehicle);
Expand Down
4 changes: 3 additions & 1 deletion api/AltV.Net/ScriptEventType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public enum ScriptEventType
VehicleHorn,
VehicleSiren,
PlayerSpawn,
RequestSyncedScene
RequestSyncedScene,
BaseObjectCreate,
BaseObjectRemove,
}
}

0 comments on commit 8457a8d

Please sign in to comment.