From 7e1374d96ab4503a67512f944b5c4062b7ad2223 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Thu, 3 Jul 2025 18:07:18 +0300 Subject: [PATCH] Adjusted ActionQueue killing logic --- .../npcs/controllers/data/action/Action.java | 20 ++++-- .../data/action/ActionManager.java | 15 ++++- .../controllers/data/action/ActionQueue.java | 65 ++++++++++++++----- .../data/action/action/ConditionalAction.java | 5 +- 4 files changed, 80 insertions(+), 25 deletions(-) diff --git a/src/main/java/noppes/npcs/controllers/data/action/Action.java b/src/main/java/noppes/npcs/controllers/data/action/Action.java index 8c12da222..29d4dfeb5 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/Action.java +++ b/src/main/java/noppes/npcs/controllers/data/action/Action.java @@ -186,6 +186,10 @@ public class Action implements IAction { @Override public void markDone() { done = true; + + if (isThreaded && actionThread.inActionThread()) { + queue.cancel(this); + } } @Override @@ -335,13 +339,17 @@ public class Action implements IAction { return befores; } - protected void scheduleAllBefore(Deque actionQueue) { + protected void scheduleAllBefore(Deque deque) { allUnscheduledBefore().forEach((bef) -> { if (bef.unscheduledAfter != null) bef.unscheduledAfter.unscheduledBefore = null; bef.unscheduledAfter = null; - actionQueue.addLast(bef); + deque.addLast(bef); + + if (bef.queue != queue) + bef.queue = queue; + bef.isScheduled = true; }); } @@ -357,13 +365,17 @@ public class Action implements IAction { return afters; } - protected void scheduleAllAfter(Deque actionQueue) { + protected void scheduleAllAfter(Deque deque) { allUnscheduledAfter().forEach((aft) -> { if (aft.unscheduledBefore != null) aft.unscheduledBefore.unscheduledAfter = null; aft.unscheduledBefore = null; - actionQueue.addLast(aft); + deque.addLast(aft); + + if (aft.queue != queue) + aft.queue = queue; + aft.isScheduled = true; }); } diff --git a/src/main/java/noppes/npcs/controllers/data/action/ActionManager.java b/src/main/java/noppes/npcs/controllers/data/action/ActionManager.java index 478decb46..9322fd836 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/ActionManager.java +++ b/src/main/java/noppes/npcs/controllers/data/action/ActionManager.java @@ -8,6 +8,7 @@ import noppes.npcs.api.handler.data.actions.IConditionalAction; import noppes.npcs.controllers.data.action.action.ConditionalAction; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.function.Consumer; import java.util.function.Function; @@ -145,8 +146,7 @@ public class ActionManager implements IActionManager { if (queue == null) return false; - otherQueues.remove(name); - queue.clear(); + queue.kill(); return true; } @@ -385,7 +385,16 @@ public class ActionManager implements IActionManager { conditionalQueue.tick(ticksExisted); // ─── Other Queues ───────────────────────────────────────── - otherQueues.forEach((name, queue) -> ((ActionQueue) queue).tick(ticksExisted)); + Iterator it = otherQueues.values().iterator(); + while (it.hasNext()) { + IActionQueue other = it.next(); + ((ActionQueue) other).tick(ticksExisted); + + if (other.isKilled()) { + other.clear(); + it.remove(); + } + } } @Override diff --git a/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java b/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java index 56a5fa6bc..a068121dc 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java +++ b/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java @@ -14,11 +14,15 @@ public class ActionQueue implements IActionQueue { protected final ActionManager manager; protected final String name; - protected boolean isWorking; + protected boolean isWorking = true; protected boolean isParallel; + + protected boolean kill; protected boolean killWhenEmpty; protected int killAfterTicks = 100; + private Action autoKill; + public ActionQueue(ActionManager manager, String name) { this.manager = manager; this.name = name; @@ -87,6 +91,17 @@ public class ActionQueue implements IActionQueue { return this; } + @Override + public boolean isKilled() { + return kill; + } + + @Override + public IActionQueue kill() { + this.kill = true; + return this; + } + /////////////////////////////////////////////////// /////////////////////////////////////////////////// // Schedulers @@ -95,20 +110,19 @@ public class ActionQueue implements IActionQueue { public IAction schedule(IAction action) { Action act = (Action) action; - if (!isParallel && act.unscheduledBefore != null) - act.scheduleAllBefore(queue); - - - queue.addLast(action); - - - if (!isParallel && act.unscheduledAfter != null) - act.scheduleAllAfter(queue); - if (act.queue != this) act.setQueue(this); act.isScheduled = true; + + if (!isParallel && act.unscheduledBefore != null) + act.scheduleAllBefore(queue); + + queue.addLast(action); + + if (!isParallel && act.unscheduledAfter != null) + act.scheduleAllAfter(queue); + return action; } @@ -176,6 +190,10 @@ public class ActionQueue implements IActionQueue { /////////////////////////////////////////////////// // Queue Data + public boolean hasActiveTasks() { + return !queue.isEmpty(); + } + @Override public int getIndex(IAction action) { int i = 0; @@ -218,7 +236,12 @@ public class ActionQueue implements IActionQueue { @Override public boolean cancel(IAction action) { - return cancel(action.getName()); + if (queue.remove(action)) { + action.kill(); + return true; + } + + return false; } @Override @@ -243,6 +266,7 @@ public class ActionQueue implements IActionQueue { if (!isWorking) return; + killWhenEmpty(ticksExisted); if (!isParallel) { IAction current = getCurrentAction(); @@ -265,15 +289,22 @@ public class ActionQueue implements IActionQueue { } } } + } - if (killWhenEmpty && queue.isEmpty()) - schedule(killAfterTicks, (act) -> { - if (!queue.isEmpty()) - act.markDone(); + protected void killWhenEmpty(int ticksExisted) { + if (killWhenEmpty && !hasActiveTasks() && autoKill == null) + autoKill = (Action) manager.create(killAfterTicks, (act) -> { + if (hasActiveTasks()) + return; manager.removeQueue(name); - }); + }).updateEvery(1).once(); + if (autoKill != null) { + autoKill.tick(ticksExisted); + if (autoKill.isDone()) + autoKill = null; + } } @Override diff --git a/src/main/java/noppes/npcs/controllers/data/action/action/ConditionalAction.java b/src/main/java/noppes/npcs/controllers/data/action/action/ConditionalAction.java index b2e81d344..73a326eab 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/action/ConditionalAction.java +++ b/src/main/java/noppes/npcs/controllers/data/action/action/ConditionalAction.java @@ -94,7 +94,8 @@ public class ConditionalAction extends Action implements IConditionalAction { @Override public void tick(int ticksExisted) { - if (done) + updateEveryXTick = 10; + if (isDone()) return; if (maxChecks > -1 && checkCount > maxChecks || maxCount == 0) { @@ -103,6 +104,7 @@ public class ConditionalAction extends Action implements IConditionalAction { } if (ticksExisted % updateEveryXTick == 0) { + System.out.println("start"); checkCount++; Runnable execute = () -> { @@ -114,6 +116,7 @@ public class ConditionalAction extends Action implements IConditionalAction { executeOnTermination(); markDone(); } + System.out.println("exec'd!"); }; if (isThreaded)