Class ChunkUtils

java.lang.Object
net.minestom.server.utils.chunk.ChunkUtils

public final class ChunkUtils
extends java.lang.Object
  • Method Summary

    Modifier and Type Method Description
    static byte blockIndexToChunkPositionX​(int index)
    Converts a block index to a chunk position X.
    static short blockIndexToChunkPositionY​(int index)
    Converts a block index to a chunk position Y.
    static byte blockIndexToChunkPositionZ​(int index)
    Converts a block index to a chunk position Z.
    static int blockIndexToPositionX​(int index, int chunkX)
    Converts a block chunk index to its instance position X.
    static int blockIndexToPositionY​(int index)
    Converts a block chunk index to its instance position Y.
    static int blockIndexToPositionZ​(int index, int chunkZ)
    Converts a block chunk index to its instance position Z.
    static int getBlockIndex​(int x, int y, int z)
    Gets the block index of a position.
    static BlockPosition getBlockPosition​(int index, int chunkX, int chunkZ)  
    static int getChunkCoordinate​(int xz)  
    static int getChunkCoordX​(long index)
    Converts a chunk index to its chunk X position.
    static int getChunkCoordZ​(long index)
    Converts a chunk index to its chunk Z position.
    static long getChunkIndex​(int chunkX, int chunkZ)
    Gets the chunk index of chunk coordinates.
    static long getChunkIndexWithSection​(int chunkX, int chunkZ, int section)  
    static long[] getChunksInRange​(Position position, int range)
    Gets the chunks in range of a position.
    static long[] getNeighbours​(Instance instance, int chunkX, int chunkZ)
    Gets all the loaded neighbours of a chunk and itself, no diagonals.
    static int getSectionAt​(int y)  
    static boolean isLoaded​(Chunk chunk)
    Gets if a chunk is loaded.
    static boolean isLoaded​(Instance instance, float x, float z)
    Gets if a chunk is loaded.

    Methods inherited from class java.lang.Object

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

    • isLoaded

      public static boolean isLoaded​(@Nullable Chunk chunk)
      Gets if a chunk is loaded.
      Parameters:
      chunk - the chunk to check
      Returns:
      true if the chunk is loaded, false otherwise
    • isLoaded

      public static boolean isLoaded​(@NotNull Instance instance, float x, float z)
      Gets if a chunk is loaded.
      Parameters:
      instance - the instance to check
      x - instance X coordinate
      z - instance Z coordinate
      Returns:
      true if the chunk is loaded, false otherwise
    • getChunkCoordinate

      public static int getChunkCoordinate​(int xz)
      Parameters:
      xz - the instance coordinate to convert
      Returns:
      the chunk X or Z based on the argument
    • getChunkIndex

      public static long getChunkIndex​(int chunkX, int chunkZ)
      Gets the chunk index of chunk coordinates.

      Used when you want to store a chunk somewhere without using a reference to the whole object (as this can lead to memory leaks).

      Parameters:
      chunkX - the chunk X
      chunkZ - the chunk Z
      Returns:
      a number storing the chunk X and Z
    • getChunkIndexWithSection

      public static long getChunkIndexWithSection​(int chunkX, int chunkZ, int section)
    • getChunkCoordX

      public static int getChunkCoordX​(long index)
      Converts a chunk index to its chunk X position.
      Parameters:
      index - the chunk index computed by getChunkIndex(int, int)
      Returns:
      the chunk X based on the index
    • getChunkCoordZ

      public static int getChunkCoordZ​(long index)
      Converts a chunk index to its chunk Z position.
      Parameters:
      index - the chunk index computed by getChunkIndex(int, int)
      Returns:
      the chunk Z based on the index
    • getSectionAt

      public static int getSectionAt​(int y)
    • getChunksInRange

      @NotNull public static long[] getChunksInRange​(@NotNull Position position, int range)
      Gets the chunks in range of a position.
      Parameters:
      position - the initial position
      range - how far should it retrieves chunk
      Returns:
      an array containing chunks index
    • getNeighbours

      @NotNull public static long[] getNeighbours​(@NotNull Instance instance, int chunkX, int chunkZ)
      Gets all the loaded neighbours of a chunk and itself, no diagonals.
      Parameters:
      instance - the instance of the chunks
      chunkX - the chunk X
      chunkZ - the chunk Z
      Returns:
      an array containing all the loaded neighbours chunk index
    • getBlockIndex

      public static int getBlockIndex​(int x, int y, int z)
      Gets the block index of a position.

      This can be cast as a short as long as you don't mind receiving a negative value (not array-friendly).

      Parameters:
      x - the block X
      y - the block Y
      z - the block Z
      Returns:
      an index which can be used to store and retrieve later data linked to a block position
    • getBlockPosition

      @NotNull public static BlockPosition getBlockPosition​(int index, int chunkX, int chunkZ)
      Parameters:
      index - an index computed from getBlockIndex(int, int, int)
      chunkX - the chunk X
      chunkZ - the chunk Z
      Returns:
      the instance position of the block located in index
    • blockIndexToPositionX

      public static int blockIndexToPositionX​(int index, int chunkX)
      Converts a block chunk index to its instance position X.
      Parameters:
      index - the block chunk index from getBlockIndex(int, int, int)
      chunkX - the chunk X
      Returns:
      the X coordinate of the block index
    • blockIndexToPositionY

      public static int blockIndexToPositionY​(int index)
      Converts a block chunk index to its instance position Y.
      Parameters:
      index - the block chunk index from getBlockIndex(int, int, int)
      Returns:
      the Y coordinate of the block index
    • blockIndexToPositionZ

      public static int blockIndexToPositionZ​(int index, int chunkZ)
      Converts a block chunk index to its instance position Z.
      Parameters:
      index - the block chunk index from getBlockIndex(int, int, int)
      chunkZ - the chunk Z
      Returns:
      the Z coordinate of the block index
    • blockIndexToChunkPositionX

      public static byte blockIndexToChunkPositionX​(int index)
      Converts a block index to a chunk position X.
      Parameters:
      index - an index computed from getBlockIndex(int, int, int)
      Returns:
      the chunk position X (O-15) of the specified index
    • blockIndexToChunkPositionY

      public static short blockIndexToChunkPositionY​(int index)
      Converts a block index to a chunk position Y.
      Parameters:
      index - an index computed from getBlockIndex(int, int, int)
      Returns:
      the chunk position Y (O-255) of the specified index
    • blockIndexToChunkPositionZ

      public static byte blockIndexToChunkPositionZ​(int index)
      Converts a block index to a chunk position Z.
      Parameters:
      index - an index computed from getBlockIndex(int, int, int)
      Returns:
      the chunk position Z (O-15) of the specified index