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 7bcd0ef97..9e608df37 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 @@ -88,7 +88,7 @@ public class ConditionalAction extends Action implements IConditionalAction { @Override public boolean isTerminated() { - return terminateWhen != null && terminateWhen.apply(this); + return checkTermination(); } public void inheritedTick() { @@ -107,10 +107,10 @@ public class ConditionalAction extends Action implements IConditionalAction { checkCount++; Runnable execute = () -> { - if (condition != null && condition.apply(this)) + if (evaluatePredicate(condition, "Condition")) executeTask(); - if (isTerminated()) { + if (checkTermination()) { if (onTermination != null) executeOnTermination(); markDone(); @@ -153,6 +153,32 @@ public class ConditionalAction extends Action implements IConditionalAction { manager.LOGGER.log("Finished executing onTermination task", this); } + private boolean checkTermination() { + return evaluatePredicate(terminateWhen, "Termination condition"); + } + + private boolean evaluatePredicate(Function predicate, String name) { + if (predicate == null) + return false; + + Boolean result; + try { + result = predicate.apply(this); + } catch (Throwable t) { + manager.LOGGER.error(name + " of " + this + " threw an exception:", t); + markDone(); + return false; + } + + if (result == null) { + if (manager.inDebugMode()) + manager.LOGGER.log(name + " returned null; treating as false", this); + return false; + } + + return result; + } + public String toString() { return String.format("%s [queue='%s', scheduled=%s, done=%s, paused=%s, updateEvery=%s, duration=%d/%d, count=%d/%d, checks=%d/%d, taskExecuted=%s, threaded=%s]", getIdentifier(), getQueueName(),