Adjusted ActionQueue killing logic

This commit is contained in:
bigguy345 committed 2025-07-03 18:07:18 +03:00
1 parent ad4aac1e43
commit 7e1374d96a
4 files changed
+80 -25

No files matched your search

@@ -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<IAction> actionQueue) {
protected void scheduleAllBefore(Deque<IAction> 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<IAction> actionQueue) {
protected void scheduleAllAfter(Deque<IAction> 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;
});
}
@@ -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<IActionQueue> it = otherQueues.values().iterator();
while (it.hasNext()) {
IActionQueue other = it.next();
((ActionQueue) other).tick(ticksExisted);
if (other.isKilled()) {
other.clear();
it.remove();
}
}
}
@Override
@@ -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
@@ -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)