From 2319160b5207b1d50f1d82d92032913986794926 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 28 Jun 2025 10:36:43 +0200 Subject: [PATCH] Refactor move selection prevention to use interface --- PkmnLib.Dynamic/Models/Battle.cs | 3 +- PkmnLib.Dynamic/ScriptHandling/Script.cs | 18 +++++++----- .../ScriptHandling/ScriptExecution.cs | 29 ++++++++++++++++++- .../Scripts/Moves/Belch.cs | 4 +-- .../Scripts/Moves/Celebrate.cs | 4 +-- .../Scripts/Moves/LastResort.cs | 4 +-- .../Scripts/Pokemon/DisableEffect.cs | 4 +-- .../Scripts/Pokemon/HealBlockEffect.cs | 4 +-- .../Scripts/Pokemon/ImprisonEffect.cs | 4 +-- .../Scripts/Pokemon/TauntEffect.cs | 4 +-- .../Scripts/Pokemon/ThroatChopEffect.cs | 4 +-- .../Scripts/Pokemon/TormentEffect.cs | 4 +-- 12 files changed, 59 insertions(+), 27 deletions(-) diff --git a/PkmnLib.Dynamic/Models/Battle.cs b/PkmnLib.Dynamic/Models/Battle.cs index fa245b2..11b36ec 100644 --- a/PkmnLib.Dynamic/Models/Battle.cs +++ b/PkmnLib.Dynamic/Models/Battle.cs @@ -331,7 +331,8 @@ public class BattleImpl : ScriptSource, IBattle moveChoice.ChosenMove.MoveData.Target, moveChoice.User)) return false; var preventMove = false; - choice.RunScriptHook(script => script.PreventMoveSelection(moveChoice, ref preventMove)); + choice.RunScriptHookInterface(script => + script.PreventMoveSelection(moveChoice, ref preventMove)); if (preventMove) return false; } diff --git a/PkmnLib.Dynamic/ScriptHandling/Script.cs b/PkmnLib.Dynamic/ScriptHandling/Script.cs index 8ee81e1..1093968 100644 --- a/PkmnLib.Dynamic/ScriptHandling/Script.cs +++ b/PkmnLib.Dynamic/ScriptHandling/Script.cs @@ -61,13 +61,6 @@ public abstract class Script : IDeepCloneable { } - /// - /// Override to customize whether the move can be selected at all. - /// - public virtual void PreventMoveSelection(IMoveChoice choice, ref bool prevent) - { - } - /// /// Force a certain move choice to be selected. If the choice is set, the Pokemon will be forced /// to use it, and will not be able to select any other choice. @@ -854,4 +847,15 @@ public interface IScriptOnInitialize /// with parameters that are passed to it. /// void OnInitialize(IReadOnlyDictionary? parameters); +} + +/// +/// This interface is used to allow scripts to prevent a move from being selected. +/// +public interface IScriptPreventMoveSelection +{ + /// + /// Override to customize whether the move can be selected at all. + /// + void PreventMoveSelection(IMoveChoice choice, ref bool prevent); } \ No newline at end of file diff --git a/PkmnLib.Dynamic/ScriptHandling/ScriptExecution.cs b/PkmnLib.Dynamic/ScriptHandling/ScriptExecution.cs index 4497063..85ff24f 100644 --- a/PkmnLib.Dynamic/ScriptHandling/ScriptExecution.cs +++ b/PkmnLib.Dynamic/ScriptHandling/ScriptExecution.cs @@ -13,7 +13,6 @@ public static class ScriptExecution /// /// Executes a hook on all scripts in a source. /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void RunScriptHook(this IScriptSource source, Action