From defb1349ca4df17a524bc6126e3574e21284f3eb Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 15 Jun 2025 11:59:17 +0200 Subject: [PATCH] Add EventHook parameter to item use scripts Items can be used on Pokemon outside of battle, and we want the user of the library to be able to check for what changed. This allows for example a heal event to be sent back to the user of the library after using an item. --- PkmnLib.Dynamic/BattleFlow/TurnRunner.cs | 2 +- PkmnLib.Dynamic/Models/Pokemon.cs | 9 ++++++--- PkmnLib.Dynamic/ScriptHandling/ItemScript.cs | 6 +++--- PkmnLib.Dynamic/ScriptHandling/PokeballScript.cs | 7 +++---- PkmnLib.Dynamic/ScriptHandling/ScriptExecution.cs | 8 +++++--- Plugins/PkmnLib.Plugin.Gen7/Scripts/Items/HealingItem.cs | 4 ++-- Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/BugBite.cs | 2 +- 7 files changed, 21 insertions(+), 17 deletions(-) diff --git a/PkmnLib.Dynamic/BattleFlow/TurnRunner.cs b/PkmnLib.Dynamic/BattleFlow/TurnRunner.cs index d9cdf3f..1e9f605 100644 --- a/PkmnLib.Dynamic/BattleFlow/TurnRunner.cs +++ b/PkmnLib.Dynamic/BattleFlow/TurnRunner.cs @@ -174,6 +174,6 @@ public static class TurnRunner var side = battle.Sides[itemChoice.TargetSide.Value]; target = side.Pokemon[itemChoice.TargetPosition.Value]; } - itemChoice.Item.RunItemScript(battle.Library.ScriptResolver, target ?? user); + itemChoice.Item.RunItemScript(battle.Library.ScriptResolver, target ?? user, battle.EventHook); } } \ No newline at end of file diff --git a/PkmnLib.Dynamic/Models/Pokemon.cs b/PkmnLib.Dynamic/Models/Pokemon.cs index 43e65d4..3bc9d73 100644 --- a/PkmnLib.Dynamic/Models/Pokemon.cs +++ b/PkmnLib.Dynamic/Models/Pokemon.cs @@ -340,7 +340,8 @@ public interface IPokemon : IScriptSource, IDeepCloneable /// Heals the Pokemon by a specific amount. Unless allow_revive is set to true, this will not /// heal if the Pokemon has 0 health. If the amount healed is 0, this will return false. /// - bool Heal(uint heal, bool allowRevive = false, EventBatchId batchId = default, bool forceHeal = false); + bool Heal(uint heal, bool allowRevive = false, EventBatchId batchId = default, bool forceHeal = false, + EventHook? customEventHook = null); /// /// Restores all PP of the Pokemon. @@ -1118,7 +1119,8 @@ public class PokemonImpl : ScriptSource, IPokemon } /// - public bool Heal(uint heal, bool allowRevive, EventBatchId batchId = default, bool forceHeal = false) + public bool Heal(uint heal, bool allowRevive, EventBatchId batchId = default, bool forceHeal = false, + EventHook? customEventHook = null) { if (IsFainted && !allowRevive) return false; @@ -1137,7 +1139,8 @@ public class PokemonImpl : ScriptSource, IPokemon } var newHealth = CurrentHealth + heal; - BattleData?.Battle.EventHook.Invoke(new HealEvent(this, CurrentHealth, newHealth) + customEventHook ??= BattleData?.Battle.EventHook; + customEventHook?.Invoke(new HealEvent(this, CurrentHealth, newHealth) { BatchId = batchId, }); diff --git a/PkmnLib.Dynamic/ScriptHandling/ItemScript.cs b/PkmnLib.Dynamic/ScriptHandling/ItemScript.cs index f5639e9..28c0149 100644 --- a/PkmnLib.Dynamic/ScriptHandling/ItemScript.cs +++ b/PkmnLib.Dynamic/ScriptHandling/ItemScript.cs @@ -1,3 +1,4 @@ +using PkmnLib.Dynamic.Events; using PkmnLib.Dynamic.Models; using PkmnLib.Static; using PkmnLib.Static.Utils; @@ -55,15 +56,14 @@ public abstract class ItemScript : IDeepCloneable /// /// Handles the use of the item. /// - public virtual void OnUse() + public virtual void OnUse(EventHook eventHook) { } /// /// Handles the use of the item on the given target. /// - /// - public virtual void OnUseWithTarget(IPokemon target) + public virtual void OnUseWithTarget(IPokemon target, EventHook eventHook) { } } \ No newline at end of file diff --git a/PkmnLib.Dynamic/ScriptHandling/PokeballScript.cs b/PkmnLib.Dynamic/ScriptHandling/PokeballScript.cs index dae0942..c8e00e5 100644 --- a/PkmnLib.Dynamic/ScriptHandling/PokeballScript.cs +++ b/PkmnLib.Dynamic/ScriptHandling/PokeballScript.cs @@ -1,3 +1,4 @@ +using PkmnLib.Dynamic.Events; using PkmnLib.Dynamic.Models; using PkmnLib.Static; @@ -19,12 +20,10 @@ public abstract class PokeballScript : ItemScript public abstract byte GetCatchRate(IPokemon target); /// - public override void OnUseWithTarget(IPokemon target) + public override void OnUseWithTarget(IPokemon target, EventHook eventHook) { var battleData = target.BattleData; - if (battleData == null) - return; - battleData.Battle.AttempCapture(battleData.SideIndex, battleData.Position, Item); + battleData?.Battle.AttempCapture(battleData.SideIndex, battleData.Position, Item); } } \ No newline at end of file diff --git a/PkmnLib.Dynamic/ScriptHandling/ScriptExecution.cs b/PkmnLib.Dynamic/ScriptHandling/ScriptExecution.cs index f1e6430..d57a3e7 100644 --- a/PkmnLib.Dynamic/ScriptHandling/ScriptExecution.cs +++ b/PkmnLib.Dynamic/ScriptHandling/ScriptExecution.cs @@ -1,4 +1,5 @@ using System.Runtime.CompilerServices; +using PkmnLib.Dynamic.Events; using PkmnLib.Dynamic.Models; using PkmnLib.Static; @@ -95,7 +96,8 @@ public static class ScriptExecution /// /// Executes a script on an item. /// - public static void RunItemScript(this IItem item, ScriptResolver scriptResolver, IPokemon? target) + public static void RunItemScript(this IItem item, ScriptResolver scriptResolver, IPokemon? target, + EventHook eventHook) { if (!scriptResolver.TryResolveBattleItemScript(item, out var itemScript)) { @@ -110,11 +112,11 @@ public static class ScriptExecution { if (target == null) throw new ArgumentNullException(nameof(target), "Item script requires a target."); - itemScript.OnUseWithTarget(target); + itemScript.OnUseWithTarget(target, eventHook); } else { - itemScript.OnUse(); + itemScript.OnUse(eventHook); } } } \ No newline at end of file diff --git a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Items/HealingItem.cs b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Items/HealingItem.cs index 04991da..dc9cea7 100644 --- a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Items/HealingItem.cs +++ b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Items/HealingItem.cs @@ -34,8 +34,8 @@ public class HealingItem : ItemScript public override bool IsTargetValid(IPokemon target) => !target.IsFainted; /// - public override void OnUseWithTarget(IPokemon target) + public override void OnUseWithTarget(IPokemon target, EventHook eventHook) { - target.Heal(_healAmount); + target.Heal(_healAmount, customEventHook: eventHook); } } \ No newline at end of file diff --git a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/BugBite.cs b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/BugBite.cs index e46528f..01e3efe 100644 --- a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/BugBite.cs +++ b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/BugBite.cs @@ -20,6 +20,6 @@ public class BugBite : Script } _ = target.SetHeldItem(null); - targetHeldItem.RunItemScript(battleData.Battle.Library.ScriptResolver, user); + targetHeldItem.RunItemScript(battleData.Battle.Library.ScriptResolver, user, move.Battle.EventHook); } } \ No newline at end of file