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

Add InspectingWeaponEvent #2063

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Exiled.Events/EventArgs/Player/InspectingWeaponEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// -----------------------------------------------------------------------
// <copyright file="InspectingWeaponEventArgs.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.EventArgs.Player
{
using API.Features;

using Exiled.API.Features.Items;

using Interfaces;

using InventorySystem.Items.Firearms.BasicMessages;

/// <summary>
/// Contains all information before a player inspects a weapon.
/// </summary>
public class InspectingWeaponEventArgs : IPlayerEvent, IDeniableEvent, IItemEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="InspectingWeaponEventArgs" /> class.
/// </summary>
/// <param name="inspector">
/// <inheritdoc cref="Player" />
/// </param>
/// <param name="firearm">
/// <inheritdoc cref="Firearm" />
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public InspectingWeaponEventArgs(Player inspector, Firearm firearm, bool isAllowed = true)
{
Player = inspector;
Firearm = firearm;
IsAllowed = isAllowed;
}

/// <summary>
/// Gets the player who's inspecting.
/// </summary>
public Player Player { get; }

/// <summary>
/// Gets the target <see cref="API.Features.Items.Firearm" /> to be inspected.
/// </summary>
public Firearm Firearm { get; }

/// <inheritdoc/>
public Item Item => Firearm;

/// <summary>
/// Gets or sets a value indicating whether or not spectators will see the inspect animation.
/// </summary>
public bool IsAllowed { get; set; } = true;
}
}
6 changes: 6 additions & 0 deletions Exiled.Events/Handlers/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ public class Player
/// </summary>
public static Event<ReloadingWeaponEventArgs> ReloadingWeapon { get; set; } = new();

/// <summary>
/// Invoked before a <see cref="API.Features.Player"/> inspects a weapon.
/// </summary>
/// <remarks>Denying this event will only make it so that spectators will not see the inspection, not the player.</remarks>
public static Event<InspectingWeaponEventArgs> InspectingWeapon { get; set; } = new();

/// <summary>
/// Invoked after a <see cref="API.Features.Player"/> reloads a weapon.
/// </summary>
Expand Down
43 changes: 38 additions & 5 deletions Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
/// <summary>
/// Patches <see cref="FirearmBasicMessagesHandler.ServerRequestReceived" />.
/// Adds <see cref="Player.ReloadingWeapon" />, <see cref="Player.ReloadedWeapon" />,
/// <see cref="Player.UnloadingWeapon" />, <see cref="Player.UnloadedWeapon" /> and
/// <see cref="Player.DryfiringWeapon" />, <see cref="Player.AimingDownSight" /> and
/// <see cref="Player.TogglingWeaponFlashlight" />, <see cref="Player.ToggledWeaponFlashlight" /> events.
/// <see cref="Player.UnloadingWeapon" />, <see cref="Player.UnloadedWeapon" />,
/// <see cref="Player.DryfiringWeapon" />, <see cref="Player.AimingDownSight" />
/// <see cref="Player.InspectingWeapon" />,
/// <see cref="Player.TogglingWeaponFlashlight" /> and <see cref="Player.ToggledWeaponFlashlight" /> events.
/// </summary>
[EventPatch(typeof(Player), nameof(Player.ReloadingWeapon))]
[EventPatch(typeof(Player), nameof(Player.ReloadedWeapon))]
Expand All @@ -42,6 +43,7 @@
[EventPatch(typeof(Player), nameof(Player.DryfiringWeapon))]
[EventPatch(typeof(Player), nameof(Player.AimingDownSight))]
[EventPatch(typeof(Player), nameof(Player.TogglingWeaponFlashlight))]
[EventPatch(typeof(Player), nameof(Player.InspectingWeapon))]
[EventPatch(typeof(Player), nameof(Player.ToggledWeaponFlashlight))]
[HarmonyPatch(typeof(FirearmBasicMessagesHandler), nameof(FirearmBasicMessagesHandler.ServerRequestReceived))]
internal static class FirearmRequestReceived
Expand Down Expand Up @@ -321,12 +323,43 @@
new(OpCodes.Call, Method(typeof(Player), nameof(Player.OnToggledWeaponFlashlight))),
});

newInstructions[newInstructions.Count - 1].WithLabels(returnLabel);
offset = -3;
index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Ldftn) + offset;
Comment on lines +326 to +327
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need verification with new update


newInstructions.InsertRange(
index,
new[]
{
// player
new CodeInstruction(OpCodes.Ldloc_S, player.LocalIndex),

// firearm
new(OpCodes.Ldloc_S, firearm.LocalIndex),

// true
new(OpCodes.Ldc_I4_1),

// InspectingWeaponEventArgs ev = new(Player, firearm, bool)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(InspectingWeaponEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),

Check failure on line 346 in Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs

View workflow job for this annotation

GitHub Actions / build

The name 'ev' does not exist in the current context

Check failure on line 346 in Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs

View workflow job for this annotation

GitHub Actions / build

The name 'ev' does not exist in the current context

Check failure on line 346 in Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs

View workflow job for this annotation

GitHub Actions / build

The name 'ev' does not exist in the current context

Check failure on line 346 in Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs

View workflow job for this annotation

GitHub Actions / build

The name 'ev' does not exist in the current context

Check failure on line 346 in Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs

View workflow job for this annotation

GitHub Actions / build

The name 'ev' does not exist in the current context

Check failure on line 346 in Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs

View workflow job for this annotation

GitHub Actions / build

The name 'ev' does not exist in the current context

Check failure on line 346 in Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs

View workflow job for this annotation

GitHub Actions / build

The name 'ev' does not exist in the current context

Check failure on line 346 in Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs

View workflow job for this annotation

GitHub Actions / build

The name 'ev' does not exist in the current context

// Player.OnInspectingWeapon(ev)
new(OpCodes.Call, Method(typeof(Player), nameof(Player.InspectingWeapon))),

// if (!ev.IsAllowed)
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(InspectingWeaponEventArgs), nameof(InspectingWeaponEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, returnLabel),
});

newInstructions[newInstructions.Count - 1].labels.Add(returnLabel);

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
}
}
Loading