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

Adding event TogglingRadio(Player, RadioItem, bool, bool) #2329

Merged
merged 20 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
67 changes: 67 additions & 0 deletions Exiled.Events/EventArgs/Player/TogglingRadioEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// -----------------------------------------------------------------------
// <copyright file="TogglingRadioEventArgs.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.Radio;

/// <summary>
/// Contains all information before radio preset is changed.
/// </summary>
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
public class TogglingRadioEventArgs : IPlayerEvent, IDeniableEvent, IItemEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="TogglingRadioEventArgs" /> class.
/// </summary>
/// <param name="player">
/// <inheritdoc cref="Player" />
/// </param>
/// <param name="radio">
/// <inheritdoc cref="Radio" />
/// </param>
/// <param name="newState">
/// <inheritdoc cref="NewState" />
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public TogglingRadioEventArgs(Player player, RadioItem radio, bool newState, bool isAllowed = true)
{
Player = player;
Radio = (Radio)Item.Get(radio);
NewState = newState;
IsAllowed = isAllowed;
}

/// <summary>
/// Gets the <see cref="API.Features.Items.Radio" /> which is being used.
/// </summary>
public Radio Radio { get; }

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

/// <summary>
/// Gets or sets a value indicating whether new state of the radio.
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public bool NewState { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the radio preset can be changed or not.
/// </summary>
public bool IsAllowed { get; set; }

/// <summary>
/// Gets the player who's using the radio.
/// </summary>
public Player Player { get; }
}
}
11 changes: 11 additions & 0 deletions Exiled.Events/Handlers/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ public class Player
/// </summary>
public static Event<TogglingOverwatchEventArgs> TogglingOverwatch { get; set; } = new();

/// <summary>
/// Invoked before turning the <see cref="API.Features.Items.Radio" /> on/off.
/// </summary>
public static Event<TogglingRadioEventArgs> TogglingRadio { get; set; } = new();

/// <summary>
/// Invoked before a <see cref="API.Features.Player"/> searches a Pickup.
/// </summary>
Expand Down Expand Up @@ -900,6 +905,12 @@ public class Player
/// <param name="ev">The <see cref="TogglingOverwatchEventArgs"/> instance.</param>
public static void OnTogglingOverwatch(TogglingOverwatchEventArgs ev) => TogglingOverwatch.InvokeSafely(ev);

/// <summary>
/// Called before turning the radio on/off.
/// </summary>
/// <param name="ev">The <see cref="TogglingRadioEventArgs"/> instance.</param>
public static void OnTogglingRadio(TogglingRadioEventArgs ev) => TogglingRadio.InvokeSafely(ev);

/// <summary>
/// Called before a <see cref="API.Features.Player"/> searches a Pickup.
/// </summary>
Expand Down
85 changes: 85 additions & 0 deletions Exiled.Events/Patches/Events/Player/TogglingRadio.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// -----------------------------------------------------------------------
// <copyright file="TogglingRadio.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.Player
{

Check failure on line 8 in Exiled.Events/Patches/Events/Player/TogglingRadio.cs

View workflow job for this annotation

GitHub Actions / build

Check failure on line 8 in Exiled.Events/Patches/Events/Player/TogglingRadio.cs

View workflow job for this annotation

GitHub Actions / build

Check failure on line 8 in Exiled.Events/Patches/Events/Player/TogglingRadio.cs

View workflow job for this annotation

GitHub Actions / build

Check failure on line 8 in Exiled.Events/Patches/Events/Player/TogglingRadio.cs

View workflow job for this annotation

GitHub Actions / build

Check failure on line 8 in Exiled.Events/Patches/Events/Player/TogglingRadio.cs

View workflow job for this annotation

GitHub Actions / build

Check failure on line 8 in Exiled.Events/Patches/Events/Player/TogglingRadio.cs

View workflow job for this annotation

GitHub Actions / build

Check failure on line 8 in Exiled.Events/Patches/Events/Player/TogglingRadio.cs

View workflow job for this annotation

GitHub Actions / build

Check failure on line 8 in Exiled.Events/Patches/Events/Player/TogglingRadio.cs

View workflow job for this annotation

GitHub Actions / build

Check failure on line 8 in Exiled.Events/Patches/Events/Player/TogglingRadio.cs

View workflow job for this annotation

GitHub Actions / build

Check failure on line 8 in Exiled.Events/Patches/Events/Player/TogglingRadio.cs

View workflow job for this annotation

GitHub Actions / build


using Exiled.API.Features;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Player;
#pragma warning disable SA1313 // Parameter names should begin with lower-case letter
using HarmonyLib;
using InventorySystem.Items;
using InventorySystem.Items.Radio;
using PluginAPI.Events;

/// <summary>
/// Patches <see cref="RadioItem.ServerProcessCmd" />.
/// Adds the <see cref="Handlers.Player.TogglingRadio" /> event.
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.TogglingRadio))]
[HarmonyPatch(typeof(RadioItem), nameof(RadioItem.ServerProcessCmd))]
internal static class TogglingRadio
{
private static bool Prefix(RadioItem __instance, RadioMessages.RadioCommand command)
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
{
if (__instance.Owner.HasBlock(BlockedInteraction.ItemUsage))
return false;

switch (command)
{
case RadioMessages.RadioCommand.Enable:
{
if (__instance._enabled || __instance._battery <= 0f || !EventManager.ExecuteEvent(new PlayerRadioToggleEvent(__instance.Owner, __instance, true)))
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
return false;

TogglingRadioEventArgs ev = new(Player.Get(__instance.Owner), __instance, true);
Handlers.Player.OnTogglingRadio(ev);
if (!ev.IsAllowed)
return false;

__instance._enabled = ev.NewState == __instance._enabled ? true : ev.NewState;
break;
}

case RadioMessages.RadioCommand.Disable:
{
if (!__instance._enabled || !EventManager.ExecuteEvent(new PlayerRadioToggleEvent(__instance.Owner, __instance, false)))
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
return false;

TogglingRadioEventArgs ev = new(Player.Get(__instance.Owner), __instance, false);
Handlers.Player.OnTogglingRadio(ev);
if (!ev.IsAllowed)
return false;

__instance._enabled = ev.NewState == __instance._enabled ? false : ev.NewState;
break;
}

case RadioMessages.RadioCommand.ChangeRange:
{
byte b = (byte)(__instance._rangeId + 1);
if (b >= __instance.Ranges.Length)
{
b = 0;
}

PlayerChangeRadioRangeEvent playerChangeRadioRangeEvent = new PlayerChangeRadioRangeEvent(__instance.Owner, __instance, (RadioMessages.RadioRangeLevel)b);
if (!EventManager.ExecuteEvent(playerChangeRadioRangeEvent))
{
return false;
}

b = __instance._rangeId = (byte)playerChangeRadioRangeEvent.Range;
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}

__instance.SendStatusMessage();
return false;
}
}
}
Loading