Skip to content

Commit

Permalink
fix: several bug fixes (#334)
Browse files Browse the repository at this point in the history
* fix: possible admin chat fix

* fix: several fixes

* IItemEvent now contain IPlayerEvent

* InteractingElevatorFix not being call inside elevator

* InteractingElevator::IsCalledFromInside

---------

Co-authored-by: Yamato <[email protected]>
  • Loading branch information
VALERA771 and louis1706 authored Dec 24, 2024
1 parent 88ce337 commit cbe1a06
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 20 deletions.
2 changes: 1 addition & 1 deletion EXILED/Exiled.Events/EventArgs/Interfaces/IItemEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Exiled.Events.EventArgs.Interfaces
/// <summary>
/// Event args used for all <see cref="API.Features.Items.Item" /> related events.
/// </summary>
public interface IItemEvent : IExiledEvent
public interface IItemEvent : IPlayerEvent
{
/// <summary>
/// Gets the <see cref="API.Features.Items.Item" /> triggering the event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Exiled.Events.EventArgs.Item
/// <summary>
/// Contains all information before a player charges a <see cref="Jailbird"/>.
/// </summary>
public class ChargingJailbirdEventArgs : IPlayerEvent, IItemEvent
public class ChargingJailbirdEventArgs : IItemEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="ChargingJailbirdEventArgs"/> class.
Expand Down
2 changes: 1 addition & 1 deletion EXILED/Exiled.Events/EventArgs/Item/SwingingEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Exiled.Events.EventArgs.Item
/// <summary>
/// Contains all information before a player swings a <see cref="Jailbird"/>.
/// </summary>
public class SwingingEventArgs : IPlayerEvent, IItemEvent, IDeniableEvent
public class SwingingEventArgs : IItemEvent, IDeniableEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="SwingingEventArgs"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Exiled.Events.EventArgs.Player
/// <summary>
/// Contains all information after a player's held item changes.
/// </summary>
public class ChangedItemEventArgs : IPlayerEvent, IItemEvent
public class ChangedItemEventArgs : IItemEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="ChangedItemEventArgs" /> class.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// <copyright file="ChangingDisruptorModeEventArgs.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
Expand All @@ -8,6 +8,7 @@
namespace Exiled.Events.EventArgs.Player
{
using Exiled.API.Enums;
using Exiled.API.Features;
using Exiled.API.Features.Items;
using Exiled.Events.EventArgs.Interfaces;

Expand Down Expand Up @@ -37,5 +38,8 @@ public ChangingDisruptorModeEventArgs(Item firearm, bool mode)
/// Gets a new disruptor's fire mode.
/// </summary>
public DisruptorMode NewMode { get; }

/// <inheritdoc />
public Player Player => Item.Owner;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ public ChangingMicroHIDStateEventArgs(Item microHID, MicroHidPhase newPhase, boo

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

/// <inheritdoc/>
public Player Player => MicroHID.Owner;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Exiled.Events.EventArgs.Player
/// <summary>
/// Contains all information before radio preset is changed.
/// </summary>
public class ChangingRadioPresetEventArgs : IPlayerEvent, IItemEvent, IDeniableEvent
public class ChangingRadioPresetEventArgs : IItemEvent, IDeniableEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="ChangingRadioPresetEventArgs" /> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Exiled.Events.EventArgs.Player
/// <summary>
/// Contains all information before a player drops an item.
/// </summary>
public class DroppingItemEventArgs : IPlayerEvent, IItemEvent, IDeniableEvent
public class DroppingItemEventArgs : IItemEvent, IDeniableEvent
{
private bool isAllowed = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ public class InteractingElevatorEventArgs : IPlayerEvent, IDeniableEvent
/// <param name="elevator">
/// <inheritdoc cref="Elevator" />
/// </param>
/// <param name="isCalledFromInside">
/// <inheritdoc cref="IsCalledFromInside" />
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public InteractingElevatorEventArgs(Player player, ElevatorChamber elevator, bool isAllowed = true)
public InteractingElevatorEventArgs(Player player, ElevatorChamber elevator, bool isCalledFromInside, bool isAllowed = true)
{
Player = player;
Lift = Lift.Get(elevator);
Elevator = elevator;
IsCalledFromInside = isCalledFromInside;
IsAllowed = isAllowed;
}

Expand All @@ -54,6 +58,11 @@ public InteractingElevatorEventArgs(Player player, ElevatorChamber elevator, boo
/// </summary>
public bool IsAllowed { get; set; }

/// <summary>
/// Gets a value indicating whether the player as interact with the elevator from the inside.
/// </summary>
public bool IsCalledFromInside { get; }

/// <summary>
/// Gets the player who's interacting with the elevator.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Exiled.Events.EventArgs.Player
/// <summary>
/// Contains all information after adding an item to a player's inventory.
/// </summary>
public class ItemAddedEventArgs : IPlayerEvent, IItemEvent, IPickupEvent
public class ItemAddedEventArgs : IItemEvent, IPickupEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="ItemAddedEventArgs"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Exiled.Events.EventArgs.Player
/// <summary>
/// Contains all information after removing an item from a player's inventory.
/// </summary>
public class ItemRemovedEventArgs : IPlayerEvent, IItemEvent, IPickupEvent
public class ItemRemovedEventArgs : IItemEvent, IPickupEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="ItemRemovedEventArgs"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Exiled.Events.EventArgs.Player
/// <summary>
/// Contains all information before receving a throwing request.
/// </summary>
public class ThrowingRequestEventArgs : IPlayerEvent, IItemEvent
public class ThrowingRequestEventArgs : IItemEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="ThrowingRequestEventArgs"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Exiled.Events.EventArgs.Player
/// <summary>
/// Contains all information after a player throws a grenade.
/// </summary>
public class ThrownProjectileEventArgs : IPlayerEvent, IItemEvent, IPickupEvent
public class ThrownProjectileEventArgs : IItemEvent, IPickupEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="ThrownProjectileEventArgs"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ public UsingMicroHIDEnergyEventArgs(MicroHIDItem microHIDitem, float newEnergy,
/// Gets or sets a value indicating whether the MicroHID energy can be changed.
/// </summary>
public bool IsAllowed { get; set; }

/// <inheritdoc/>
public Player Player => MicroHID.Owner;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Exiled.Events.EventArgs.Scp914
/// <summary>
/// Contains all information before SCP-914 upgrades an item.
/// </summary>
public class UpgradingInventoryItemEventArgs : IPlayerEvent, IItemEvent, IDeniableEvent
public class UpgradingInventoryItemEventArgs : IItemEvent, IDeniableEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="UpgradingInventoryItemEventArgs" /> class.
Expand Down
72 changes: 67 additions & 5 deletions EXILED/Exiled.Events/Patches/Events/Player/InteractingElevator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Exiled.Events.Patches.Events.Player
{
#pragma warning disable SA1402 // File may only contain a single type
using System.Collections.Generic;
using System.Reflection.Emit;

Expand All @@ -19,16 +20,15 @@ namespace Exiled.Events.Patches.Events.Player

using Interactables.Interobjects;

using Mirror;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="ElevatorManager" />.
/// Patches <see cref="ElevatorDoor.ServerInteract" />.
/// Adds the <see cref="Handlers.Player.InteractingElevator" /> event.
/// <remarks>This event only get call when interact on the outside button.</remarks>
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.InteractingElevator))]
[HarmonyPatch(typeof(ElevatorChamber), nameof(ElevatorChamber.ServerInteract))]
[HarmonyPatch(typeof(ElevatorDoor), nameof(ElevatorDoor.ServerInteract))]
internal class InteractingElevator
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
Expand All @@ -39,6 +39,65 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

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

// InteractingElevatorEventArgs ev = new(Player.Get(referenceHub), elevatorChamber, true);
//
// Handlers.Player.OnInteractingElevator(ev);
//
// if (!ev.IsAllowed)
// continue;
newInstructions.InsertRange(0, new CodeInstruction[]
{
// Player.Get(referenceHub)
new(OpCodes.Ldarg_1),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// this.Chamber
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(ElevatorDoor), nameof(ElevatorDoor.Chamber))),

// false
new(OpCodes.Ldc_I4_0),

// true
new(OpCodes.Ldc_I4_1),

// InteractingElevatorEventArgs ev = new(Player, ElevatorChamber, bool, bool)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(InteractingElevatorEventArgs))[0]),
new(OpCodes.Dup),

// Handlers.Player.OnInteractingElevator(ev)
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnInteractingElevator))),

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

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

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

/// <summary>
/// Patches <see cref="ElevatorChamber.ServerInteract" />.
/// Adds the <see cref="Handlers.Player.InteractingElevator" /> event.
/// <remarks>This event only get call when interact on the inside button.</remarks>
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.InteractingElevator))]
[HarmonyPatch(typeof(ElevatorChamber), nameof(ElevatorChamber.ServerInteract))]
internal class InteractingElevator2
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label returnLabel = generator.DefineLabel();

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

// InteractingElevatorEventArgs ev = new(Player.Get(referenceHub), elevatorChamber, true);
//
// Handlers.Player.OnInteractingElevator(ev);
Expand All @@ -57,7 +116,10 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
// true
new(OpCodes.Ldc_I4_1),

// InteractingElevatorEventArgs ev = new(Player, ElevatorChamber, bool)
// true
new(OpCodes.Ldc_I4_1),

// InteractingElevatorEventArgs ev = new(Player, ElevatorChamber, bool, bool)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(InteractingElevatorEventArgs))[0]),
new(OpCodes.Dup),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

LocalBuilder ev = generator.DeclareLocal(typeof(SendingAdminChatMessageEventsArgs));

int offset = 0;
int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ldloca_S) + offset;
int offset = 3;
int index = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(Misc), nameof(Misc.SanitizeRichText)))) + offset;

newInstructions.InsertRange(
index,
Expand Down

0 comments on commit cbe1a06

Please sign in to comment.