Class TaskChain


  • public class TaskChain
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      TaskChain add​(TaskChain otherChain)
      Adds all tasks from the specified @TaskChain to the current task chain.
      static TaskChain builder​(Context ctx)
      Creates a new instance of TaskChain using the specified Context.
      TaskChain delay​(int ms)
      Adds a delay to the task chain, pausing execution for the specified duration in milliseconds before proceeding to the next task.
      TaskChain delayTicks​(int ticks)
      Adds a delay to the task chain, pausing execution for the specified duration measured in game ticks before proceeding to the next task.
      boolean execute()
      Executes the task chain sequentially, proceeding through each task until all tasks are complete, an error occurs, or the chain is interrupted.
      TaskChain retryUntil​(java.lang.Runnable action, java.util.function.BooleanSupplier successCondition)
      Retries an action until a condition is met using default settings (3 retries, 600ms delay).
      TaskChain retryUntil​(java.lang.Runnable action, java.util.function.BooleanSupplier successCondition, int maxRetries, int retryDelayMs)
      Retries a specific action until a condition is met or the maximum number of retries is reached.
      TaskChain run​(java.lang.Runnable action)
      Adds a task to the chain that executes the specified @Runnable action on the client thread and immediately proceeds to the next task in the chain.
      TaskChain waitUntil​(java.util.function.BooleanSupplier condition)
      Waits until the given condition is met, using default delay and timeout values.
      TaskChain waitUntil​(java.util.function.BooleanSupplier condition, int timeout)
      Waits until the given condition is met or a timeout occurs.
      TaskChain waitUntil​(java.util.function.BooleanSupplier condition, int checkDelayMs, int timeoutMs)
      Waits until the given condition is met or a timeout occurs, checking the condition periodically at the specified interval.
      TaskChain walkTo​(net.runelite.api.coords.WorldArea area)
      Walks to a random reachable point inside a specific WorldArea.
      TaskChain walkTo​(net.runelite.api.coords.WorldPoint target)
      Walks to a specific WorldPoint using the LocalPathfinder and MovementService.
      TaskChain walkTo​(net.runelite.api.coords.WorldPoint target, int radius)
      Walks to an approximate location within a radius of the target.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • builder

        public static TaskChain builder​(Context ctx)
        Creates a new instance of TaskChain using the specified Context.

        This method serves as a static factory for initializing a TaskChain with the given Context. The resulting TaskChain can be used to construct and execute a sequence of tasks.

        Parameters:
        ctx - the Context used to initialize the TaskChain. This context is typically required for operations that depend on thread-safe execution and client interactions. Cannot be null. Passing null will result in a NullPointerException.
        Returns:
        a new TaskChain instance initialized with the specified Context.
      • run

        public TaskChain run​(java.lang.Runnable action)
        Adds a task to the chain that executes the specified @Runnable action on the client thread and immediately proceeds to the next task in the chain.

        The action is executed synchronously on the client thread using the @Context's runOnClientThread method. This ensures the action is performed in a thread-safe manner with respect to client operations.

        Parameters:
        action - the @Runnable task to be executed on the client thread. Cannot be null. Throws a @NullPointerException if null is passed.
        Returns:
        the current instance of @TaskChain, allowing for method chaining of additional tasks.
      • walkTo

        public TaskChain walkTo​(net.runelite.api.coords.WorldPoint target)
        Walks to a specific WorldPoint using the LocalPathfinder and MovementService.

        This method utilizes findSparsePath to calculate an efficient route and processes the path sequentially. It will wait for the player to reach each waypoint (within 1 tile) before proceeding to the next.

        Parameters:
        target - The destination WorldPoint.
        Returns:
        The current TaskChain instance.
      • walkTo

        public TaskChain walkTo​(net.runelite.api.coords.WorldPoint target,
                                int radius)
        Walks to an approximate location within a radius of the target. Useful for banking or interacting with large objects where exact tile precision isn't needed.
        Parameters:
        target - The center WorldPoint.
        radius - The radius to search for a walkable tile.
        Returns:
        The current TaskChain instance.
      • walkTo

        public TaskChain walkTo​(net.runelite.api.coords.WorldArea area)
        Walks to a random reachable point inside a specific WorldArea.
        Parameters:
        area - The WorldArea to walk into.
        Returns:
        The current TaskChain instance.
      • retryUntil

        public TaskChain retryUntil​(java.lang.Runnable action,
                                    java.util.function.BooleanSupplier successCondition,
                                    int maxRetries,
                                    int retryDelayMs)
        Retries a specific action until a condition is met or the maximum number of retries is reached.

        This is useful for "flaky" interactions, such as clicking a bank booth (which might miss) or attacking an NPC (which might be interrupted).

        Parameters:
        action - The Runnable action to perform (e.g., () -> object.interact("Open")). This is automatically executed on the client thread.
        successCondition - A BooleanSupplier that determines if the action succeeded (e.g., () -> player.getAnimation() != IDLE). Checked on the client thread.
        maxRetries - The maximum number of times to retry the action if the condition returns false.
        retryDelayMs - The time to wait (in ms) between the action and the check (and before the next retry). This should be at least 600ms (1 tick) for most game actions.
        Returns:
        The current TaskChain instance.
      • retryUntil

        public TaskChain retryUntil​(java.lang.Runnable action,
                                    java.util.function.BooleanSupplier successCondition)
        Retries an action until a condition is met using default settings (3 retries, 600ms delay).
        Parameters:
        action - The action to perform.
        successCondition - The condition to check for success.
        Returns:
        The current TaskChain instance.
      • waitUntil

        public TaskChain waitUntil​(java.util.function.BooleanSupplier condition,
                                   int checkDelayMs,
                                   int timeoutMs)
        Waits until the given condition is met or a timeout occurs, checking the condition periodically at the specified interval. If the condition is not met within the timeout period, the task chain will stop execution and log a warning.

        This method continuously evaluates the condition on the client thread using the runOnClientThread method provided by the Context. The evaluation is performed in a thread-safe manner, with the specified delay between checks.

        Parameters:
        condition - a @BooleanSupplier representing the condition to wait for. The condition is evaluated on the client thread. Cannot be null. A null value may result in unexpected runtime behavior.
        checkDelayMs - the delay (in milliseconds) between subsequent evaluations of the condition. This determines how frequently the condition is checked.
        timeoutMs - the maximum time (in milliseconds) to wait for the condition to evaluate to true. If the condition is not met within this duration, the chain execution terminates.
        Returns:
        the current instance of @TaskChain, allowing for further chaining of tasks.
      • waitUntil

        public TaskChain waitUntil​(java.util.function.BooleanSupplier condition)
        Waits until the given condition is met, using default delay and timeout values. If the condition is not met within the default timeout period, the task chain will stop execution and log a warning.

        This method evaluates the condition on the client thread and continues to the next task in the chain when the condition is satisfied. The evaluations are performed in a thread-safe manner.

        Parameters:
        condition - a @BooleanSupplier representing the condition to wait for. The condition is evaluated on the client thread. Cannot be null. A null value may result in unexpected runtime behavior.
        Returns:
        the current instance of @TaskChain, allowing for method chaining of additional tasks.
      • waitUntil

        public TaskChain waitUntil​(java.util.function.BooleanSupplier condition,
                                   int timeout)
        Waits until the given condition is met or a timeout occurs. The condition is checked periodically at a default interval. If the condition is not met within the timeout period, the task chain will stop execution and log a warning.

        This method evaluates the condition on the client thread and continues to the next task in the chain when the condition is satisfied. The evaluations are performed in a thread-safe manner using the runOnClientThread method of the Context.

        Parameters:
        condition - a @BooleanSupplier representing the condition to wait for. The condition is evaluated on the client thread. Cannot be null. A null value may result in unintended runtime behavior.
        timeout - the maximum time (in milliseconds) to wait for the condition to evaluate to true. If the condition is not met within this duration, the chain execution terminates.
        Returns:
        the current instance of @TaskChain, allowing for method chaining of additional tasks.
      • delay

        public TaskChain delay​(int ms)
        Adds a delay to the task chain, pausing execution for the specified duration in milliseconds before proceeding to the next task.

        This method schedules a task that introduces the delay by invoking Thread.sleep(ms). As a result, chain progression is paused during the delay duration.

        Parameters:
        ms - The duration of the delay in milliseconds. Must be a non-negative integer; a value less than 0 may result in unexpected behavior.
        Returns:
        The current instance of TaskChain, allowing for method chaining of additional tasks.
      • delayTicks

        public TaskChain delayTicks​(int ticks)
        Adds a delay to the task chain, pausing execution for the specified duration measured in game ticks before proceeding to the next task.

        A tick is typically defined as the basic unit of game time, depending on the underlying game architecture. This method schedules a task that introduces a delay by syncing with the @SleepService, which ensures proper integration with the game's scheduling system.

        Parameters:
        ticks - The number of game ticks to delay execution. Must be a positive integer; providing a value less than or equal to 0 may result in undefined behavior.
        Returns:
        The current instance of @TaskChain, allowing for method chaining of additional tasks.
      • execute

        public boolean execute()
        Executes the task chain sequentially, proceeding through each task until all tasks are complete, an error occurs, or the chain is interrupted.

        The method polls tasks from an internal task queue and executes them in the order they are added. If any task fails (e.g., returns false) or an InterruptedException occurs, the chain halts execution and returns false.

        This method performs tasks synchronously, blocking until the completion or failure of all tasks in the chain.

        Returns:
        true if all tasks in the chain are executed successfully; false if any task fails or the chain is interrupted.
      • add

        public TaskChain add​(TaskChain otherChain)
        Adds all tasks from the specified @TaskChain to the current task chain.

        This method incorporates all tasks from the given @TaskChain instance, ensuring they are executed sequentially as part of the current task chain. The added tasks are executed in the order they exist in otherChain, maintaining their original sequence within the chain.

        Parameters:
        otherChain - the @TaskChain whose tasks are to be added to the current chain. Cannot be null. If null is passed, the behavior is undefined and may result in a runtime exception.
        Returns:
        the current instance of @TaskChain, allowing for method chaining of additional tasks.