From 06d73d7f11dbe797a9ead091d2476e02a0fef903 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Mon, 28 Apr 2025 09:57:24 +0300 Subject: [PATCH] Added termination task to ConditionalActions (code that runs when task termination predicate is satisfied) --- .../npcs/api/handler/IActionManager.java | 79 +++++++++++++++---- .../data/actions/IConditionalAction.java | 29 ++++++- 2 files changed, 93 insertions(+), 15 deletions(-) diff --git a/java/noppes/npcs/api/handler/IActionManager.java b/java/noppes/npcs/api/handler/IActionManager.java index e821c2d..846614f 100644 --- a/java/noppes/npcs/api/handler/IActionManager.java +++ b/java/noppes/npcs/api/handler/IActionManager.java @@ -21,7 +21,7 @@ public interface IActionManager { * @param name a unique name for this action * @param maxDuration the maximum lifetime of the action in ticks * @param startAfterTicks number of ticks to wait before the first run - * @param action logic to execute each time the action “fires” + * @param action code to execute each time the action “fires” * @return a fresh {@link IAction} object */ IAction create(String name, int maxDuration, int startAfterTicks, Consumer action); @@ -34,13 +34,68 @@ public interface IActionManager { IAction create(Consumer t); + /** + * Schedule a conditional action that gives up after at most maxChecks attempts. + * + * @param predicate supplier checked every tick, if it returns true, task is fired + * @param task code to run once predicate first becomes true + * @return the action scheduled + */ IConditionalAction create(Supplier predicate, Consumer task); + /** + * Schedule a conditional action that gives up after at most maxChecks attempts. + * + * @param name unique name + * @param predicate supplier checked every tick, if it returns true, task is fired + * @param task code to run once predicate first becomes true + * @return the action scheduled + */ IConditionalAction create(String name, Supplier predicate, Consumer task); - IConditionalAction create(Supplier predicate, Supplier terminate, Consumer task); + /** + * Schedule a conditional action that gives up after at most maxChecks attempts. + * + * @param predicate supplier checked every tick, if it returns true, task is fired + * @param terminateWhen supplier checked every tick, if it returns true, action is terminated (gets marked done) + * @param task code to run once predicate first becomes true + * @return the action scheduled + */ + IConditionalAction create(Supplier predicate, Supplier terminateWhen, Consumer task); - IConditionalAction create(String name, Supplier predicate, Supplier terminate, Consumer task); + /** + * Schedule a conditional action that gives up after at most maxChecks attempts. + * + * @param name unique name + * @param predicate supplier checked every tick, if it returns true, task is fired + * @param terminateWhen supplier checked every tick, if it returns true, action is terminated (gets marked done) + * @param task code to run once predicate first becomes true + * @return the action scheduled + */ + IConditionalAction create(String name, Supplier predicate, Supplier terminateWhen, Consumer task); + + /** + * Schedule a conditional action that gives up after at most maxChecks attempts. + * + * @param predicate supplier checked every tick, if it returns true, task is fired + * @param terminateWhen supplier checked every tick, if it returns true, action is terminated (gets marked done) + * @param onTermination code to run when the termination predicate returns true + * @param task code to run once predicate first becomes true + * @return the action scheduled + */ + IConditionalAction create(Supplier predicate, Supplier terminateWhen, Consumer onTermination, Consumer task); + + /** + * Schedule a conditional action that gives up after at most maxChecks attempts. + * + * @param name unique name + * @param predicate supplier checked every tick, if it returns true, task is fired + * @param terminateWhen supplier checked every tick, if it returns true, action is terminated (gets marked done) + * @param onTermination code to run when the termination predicate returns true + * @param task code to run once predicate first becomes true + * @return the action scheduled + */ + IConditionalAction create(String name, Supplier predicate, Supplier terminateWhen, Consumer onTermination, Consumer task); /** * Begin processing scheduled actions. Must be called once. @@ -67,7 +122,7 @@ public interface IActionManager { * @param name a unique name for this action * @param maxDuration the maximum lifetime of the action in ticks * @param startAfterTicks number of ticks to wait before the first run - * @param action logic to execute each time the action “fires” + * @param action code to execute each time the action “fires” * @return the action scheduled */ IAction scheduleAction(String name, int maxDuration, int startAfterTicks, Consumer action); @@ -108,10 +163,8 @@ public interface IActionManager { * Schedule a conditional action that gives up after at most maxChecks attempts. * * @param name unique name - * checkIntervalTicks ticks between predicate tests - * @param predicate supplier returning true when it should fire - * @param task logic to run once when predicate first becomes true - * @ maxChecks maximum times to test predicate before auto‐cancelling + * @param predicate supplier checked every tick, if it returns true, task is fired + * @param task code to run once predicate first becomes true * @return the action scheduled */ IConditionalAction scheduleAction(String name, Supplier predicate, Consumer task); @@ -120,11 +173,9 @@ public interface IActionManager { * Schedule a conditional action that gives up after at most maxChecks attempts. * * @param name unique name - * @ checkIntervalTicks ticks between predicate tests - * @param predicate supplier returning true when it should fire - * @param terminateWhen supplier returning true when it should terminate (gets marked done) - * @param task logic to run once when predicate first becomes true - * @ maxChecks maximum times to test predicate before auto‐cancelling + * @param predicate supplier checked every tick, if it returns true, task is fired + * @param terminateWhen supplier checked every tick, if it returns true, action is terminated (gets marked done) + * @param task code to run once predicate first becomes true * @return the action scheduled */ IConditionalAction scheduleAction(String name, Supplier predicate, Supplier terminateWhen, Consumer task); @@ -163,7 +214,7 @@ public interface IActionManager { * * @param name a unique name for this action * @param startAfterTicks delay before it runs (in ticks) - * @param task logic to execute once + * @param task code to execute once */ default void addSingleTask(String name, int startAfterTicks, Consumer task) { IAction action = create(name, 1, startAfterTicks, act -> { diff --git a/java/noppes/npcs/api/handler/data/actions/IConditionalAction.java b/java/noppes/npcs/api/handler/data/actions/IConditionalAction.java index ba41d88..acea284 100644 --- a/java/noppes/npcs/api/handler/data/actions/IConditionalAction.java +++ b/java/noppes/npcs/api/handler/data/actions/IConditionalAction.java @@ -8,7 +8,30 @@ import java.util.function.Supplier; public interface IConditionalAction extends IAction { /** - * + * @param predicate supplier checked every tick, if it returns true, task is fired + * @return + */ + IConditionalAction setPredicate(Supplier predicate); + + /** + * @param terminateWhen supplier checked every tick, if it returns true, action is terminated (gets marked done) + * @return this action + */ + IConditionalAction setTerminationPredicate(Supplier terminateWhen); + + /** + * @param onTermination code to run when the termination predicate returns true + * @return this action + */ + IConditionalAction setTerminationTask(Consumer onTermination); + + /** + * @return true if predicate was satisfied and task ran (i.e can be called in termination task to see if original task was executed + * then do code based on that, if not return early) + */ + boolean wasTaskExecuted(); + + /** * @param maxChecks maximum times to test predicate before auto‐cancelling * @return this action */ @@ -23,4 +46,8 @@ public interface IConditionalAction extends IAction { IConditionalAction after(Supplier predicate, Supplier terminate, Consumer task); IConditionalAction after(String name, Supplier predicate, Supplier terminate, Consumer task); + + IConditionalAction after(Supplier predicate, Supplier terminateWhen, Consumer onTermination, Consumer task); + + IConditionalAction after(String name, Supplier predicate, Supplier terminateWhen, Consumer onTermination, Consumer task); }