From 8d63134a72956d81a1bacabf42bb655deac717d6 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Sun, 6 Jul 2025 23:57:20 +0300 Subject: [PATCH] Improved debugging --- .../npcs/controllers/data/action/Action.java | 43 +++++++++++++------ .../controllers/data/action/ActionList.java | 6 +-- .../controllers/data/action/ActionQueue.java | 5 +-- .../data/action/action/ConditionalAction.java | 22 ++-------- 4 files changed, 35 insertions(+), 41 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 d27efe140..b19748495 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/Action.java +++ b/src/main/java/noppes/npcs/controllers/data/action/Action.java @@ -97,6 +97,13 @@ public class Action implements IAction { return this; } + protected void schedule(IActionQueue queue) { + queue.getQueue().add(this); + + this.queue = queue; + this.isScheduled = true; + } + @Override public boolean isScheduled() { return isScheduled; @@ -248,30 +255,27 @@ public class Action implements IAction { return; } - if (duration % updateEveryXTick == 0 && task != null) { - if (isThreaded) - actionThread.execute("task", this::executeTask); - else - executeTask(); - } + if (duration % updateEveryXTick == 0 && task != null) + execute("task", this::executeTask); if (maxCount > -1 && count >= maxCount) markDone(); } + public void execute(String taskName, Runnable task) { + if (isThreaded) + actionThread.execute(taskName, task); + else + task.run(); + } + protected void executeTask() { try { task.accept(this); count++; } catch (Throwable t) { - String err = "Task of " + this + " threw an exception:"; - - if (reportTo != null) - reportTo.appendConsole(err + "\n" + ExceptionUtils.getStackTrace(t)); - - System.err.println(err); - t.printStackTrace(); + logDebug("Task of " + this + " threw an exception:", t); markDone(); } } @@ -286,6 +290,19 @@ public class Action implements IAction { 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(); + } + 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); } diff --git a/src/main/java/noppes/npcs/controllers/data/action/ActionList.java b/src/main/java/noppes/npcs/controllers/data/action/ActionList.java index 79ad8bcc0..8cee13544 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/ActionList.java +++ b/src/main/java/noppes/npcs/controllers/data/action/ActionList.java @@ -42,11 +42,7 @@ public class ActionList { } public ActionList scheduleAll(IActionQueue queue) { - list.forEach(act -> { - queue.getQueue().add(act); - act.queue = queue; - act.isScheduled = true; - }); + list.forEach(act -> act.schedule(queue)); return this; } 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 d76bfdcb0..1d2f47e26 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java +++ b/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java @@ -116,10 +116,7 @@ public class ActionQueue implements IActionQueue { return action; } - queue.addLast(action); - - act.queue = this; - act.isScheduled = true; + act.schedule(this); return action; } 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 206be0ed0..7d2159915 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 @@ -4,7 +4,6 @@ import noppes.npcs.api.handler.data.IAction; import noppes.npcs.api.handler.data.actions.IConditionalAction; import noppes.npcs.controllers.data.action.Action; import noppes.npcs.controllers.data.action.ActionManager; -import org.apache.commons.lang3.exception.ExceptionUtils; import java.util.function.Consumer; import java.util.function.Function; @@ -118,10 +117,7 @@ public class ConditionalAction extends Action implements IConditionalAction { } }; - if (isThreaded) - actionThread.execute("task", execute); - else - execute.run(); + execute("task", execute); } if (maxCount > -1 && count >= maxCount) @@ -134,13 +130,7 @@ public class ConditionalAction extends Action implements IConditionalAction { count++; taskExecuted = true; } catch (Throwable t) { - String err = "Task of " + this + " threw an exception:"; - - if (reportTo != null) - reportTo.appendConsole(err + "\n" + ExceptionUtils.getStackTrace(t)); - - System.err.println(err); - t.printStackTrace(); + logDebug("Task of " + this + " threw an exception:", t); markDone(); } } @@ -149,13 +139,7 @@ public class ConditionalAction extends Action implements IConditionalAction { try { onTermination.accept(this); } catch (Throwable t) { - String err = "Termination Task of " + this + " threw an exception:"; - - if (reportTo != null) - reportTo.appendConsole(err + "\n" + ExceptionUtils.getStackTrace(t)); - - System.err.println(err); - t.printStackTrace(); + logDebug("Termination Task of " + this + " threw an exception:", t); } }