Class ScriptExecutor

java.lang.Object
privbot.task.ScriptExecutor

public final class ScriptExecutor extends Object
Runs script logic on a dedicated thread so that sleep() and sleepUntil() block only the script — never the client (game-tick) thread.

Usage:


 ScriptExecutor executor = new ScriptExecutor("my-plugin-worker", () -> {
     maintenance.tick(context);
     TaskNode<MyContext> node = GameThread.ensure(runner::selectNode);
     runner.executeNode(node);
 });
 executor.start();

 // In onGameTick (client thread):
 executor.signalTick();

 // On shutdown:
 executor.stop();
 

When a game tick fires while the script thread is still busy (e.g. inside a sleepUntil), the tick is queued. On wake the script processes exactly one tick and drains any extras, so it always works with the latest game state rather than replaying stale ticks.

  • Constructor Details

    • ScriptExecutor

      public ScriptExecutor(String name, Runnable tickWork)
      Owned by the plugin that wrote tickWork: a lambda or method reference is defined in its host class's loader, so a plugin's own lambda names the plugin. Framework code that builds the tick work itself must use ScriptExecutor(String, Runnable, ClassLoader) — its lambda would name shared-lib instead.
      Parameters:
      name - thread name (shown in thread dumps)
      tickWork - the work to run each tick; select on the client thread and execute here
    • ScriptExecutor

      public ScriptExecutor(String name, Runnable tickWork, ClassLoader ownerLoader)
      Parameters:
      name - thread name (shown in thread dumps)
      tickWork - the work to run each tick; select on the client thread and execute here
      ownerLoader - classloader of the plugin this executor runs for; null reports nothing
  • Method Details

    • start

      public void start()
      Starts the script thread. Safe to call multiple times — subsequent calls are ignored while the thread is alive.
    • signalTick

      public void signalTick()
      Called from onGameTick() on the client thread. Wakes the script thread to process one tick.
    • stop

      public void stop()
      Stops the script thread. Blocks up to 2 seconds for a clean shutdown.

      Interrupts the worker, so a node inside a sleep or a marshalled read unwinds (sleeps return false, GameThread.ensure returns null). Called from the worker itself it returns without joining.

    • isRunning

      public boolean isRunning()
      Returns true if the script thread is alive. False after stop(), even when the old thread had not exited within the 2 s join.