mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-19 10:49:07 +00:00
ActionManager DEBUG MODE
Implemented for Actions
This commit is contained in:
1 parent
25ab7b2f71
commit
5cb5e3da85
6 files changed
+134
-27
No files matched your search
@@ -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);
|
||||
}
|
||||
///////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
@@ -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<String, IActionQueue> 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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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(),
|
||||
|
||||
Reference in new issue
Block a user