Class SimpleNode<C>
java.lang.Object
privbot.task.SimpleNode<C>
- Type Parameters:
C- the context type shared across all nodes in the tree
- All Implemented Interfaces:
TaskNode<C>
Abstract convenience base for single-tick task nodes.
Provides sensible defaults so subclasses only need to implement
TaskNode.shouldExecute(Object) and TaskNode.execute(Object).
By default, leaf tasks are considered complete immediately after execution
and reset() is a no-op.
For multi-tick tasks (walking, waiting for animations, etc.), override
isComplete(Object) and reset() as needed.
Rich features ported from the Node Reference framework:
- Enable/disable —
setEnabled(boolean)to dynamically skip nodes. - Execution tracking —
markExecuted()/getTimeSinceLastExecution(). - Status convenience —
status(String, Object...)sets status with logging and notifies listeners. - Status listeners —
addStatusListener(Consumer)for observer pattern. - Error handling —
handleError(Exception)hook for subclass customization. - Cleanup lifecycle —
cleanup()for resource release on shutdown.
Usage Example:
public class EatFoodTask extends SimpleNode<MyContext> {
public EatFoodTask() {
super("Eat Food", 80);
}
@Override
public boolean shouldExecute(MyContext ctx) {
return ctx.skills.getHitpoints() < 30 && ctx.inventory.contains("Shark");
}
@Override
public boolean execute(MyContext ctx) {
return ctx.inventory.eatFirst("Shark");
}
}
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSimpleNode(String name) Creates a leaf task node with priority0.protectedSimpleNode(String name, int priority) Creates a leaf task node. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddStatusListener(Consumer<String> listener) Registers a listener that is notified wheneverstatus(String, Object...)is called.voidcleanup()Called when the script shuts down or the task tree is torn down.longReturns the timestamp (millis) of the last execution, or 0 if never executed.getName()The name given to the constructor.intThe priority given to the constructor.Returns the last status set viastatus(String, Object...), or falls back togetName().longReturns milliseconds since last execution, or -1 if never executed.protected voidCalled when an exception occurs during execution.booleanisComplete(C ctx) Always true: a leaf is complete as soon as it has run.booleanTrue unless disabled withsetEnabled(boolean).voidRecords the current time as the last execution timestamp.voidremoveStatusListener(Consumer<String> listener) Removes a previously registered status listener.voidreset()No-op by default.voidsetEnabled(boolean enabled) Enables or disables this node.protected voidSets the node's status with optional formatting and notifies listeners.toString()Debug form with name, priority and enabled flag.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface privbot.task.TaskNode
execute, shouldExecute
-
Constructor Details
-
SimpleNode
Creates a leaf task node. Higherprioritywins when multiple nodes are eligible. Equal priorities keep registration order.- Parameters:
name- human-readable name for logging and overlay displaypriority- higher = evaluated first
-
SimpleNode
Creates a leaf task node with priority0.- Parameters:
name- human-readable name for logging and overlay display
-
-
Method Details
-
isComplete
Always true: a leaf is complete as soon as it has run. Override for multi-tick work.- Specified by:
isCompletein interfaceTaskNode<C>- Parameters:
ctx- the shared context- Returns:
- true if the task has completed, false if still in progress
-
reset
-
getPriority
public int getPriority()The priority given to the constructor.- Specified by:
getPriorityin interfaceTaskNode<C>- Returns:
- the task priority (higher = more important). Default is 0.
-
getName
-
isEnabled
public boolean isEnabled()True unless disabled withsetEnabled(boolean). -
setEnabled
public void setEnabled(boolean enabled) Enables or disables this node. Disabled nodes are skipped byCompositeTaskNodeduring execution.- Parameters:
enabled- true to enable, false to disable
-
markExecuted
public void markExecuted()Records the current time as the last execution timestamp. Called automatically byCompositeTaskNodewhen this node executes. -
getLastExecutionTime
public long getLastExecutionTime()Returns the timestamp (millis) of the last execution, or 0 if never executed. -
getTimeSinceLastExecution
public long getTimeSinceLastExecution()Returns milliseconds since last execution, or -1 if never executed. -
status
Sets the node's status with optional formatting and notifies listeners. The status is returned bygetStatus(Object)on subsequent calls.- Parameters:
format- format string (or plain message if no args)args- optional format arguments
-
getStatus
Returns the last status set viastatus(String, Object...), or falls back togetName().- Specified by:
getStatusin interfaceTaskNode<C>- Parameters:
ctx- the shared context- Returns:
- a brief status description. Defaults to
TaskNode.getName().
-
addStatusListener
Registers a listener that is notified wheneverstatus(String, Object...)is called.- Parameters:
listener- the status change listener
-
removeStatusListener
-
handleError
Called when an exception occurs during execution. Subclasses can override to provide custom error handling (e.g., state reset, fallback behavior).Default implementation logs the error via SLF4J.
- Parameters:
e- the exception that occurred
-
cleanup
-
toString
-