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

[Exiled::Events] [Exiled::API] New event + new property #31

Merged
merged 9 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 10 additions & 1 deletion EXILED/Exiled.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,18 @@ public PlayerPermissions RemoteAdminPermissions
public Role Role
{
get => role ??= Role.Create(RoleManager.CurrentRole);
internal set => role = value;
internal set
{
PreviousRole = role;
role = value;
}
}

/// <summary>
/// Gets the role that player had before changing role.
/// </summary>
public Role PreviousRole { get; private set; }
VALERA771 marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets or sets the player's SCP preferences.
/// </summary>
Expand Down
66 changes: 66 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Player/EscapedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// -----------------------------------------------------------------------
// <copyright file="EscapedEventArgs.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 System;

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

/// <summary>
/// Contains all information after player has escaped.
/// </summary>
public class EscapedEventArgs : IPlayerEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="EscapedEventArgs"/> class.
/// </summary>
/// <param name="player"><inheritdoc cref="Player"/></param>
/// <param name="escapeScenario"><inheritdoc cref="EscapeScenario"/></param>
public EscapedEventArgs(Player player, EscapeScenario escapeScenario)
{
Player = player;
EscapeScenario = escapeScenario;
Team = EscapeScenario is EscapeScenario.Scientist or EscapeScenario.CuffedClassD ? SpawnableTeamType.NineTailedFox : SpawnableTeamType.ChaosInsurgency;
Tickets = Team == SpawnableTeamType.ChaosInsurgency ? 4 : 3;
OldRole = EscapeScenario is EscapeScenario.Scientist or EscapeScenario.CuffedScientist ? RoleTypeId.Scientist : RoleTypeId.ClassD;
VALERA771 marked this conversation as resolved.
Show resolved Hide resolved
EscapeTime = (int)Math.Ceiling(player.Role.ActiveTime.TotalSeconds);
}

/// <inheritdoc/>
public Player Player { get; }

/// <summary>
/// Gets the type of escape.
/// </summary>
public EscapeScenario EscapeScenario { get; }

/// <summary>
/// Gets the <see cref="SpawnableTeamType"/> that gained tickets for this escape.
/// </summary>
public SpawnableTeamType Team { get; }

/// <summary>
/// Gets the amount of tickets gained for this escape.
/// </summary>
public int Tickets { get; }

/// <summary>
/// Gets the previous role for this player.
/// </summary>
public RoleTypeId OldRole { get; }
VALERA771 marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets the time in seconds since round started.
/// </summary>
public int EscapeTime { get; }
}
}
11 changes: 11 additions & 0 deletions EXILED/Exiled.Events/Handlers/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ public class Player
/// </summary>
public static Event<EscapingEventArgs> Escaping { get; set; } = new();

/// <summary>
/// Invoked after a <see cref="API.Features.Player"/> escapes.
/// </summary>
public static Event<EscapedEventArgs> Escaped { get; set; } = new();

/// <summary>
/// Invoked before a <see cref="API.Features.Player"/> begins speaking to the intercom.
/// </summary>
Expand Down Expand Up @@ -710,6 +715,12 @@ public class Player
/// <param name="ev">The <see cref="EscapingEventArgs"/> instance.</param>
public static void OnEscaping(EscapingEventArgs ev) => Escaping.InvokeSafely(ev);

/// <summary>
/// Called after a <see cref="API.Features.Player"/> escapes.
/// </summary>
/// <param name="ev">The <see cref="EscapedEventArgs"/> instance.</param>
public static void OnEscaped(EscapedEventArgs ev) => Escaped.InvokeSafely(ev);

/// <summary>
/// Called before a <see cref="API.Features.Player"/> begins speaking to the intercom.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -----------------------------------------------------------------------
// <copyright file="Escaping.cs" company="Exiled Team">
// <copyright file="EscapingAndEscaped.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
Expand Down Expand Up @@ -29,11 +29,12 @@ namespace Exiled.Events.Patches.Events.Player
using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="Escape.ServerHandlePlayer(ReferenceHub)"/> for <see cref="Handlers.Player.Escaping" />.
/// Patches <see cref="Escape.ServerHandlePlayer(ReferenceHub)"/> for <see cref="Handlers.Player.Escaping" /> and <see cref="Handlers.Player.Escaped"/>.
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.Escaping))]
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.Escaped))]
[HarmonyPatch(typeof(Escape), nameof(Escape.ServerHandlePlayer))]
internal static class Escaping
internal static class EscapingAndEscaped
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
Expand Down Expand Up @@ -98,9 +99,25 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
// GrantAllTickets(ev)
new CodeInstruction(OpCodes.Ldloc, ev.LocalIndex).WithLabels(labels),
new(OpCodes.Call, Method(typeof(Escaping), nameof(GrantAllTickets))),
new(OpCodes.Call, Method(typeof(EscapingAndEscaped), nameof(GrantAllTickets))),
});

newInstructions.InsertRange(newInstructions.Count - 1, new CodeInstruction[]
{
// ev.Player
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(EscapingEventArgs), nameof(EscapingEventArgs.Player))),

// ev.EscapeScenario
new(OpCodes.Ldloc_1),

// EscapedEventArgs ev2 = new(ev.Player, ev.EscapeScenario);
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(EscapedEventArgs))[0]),

// Handlers.Player.OnEscaped(ev);
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnEscaped))),
});

newInstructions[newInstructions.Count - 1].WithLabels(returnLabel);

for (int z = 0; z < newInstructions.Count; z++)
Expand Down