mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
events 2 electric boogaloo
This commit is contained in:
1 parent
4c113e5fb8
commit
eac41a7c9e
8 files changed
+112
-35
No files matched your search
@@ -3,11 +3,13 @@ using BepInEx.Configuration;
|
||||
using BepInEx.Unity.IL2CPP;
|
||||
using HarmonyLib;
|
||||
using MiraAPI.Events;
|
||||
using MiraAPI.Events.Vanilla;
|
||||
using MiraAPI.Events.Mira;
|
||||
using MiraAPI.Example.Buttons.Freezer;
|
||||
using MiraAPI.PluginLoading;
|
||||
using Reactor;
|
||||
using Reactor.Networking;
|
||||
using Reactor.Networking.Attributes;
|
||||
using Reactor.Utilities;
|
||||
|
||||
namespace MiraAPI.Example;
|
||||
|
||||
@@ -23,14 +25,18 @@ public partial class ExamplePlugin : BasePlugin, IMiraPlugin
|
||||
public ConfigFile GetConfigFile() => Config;
|
||||
public override void Load()
|
||||
{
|
||||
MiraEventManager.RegisterEventHandler<BeforeMurderEvent>(
|
||||
e =>
|
||||
{
|
||||
if (e.Source.PlayerId == PlayerControl.LocalPlayer.PlayerId)
|
||||
{
|
||||
e.Cancel();
|
||||
}
|
||||
});
|
||||
MiraEventManager.RegisterEventHandler<MiraButtonClickEvent<FreezeButton>>(e=>
|
||||
{
|
||||
Logger<ExamplePlugin>.Warning("Freeze button clicked!");
|
||||
e.Cancel();
|
||||
e.Button.SetTimer(5f);
|
||||
});
|
||||
|
||||
MiraEventManager.RegisterEventHandler<MiraButtonCancelledEvent<FreezeButton>>(e=>
|
||||
{
|
||||
Logger<ExamplePlugin>.Warning("Freeze button cancelled!");
|
||||
e.Button.OverrideName("Freeze Canceled");
|
||||
});
|
||||
Harmony.PatchAll();
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,21 @@
|
||||
|
||||
namespace MiraAPI.Events.Mira;
|
||||
|
||||
public class MiraButtonCancelledEvent<T> : MiraEvent where T : CustomActionButton
|
||||
/// <summary>
|
||||
/// Invoked when a Mira Button click is cancelled. Do not use for vanilla buttons.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The Mira Button type.</typeparam>
|
||||
public sealed class MiraButtonCancelledEvent<T> : MiraEvent where T : CustomActionButton
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets Mira Button whose click was cancelled.
|
||||
/// </summary>
|
||||
public T Button { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MiraButtonCancelledEvent{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="button">The Mira Button whose click was cancelled.</param>
|
||||
public MiraButtonCancelledEvent(T button)
|
||||
{
|
||||
Button = button;
|
||||
|
||||
@@ -2,10 +2,21 @@
|
||||
|
||||
namespace MiraAPI.Events.Mira;
|
||||
|
||||
public class MiraButtonClickEvent<T> : MiraCancelableEvent where T : CustomActionButton
|
||||
/// <summary>
|
||||
/// Button click event for Mira Buttons only. Do not use for vanilla buttons.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The Mira Button type.</typeparam>
|
||||
public sealed class MiraButtonClickEvent<T> : MiraCancelableEvent where T : CustomActionButton
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets Mira Button that was clicked.
|
||||
/// </summary>
|
||||
public T Button { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MiraButtonClickEvent{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="button">The Mira Button that was clicked.</param>
|
||||
public MiraButtonClickEvent(T button)
|
||||
{
|
||||
Button = button;
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace MiraAPI.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract class for Mira Events that can be cancelled.
|
||||
/// </summary>
|
||||
public abstract class MiraCancelableEvent : MiraEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the event is cancelled.
|
||||
/// </summary>
|
||||
public bool IsCancelled { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cancels the event.
|
||||
/// </summary>
|
||||
public void Cancel()
|
||||
{
|
||||
IsCancelled = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uncancels the event.
|
||||
/// </summary>
|
||||
public void UnCancel()
|
||||
{
|
||||
IsCancelled = false;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,6 @@
|
||||
namespace MiraAPI.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract class for Mira Events.
|
||||
/// </summary>
|
||||
public abstract class MiraEvent;
|
||||
|
||||
public abstract class MiraCancelableEvent : MiraEvent
|
||||
{
|
||||
public bool IsCancelled { get; private set; }
|
||||
|
||||
public void Cancel()
|
||||
{
|
||||
IsCancelled = true;
|
||||
}
|
||||
|
||||
public void UnCancel()
|
||||
{
|
||||
IsCancelled = false;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Reactor.Utilities;
|
||||
|
||||
namespace MiraAPI.Events;
|
||||
|
||||
@@ -17,7 +18,14 @@ public static class MiraEventManager
|
||||
/// <typeparam name="T">Type of Event.</typeparam>
|
||||
public static void InvokeEvent<T>(T eventInstance) where T : MiraEvent
|
||||
{
|
||||
foreach (var handler in EventHandlers[typeof(T)])
|
||||
EventHandlers.TryGetValue(typeof(T), out var handlers);
|
||||
if (handlers == null)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Warning("No handlers for event " + typeof(T).Name);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var handler in handlers)
|
||||
{
|
||||
((Action<T>)handler).Invoke(eventInstance);
|
||||
}
|
||||
@@ -35,5 +43,6 @@ public static class MiraEventManager
|
||||
EventHandlers.Add(typeof(T), []);
|
||||
}
|
||||
EventHandlers[typeof(T)].Add(handler);
|
||||
Logger<MiraApiPlugin>.Info("Registered event handler for " + typeof(T).Name);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,28 @@
|
||||
namespace MiraAPI.Events.Vanilla;
|
||||
|
||||
public class BeforeMurderEvent : MiraCancelableEvent
|
||||
/// <summary>
|
||||
/// Event that is invoked before a player is murdered. This event is cancelable.
|
||||
/// </summary>
|
||||
public sealed class BeforeMurderEvent : MiraCancelableEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the player that is killing the target.
|
||||
/// </summary>
|
||||
public PlayerControl Source { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the player that is being killed.
|
||||
/// </summary>
|
||||
public PlayerControl Target { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BeforeMurderEvent"/> class.
|
||||
/// </summary>
|
||||
/// <param name="source">The player that is killing the target.</param>
|
||||
/// <param name="target">The player that is being killed.</param>
|
||||
public BeforeMurderEvent(PlayerControl source, PlayerControl target)
|
||||
{
|
||||
Source = source;
|
||||
Target = target;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
using MiraAPI.Events;
|
||||
using System;
|
||||
using MiraAPI.Events;
|
||||
using MiraAPI.Events.Mira;
|
||||
using MiraAPI.Utilities.Assets;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.UI;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace MiraAPI.Hud;
|
||||
|
||||
@@ -99,17 +101,18 @@ public abstract class CustomActionButton
|
||||
pb.OnClick = new Button.ButtonClickedEvent();
|
||||
pb.OnClick.AddListener((UnityAction)(() =>
|
||||
{
|
||||
var @event = new MiraButtonClickEvent<CustomActionButton>(this);
|
||||
var eventType = typeof(MiraButtonClickEvent<>).MakeGenericType(GetType());
|
||||
var @event = (MiraCancelableEvent)Activator.CreateInstance(eventType, this)!;
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
if (!@event.IsCancelled)
|
||||
{
|
||||
ClickHandler();
|
||||
}
|
||||
else
|
||||
if (@event.IsCancelled)
|
||||
{
|
||||
var cancelledEvent = new MiraButtonCancelledEvent<CustomActionButton>(this);
|
||||
MiraEventManager.InvokeEvent(cancelledEvent);
|
||||
}
|
||||
else
|
||||
{
|
||||
ClickHandler();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user