Class Chunk

java.lang.Object
net.minestom.server.instance.Chunk
All Implemented Interfaces:
Block.Getter, Block.Setter, Snapshotable, Taggable, TagReadable, TagWritable, Tickable, Viewable, Biome.Getter, Biome.Setter
Direct Known Subclasses:
DynamicChunk

public abstract class Chunk extends Object implements Block.Getter, Block.Setter, Biome.Getter, Biome.Setter, Viewable, Tickable, Taggable, Snapshotable
A chunk is a part of an Instance, limited by a size of 16x256x16 blocks and subdivided in 16 sections of 16 blocks height. Should contain all the blocks located at those positions and manage their tick updates. Be aware that implementations do not need to be thread-safe, all chunks are guarded by their own instance ('this').

You can create your own implementation of this class by extending it and create the objects in InstanceContainer.setChunkSupplier(ChunkSupplier).

You generally want to avoid storing references of this object as this could lead to a huge memory leak, you should store the chunk coordinates instead.

  • Field Details

    • CHUNK_SIZE_X

      public static final int CHUNK_SIZE_X
      See Also:
    • CHUNK_SIZE_Z

      public static final int CHUNK_SIZE_Z
      See Also:
    • CHUNK_SECTION_SIZE

      public static final int CHUNK_SECTION_SIZE
      See Also:
    • instance

      protected Instance instance
    • chunkX

      protected final int chunkX
    • chunkZ

      protected final int chunkZ
    • minSection

      protected final int minSection
    • maxSection

      protected final int maxSection
    • loaded

      protected volatile boolean loaded
    • columnarSpace

      protected PFColumnarSpace columnarSpace
  • Constructor Details

    • Chunk

      public Chunk(@NotNull @NotNull Instance instance, int chunkX, int chunkZ, boolean shouldGenerate)
  • Method Details

    • setBlock

      public void setBlock(int x, int y, int z, @NotNull @NotNull Block block)
      Sets a block at a position.

      This is used when the previous block has to be destroyed/replaced, meaning that it clears the previous data and update method.

      WARNING: this method is not thread-safe (in order to bring performance improvement with batches) The thread-safe version is Instance.setBlock(int, int, int, Block) (or any similar instance methods) Otherwise, you can simply do not forget to have this chunk synchronized when this is called.

      Specified by:
      setBlock in interface Block.Setter
      Parameters:
      x - the block X
      y - the block Y
      z - the block Z
      block - the block to place
    • setBlock

      protected abstract void setBlock(int x, int y, int z, @NotNull @NotNull Block block, @Nullable BlockHandler.Placement placement, @Nullable BlockHandler.Destroy destroy)
    • getSections

      @NotNull public abstract @NotNull List<Section> getSections()
    • getSection

      @NotNull public abstract @NotNull Section getSection(int section)
    • motionBlockingHeightmap

      @NotNull public abstract @NotNull Heightmap motionBlockingHeightmap()
    • worldSurfaceHeightmap

      @NotNull public abstract @NotNull Heightmap worldSurfaceHeightmap()
    • loadHeightmapsFromNBT

      public abstract void loadHeightmapsFromNBT(org.jglrxavpok.hephaistos.nbt.NBTCompound heightmaps)
    • getSectionAt

      @NotNull public @NotNull Section getSectionAt(int blockY)
    • tick

      public abstract void tick(long time)
      Executes a chunk tick.

      Should be used to update all the blocks in the chunk.

      WARNING: this method doesn't necessary have to be thread-safe, proceed with caution.

      Specified by:
      tick in interface Tickable
      Parameters:
      time - the time of the update in milliseconds
    • getLastChangeTime

      public abstract long getLastChangeTime()
      Gets the last time that this chunk changed.

      "Change" means here data used in ChunkDataPacket. It is necessary to see if the cached version of this chunk can be used instead of re-writing and compressing everything.

      Returns:
      the last change time in milliseconds
    • sendChunk

      public void sendChunk(@NotNull @NotNull Player player)
      Sends the chunk data to player.
      Parameters:
      player - the player
    • sendChunk

      public void sendChunk()
    • getFullDataPacket

      @Internal @NotNull public abstract @NotNull SendablePacket getFullDataPacket()
    • copy

      @NotNull public abstract @NotNull Chunk copy(@NotNull @NotNull Instance instance, int chunkX, int chunkZ)
      Creates a copy of this chunk, including blocks state id, custom block id, biomes, update data.

      The chunk position (X/Z) can be modified using the given arguments.

      Parameters:
      instance - the chunk owner
      chunkX - the chunk X of the copy
      chunkZ - the chunk Z of the copy
      Returns:
      a copy of this chunk with a potentially new instance and position
    • reset

      public abstract void reset()
      Resets the chunk, this means clearing all the data making it empty.
    • getIdentifier

      @NotNull public @NotNull UUID getIdentifier()
      Gets the unique identifier of this chunk.

      WARNING: this UUID is not persistent but randomized once the object is instantiated.

      Returns:
      the chunk identifier
    • getInstance

      @NotNull public @NotNull Instance getInstance()
      Gets the instance where this chunk is stored
      Returns:
      the linked instance
    • getChunkX

      public int getChunkX()
      Gets the chunk X.
      Returns:
      the chunk X
    • getChunkZ

      public int getChunkZ()
      Gets the chunk Z.
      Returns:
      the chunk Z
    • getMinSection

      public int getMinSection()
      Gets the lowest (inclusive) section Y available in this chunk
      Returns:
      the lowest (inclusive) section Y available in this chunk
    • getMaxSection

      public int getMaxSection()
      Gets the highest (exclusive) section Y available in this chunk
      Returns:
      the highest (exclusive) section Y available in this chunk
    • toPosition

      @NotNull public @NotNull Point toPosition()
      Gets the world position of this chunk.
      Returns:
      the position of this chunk
    • shouldGenerate

      public boolean shouldGenerate()
      Gets if this chunk will or had been loaded with a ChunkGenerator.

      If false, the chunk will be entirely empty when loaded.

      Returns:
      true if this chunk is affected by a ChunkGenerator
    • isReadOnly

      public boolean isReadOnly()
      Gets if this chunk is read-only.

      Being read-only should prevent block placing/breaking and setting block from an Instance. It does not affect IChunkLoader and ChunkGenerator.

      Returns:
      true if the chunk is read-only
    • setReadOnly

      public void setReadOnly(boolean readOnly)
      Changes the read state of the chunk.

      Being read-only should prevent block placing/breaking and setting block from an Instance. It does not affect IChunkLoader and ChunkGenerator.

      Parameters:
      readOnly - true to make the chunk read-only, false otherwise
    • setColumnarSpace

      public void setColumnarSpace(PFColumnarSpace columnarSpace)
      Changes this chunk columnar space.
      Parameters:
      columnarSpace - the new columnar space
    • isLoaded

      public boolean isLoaded()
      Used to verify if the chunk should still be kept in memory.
      Returns:
      true if the chunk is loaded
    • onLoad

      protected void onLoad()
      Called when the chunk has been successfully loaded.
    • onGenerate

      public void onGenerate()
      Called when the chunk generator has finished generating the chunk.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • addViewer

      public boolean addViewer(@NotNull @NotNull Player player)
      Description copied from interface: Viewable
      Adds a viewer.
      Specified by:
      addViewer in interface Viewable
      Parameters:
      player - the viewer to add
      Returns:
      true if the player has been added, false otherwise (could be because he is already a viewer)
    • removeViewer

      public boolean removeViewer(@NotNull @NotNull Player player)
      Description copied from interface: Viewable
      Removes a viewer.
      Specified by:
      removeViewer in interface Viewable
      Parameters:
      player - the viewer to remove
      Returns:
      true if the player has been removed, false otherwise (could be because he was not a viewer)
    • getViewers

      @NotNull public @NotNull Set<Player> getViewers()
      Description copied from interface: Viewable
      Gets all the viewers of this viewable element.
      Specified by:
      getViewers in interface Viewable
      Returns:
      A Set containing all the element's viewers
    • tagHandler

      @NotNull public @NotNull TagHandler tagHandler()
      Specified by:
      tagHandler in interface Taggable
    • unload

      protected void unload()
      Sets the chunk as "unloaded".
    • invalidate

      public abstract void invalidate()
      Invalidate the chunk caches