From 5cb5e3da85a066450195272987b9095e46e8fb6d Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Mon, 7 Jul 2025 13:08:51 +0300 Subject: [PATCH] ActionManager DEBUG MODE Implemented for Actions --- src/api | 2 +- .../npcs/controllers/data/action/Action.java | 63 +++++++++++++------ .../data/action/ActionManager.java | 30 ++++++++- .../controllers/data/action/ActionQueue.java | 4 ++ .../controllers/data/action/ActionThread.java | 17 ++++- .../data/action/action/ConditionalAction.java | 45 +++++++++++-- 6 files changed, 134 insertions(+), 27 deletions(-) diff --git a/src/api b/src/api index 14f7bd3dd..aa8ea101b 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit 14f7bd3dd402498e3217aca8d3de26f53340169b +Subproject commit aa8ea101bd9caf3679ebfa02d0cc19c982a535e3 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 8d1e929e8..e98b6c70a 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/Action.java +++ b/src/main/java/noppes/npcs/controllers/data/action/Action.java @@ -6,7 +6,6 @@ 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; -import org.apache.commons.lang3.exception.ExceptionUtils; import java.util.HashMap; import java.util.Map; @@ -84,6 +83,10 @@ public class Action implements IAction { return queue; } + public String getQueueName() { + return queue != null ? queue.getName() : "null"; + } + @Override public IAction setQueue(IActionQueue queue) { if (this.queue == queue) @@ -102,6 +105,9 @@ public class Action implements IAction { this.queue = queue; this.isScheduled = true; + + if (manager.debug) + manager.logDebug(String.format("Scheduled Action '%s' on queue '%s'", name, getQueueName())); } @Override @@ -241,6 +247,9 @@ public class Action implements IAction { @Override public void markDone() { done = true; + + if (manager.debug) + manager.logDebug(String.format("Marked done Action '%s' on queue '%s'", name, getQueueName())); } @Override @@ -270,11 +279,18 @@ public class Action implements IAction { duration++; + if (manager.debug) + manager.logDebug(String.format("Ticking Action '%s' (duration = %s/%s, count = %s/%s) on queue '%s'", name, duration, maxDuration, count, maxCount, getQueueName())); + if (startAfterTicks > 0) { startAfterTicks--; return; } + if (maxDuration > -1 && duration >= maxDuration || maxCount == 0) { + if (manager.debug) + manager.logDebug(String.format("Reached max duration of Action '%s' on queue '%s'", name, getQueueName())); + markDone(); return; } @@ -282,8 +298,12 @@ public class Action implements IAction { if (duration % updateEveryXTick == 0 && task != null) execute("task", this::executeTask); - if (maxCount > -1 && count >= maxCount) + if (maxCount > -1 && count >= maxCount) { + if (manager.debug) + manager.logDebug(String.format("Reached max count of Action '%s' on queue '%s'", name, getQueueName())); + markDone(); + } } @@ -295,29 +315,46 @@ public class Action implements IAction { } protected void executeOnStart() { + if (manager.debug) + manager.logDebug(String.format("Started executing onStart task of Action '%s' on queue '%s'", name, getQueueName())); try { onStart.accept(this); } catch (Throwable t) { - logDebug("Start Task of " + this + " threw an exception:", t); + manager.logDebug("Start Task of " + this + " threw an exception:", t); } + + if (manager.debug) + manager.logDebug(String.format("Finished executing onStart task of Action '%s' on queue '%s'", name, getQueueName())); } protected void executeTask() { + if (manager.debug) + manager.logDebug(String.format("Started executing task of Action '%s' on queue '%s'", name, getQueueName())); + try { task.accept(this); count++; } catch (Throwable t) { - logDebug("Task of " + this + " threw an exception:", t); + manager.logDebug("Task of " + this + " threw an exception:", t); markDone(); } + + if (manager.debug) + manager.logDebug(String.format("Finished executing task of Action '%s' on queue '%s'", name, getQueueName())); } protected void executeOnDone() { + if (manager.debug) + manager.logDebug(String.format("Started executing onDone task of Action '%s' on queue '%s'", name, getQueueName())); + try { onDone.accept(this); } catch (Throwable t) { - logDebug("Done Task of " + this + " threw an exception:", t); + manager.logDebug("Done Task of " + this + " threw an exception:", t); } + + if (manager.debug) + manager.logDebug(String.format("Finished executing onDone task of Action '%s' on queue '%s'", name, getQueueName())); } @@ -329,23 +366,13 @@ public class Action implements IAction { dataStore.clear(); isScheduled = false; done = true; - } - public void logDebug(String err) { - logDebug(err, null); - } - - public void logDebug(String err, Throwable t) { - if (reportTo != null) - reportTo.appendConsole(err + (t != null ? "\n" + ExceptionUtils.getStackTrace(t) : "")); - - System.err.println(err); - if (t != null) - t.printStackTrace(); + if (manager.debug) + manager.logDebug(String.format("Killing Action '%s' on queue '%s'", name, queue != null ? getQueueName() : "null")); } public String toString() { - return String.format("IAction '%s' [queue='%s', scheduled=%s, done=%s, paused=%s, updateEvery=%s, duration=%d/%d, count=%d/%d, threaded=%s]", name != null ? name : "unnamed", queue != null ? queue.getName() : "null", isScheduled, done, isPaused(), updateEveryXTick, duration, maxDuration, count, maxCount, isThreaded); + return String.format("IAction '%s' [queue='%s', scheduled=%s, done=%s, paused=%s, updateEvery=%s, duration=%d/%d, count=%d/%d, threaded=%s]", name != null ? name : "unnamed", getQueueName(), isScheduled, done, isPaused(), updateEveryXTick, duration, maxDuration, count, maxCount, isThreaded); } /////////////////////////////////////////////////// ////////////////////////////////////////////////// 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 95bbeb0de..924fec103 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/ActionManager.java +++ b/src/main/java/noppes/npcs/controllers/data/action/ActionManager.java @@ -5,7 +5,9 @@ import noppes.npcs.api.handler.data.IAction; import noppes.npcs.api.handler.data.IActionChain; import noppes.npcs.api.handler.data.IActionQueue; import noppes.npcs.api.handler.data.actions.IConditionalAction; +import noppes.npcs.controllers.ScriptContainer; import noppes.npcs.controllers.data.action.action.ConditionalAction; +import org.apache.commons.lang3.exception.ExceptionUtils; import java.util.*; import java.util.function.Consumer; @@ -27,6 +29,9 @@ public class ActionManager implements IActionManager { protected final ActionQueue conditionalQueue = new ActionQueue(this, "mainConditional", true); protected final Map otherQueues = new HashMap<>(); + protected boolean debug; + protected ScriptContainer reportTo; + @Override public IActionManager start() { @@ -40,6 +45,17 @@ public class ActionManager implements IActionManager { return this; } + @Override + public boolean inDebugMode() { + return debug; + } + + @Override + public IActionManager setDebugMode(boolean debug) { + this.debug = debug; + return this; + } + /////////////////////////////////////////////////// /////////////////////////////////////////////////// // Creators @@ -424,6 +440,19 @@ public class ActionManager implements IActionManager { otherQueues.forEach((name, queue) -> queue.clear()); } + public void logDebug(String err) { + logDebug(err, null); + } + + public void logDebug(String err, Throwable t) { + if (this.reportTo != null) + this.reportTo.appendConsole(err + (t != null ? "\n" + ExceptionUtils.getStackTrace(t) : "")); + + System.err.println(err); + if (t != null) + t.printStackTrace(); + } + @Override public IActionChain chain() { return sequentialQueue.chain(); @@ -448,5 +477,4 @@ public class ActionManager implements IActionManager { return sb.toString(); } - } 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 627f0bbbf..69d0c5357 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java +++ b/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java @@ -262,7 +262,11 @@ public class ActionQueue implements IActionQueue { if (a.isDone()) { if (a.onDone != null) a.execute("done", a::executeOnDone); + a.kill(); + + if (manager.debug) + a.manager.logDebug(String.format("Removing Action '%s' from queue '%s'", a.name, name)); return true; } diff --git a/src/main/java/noppes/npcs/controllers/data/action/ActionThread.java b/src/main/java/noppes/npcs/controllers/data/action/ActionThread.java index c043be3e5..684031c4e 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/ActionThread.java +++ b/src/main/java/noppes/npcs/controllers/data/action/ActionThread.java @@ -7,7 +7,6 @@ import java.util.Set; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; -import java.util.function.Supplier; public class ActionThread { private Action action; @@ -39,6 +38,10 @@ public class ActionThread { }); executor.shutdown(); + + + if (action.manager.debug) + action.manager.logDebug(String.format("Stopping thread of Action '%s' on queue '%s'", action.name, action.getQueueName())); } public void pauseFor(long millis) { @@ -47,11 +50,17 @@ public class ActionThread { try { threadSleeping = true; + if (action.manager.debug) + action.manager.logDebug(String.format("Sleeping thread of Action '%s' on queue '%s' for %s ticks", action.name, action.getQueueName(), millis / 50)); + Thread.sleep(millis); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { threadSleeping = false; + + if (action.manager.debug) + action.manager.logDebug(String.format("Woken up thread of Action '%s' on queue '%s' after sleeping for %s ticks", action.name, action.getQueueName(), millis / 50)); } } @@ -67,6 +76,9 @@ public class ActionThread { synchronized (lock) { while (threadPaused) { try { + if (action.manager.debug) + action.manager.logDebug(String.format("Pausing thread of Action '%s' on queue '%s'", action.name, action.getQueueName())); + lock.wait(); // Wait until notified } catch (InterruptedException e) { Thread.currentThread().interrupt(); @@ -85,6 +97,9 @@ public class ActionThread { if (threadPaused) { threadPaused = false; lock.notify(); + + if (action.manager.debug) + action.manager.logDebug(String.format("Resumed thread of Action '%s' on queue '%s'", action.name, action.getQueueName())); } if (threadSleeping) 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 2d4cab4cc..bec6b50d0 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 @@ -99,9 +99,26 @@ public class ConditionalAction extends Action implements IConditionalAction { if (duration == 0 && onStart != null) execute("start", this::executeOnStart); + if (isDone()) + return; + duration++; - if (maxChecks > -1 && checkCount > maxChecks || maxCount == 0) { + if (manager.inDebugMode()) + manager.logDebug(String.format("Ticking ConditionalAction '%s' (duration = %s/%s, count = %s/%s, checkCount = %s/%s) on queue '%s'", name, duration, maxDuration, count, maxCount, checkCount, maxChecks, getQueueName())); + + if (maxDuration > -1 && duration >= maxDuration || maxCount == 0) { + if (manager.inDebugMode()) + manager.logDebug(String.format("Reached max duration of Action '%s' on queue '%s'", name, getQueueName())); + + markDone(); + return; + } + + if (maxChecks > -1 && checkCount > maxChecks) { + if (manager.inDebugMode()) + manager.logDebug(String.format("Reached max check count of ConditionalAction '%s' on queue '%s'", name, getQueueName())); + markDone(); return; } @@ -123,34 +140,50 @@ public class ConditionalAction extends Action implements IConditionalAction { execute("task", execute); } - if (maxCount > -1 && count >= maxCount) + if (maxCount > -1 && count >= maxCount) { + if (manager.inDebugMode()) + manager.logDebug(String.format("Reached max count of ConditionalAction '%s' on queue '%s'", name, getQueueName())); + markDone(); + } } protected void executeTask() { + if (manager.inDebugMode()) + manager.logDebug(String.format("Started executing task of ConditionalAction '%s' on queue '%s'", name, getQueueName())); + try { task.accept(this); count++; taskExecuted = true; } catch (Throwable t) { - logDebug("Task of " + this + " threw an exception:", t); + manager.logDebug("Task of " + this + " threw an exception:", t); markDone(); } + + if (manager.inDebugMode()) + manager.logDebug(String.format("Finished executing task of ConditionalAction '%s' on queue '%s'", name, getQueueName())); + } protected void executeOnTermination() { + if (manager.inDebugMode()) + manager.logDebug(String.format("Started executing onTermination task of ConditionalAction '%s' on queue '%s'", name, getQueueName())); + try { onTermination.accept(this); } catch (Throwable t) { - logDebug("Termination Task of " + this + " threw an exception:", t); + manager.logDebug("Termination Task of " + this + " threw an exception:", t); } + + if (manager.inDebugMode()) + manager.logDebug(String.format("Finished executing onTermination task of ConditionalAction '%s' on queue '%s'", name, getQueueName())); } public String toString() { return String.format("IConditionalAction '%s' [queue='%s', scheduled=%s, done=%s, paused=%s, updateEvery=%s, duration=%d/%d, count=%d/%d, checks=%d/%d, taskExecuted=%s, threaded=%s]", - name != null ? name : "unnamed", - queue != null ? queue.getName() : "null", + name != null ? name : "unnamed", getQueueName(), isScheduled, done, isPaused(),