fix issues with vent events

This commit is contained in:
XtraCube committed 2024-12-08 17:58:37 -05:00
1 parent 870c913b5d
commit 13268ac70a
8 files changed
+101 -18

No files matched your search

+23
View File
@@ -1,5 +1,6 @@
using MiraAPI.Events;
using MiraAPI.Events.Mira;
using MiraAPI.Events.Vanilla;
using MiraAPI.Example.Buttons.Freezer;
using Reactor.Utilities;
@@ -12,6 +13,28 @@ 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);
}
public static void EnterVentHandler(EnterVentEvent @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();
}
}
// Example event handler
+1
View File
@@ -25,6 +25,7 @@ public partial class ExamplePlugin : BasePlugin, IMiraPlugin
public ConfigFile GetConfigFile() => Config;
public override void Load()
{
ExampleEventHandlers.Initialize();
Harmony.PatchAll();
}
}
+2 -2
View File
@@ -13,14 +13,14 @@ public class EnterVentEvent : MiraCancelableEvent
/// <summary>
/// Gets the vent that the player is entering.
/// </summary>
public Vent Vent { get; }
public Vent? Vent { get; }
/// <summary>
/// Initializes a new instance of the <see cref="EnterVentEvent"/> class.
/// </summary>
/// <param name="player">The player that is entering the vent.</param>
/// <param name="vent">The vent being entered.</param>
public EnterVentEvent(PlayerControl player, Vent vent)
public EnterVentEvent(PlayerControl player, Vent? vent)
{
Player = player;
Vent = vent;
+2 -2
View File
@@ -13,14 +13,14 @@ public class ExitVentEvent : MiraCancelableEvent
/// <summary>
/// Gets the vent that the player is exiting.
/// </summary>
public Vent Vent { get; }
public Vent? Vent { get; }
/// <summary>
/// Initializes a new instance of the <see cref="ExitVentEvent"/> class.
/// </summary>
/// <param name="player">The player who is exiting the vent.</param>
/// <param name="vent">The vent being exited from.</param>
public ExitVentEvent(PlayerControl player, Vent vent)
public ExitVentEvent(PlayerControl player, Vent? vent)
{
Player = player;
Vent = vent;
+7 -1
View File
@@ -10,6 +10,11 @@ public class UpdateSystemEvent : MiraCancelableEvent
/// </summary>
public SystemTypes SystemType { get; }
/// <summary>
/// Gets the player that is updating the system.
/// </summary>
public PlayerControl Player { get; }
/// <summary>
/// Gets the byte amount the system is being updated by.
/// </summary>
@@ -20,9 +25,10 @@ public class UpdateSystemEvent : MiraCancelableEvent
/// </summary>
/// <param name="systemType">The SystemType being updated.</param>
/// <param name="amount">Amount to update System to.</param>
public UpdateSystemEvent(SystemTypes systemType, byte amount)
public UpdateSystemEvent(SystemTypes systemType, PlayerControl player, byte amount)
{
SystemType = systemType;
Player = player;
Amount = amount;
}
}
@@ -11,10 +11,10 @@ namespace MiraAPI.Patches.Events;
public static class SabotageEventPatches
{
[HarmonyPrefix]
[HarmonyPatch(typeof(ShipStatus), nameof(ShipStatus.UpdateSystem), typeof(SystemTypes), typeof(byte))]
public static bool ShipStatusUpdateSystemPrefix(ShipStatus __instance, SystemTypes systemType, byte amount)
[HarmonyPatch(typeof(ShipStatus), nameof(ShipStatus.UpdateSystem), typeof(SystemTypes), typeof(PlayerControl), typeof(byte))]
public static bool ShipStatusUpdateSystemPrefix(ShipStatus __instance, SystemTypes systemType, PlayerControl player, byte amount)
{
var @event = new UpdateSystemEvent(systemType, amount);
var @event = new UpdateSystemEvent(systemType, player, amount);
MiraEventManager.InvokeEvent(@event);
return !@event.IsCancelled;
}
+52 -9
View File
@@ -1,6 +1,10 @@
using HarmonyLib;
using System.Linq;
using BepInEx.Unity.IL2CPP.Utils.Collections;
using HarmonyLib;
using MiraAPI.Events;
using MiraAPI.Events.Vanilla;
using MiraAPI.Utilities;
using Reactor.Utilities;
namespace MiraAPI.Patches.Events;
@@ -10,19 +14,58 @@ namespace MiraAPI.Patches.Events;
[HarmonyPatch]
public static class VentEventPatches
{
[HarmonyPrefix]
[HarmonyPatch(typeof(Vent), nameof(Vent.EnterVent))]
public static void EnterVentPrefix(Vent __instance, PlayerControl pc)
private static bool _showButtons;
[HarmonyPostfix]
[HarmonyPatch(typeof(VentButton), nameof(VentButton.DoClick))]
public static void VentUsePostfix(VentButton __instance)
{
var @event = new EnterVentEvent(pc, __instance);
MiraEventManager.InvokeEvent(@event);
__instance.currentTarget?.SetButtons(_showButtons);
}
[HarmonyPrefix]
[HarmonyPatch(typeof(Vent), nameof(Vent.ExitVent))]
public static void ExitVentPrefix(Vent __instance, PlayerControl pc)
[HarmonyPatch(typeof(PlayerPhysics), nameof(PlayerPhysics.CoEnterVent))]
public static bool EnterVentPrefix(PlayerPhysics __instance, int id, ref Il2CppSystem.Collections.IEnumerator __result)
{
var @event = new ExitVentEvent(pc, __instance);
var pc = __instance.myPlayer;
var vent = ShipStatus.Instance.AllVents.FirstOrDefault(v => v.Id == id);
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;
}
return !@event.IsCancelled;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerPhysics), nameof(PlayerPhysics.CoExitVent))]
public static bool ExitVentPrefix(PlayerPhysics __instance, int id, ref Il2CppSystem.Collections.IEnumerator __result)
{
var pc = __instance.myPlayer;
var vent = ShipStatus.Instance.AllVents.First(v => v.Id == id);
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;
}
return !@event.IsCancelled;
}
}
+11 -1
View File
@@ -1,4 +1,5 @@
using MiraAPI.Roles;
using System.Collections;
using MiraAPI.Roles;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
@@ -14,6 +15,15 @@ namespace MiraAPI.Utilities;
/// </summary>
public static class Helpers
{
/// <summary>
/// Returns an empty coroutine.
/// </summary>
/// <returns>Empty coroutine.</returns>
public static IEnumerator EmptyCoroutine()
{
yield break;
}
/// <summary>
/// Creates a ContactFilter2D from a layer mask.
/// </summary>