Class TaskNodeRunner<C>
- Type Parameters:
C- the context type shared across all nodes
TaskNodes and executes the highest-priority
valid node each tick. Higher TaskNode.getPriority() wins; equal
priorities keep registration order.
Ordinary plugins construct the runner with the context object, register nodes,
and call start() / onGameTick() / stop(). The runner
owns the ScriptExecutor: selection runs on the client thread and
execution on the script worker. Catalog plugins that already own a worker may
keep selectNode() + executeNode(TaskNode).
Usage:
TaskNodeRunner<MyContext> runner = new TaskNodeRunner<>(context);
runner.addNodes(
new DeathRecoveryTask(),
new EatFoodTask(),
new LootTask(),
new AttackTask());
runner.start();
// From onGameTick:
runner.onGameTick();
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionTaskNodeRunner(C context) Creates a runner that holdscontextfor every tick.TaskNodeRunner(C context, String workerName) TaskNodeRunner(C context, BooleanSupplier runnableCheck) Creates a runner that holdscontextand skips ticks whenrunnableCheckreturns false.TaskNodeRunner(Supplier<C> contextSupplier) Creates a runner with the given context supplier.TaskNodeRunner(Supplier<C> contextSupplier, BooleanSupplier runnableCheck) Creates a runner with a runnable guard. -
Method Summary
Modifier and TypeMethodDescriptionvoidRegisters a node.final voidRegisters tasks.voidRemoves all registered nodes.booleanReturns whether the last tick resulted in a blocking action.booleanexecuteNode(TaskNode<C> node) Executes a previously selected node.Returns the currently active node (the one that last executed), or null.Returns the name of the currently active node, or null if idle.Returns the status of the currently active node, or "Idle".Returns an unmodifiable map of node name to total execution count.Returns an unmodifiable map of node name to last execution duration (ms).intReturns the number of registered nodes.getNodes()Returns an unmodifiable view of all registered nodes.Builds a human-readable statistics report for all registered nodes.longReturns the total number of ticks processed.voidInitializes (or reinitializes) the runner, clearing all tracking state.voidmarkIdle()Clears the active task/status when no node validates during split-thread execution.voidPauses the owned worker and requests a node reset on the next cycle.voidGame-tick pulse.voidResumes after disconnect with a 5-tick grace window.voidonReconnect(int graceTicks) Resumes after disconnect withgraceTicksof idle ticks.voidremoveNode(String name) Removes a node by name.voidreset()Resets all nodes and clears active state.Finds the highest-priority enabled node whoseTaskNode.shouldExecute(C)returns true, without executing it.voidInstalls an optional diagnostics listener for selection and execution events, or clears it withnull.voidsetWorkerPrelude(Runnable workerPrelude) Installs detached observation work that must be serialized with node selection.voidshutdown()Shuts down the runner: stops the owned worker if present and callsTaskNode.cleanup()on all nodes.voidstart()Starts the owned script worker.voidstop()Stops the owned worker (waiting up to 2 s for it to exit), cancels pending HANDS dispatches and resets nodes.booleantick()Executes one tick.
-
Constructor Details
-
TaskNodeRunner
-
TaskNodeRunner
-
TaskNodeRunner
Creates a runner that holdscontextand skips ticks whenrunnableCheckreturns false. -
TaskNodeRunner
Creates a runner with the given context supplier. Register nodes viaaddNode(TaskNode).- Parameters:
contextSupplier- supplies the shared context each tick
-
TaskNodeRunner
Creates a runner with a runnable guard. When the guard returns false,tick()is a no-op.- Parameters:
contextSupplier- supplies the shared context each tickrunnableCheck- returns true when the script is in a runnable state
-
-
Method Details
-
addNode
-
addNodes
Registers tasks. Higher-priority eligible tasks win; equal priorities keep this registration order.- Parameters:
tasks- tasks to register
-
removeNode
Removes a node by name.- Parameters:
name- the name of the node to remove
-
getNodes
-
getActiveNode
-
getNodeCount
public int getNodeCount()Returns the number of registered nodes. -
initialize
public void initialize()Initializes (or reinitializes) the runner, clearing all tracking state. Does not reset nodes; usereset()for that. -
shutdown
public void shutdown()Shuts down the runner: stops the owned worker if present and callsTaskNode.cleanup()on all nodes. -
start
public void start()Starts the owned script worker. Safe to call again while running. Catalog plugins that construct their ownScriptExecutormust not call this. -
stop
public void stop()Stops the owned worker (waiting up to 2 s for it to exit), cancels pending HANDS dispatches and resets nodes. Does not remove registered nodes;start()may follow. -
onGameTick
public void onGameTick()Game-tick pulse. Decrements reconnect grace, then signals the owned worker. No-op while disconnected or whenstart()has never been called.Call from
onGameTickon the client thread. It returns at once: selection and execution happen on the worker. While the forced-move guard holds bot actions the worker runs no node and the status reads"HELD: ...". -
setWorkerPrelude
Installs detached observation work that must be serialized with node selection.The callback runs on this runner's script worker immediately before
GameThread.ensure(Supplier)selects a node. It may marshal bounded reads, but it must not dispatch gameplay actions or sleep. This gives plugins with passive observers one tick pipeline instead of a second worker competing for the client-thread queue. -
onDisconnect
public void onDisconnect()Pauses the owned worker and requests a node reset on the next cycle. -
onReconnect
public void onReconnect()Resumes after disconnect with a 5-tick grace window. -
onReconnect
public void onReconnect(int graceTicks) Resumes after disconnect withgraceTicksof idle ticks. -
tick
public boolean tick()Executes one tick. Iterates all registered nodes, finds the highest-priority node whoseTaskNode.shouldExecute(Object)returns true, and executes it.Only ONE node executes per tick.
Threading: This method runs everything on the calling thread. For script-thread execution (where
sleepUntilmust not block the client), useselectNode()+executeNode(TaskNode)instead.- Returns:
- true if the selected node performed a blocking action
-
setTraceListener
Installs an optional diagnostics listener for selection and execution events, or clears it withnull. Callbacks run on the selecting or executing thread and must return quickly. -
selectNode
Finds the highest-priority enabled node whoseTaskNode.shouldExecute(C)returns true, without executing it. Ties keep registration order; a predicate that throws is logged and skipped. All predicates share oneReadScope, so repeated snapshot reads within this pass are served once.Threading: predicates read game state, so call this on the client thread, e.g.
GameThread.ensure(runner::selectNode)from a worker. The owned worker started bystart()already does this.- Returns:
- the selected node, or null if none validates
-
executeNode
Executes a previously selected node. Call this afterselectNode().Threading: This method runs the node's
execute(), which may callsleepUntil(). It should run on the script thread so that sleeping blocks only the script, not the game.- Parameters:
node- the node to execute (fromselectNode())- Returns:
- true if the node performed a blocking action
-
getActiveTaskName
Returns the name of the currently active node, or null if idle. -
getActiveTaskStatus
Returns the status of the currently active node, or "Idle". -
didLastTickBlock
public boolean didLastTickBlock()Returns whether the last tick resulted in a blocking action. -
getExecutionTimes
-
getExecutionCounts
-
getTotalTicks
public long getTotalTicks()Returns the total number of ticks processed. -
getNodeStats
Builds a human-readable statistics report for all registered nodes. -
reset
public void reset()Resets all nodes and clears active state. -
markIdle
public void markIdle()Clears the active task/status when no node validates during split-thread execution. -
clearNodes
public void clearNodes()Removes all registered nodes.
-