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>
Direct Known Subclasses:
Child, Parent

public abstract class SimpleNode<C> extends Object implements 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:

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 Details

    • SimpleNode

      protected SimpleNode(String name, int priority)
      Creates a leaf task node. Higher priority wins when multiple nodes are eligible. Equal priorities keep registration order.
      Parameters:
      name - human-readable name for logging and overlay display
      priority - higher = evaluated first
    • SimpleNode

      protected SimpleNode(String name)
      Creates a leaf task node with priority 0.
      Parameters:
      name - human-readable name for logging and overlay display
  • Method Details

    • isComplete

      public boolean isComplete(C ctx)
      Always true: a leaf is complete as soon as it has run. Override for multi-tick work.
      Specified by:
      isComplete in interface TaskNode<C>
      Parameters:
      ctx - the shared context
      Returns:
      true if the task has completed, false if still in progress
    • reset

      public void reset()
      No-op by default. Override to clear per-run state; runners call it on stop and reset.
      Specified by:
      reset in interface TaskNode<C>
    • getPriority

      public int getPriority()
      The priority given to the constructor.
      Specified by:
      getPriority in interface TaskNode<C>
      Returns:
      the task priority (higher = more important). Default is 0.
    • getName

      public String getName()
      The name given to the constructor.
      Specified by:
      getName in interface TaskNode<C>
      Returns:
      the task name
    • isEnabled

      public boolean isEnabled()
      True unless disabled with setEnabled(boolean).
      Specified by:
      isEnabled in interface TaskNode<C>
      Returns:
      true if this node is enabled, false if it should be skipped
    • setEnabled

      public void setEnabled(boolean enabled)
      Enables or disables this node. Disabled nodes are skipped by CompositeTaskNode during execution.
      Parameters:
      enabled - true to enable, false to disable
    • markExecuted

      public void markExecuted()
      Records the current time as the last execution timestamp. Called automatically by CompositeTaskNode when 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

      protected void status(String format, Object... args)
      Sets the node's status with optional formatting and notifies listeners. The status is returned by getStatus(Object) on subsequent calls.
      Parameters:
      format - format string (or plain message if no args)
      args - optional format arguments
    • getStatus

      public String getStatus(C ctx)
      Returns the last status set via status(String, Object...), or falls back to getName().
      Specified by:
      getStatus in interface TaskNode<C>
      Parameters:
      ctx - the shared context
      Returns:
      a brief status description. Defaults to TaskNode.getName().
    • addStatusListener

      public void addStatusListener(Consumer<String> listener)
      Registers a listener that is notified whenever status(String, Object...) is called.
      Parameters:
      listener - the status change listener
    • removeStatusListener

      public void removeStatusListener(Consumer<String> listener)
      Removes a previously registered status listener.
      Parameters:
      listener - the listener to remove
    • handleError

      protected void handleError(Exception e)
      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

      public void cleanup()
      Called when the script shuts down or the task tree is torn down. Override in subclasses to release resources (e.g., close streams, cancel timers).
      Specified by:
      cleanup in interface TaskNode<C>
    • toString

      public String toString()
      Debug form with name, priority and enabled flag.
      Overrides:
      toString in class Object