Improved debugging

This commit is contained in:
bigguy345 committed 2025-07-06 23:57:20 +03:00
1 parent ffd331fbc5
commit 8d63134a72
4 files changed
+35 -41

No files matched your search

@@ -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);
}
@@ -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;
}
@@ -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;
}
@@ -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);
}
}