Add EventHook parameter to item use scripts
All checks were successful
Build / Build (push) Successful in 47s

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.
This commit is contained in:
2025-06-15 11:59:17 +02:00
parent f5d18d7186
commit defb1349ca
7 changed files with 21 additions and 17 deletions

View File

@@ -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
/// <summary>
/// Handles the use of the item.
/// </summary>
public virtual void OnUse()
public virtual void OnUse(EventHook eventHook)
{
}
/// <summary>
/// Handles the use of the item on the given target.
/// </summary>
/// <param name="target"></param>
public virtual void OnUseWithTarget(IPokemon target)
public virtual void OnUseWithTarget(IPokemon target, EventHook eventHook)
{
}
}

View File

@@ -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);
/// <inheritdoc />
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);
}
}

View File

@@ -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
/// <summary>
/// Executes a script on an item.
/// </summary>
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);
}
}
}