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

SelectingRespawnTeamEventArgs #2315

Merged
merged 4 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 35 additions & 0 deletions Exiled.Events/EventArgs/Server/SelectingRespawnTeamEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// -----------------------------------------------------------------------
// <copyright file="SelectingRespawnTeamEventArgs.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.Server
{
using System;

using Exiled.API.Features;
using Exiled.Events.EventArgs.Interfaces;
using Respawning;

/// <summary>
/// Contains all information before selecting the team to respawn next.
/// </summary>
public class SelectingRespawnTeamEventArgs : IExiledEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="SelectingRespawnTeamEventArgs"/> class.
/// </summary>
/// <param name="type">The <see cref="SpawnableTeamType"/> used as the starting value for this event.</param>
public SelectingRespawnTeamEventArgs(SpawnableTeamType type)
{
ChosenTeam = type;
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Gets or sets <see cref="SpawnableTeamType"/> that represents the team chosen to spawn.
/// </summary>
public SpawnableTeamType ChosenTeam { get; set; }
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
}
}
11 changes: 11 additions & 0 deletions Exiled.Events/Handlers/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public static class Server
/// </summary>
public static Event<ChoosingStartTeamQueueEventArgs> ChoosingStartTeamQueue { get; set; } = new();

/// <summary>
/// Invoked before choosing the team that will respawn.
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public static Event<SelectingRespawnTeamEventArgs> SelectingRespawnTeam { get; set; } = new();

/// <summary>
/// Invoked after the "reload configs" command is ran.
/// </summary>
Expand Down Expand Up @@ -184,5 +189,11 @@ public static class Server
/// Called after the "reload permissions" command is ran.
/// </summary>
public static void OnReloadedPermissions() => ReloadedPermissions.InvokeSafely();

/// <summary>
/// Called before selecting the team that will respawn next.
/// </summary>
/// <param name="ev">The <see cref="SelectingRespawnTeamEventArgs"/> instance.</param>
public static void OnSelectingRespawnTeam(SelectingRespawnTeamEventArgs ev) => SelectingRespawnTeam.InvokeSafely(ev);
}
}
64 changes: 64 additions & 0 deletions Exiled.Events/Patches/Events/Server/SelectingRespawnTeam.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// -----------------------------------------------------------------------
// <copyright file="SelectingRespawnTeam.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.Patches.Events.Server
{
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

using Exiled.API.Features;
using Exiled.API.Features.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Server;

using HarmonyLib;

using Respawning;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="RespawnManager.Update"/> to add the <see cref="Handlers.Server.SelectingRespawnTeam"/> event.
/// </summary>
[EventPatch(typeof(Handlers.Server), nameof(Handlers.Server.SelectingRespawnTeam))]
[HarmonyPatch(typeof(RespawnManager), nameof(RespawnManager.Update))]
internal static class SelectingRespawnTeam
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);
LocalBuilder ev = generator.DeclareLocal(typeof(SelectingRespawnTeamEventArgs));

const int offset = 1;
int index = newInstructions.FindLastIndex(i => i.opcode == OpCodes.Stloc_1) + offset;

newInstructions.InsertRange(index, new[]
{
// SelectingRespawnTeamEventArgs ev = new(dominatingTeam);
new CodeInstruction(OpCodes.Ldloc_1),
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(SelectingRespawnTeamEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev),

// Handlers.Server.OnSelectingRespawnTeam(ev);
new(OpCodes.Call, Method(typeof(Handlers.Server), nameof(Handlers.Server.OnSelectingRespawnTeam))),

// dominatingTeam = ev.ChosenTeam;
NaoUnderscore marked this conversation as resolved.
Show resolved Hide resolved
new(OpCodes.Ldloc, ev),
new CodeInstruction(OpCodes.Callvirt, PropertyGetter(typeof(SelectingRespawnTeamEventArgs), nameof(SelectingRespawnTeamEventArgs.ChosenTeam))),
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
new(OpCodes.Stloc_1),
});

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

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