Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 0475412

Browse files
committed
Multiple events fixing
1 parent f8409c4 commit 0475412

File tree

6 files changed

+77
-33
lines changed

6 files changed

+77
-33
lines changed

Exiled.Events/EventArgs/Player/AddingItemEventArgs.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace Exiled.Events.EventArgs.Player
1010
using Exiled.API.Features;
1111
using Exiled.API.Features.Items;
1212
using Exiled.API.Features.Pickups;
13-
using Exiled.Events.EventArgs.Interfaces;
13+
using Interfaces;
14+
using InventorySystem.Items;
1415

1516
/// <summary>
1617
/// Contains all information before adding an item to a player's inventory.
@@ -23,12 +24,14 @@ public class AddingItemEventArgs : IPlayerEvent, IDeniableEvent, IItemEvent, IPi
2324
/// <param name="player"><inheritdoc cref="Player"/></param>
2425
/// <param name="item"><inheritdoc cref="Item"/></param>
2526
/// <param name="pickup"><inheritdoc cref="Pickup"/></param>
27+
/// <param name="addReason"><inheritdoc cref="AddReason"/></param>
2628
/// <param name="isAllowed"><inheritdoc cref="IsAllowed"/></param>
27-
public AddingItemEventArgs(Player player, Item item, Pickup pickup, bool isAllowed = true)
29+
public AddingItemEventArgs(Player player, Item item, Pickup pickup, ItemAddReason addReason, bool isAllowed = true)
2830
{
2931
Player = player;
3032
Item = item;
3133
Pickup = pickup;
34+
AddReason = addReason;
3235
IsAllowed = isAllowed;
3336
}
3437

@@ -45,6 +48,11 @@ public AddingItemEventArgs(Player player, Item item, Pickup pickup, bool isAllow
4548
/// </summary>
4649
public Pickup Pickup { get; }
4750

51+
/// <summary>
52+
/// Gets or sets the reason of why the item was added to the player inventory.
53+
/// </summary>
54+
public ItemAddReason AddReason { get; set; }
55+
4856
/// <inheritdoc/>
4957
public bool IsAllowed { get; set; }
5058
}

Exiled.Events/Patches/Events/Player/AddingItem.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Exiled.Events.Patches.Events.Player
2525
using static HarmonyLib.AccessTools;
2626

2727
/// <summary>
28-
/// Patches <see cref="InventoryExtensions.ServerAddItem(Inventory, ItemType, ushort, ItemPickupBase)"/>.
28+
/// Patches <see cref="InventoryExtensions.ServerAddItem(Inventory, ItemType, ItemAddReason, ushort, ItemPickupBase)"/>.
2929
/// Adds the <see cref="Handlers.Player.AddingItem"/> event.
3030
/// </summary>
3131
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.AddingItem))]
@@ -40,28 +40,34 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
4040
int index = newInstructions.FindIndex(i => i.opcode == OpCodes.Ldarg_3) + offset;
4141

4242
Label ret = generator.DefineLabel();
43+
LocalBuilder ev = generator.DeclareLocal(typeof(AddingItemEventArgs));
4344

44-
newInstructions.InsertRange(index, new CodeInstruction[]
45+
newInstructions.InsertRange(index, new[]
4546
{
4647
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
4748
new(OpCodes.Ldfld, Field(typeof(Inventory), nameof(Inventory._hub))),
4849
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),
4950
new(OpCodes.Ldloc_1),
5051
new(OpCodes.Call, Method(typeof(Item), nameof(Item.Get), new[] { typeof(ItemBase) })),
51-
new(OpCodes.Ldarg_3),
52+
new(OpCodes.Ldarg_S, 4),
5253
new(OpCodes.Call, Method(typeof(Pickup), nameof(Pickup.Get), new[] { typeof(ItemPickupBase) })),
5354
new(OpCodes.Ldc_I4_1),
5455
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(AddingItemEventArgs))[0]),
5556
new(OpCodes.Dup),
57+
new(OpCodes.Dup),
58+
new(OpCodes.Stloc_S, ev.LocalIndex),
5659
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnAddingItem))),
5760
new(OpCodes.Callvirt, PropertyGetter(typeof(AddingItemEventArgs), nameof(AddingItemEventArgs.IsAllowed))),
5861
new(OpCodes.Brfalse_S, ret),
62+
new(OpCodes.Ldloc_S, ev.LocalIndex),
63+
new(OpCodes.Callvirt, PropertyGetter(typeof(AddingItemEventArgs), nameof(AddingItemEventArgs.AddReason))),
64+
new(OpCodes.Starg_S, 3),
5965
});
6066

6167
newInstructions[newInstructions.Count - 1].labels.Add(ret);
6268

63-
for (int z = 0; z < newInstructions.Count; z++)
64-
yield return newInstructions[z];
69+
foreach (CodeInstruction instruction in newInstructions)
70+
yield return instruction;
6571

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

Exiled.Events/Patches/Events/Player/EnteringKillerCollision.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ namespace Exiled.Events.Patches.Events.Player
1919
using static HarmonyLib.AccessTools;
2020

2121
/// <summary>
22-
/// Patches <see cref="CheckpointKiller.OnTriggerEnter"/>.
22+
/// Patches <see cref="PitKiller.OnTriggerEnter"/>.
2323
/// Adds the <see cref="Handlers.Player.EnteringKillerCollision"/> event.
2424
/// </summary>
2525
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.EnteringKillerCollision))]
26-
[HarmonyPatch(typeof(CheckpointKiller), nameof(CheckpointKiller.OnTriggerEnter))]
26+
[HarmonyPatch(typeof(PitKiller), nameof(PitKiller.OnTriggerEnter))]
2727
internal static class EnteringKillerCollision
2828
{
2929
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
@@ -32,8 +32,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
3232

3333
Label ret = generator.DefineLabel();
3434

35-
int offset = -5;
36-
int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Newobj) + offset;
35+
const int offset = -1;
36+
int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Ldfld) + offset;
3737

3838
newInstructions.InsertRange(
3939
index,
@@ -61,8 +61,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
6161

6262
newInstructions[newInstructions.Count - 1].WithLabels(ret);
6363

64-
for (int z = 0; z < newInstructions.Count; z++)
65-
yield return newInstructions[z];
64+
foreach (CodeInstruction instruction in newInstructions)
65+
yield return instruction;
6666

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

Exiled.Events/Patches/Events/Player/InteractingElevator.cs

+12-10
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ namespace Exiled.Events.Patches.Events.Player
2424
using static HarmonyLib.AccessTools;
2525

2626
/// <summary>
27-
/// Patches <see cref="ElevatorManager.ServerReceiveMessage(NetworkConnection, ElevatorManager.ElevatorSyncMsg)" />.
27+
/// Patches <see cref="ElevatorChamber.ServerInteract(ReferenceHub, byte)" />.
2828
/// Adds the <see cref="Handlers.Player.InteractingElevator" /> event.
2929
/// </summary>
3030
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.InteractingElevator))]
31-
[HarmonyPatch(typeof(ElevatorManager), nameof(ElevatorManager.ServerReceiveMessage))]
31+
[HarmonyPatch(typeof(ElevatorChamber), nameof(ElevatorChamber.ServerInteract))]
3232
internal class InteractingElevator
3333
{
3434
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
3535
{
3636
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);
3737

38-
Label @break = (Label)newInstructions.FindLast(i => i.opcode == OpCodes.Leave_S).operand;
38+
Label retLabel = generator.DefineLabel();
3939

40-
int offset = -2;
41-
int index = newInstructions.FindLastIndex(i => i.opcode == OpCodes.Newobj) + offset;
40+
const int offset = -1;
41+
int index = newInstructions.FindLastIndex(i => i.opcode == OpCodes.Ldarg_0) + offset;
4242

4343
// InteractingElevatorEventArgs ev = new(Player.Get(referenceHub), elevatorChamber, true);
4444
//
@@ -51,11 +51,11 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
5151
new[]
5252
{
5353
// Player.Get(referenceHub)
54-
new CodeInstruction(OpCodes.Ldloc_0).MoveLabelsFrom(newInstructions[index]),
54+
new CodeInstruction(OpCodes.Ldarg_1).MoveLabelsFrom(newInstructions[index]),
5555
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),
5656

5757
// elevatorChamber
58-
new(OpCodes.Ldloc_3),
58+
new(OpCodes.Ldarg_0),
5959

6060
// true
6161
new(OpCodes.Ldc_I4_1),
@@ -70,11 +70,13 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
7070
// if (!ev.IsAllowed)
7171
// continue;
7272
new(OpCodes.Callvirt, PropertyGetter(typeof(InteractingElevatorEventArgs), nameof(InteractingElevatorEventArgs.IsAllowed))),
73-
new(OpCodes.Brfalse_S, @break),
73+
new(OpCodes.Brfalse_S, retLabel),
7474
});
7575

76-
for (int z = 0; z < newInstructions.Count; z++)
77-
yield return newInstructions[z];
76+
newInstructions[newInstructions.Count - 1].labels.Add(retLabel);
77+
78+
foreach (CodeInstruction instruction in newInstructions)
79+
yield return instruction;
7880

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

Exiled.Events/Patches/Events/Player/Joined.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal static void CallEvent(ReferenceHub hub, out Player player)
3939
#endif
4040
Player.UnverifiedPlayers.Add(hub.gameObject, player);
4141

42-
if (ReferenceHub.HostHub == null)
42+
if (ReferenceHub._hostHub == null)
4343
{
4444
Server.Host = player;
4545
}

Exiled.Events/Patches/Generic/LiftList.cs

+37-9
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,53 @@
77

88
namespace Exiled.Events.Patches.Generic
99
{
10-
using API.Features;
10+
#pragma warning disable SA1313
11+
#pragma warning disable SA1402
12+
using System.Collections.Generic;
13+
using System.Reflection.Emit;
1114

15+
using API.Features;
16+
using Exiled.API.Features.Core.Generic.Pools;
1217
using HarmonyLib;
1318
using Interactables.Interobjects;
1419

20+
using static HarmonyLib.AccessTools;
21+
22+
// TODO: CHECK IF THIS WORKS AS ISTENDED.
23+
1524
/// <summary>
16-
/// Patches <see cref="ElevatorManager.SpawnAllChambers"/>.
25+
/// Patches <see cref="ElevatorManager.SpawnChamber"/> to register all ElevatorChambers inside the Lift wrapper.
1726
/// </summary>
18-
[HarmonyPatch(typeof(ElevatorManager), nameof(ElevatorManager.SpawnAllChambers))]
19-
internal class LiftList
27+
[HarmonyPatch(typeof(ElevatorManager), nameof(ElevatorManager.SpawnChamber))]
28+
internal static class LiftList
2029
{
21-
private static void Postfix()
30+
[HarmonyTranspiler]
31+
private static IEnumerable<CodeInstruction> OnSpawnChamber(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
2232
{
23-
Lift.ElevatorChamberToLift.Clear();
33+
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);
34+
35+
int index = newInstructions.FindLastIndex(x => x.opcode == OpCodes.Ldarg_1);
2436

25-
foreach (ElevatorChamber lift in ElevatorChamber.AllChambers)
37+
newInstructions.InsertRange(index, new CodeInstruction[]
2638
{
27-
Lift.Get(lift);
28-
}
39+
new(OpCodes.Ldloc_0),
40+
new(OpCodes.Call, Method(typeof(Lift), nameof(Lift.Get), new[] { typeof(ElevatorChamber) })),
41+
});
42+
43+
foreach (CodeInstruction instruction in newInstructions)
44+
yield return instruction;
45+
46+
ListPool<CodeInstruction>.Pool.Return(newInstructions);
2947
}
3048
}
49+
50+
/// <summary>
51+
/// Patches <see cref="ElevatorChamber.OnDestroy"/> to remove the destroyed ElevatorChambers from the Lift wrapper list.
52+
/// </summary>
53+
[HarmonyPatch(typeof(ElevatorChamber), nameof(ElevatorChamber.OnDestroy))]
54+
internal static class LiftListRemove
55+
{
56+
[HarmonyPrefix]
57+
private static void OnDestroy(ElevatorChamber __instance) => Lift.ElevatorChamberToLift.Remove(__instance);
58+
}
3159
}

0 commit comments

Comments
 (0)