Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: DroppingAmmo, DroppedAmmo and InteractingDoor #371

Merged
merged 8 commits into from
Jan 8, 2025
38 changes: 35 additions & 3 deletions EXILED/Exiled.CustomItems/API/Features/CustomItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,8 @@ internal bool TryUnregister()
protected virtual void SubscribeEvents()
{
Exiled.Events.Handlers.Player.Dying += OnInternalOwnerDying;
Exiled.Events.Handlers.Player.DroppingItem += OnInternalDropping;
Exiled.Events.Handlers.Player.DroppingItem += OnInternalDroppingItem;
Exiled.Events.Handlers.Player.DroppingAmmo += OnInternalDroppingAmmo;
Exiled.Events.Handlers.Player.ChangingItem += OnInternalChanging;
Exiled.Events.Handlers.Player.Escaping += OnInternalOwnerEscaping;
Exiled.Events.Handlers.Player.PickingUpItem += OnInternalPickingUp;
Expand All @@ -852,7 +853,8 @@ protected virtual void SubscribeEvents()
protected virtual void UnsubscribeEvents()
{
Exiled.Events.Handlers.Player.Dying -= OnInternalOwnerDying;
Exiled.Events.Handlers.Player.DroppingItem -= OnInternalDropping;
Exiled.Events.Handlers.Player.DroppingItem -= OnInternalDroppingItem;
Exiled.Events.Handlers.Player.DroppingAmmo -= OnInternalDroppingAmmo;
Exiled.Events.Handlers.Player.ChangingItem -= OnInternalChanging;
Exiled.Events.Handlers.Player.Escaping -= OnInternalOwnerEscaping;
Exiled.Events.Handlers.Player.PickingUpItem -= OnInternalPickingUp;
Expand Down Expand Up @@ -900,10 +902,27 @@ protected virtual void OnOwnerHandcuffing(OwnerHandcuffingEventArgs ev)
/// Handles tracking items when they are dropped by a player.
/// </summary>
/// <param name="ev"><see cref="DroppingItemEventArgs"/>.</param>
protected virtual void OnDroppingItem(DroppingItemEventArgs ev)
{
}

/// <summary>
/// Handles tracking items when they are dropped by a player.
/// </summary>
/// <param name="ev"><see cref="DroppingItemEventArgs"/>.</param>
[Obsolete("Use OnDroppingItem instead.", false)]
protected virtual void OnDropping(DroppingItemEventArgs ev)
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
{
}

/// <summary>
/// Handles tracking when player requests drop of item which <see cref="ItemType"/> equals to the <see cref="ItemType"/> specified by <see cref="CustomItem"/>.
/// </summary>
/// <param name="ev"><see cref="DroppingAmmoEventArgs"/>.</param>
protected virtual void OnDroppingAmmo(DroppingAmmoEventArgs ev)
{
}

/// <summary>
/// Handles tracking items when they are picked up by a player.
/// </summary>
Expand Down Expand Up @@ -1061,12 +1080,25 @@ private void OnInternalOwnerHandcuffing(HandcuffingEventArgs ev)
}
}

private void OnInternalDropping(DroppingItemEventArgs ev)
private void OnInternalDroppingItem(DroppingItemEventArgs ev)
{
if (!Check(ev.Item))
return;

OnDroppingItem(ev);

// TODO: Don't forget to remove this with next update
#pragma warning disable CS0618
OnDropping(ev);
#pragma warning restore CS0618
}

private void OnInternalDroppingAmmo(DroppingAmmoEventArgs ev)
{
if (Type != ev.ItemType)
return;

OnDroppingAmmo(ev);
}

private void OnInternalPickingUp(PickingUpItemEventArgs ev)
Expand Down
15 changes: 11 additions & 4 deletions EXILED/Exiled.Events/EventArgs/Player/DroppedAmmoEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Exiled.Events.EventArgs.Player

using API.Enums;
using API.Features;
using Exiled.API.Extensions;
using Exiled.API.Features.Pickups;
using Interfaces;

Expand All @@ -27,23 +28,29 @@ public class DroppedAmmoEventArgs : IPlayerEvent
/// <param name="player">
/// <inheritdoc cref="Player" />
/// </param>
/// <param name="ammoType">
/// <inheritdoc cref="AmmoType" />
/// <param name="itemType">
/// <inheritdoc cref="ItemType"/>
/// </param>
/// <param name="amount">
/// <inheritdoc cref="Amount" />
/// </param>
/// <param name="ammoPickups">
/// <inheritdoc cref="AmmoPickups" />
/// </param>
public DroppedAmmoEventArgs(Player player, AmmoType ammoType, ushort amount, List<InventorySystem.Items.Firearms.Ammo.AmmoPickup> ammoPickups)
public DroppedAmmoEventArgs(Player player, ItemType itemType, ushort amount, List<InventorySystem.Items.Firearms.Ammo.AmmoPickup> ammoPickups)
{
Player = player;
AmmoType = ammoType;
ItemType = itemType;
AmmoType = ItemExtensions.GetAmmoType(itemType);
Amount = amount;
AmmoPickups = Pickup.Get<AmmoPickup>(ammoPickups);
}

/// <summary>
/// Gets the type of dropped item instead of <see cref="API.Enums.AmmoType"/>.
/// </summary>
public ItemType ItemType { get; }

/// <summary>
/// Gets the type of dropped ammo.
/// </summary>
Expand Down
17 changes: 12 additions & 5 deletions EXILED/Exiled.Events/EventArgs/Player/DroppingAmmoEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Exiled.Events.EventArgs.Player
{
using API.Enums;
using API.Features;

using Exiled.API.Extensions;
using Interfaces;

using PlayerRoles;
Expand All @@ -27,23 +27,30 @@ public class DroppingAmmoEventArgs : IPlayerEvent, IDeniableEvent
/// <param name="player">
/// <inheritdoc cref="Player" />
/// </param>
/// <param name="ammoType">
/// <inheritdoc cref="AmmoType" />
/// <param name="itemType">
/// <inheritdoc cref="ItemType"/>
/// </param>
/// <param name="amount">
/// <inheritdoc cref="int" />
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public DroppingAmmoEventArgs(Player player, AmmoType ammoType, ushort amount, bool isAllowed = true)
public DroppingAmmoEventArgs(Player player, ItemType itemType, ushort amount, bool isAllowed = true)
{
Player = player;
AmmoType = ammoType;
ItemType = itemType;
AmmoType = ItemExtensions.GetAmmoType(itemType);
Amount = amount;
IsAllowed = isAllowed;
}

/// <summary>
/// Gets the type of item being dropped instead of <see cref="API.Enums.AmmoType"/>.
/// For example, if the plugin gives the player one of the items instead of ammo.
/// </summary>
public ItemType ItemType { get; }

/// <summary>
/// Gets the type of ammo being dropped.
/// </summary>
Expand Down
20 changes: 18 additions & 2 deletions EXILED/Exiled.Events/EventArgs/Player/InteractingDoorEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Exiled.Events.EventArgs.Player
{
using API.Features;
using Exiled.API.Features.Doors;
using Interactables;
using Interactables.Interobjects.DoorUtils;
using Interfaces;

Expand All @@ -26,16 +27,21 @@ public class InteractingDoorEventArgs : IPlayerEvent, IDoorEvent, IDeniableEvent
/// <param name="door">
/// <inheritdoc cref="Door" />
/// </param>
/// <param name="colliderId">
/// <inheritdoc cref="ColliderId"/>
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
/// <param name="canInteract">
/// <inheritdoc cref="CanInteract" />
/// </param>
public InteractingDoorEventArgs(Player player, DoorVariant door, bool isAllowed = true, bool canInteract = true)
public InteractingDoorEventArgs(Player player, DoorVariant door, byte colliderId, bool isAllowed = true, bool canInteract = true)
{
Player = player;
Door = Door.Get(door);
ColliderId = colliderId;
Collider = InteractableCollider.TryGetCollider(door, colliderId, out InteractableCollider interactableCollider) ? interactableCollider : null;
IsAllowed = isAllowed;
CanInteract = canInteract;
}
Expand All @@ -51,10 +57,20 @@ public InteractingDoorEventArgs(Player player, DoorVariant door, bool isAllowed
public bool IsAllowed { get; set; }

/// <summary>
/// Gets or sets the <see cref="API.Features.Doors.Door" /> instance.
/// Gets or sets the <see cref="API.Features.Doors.Door"/> instance.
/// </summary>
public Door Door { get; set; }

/// <summary>
/// Gets the <see cref="InteractableCollider"/> instance that the player interacted with.
/// </summary>
public InteractableCollider Collider { get; }

/// <summary>
/// Gets the ColliderId of <see cref="InteractableCollider"/> that the player interacted with.
/// </summary>
public byte ColliderId { get; }

/// <summary>
/// Gets the player who's interacting with the door.
/// </summary>
Expand Down
11 changes: 5 additions & 6 deletions EXILED/Exiled.Events/Patches/Events/Player/DroppingAmmo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,16 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Ldfld, Field(typeof(Inventory), nameof(Inventory._hub))),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// ammoType
// itemType
new(OpCodes.Ldarg_1),
new(OpCodes.Call, Method(typeof(API.Extensions.ItemExtensions), nameof(API.Extensions.ItemExtensions.GetAmmoType))),

// amount
new(OpCodes.Ldarg_2),

// true
new(OpCodes.Ldc_I4_1),

// DroppingAmmoEventArgs ev = new(Player, AmmoType, ushort, bool)
// DroppingAmmoEventArgs ev = new(Player, ItemType, ushort, bool)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(DroppingAmmoEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
Expand All @@ -84,9 +83,9 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(DroppingAmmoEventArgs), nameof(DroppingAmmoEventArgs.Player))),

// ev.AmmoType
// ev.ItemType
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(DroppingAmmoEventArgs), nameof(DroppingAmmoEventArgs.AmmoType))),
new(OpCodes.Callvirt, PropertyGetter(typeof(DroppingAmmoEventArgs), nameof(DroppingAmmoEventArgs.ItemType))),

// ev.Amount
new(OpCodes.Ldloc_S, ev.LocalIndex),
Expand All @@ -95,7 +94,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
// ammoPickups
new(OpCodes.Ldloc_S, ammoPickups.LocalIndex),

// Handlers::Player::OnDroppedAmmo(new DroppedAmmoEventArgs(ev.Player, ev.AmmoType, ev.Amount, ammoPickups))
// Handlers::Player::OnDroppedAmmo(new DroppedAmmoEventArgs(ev.Player, ev.ItemType, ev.Amount, ammoPickups))
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(DroppedAmmoEventArgs))[0]),
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnDroppedAmmo))),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
0,
new CodeInstruction[]
{
// InteractingDoorEventArgs ev = new(Player.Get(ply), __instance, false, true);
// InteractingDoorEventArgs ev = new(Player.Get(ply), __instance, colliderId, false, true);
new(OpCodes.Ldarg_1),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),
new(OpCodes.Ldarg_0),
new(OpCodes.Ldarg_2),
new(OpCodes.Ldc_I4_0),
new(OpCodes.Ldc_I4_1),
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(InteractingDoorEventArgs))[0]),
Expand Down
Loading