mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
event system fixes
This commit is contained in:
1 parent
13268ac70a
commit
a23de7b51c
6 files changed
+45
-40
No files matched your search
@@ -13,28 +13,12 @@ public static class ExampleEventHandlers
|
||||
// Register event handlers here
|
||||
MiraEventManager.RegisterEventHandler<MiraButtonClickEvent<FreezeButton>>(FreezeButtonClickHandler, 1);
|
||||
MiraEventManager.RegisterEventHandler<MiraButtonCancelledEvent<FreezeButton>>(FreezeButtonCancelledHandler);
|
||||
MiraEventManager.RegisterEventHandler<EnterVentEvent>(EnterVentHandler);
|
||||
MiraEventManager.RegisterEventHandler<ExitVentEvent>(ExitVentHandler);
|
||||
MiraEventManager.RegisterEventHandler<UpdateSystemEvent>(UpdateSystemEventHandler);
|
||||
}
|
||||
|
||||
public static void EnterVentHandler(EnterVentEvent @event)
|
||||
public static void UpdateSystemEventHandler(UpdateSystemEvent @event)
|
||||
{
|
||||
Logger<ExamplePlugin>.Warning("Entering vent!");
|
||||
var random = new System.Random();
|
||||
if (random.Next(0, 2) == 0)
|
||||
{
|
||||
@event.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
public static void ExitVentHandler(ExitVentEvent @event)
|
||||
{
|
||||
Logger<ExamplePlugin>.Warning("Exiting vent!");
|
||||
var random = new System.Random();
|
||||
if (random.Next(0, 2) == 0)
|
||||
{
|
||||
@event.Cancel();
|
||||
}
|
||||
Logger<ExamplePlugin>.Error(@event.SystemType.ToString());
|
||||
}
|
||||
|
||||
// Example event handler
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace MiraAPI.Events.Vanilla;
|
||||
|
||||
/// <summary>
|
||||
/// Event fired when a player closes the doors in a room.
|
||||
/// </summary>
|
||||
public class CloseDoorsEvent : MiraCancelableEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the room that the doors were closed in.
|
||||
/// </summary>
|
||||
public SystemTypes Room { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CloseDoorsEvent"/> class.
|
||||
/// </summary>
|
||||
/// <param name="room">The room that was closed.</param>
|
||||
public CloseDoorsEvent(SystemTypes room)
|
||||
{
|
||||
Room = room;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using HarmonyLib;
|
||||
using MiraAPI.Events;
|
||||
using MiraAPI.Events.Vanilla;
|
||||
using Reactor.Utilities;
|
||||
|
||||
namespace MiraAPI.Patches.Events;
|
||||
|
||||
|
||||
@@ -11,10 +11,19 @@ namespace MiraAPI.Patches.Events;
|
||||
public static class SabotageEventPatches
|
||||
{
|
||||
[HarmonyPrefix]
|
||||
[HarmonyPatch(typeof(ShipStatus), nameof(ShipStatus.UpdateSystem), typeof(SystemTypes), typeof(PlayerControl), typeof(byte))]
|
||||
public static bool ShipStatusUpdateSystemPrefix(ShipStatus __instance, SystemTypes systemType, PlayerControl player, byte amount)
|
||||
[HarmonyPatch(typeof(ShipStatus), nameof(ShipStatus.RpcUpdateSystem), typeof(SystemTypes), typeof(byte))]
|
||||
public static bool ShipStatusUpdateSystemPrefix(ShipStatus __instance, SystemTypes systemType, byte amount)
|
||||
{
|
||||
var @event = new UpdateSystemEvent(systemType, player, amount);
|
||||
var @event = new UpdateSystemEvent(systemType, PlayerControl.LocalPlayer, amount);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
return !@event.IsCancelled;
|
||||
}
|
||||
|
||||
[HarmonyPrefix]
|
||||
[HarmonyPatch(typeof(ShipStatus), nameof(ShipStatus.RpcCloseDoorsOfType))]
|
||||
public static bool ShipStatusCloseDoorsOfTypePrefix(ShipStatus __instance, SystemTypes type)
|
||||
{
|
||||
var @event = new CloseDoorsEvent(type);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
return !@event.IsCancelled;
|
||||
}
|
||||
|
||||
@@ -9,10 +9,11 @@ namespace MiraAPI.Patches.Events;
|
||||
public static class StartMeetingEventPatch
|
||||
{
|
||||
[HarmonyPrefix]
|
||||
[HarmonyPatch(typeof(PlayerControl), nameof(PlayerControl.ReportDeadBody))]
|
||||
public static bool PlayerControlReportDeadBodyPrefix(PlayerControl __instance, NetworkedPlayerInfo? target)
|
||||
[HarmonyPatch(typeof(PlayerControl), nameof(PlayerControl.CmdReportDeadBody))]
|
||||
public static bool PlayerControlCmdReportDeadBodyPrefix(PlayerControl __instance, NetworkedPlayerInfo? target)
|
||||
{
|
||||
var body = target != null ? Helpers.GetBodyById(target.PlayerId) : null;
|
||||
|
||||
var @event = new StartMeetingEvent(__instance, target, body);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
return !@event.IsCancelled;
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace MiraAPI.Patches.Events;
|
||||
[HarmonyPatch]
|
||||
public static class VentEventPatches
|
||||
{
|
||||
// necessary because Vent.Use is inlined in il2cpp.
|
||||
private static bool _showButtons;
|
||||
|
||||
[HarmonyPostfix]
|
||||
@@ -24,8 +25,8 @@ public static class VentEventPatches
|
||||
}
|
||||
|
||||
[HarmonyPrefix]
|
||||
[HarmonyPatch(typeof(PlayerPhysics), nameof(PlayerPhysics.CoEnterVent))]
|
||||
public static bool EnterVentPrefix(PlayerPhysics __instance, int id, ref Il2CppSystem.Collections.IEnumerator __result)
|
||||
[HarmonyPatch(typeof(PlayerPhysics), nameof(PlayerPhysics.RpcEnterVent))]
|
||||
public static bool PlayerPhysicsRpcEnterVentPrefix(PlayerPhysics __instance, int id)
|
||||
{
|
||||
var pc = __instance.myPlayer;
|
||||
var vent = ShipStatus.Instance.AllVents.FirstOrDefault(v => v.Id == id);
|
||||
@@ -33,12 +34,6 @@ public static class VentEventPatches
|
||||
var @event = new EnterVentEvent(pc, vent);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
|
||||
if (@event.IsCancelled)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error("Enter vent event cancelled!");
|
||||
__result = Helpers.EmptyCoroutine().WrapToIl2Cpp();
|
||||
}
|
||||
|
||||
if (pc.AmOwner)
|
||||
{
|
||||
_showButtons = !@event.IsCancelled;
|
||||
@@ -47,8 +42,8 @@ public static class VentEventPatches
|
||||
}
|
||||
|
||||
[HarmonyPrefix]
|
||||
[HarmonyPatch(typeof(PlayerPhysics), nameof(PlayerPhysics.CoExitVent))]
|
||||
public static bool ExitVentPrefix(PlayerPhysics __instance, int id, ref Il2CppSystem.Collections.IEnumerator __result)
|
||||
[HarmonyPatch(typeof(PlayerPhysics), nameof(PlayerPhysics.RpcExitVent))]
|
||||
public static bool PlayerPhysicsRpcExitVentPrefix(PlayerPhysics __instance, int id)
|
||||
{
|
||||
var pc = __instance.myPlayer;
|
||||
var vent = ShipStatus.Instance.AllVents.First(v => v.Id == id);
|
||||
@@ -56,12 +51,6 @@ public static class VentEventPatches
|
||||
var @event = new ExitVentEvent(pc, vent);
|
||||
MiraEventManager.InvokeEvent(@event);
|
||||
|
||||
if (@event.IsCancelled)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error("Exit vent event cancelled!");
|
||||
__result = Helpers.EmptyCoroutine().WrapToIl2Cpp();
|
||||
}
|
||||
|
||||
if (pc.AmOwner)
|
||||
{
|
||||
_showButtons = @event.IsCancelled;
|
||||
|
||||
Reference in new issue
Block a user