From 3e63a9ec9454efab6919ed9c94af7d2cf028cc71 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Sun, 27 Apr 2025 07:33:50 +0300 Subject: [PATCH] Added terminateWhen predicate for ConditionalAction --- src/api | 2 +- .../noppes/npcs/scripted/ScriptedActionManager.java | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/api b/src/api index f6fac3836..581cf4268 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit f6fac3836097f42c10a52588f6864c69c57110fe +Subproject commit 581cf4268b0af6a1e6cbb60ccac343cfced8ca13 diff --git a/src/main/java/noppes/npcs/scripted/ScriptedActionManager.java b/src/main/java/noppes/npcs/scripted/ScriptedActionManager.java index afbd2f7a5..6a259c153 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptedActionManager.java +++ b/src/main/java/noppes/npcs/scripted/ScriptedActionManager.java @@ -104,6 +104,11 @@ public class ScriptedActionManager implements IActionManager { scheduleAction(new ConditionalAction(name, checkIntervalTicks, predicate, task, maxChecks)); } + @Override + public void scheduleConditionalAction(String name, int checkIntervalTicks, Supplier predicate, Supplier terminateWhen, Consumer task, int maxChecks) { + scheduleAction(new ConditionalAction(name, checkIntervalTicks, predicate, terminateWhen, task, maxChecks)); + } + /** * Call once per tick from your main loop. * @@ -261,6 +266,7 @@ public class ScriptedActionManager implements IActionManager { private class ConditionalAction extends ActionBase { private final Supplier predicate; + private Supplier terminate; private final int maxChecks; private int checkCount = 0; @@ -275,12 +281,17 @@ public class ScriptedActionManager implements IActionManager { this.maxChecks = maxChecks; } + public ConditionalAction(String name, int checkIntervalTicks, Supplier predicate, Supplier terminate, Consumer task, int maxChecks) { + this(name, checkIntervalTicks, predicate, task, maxChecks); + this.terminate = terminate; + } + @Override public void tick(int ticksExisted) { if (isDone()) return; if (ticksExisted % updateEveryXTick == 0) { checkCount++; - if (maxChecks >= 0 && checkCount > maxChecks) { + if ((maxChecks >= 0 && checkCount > maxChecks) || terminate.get()) { markDone(); return; }