Handle null results in conditional actions

This commit is contained in:
Kam committed 2025-10-31 17:57:13 -04:00
1 parent 8efa1d354d
commit db09c9a44f
1 file changed
+29 -3
@@ -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<IAction, Boolean> 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(),