From f662d8416e67ddc655ef31dbf6d51089d22855c6 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Sun, 29 Jun 2025 21:57:31 +0300 Subject: [PATCH] Implemented actionThread to IConditionalActions --- src/api | 2 +- .../data/action/ConditionalAction.java | 77 ++++++++++++------- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/api b/src/api index 65c81997b..34ed09542 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit 65c81997b9d552d39ed1005975f9e9824eebccf6 +Subproject commit 34ed095423eda1a1289ba10cbe07e2d93875d0a6 diff --git a/src/main/java/noppes/npcs/controllers/data/action/ConditionalAction.java b/src/main/java/noppes/npcs/controllers/data/action/ConditionalAction.java index 9baf1d999..522b626ad 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/ConditionalAction.java +++ b/src/main/java/noppes/npcs/controllers/data/action/ConditionalAction.java @@ -47,41 +47,64 @@ public class ConditionalAction extends Action implements IConditionalAction { @Override public void tick(int ticksExisted) { - if (isDone()) return; + if (done) + return; - try { - if (ticksExisted % updateEveryXTick == 0) { - checkCount++; - boolean terminated = terminateWhen != null && terminateWhen.get(); + if (maxChecks > -1 && checkCount > maxChecks) { + markDone(); + return; + } - if (terminated && onTermination != null) { - try { - onTermination.accept(this); - } catch (Throwable t) { - System.err.println("Error in onTermination for conditional '" + getName() + "':"); - t.printStackTrace(); - } - } - - if ((maxChecks >= 0 && checkCount > maxChecks) || terminated) { - markDone(); - return; - } - - if (condition != null && condition.get()) { - task.accept(this); - taskExecuted = true; - } + if (ticksExisted % updateEveryXTick == 0) { + if (condition != null && condition.get()) { + if (isThreaded) + actionThread.execute("task", this::executeTask); + else + executeTask(); } + + if (isTerminated()) { + if (onTermination != null) { + if (isThreaded) + actionThread.execute("onTermination", this::executeOnTermination); + else + executeOnTermination(); + } + markDone(); + return; + } + + checkCount++; + } + duration++; + } + + protected void executeTask() { + try { + task.accept(this); + count++; + taskExecuted = true; } catch (Throwable t) { - System.err.println("Scripted conditional '" + getName() + "' threw an exception:"); + System.err.println("IConditionalAction '" + getName() + "' threw an exception:"); t.printStackTrace(); markDone(); - } finally { - duration++; } } + protected void executeOnTermination() { + try { + onTermination.accept(this); + } catch (Throwable t) { + System.err.println("IConditionalAction's onTermination '" + getName() + "' threw an exception:"); + t.printStackTrace(); + } + } + + @Override + public boolean isTerminated() { + return terminateWhen != null && terminateWhen.get(); + } + @Override public IConditionalAction setMaxChecks(int maxChecks) { this.maxChecks = maxChecks; @@ -123,7 +146,7 @@ public class ConditionalAction extends Action implements IConditionalAction { @Override public IConditionalAction after(IConditionalAction after) { - return ((ScriptedActionManager) manager).scheduleAction(after); + return manager.scheduleAction(after); } @Override