Interface Acquirable<T>


@Experimental public sealed interface Acquirable<T>
  • Method Summary

    Modifier and Type
    Method
    Description
    @NotNull TickThread
     
    static @NotNull Stream<@NotNull Entity>
    Gets all the entities being ticked in the current thread.
    default boolean
    Gets if the acquirable element is local to this thread
    default @NotNull Optional<T>
    Retrieves the acquirable value if and only if the element is already present/ticked in the current thread.
    default @NotNull Acquired<T>
    Returns a new Acquired object which will be locked to the current thread.
    static <T> @NotNull Acquirable<T>
    of(T value)
    Creates a new Acquirable object.
    static long
    Retrieve and reset acquiring time.
    default void
    sync(@NotNull Consumer<T> consumer)
    Locks the acquirable element, execute consumer synchronously and unlock the thread.
    Unwrap the contained object unsafely.
  • Method Details

    • currentEntities

      @NotNull static @NotNull Stream<@NotNull Entity> currentEntities()
      Gets all the entities being ticked in the current thread.

      Useful when you want to ensure that no acquisition is ever done.

      Be aware that the entity stream is only updated at the beginning of the thread tick.

      Returns:
      the entities ticked in the current thread
    • resetAcquiringTime

      @Internal static long resetAcquiringTime()
      Retrieve and reset acquiring time.
    • of

      @Internal @NotNull static <T> @NotNull Acquirable<T> of(@NotNull T value)
      Creates a new Acquirable object.

      Mostly for internal use, as a TickThread has to be used and properly synchronized.

      Type Parameters:
      T - the acquirable element type
      Parameters:
      value - the acquirable element
      Returns:
      a new acquirable object
    • lock

      @NotNull default @NotNull Acquired<T> lock()
      Returns a new Acquired object which will be locked to the current thread.

      Useful when your code cannot be done inside a callback and need to be sync. Do not forget to call Acquired.unlock() once you are done with it.

      Returns:
      an acquired object
      See Also:
    • local

      @NotNull default @NotNull Optional<T> local()
      Retrieves the acquirable value if and only if the element is already present/ticked in the current thread.

      Useful when you want only want to acquire an element when you are guaranteed to do not create a huge performance impact.

      Returns:
      an optional containing the acquired element if safe, Optional.empty() otherwise
    • isLocal

      default boolean isLocal()
      Gets if the acquirable element is local to this thread
      Returns:
      true if the element is linked to the current thread
    • sync

      default void sync(@NotNull @NotNull Consumer<T> consumer)
      Locks the acquirable element, execute consumer synchronously and unlock the thread.

      Free if the element is already present in the current thread, blocking otherwise.

      Parameters:
      consumer - the callback to execute once the element has been safely acquired
    • unwrap

      @NotNull T unwrap()
      Unwrap the contained object unsafely.

      Should only be considered when thread-safety is not necessary (e.g. comparing positions)

      Returns:
      the unwrapped value
    • assignedThread

      @Internal @NotNull @NotNull TickThread assignedThread()