Class CompositeTaskNode<C>

java.lang.Object
privbot.task.CompositeTaskNode<C>
Type Parameters:
C - the context type shared across all nodes in the tree
All Implemented Interfaces:
TaskNode<C>

public class CompositeTaskNode<C> extends Object implements TaskNode<C>
A task node that holds child nodes and delegates execution using a strategy.

Two strategies are available:

Disabled children (TaskNode.isEnabled() returning false) are skipped. Exceptions thrown by children are caught and logged, allowing the tree to continue.

Usage Example (Priority):


 CompositeTaskNode<MyContext> root = new CompositeTaskNode<>("Combat", Strategy.PRIORITY);
 root.addNode(new EatFoodTask());       // priority 90
 root.addNode(new PrayerSwitchTask());  // priority 85
 root.addNode(new AttackNpcTask());     // priority 50
 // EatFood checked first, then PrayerSwitch, then Attack
 

Usage Example (Sequence):


 CompositeTaskNode<MyContext> root = new CompositeTaskNode<>("Bank Run", Strategy.SEQUENCE);
 root.addNode(new WalkToBankTask());
 root.addNode(new DepositLootTask());
 root.addNode(new WithdrawSuppliesTask());
 root.addNode(new WalkToSpotTask());
 // Runs in order; each must complete before the next starts
 

Nesting:

Composites can contain other composites, forming a tree:

 CompositeTaskNode<Ctx> root = new CompositeTaskNode<>("Main", Strategy.PRIORITY);
 root.addNode(emergencyComposite);  // priority 100 — eat/teleport
 root.addNode(combatComposite);     // priority 50  — fight loop
 root.addNode(bankingSequence);     // priority 10  — bank run
 
See Also:
  • Constructor Details

    • CompositeTaskNode

      public CompositeTaskNode(String name, CompositeTaskNode.Strategy strategy, int priority)
      Creates a composite task node.
      Parameters:
      name - human-readable name for logging and overlay display
      strategy - execution strategy for children
      priority - priority of this composite within its parent
    • CompositeTaskNode

      public CompositeTaskNode(String name, CompositeTaskNode.Strategy strategy)
      Creates a composite task node with default priority (0).
      Parameters:
      name - human-readable name
      strategy - execution strategy for children
  • Method Details

    • addNode

      public CompositeTaskNode<C> addNode(TaskNode<C> child)
      Adds a child task node.
      Parameters:
      child - the task node to add
      Returns:
      this composite, for chaining
    • removeNode

      public boolean removeNode(TaskNode<C> child)
      Removes a child task node.
      Parameters:
      child - the task node to remove
      Returns:
      true if the child was found and removed
    • getChildren

      public List<TaskNode<C>> getChildren()
      Returns an unmodifiable view of the children.
    • getActiveNode

      public TaskNode<C> getActiveNode()
      Returns the currently active child (the one that last executed), or null.
    • shouldExecute

      public boolean shouldExecute(C ctx)
      CompositeTaskNode.Strategy.PRIORITY: true when any enabled child validates (checked in insertion order). CompositeTaskNode.Strategy.SEQUENCE: true while the sequence has children left, without asking them.
      Specified by:
      shouldExecute in interface TaskNode<C>
      Parameters:
      ctx - the shared context containing state and dependencies
      Returns:
      true if the task should execute, false otherwise
    • execute

      public boolean execute(C ctx)
      CompositeTaskNode.Strategy.PRIORITY: runs the highest-priority enabled child that validates; if it throws, the next candidate is tried. CompositeTaskNode.Strategy.SEQUENCE: skips children that report TaskNode.isComplete(C), then runs the current child if it validates and advances once it is complete. A disabled child in a sequence is skipped. Returns the executed child's result, or false when nothing ran.
      Specified by:
      execute in interface TaskNode<C>
      Parameters:
      ctx - the shared context
      Returns:
      true if the task performed an action (blocks sibling tasks from executing this tick), false otherwise
    • isComplete

      public boolean isComplete(C ctx)
      CompositeTaskNode.Strategy.PRIORITY: complete when no enabled child validates. CompositeTaskNode.Strategy.SEQUENCE: complete once every child has been passed.
      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()
      Rewinds a sequence to its first child, clears the active node and resets every child.
      Specified by:
      reset in interface TaskNode<C>
    • cleanup

      public void cleanup()
      Propagates cleanup to all children.
      Specified by:
      cleanup in interface TaskNode<C>
    • getPriority

      public int getPriority()
      The priority given to the constructor (0 by default).
      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
    • getStatus

      public String getStatus(C ctx)
      "name > activeChildStatus" while a child is active, otherwise the name.
      Specified by:
      getStatus in interface TaskNode<C>
      Parameters:
      ctx - the shared context
      Returns:
      a brief status description. Defaults to TaskNode.getName().