From c1cfcc19d59c9a075f501b44eb8156848193e603 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Wed, 2 Jul 2025 21:14:58 +0300 Subject: [PATCH] Implemented ActionQueue to Action --- .../npcs/controllers/data/action/Action.java | 101 ++++++++++++------ .../controllers/data/action/ActionQueue.java | 3 + 2 files changed, 74 insertions(+), 30 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 50b85198e..bebb138e7 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/Action.java +++ b/src/main/java/noppes/npcs/controllers/data/action/Action.java @@ -1,6 +1,7 @@ package noppes.npcs.controllers.data.action; import noppes.npcs.api.handler.data.IAction; +import noppes.npcs.api.handler.data.IActionQueue; import noppes.npcs.api.handler.data.actions.IConditionalAction; import noppes.npcs.controllers.ScriptContainer; import noppes.npcs.scripted.CustomNPCsException; @@ -15,6 +16,7 @@ import java.util.function.Function; public class Action implements IAction { protected final ActionManager manager; + protected IActionQueue queue; protected boolean isScheduled; protected final String name; protected int startAfterTicks; @@ -65,6 +67,23 @@ public class Action implements IAction { this.maxDuration = maxDuration; } + @Override + public IActionQueue getQueue() { + return queue; + } + + @Override + public IAction setQueue(IActionQueue queue) { + if (this.queue == queue) + return this; + + if (this.queue != null) + this.queue.getQueue().remove(this); + + this.queue = queue; + queue.schedule(this); + return this; + } @Override public IAction setTask(Consumer task) { @@ -289,31 +308,10 @@ public class Action implements IAction { return this; } - @Override - public IAction getNext() { - boolean seenMe = false; - for (IAction a : manager.getActionQueue()) { - if (seenMe) { - return a; - } - if (a == this) { - seenMe = true; - } - } - return null; - } - @Override - public IAction getPrevious() { - IAction prev = null; - for (IAction a : manager.getActionQueue()) { - if (a == this) { - return prev; - } - prev = a; - } - return null; - } + /////////////////////////////////////////////////// + /////////////////////////////////////////////////// + // Sequential protected Action unscheduledBefore, unscheduledAfter; @@ -361,14 +359,46 @@ public class Action implements IAction { aft.isScheduled = true; }); } + + @Override + public IAction getNext() { + if (queue == null) + return null; + + boolean seenMe = false; + for (IAction a : queue.getQueue()) { + if (seenMe) { + return a; + } + if (a == this) { + seenMe = true; + } + } + return null; + } + + @Override + public IAction getPrevious() { + if (queue == null) + return null; + + IAction prev = null; + for (IAction a : queue.getQueue()) { + if (a == this) { + return prev; + } + prev = a; + } + return null; + } @Override public IAction after(IAction after) { if (after instanceof IConditionalAction) return conditional((IConditionalAction) after); - int idx = manager.getIndex(this); + int idx = queue == null ? -1 : queue.getIndex(this); if (idx >= 0) - manager.scheduleActionAt(idx + 1, after); + queue.scheduleActionAt(idx + 1, after); else { Action aft = (Action) after; unscheduledAfter = aft; @@ -423,9 +453,9 @@ public class Action implements IAction { if (before instanceof IConditionalAction) return conditional((IConditionalAction) before); - int idx = manager.getIndex(this); + int idx = queue == null ? -1 : queue.getIndex(this); if (idx >= 0) - manager.scheduleActionAt(Math.max(0, idx), before); + queue.scheduleActionAt(Math.max(0, idx), before); else { Action bef = (Action) before; unscheduledBefore = bef; @@ -464,6 +494,10 @@ public class Action implements IAction { return manager.schedule(after); } + /////////////////////////////////////////////////// + /////////////////////////////////////////////////// + // Conditionals + @Override public void conditional(IConditionalAction... actions) { for (IConditionalAction act : actions) @@ -500,9 +534,16 @@ public class Action implements IAction { return conditional(manager.create(name, condition, task, terminateWhen, onTermination)); } + /////////////////////////////////////////////////// + /////////////////////////////////////////////////// + // Parallels + @Override - public IAction parallel(IAction after) { - return manager.scheduleParallel(after); + public IAction parallel(IAction act) { + if (queue != null && queue.isParallel() && queue != manager.conditionalQueue) + return queue.schedule(act); + + return manager.scheduleParallel(act); } @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 a231f6e3a..a1d06089c 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java +++ b/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java @@ -74,6 +74,9 @@ public class ActionQueue implements IActionQueue { if (!isParallel && act.unscheduledAfter != null) act.scheduleAllAfter(queue); + if (act.queue != this) + act.setQueue(this); + act.isScheduled = true; return action; }