Class PositionTrackingStorage

java.lang.Object
cn.nukkit.positiontracking.PositionTrackingStorage
All Implemented Interfaces:
Closeable, AutoCloseable

@PowerNukkitOnly @Since("1.4.0.0-PN") @ParametersAreNonnullByDefault public class PositionTrackingStorage extends Object implements Closeable
Stores a sequential range of PositionTracking objects in a file. The read operation is cached.

This object holds a file handler and must be closed when it is no longer needed.

Once closed the instance cannot be reused.

Author:
joserobjr
  • Field Details

  • Constructor Details

    • PositionTrackingStorage

      @PowerNukkitOnly @Since("1.4.0.0-PN") public PositionTrackingStorage(int startIndex, File persistenceFile) throws IOException
      Opens or create the file and all directories in the path automatically. The given start index will be used in new files and will be checked when opening files. If the file being opened don't matches this value internally than an IllegalArgumentException will be thrown.
      Parameters:
      startIndex - The number of the first handler. Must be higher than 0 and must match the number of the existing file.
      persistenceFile - The file being opened or created. Parent directories will also be created if necessary.
      Throws:
      IOException - If an error has occurred while reading, parsing or creating the file
      IllegalArgumentException - If opening an existing file and the internal startIndex don't match the given startIndex
    • PositionTrackingStorage

      @PowerNukkitOnly @Since("1.4.0.0-PN") public PositionTrackingStorage(int startIndex, File persistenceFile, int maxStorage) throws IOException
      Opens or create the file and all directories in the path automatically. The given start index will be used in new files and will be checked when opening files. If the file being opened don't matches this value internally than an IllegalArgumentException will be thrown.
      Parameters:
      startIndex - The number of the first handler. Must be higher than 0 and must match the number of the existing file.
      persistenceFile - The file being opened or created. Parent directories will also be created if necessary.
      maxStorage - The maximum amount of positions that this storage may hold. It cannot be changed after creation. Ignored when loading an existing file. When zero or negative, a default value will be used.
      Throws:
      IOException - If an error has occurred while reading, parsing or creating the file
      IllegalArgumentException - If opening an existing file and the internal startIndex don't match the given startIndex
  • Method Details

    • getPosition

      @PowerNukkitOnly @Since("1.4.0.0-PN") @Nullable public PositionTracking getPosition(int trackingHandler) throws IOException
      Retrieves the PositionTracking object that is assigned to the given trackingHandler. The handler must be valid for this storage.

      This call may return a cached result but the returned object can be modified freely.

      Parameters:
      trackingHandler - A valid handler for this storage
      Returns:
      A clone of the cached result.
      Throws:
      IOException - If an error has occurred while accessing the file
      IllegalArgumentException - If the trackingHandler is not valid for this storage
    • getPosition

      @PowerNukkitOnly @Since("1.4.0.0-PN") @Nullable public PositionTracking getPosition(int trackingHandler, boolean onlyEnabled) throws IOException
      Retrieves the PositionTracking object that is assigned to the given trackingHandler. The handler must be valid for this storage.

      This call may return a cached result but the returned object can be modified freely.

      Parameters:
      trackingHandler - A valid handler for this storage
      onlyEnabled - When false, disabled positions that wasn't invalidated may be returned. Caching only works when this is set to true
      Returns:
      A clone of the cached result.
      Throws:
      IOException - If an error has occurred while accessing the file
      IllegalArgumentException - If the trackingHandler is not valid for this storage
    • addOrReusePosition

      @PowerNukkitOnly @Since("1.4.0.0-PN") public OptionalInt addOrReusePosition(NamedPosition position) throws IOException
      Attempts to reuse an existing and enabled trackingHandler for the given position, if none is found than a new handler is created if the limit was not exceeded.
      Parameters:
      position - The position that needs a handler
      Returns:
      The trackingHandler assigned to the position or an empty OptionalInt if none was found and this storage is full
      Throws:
      IOException - If an error occurred while reading or writing the file
    • addNewPosition

      @PowerNukkitOnly @Since("1.4.0.0-PN") public OptionalInt addNewPosition(NamedPosition position) throws IOException
      Adds the given position as a new entry in this storage, even if the position is already registered and enabled.
      Parameters:
      position - The position that needs a handler
      Returns:
      The trackingHandler assigned to the position or an empty OptionalInt if none was found and this storage is full
      Throws:
      IOException - If an error occurred while reading or writing the file
    • addNewPosition

      @PowerNukkitOnly @Since("1.4.0.0-PN") public OptionalInt addNewPosition(NamedPosition position, boolean enabled) throws IOException
      Adds the given position as a new entry in this storage, even if the position is already registered and enabled.
      Parameters:
      position - The position that needs a handler
      enabled - If the position will be added as enabled or disabled
      Returns:
      The trackingHandler assigned to the position or an empty OptionalInt if none was found and this storage is full
      Throws:
      IOException - If an error occurred while reading or writing the file
    • findTrackingHandler

      @PowerNukkitOnly @Since("1.4.0.0-PN") @Nonnull public OptionalInt findTrackingHandler(NamedPosition position) throws IOException
      Throws:
      IOException
    • invalidateHandler

      @PowerNukkitOnly @Since("1.4.0.0-PN") public void invalidateHandler(int trackingHandler) throws IOException
      Throws:
      IOException
    • isEnabled

      @PowerNukkitOnly @Since("1.4.0.0-PN") public boolean isEnabled(int trackingHandler) throws IOException
      Throws:
      IOException
    • setEnabled

      @PowerNukkitOnly @Since("1.4.0.0-PN") public boolean setEnabled(int trackingHandler, boolean enabled) throws IOException
      Throws:
      IOException
    • hasPosition

      @PowerNukkitOnly @Since("1.4.0.0-PN") public boolean hasPosition(int trackingHandler) throws IOException
      Throws:
      IOException
    • hasPosition

      @PowerNukkitOnly @Since("1.4.0.0-PN") public boolean hasPosition(int trackingHandler, boolean onlyEnabled) throws IOException
      Throws:
      IOException
    • findTrackingHandlers

      @PowerNukkitOnly @Since("1.4.0.0-PN") @Nonnull public it.unimi.dsi.fastutil.ints.IntList findTrackingHandlers(NamedPosition pos) throws IOException
      Throws:
      IOException
    • findTrackingHandlers

      @PowerNukkitOnly @Since("1.4.0.0-PN") @Nonnull public it.unimi.dsi.fastutil.ints.IntList findTrackingHandlers(NamedPosition pos, boolean onlyEnabled) throws IOException
      Throws:
      IOException
    • findTrackingHandlers

      @PowerNukkitOnly @Since("1.4.0.0-PN") @Nonnull public it.unimi.dsi.fastutil.ints.IntList findTrackingHandlers(NamedPosition pos, boolean onlyEnabled, int limit) throws IOException
      Throws:
      IOException
    • getStartingHandler

      @PowerNukkitOnly @Since("1.4.0.0-PN") public int getStartingHandler()
    • getMaxHandler

      @PowerNukkitOnly @Since("1.4.0.0-PN") public int getMaxHandler()
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • finalize

      protected void finalize() throws Throwable
      Overrides:
      finalize in class Object
      Throws:
      Throwable