Class GameLogic

java.lang.Object
org.rspeer.game.provider.callback.GameLogic

public class GameLogic extends Object
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
     
    static final class 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Blocks until the current main-loop iteration releases game-state access, then immediately releases it again.
    static void
    bind(Thread thread)
     
    static void
    Throws if the current thread does not have game-state access.
    static void
    Marks the start of a native main-loop iteration and gives the client thread exclusive game-state access.
    static void
    Marks the end of a native main-loop iteration and releases one level of client-thread game-state access.
    static Thread
    Returns the last thread observed entering the native main loop.
    static boolean
    Returns whether the current thread can safely access game state right now.
    static boolean
    Returns whether the current thread is the native client/main-loop thread.
    static boolean
    Returns whether the client thread is currently inside a main-loop iteration.
    Blocks until exclusive game-state access is available.
    Temporarily releases all client-access holds owned by the current non-client thread, then restores them when the returned handle is closed.
    Attempts to acquire exclusive game-state access without waiting.
    static boolean
    Attempts to run the supplied action with exclusive game-state access without waiting.
    static void
    Blocks until exclusive game-state access is available and runs the supplied action while holding it.
    static <T> T
    withIdleAccess(Callable<T> callable)
    Blocks until exclusive game-state access is available and runs the supplied callable while holding it.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • GameLogic

      public GameLogic()
  • Method Details

    • bind

      public static void bind(Thread thread)
    • enter

      public static void enter()
      Marks the start of a native main-loop iteration and gives the client thread exclusive game-state access.

      This is intended for the main-loop hook only. Calls to this method must be paired with exit() on the same thread.

    • exit

      public static void exit()
      Marks the end of a native main-loop iteration and releases one level of client-thread game-state access.

      This is intended for the main-loop hook only. It is tolerant of being called from a thread that does not currently hold the lock.

    • lockClientAccess

      public static GameLogic.ClientAccess lockClientAccess()
      Blocks until exclusive game-state access is available.

      Use this for active script logic or required operations that must run with a consistent client snapshot. Avoid using it from optional services, telemetry, passive scripts, or the EDT, because queued callers can stall the next main-loop iteration.

      Returns:
      an access handle that must be closed by the caller
    • tryLockClientAccess

      public static GameLogic.ClientAccess tryLockClientAccess()
      Attempts to acquire exclusive game-state access without waiting.

      Use this for opportunistic work such as passive scripts, telemetry, debug panels, and other tasks that can skip a pass when the client is busy.

      Returns:
      an access handle that must be closed by the caller, or null if access is currently unavailable
    • isClientThread

      public static boolean isClientThread()
      Returns whether the current thread is the native client/main-loop thread.

      Client-thread code already has game-state access while inside the main-loop hook and should not block waiting for the lock again.

    • hasClientAccess

      public static boolean hasClientAccess()
      Returns whether the current thread can safely access game state right now.

      This is true for the client thread and for non-client threads that currently hold lockClientAccess() or tryLockClientAccess().

    • pauseClientAccess

      public static GameLogic.ClientAccessPause pauseClientAccess()
      Temporarily releases all client-access holds owned by the current non-client thread, then restores them when the returned handle is closed.

      Use this around blocking waits or sleeps inside code that already holds client access. This prevents a script from sleeping while holding the main game-state lock.

      Returns:
      a pause handle that must be closed to restore the previous hold count
    • isProcessing

      public static boolean isProcessing()
      Returns whether the client thread is currently inside a main-loop iteration.
    • getClientThread

      public static Thread getClientThread()
      Returns the last thread observed entering the native main loop.
    • checkClientThread

      public static void checkClientThread(String operation)
      Throws if the current thread does not have game-state access.

      Use this as a guard in low-level getters/setters that directly touch native client state.

      Parameters:
      operation - a short name for the operation being guarded
    • awaitIdle

      public static void awaitIdle()
      Blocks until the current main-loop iteration releases game-state access, then immediately releases it again.

      Use this only when the caller needs a synchronization point with the client loop and does not need to inspect state. Prefer withIdleAccess(Runnable) if work must be performed while access is held.

    • withIdleAccess

      public static void withIdleAccess(Runnable runnable)
      Blocks until exclusive game-state access is available and runs the supplied action while holding it.

      Use this for required short reads/writes from background threads. Keep the action small and never sleep or perform network/disk/UI work inside it. Optional callers should prefer tryWithIdleAccess(Runnable).

      Parameters:
      runnable - work to run with client access
    • tryWithIdleAccess

      public static boolean tryWithIdleAccess(Runnable runnable)
      Attempts to run the supplied action with exclusive game-state access without waiting.

      Use this for optional or repeatable work that can safely be skipped when the client is busy, such as telemetry, passive scripts, and debug snapshots.

      Parameters:
      runnable - work to run if access is immediately available
      Returns:
      true if the action ran, otherwise false
    • withIdleAccess

      public static <T> T withIdleAccess(Callable<T> callable)
      Blocks until exclusive game-state access is available and runs the supplied callable while holding it.

      Use this for required short reads that return a value. Keep the callable small and never sleep or perform network/disk/UI work inside it.

      Type Parameters:
      T - the returned value type
      Parameters:
      callable - work to run with client access
      Returns:
      the callable result