Class Chunk
- All Implemented Interfaces:
Block.Getter
,Block.Setter
,Snapshotable
,Taggable
,TagReadable
,TagWritable
,Tickable
,Viewable
,Biome.Getter
,Biome.Setter
- Direct Known Subclasses:
DynamicChunk
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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.minestom.server.instance.block.Block.Getter
Block.Getter.Condition
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
static final int
static final int
protected final int
protected final int
protected PFColumnarSpace
protected Instance
protected boolean
protected final int
protected final int
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds a viewer.abstract @NotNull Chunk
Creates a copy of this chunk, including blocks state id, custom block id, biomes, update data.int
Gets the chunk X.int
Gets the chunk Z.abstract @NotNull SendablePacket
@NotNull UUID
Gets the unique identifier of this chunk.@NotNull Instance
Gets the instance where this chunk is storedabstract long
Gets the last time that this chunk changed.int
Gets the highest (exclusive) section Y available in this chunkint
Gets the lowest (inclusive) section Y available in this chunkabstract @NotNull Section
getSection
(int section) @NotNull Section
getSectionAt
(int blockY) Gets all the viewers of this viewable element.abstract void
Invalidate the chunk cachesboolean
isLoaded()
Used to verify if the chunk should still be kept in memory.boolean
Gets if this chunk is read-only.abstract void
loadHeightmapsFromNBT
(org.jglrxavpok.hephaistos.nbt.NBTCompound heightmaps) abstract @NotNull Heightmap
void
Called when the chunk generator has finished generating the chunk.protected void
onLoad()
Called when the chunk has been successfully loaded.boolean
removeViewer
(@NotNull Player player) Removes a viewer.abstract void
reset()
Resets the chunk, this means clearing all the data making it empty.void
void
Sends the chunk data toplayer
.void
Sets a block at a position.protected abstract void
setBlock
(int x, int y, int z, @NotNull Block block, BlockHandler.Placement placement, BlockHandler.Destroy destroy) void
setColumnarSpace
(PFColumnarSpace columnarSpace) Changes this chunk columnar space.void
setReadOnly
(boolean readOnly) Changes the read state of the chunk.boolean
Gets if this chunk will or had been loaded with aChunkGenerator
.@NotNull TagHandler
abstract void
tick
(long time) Executes a chunk tick.@NotNull Point
Gets the world position of this chunk.toString()
protected void
unload()
Sets the chunk as "unloaded".abstract @NotNull Heightmap
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.minestom.server.world.biomes.Biome.Getter
getBiome, getBiome
Methods inherited from interface net.minestom.server.world.biomes.Biome.Setter
setBiome, setBiome
Methods inherited from interface net.minestom.server.instance.block.Block.Getter
getBlock, getBlock, getBlock, getBlock
Methods inherited from interface net.minestom.server.instance.block.Block.Setter
setBlock
Methods inherited from interface net.minestom.server.snapshot.Snapshotable
updateSnapshot
Methods inherited from interface net.minestom.server.Viewable
getViewersAsAudience, getViewersAsAudiences, isViewer, sendPacketsToViewers, sendPacketsToViewers, sendPacketToViewers, sendPacketToViewersAndSelf
-
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
-
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
-
-
Constructor Details
-
Chunk
-
-
Method Details
-
setBlock
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 isInstance.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 interfaceBlock.Setter
- Parameters:
x
- the block Xy
- the block Yz
- the block Zblock
- 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
-
getSection
-
motionBlockingHeightmap
-
worldSurfaceHeightmap
-
loadHeightmapsFromNBT
public abstract void loadHeightmapsFromNBT(org.jglrxavpok.hephaistos.nbt.NBTCompound heightmaps) -
getSectionAt
-
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.
-
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
Sends the chunk data toplayer
.- Parameters:
player
- the player
-
sendChunk
public void sendChunk() -
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 ownerchunkX
- the chunk X of the copychunkZ
- 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
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
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
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 aChunkGenerator
.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 affectIChunkLoader
andChunkGenerator
.- 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 affectIChunkLoader
andChunkGenerator
.- Parameters:
readOnly
- true to make the chunk read-only, false otherwise
-
setColumnarSpace
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
-
addViewer
Description copied from interface:Viewable
Adds a viewer. -
removeViewer
Description copied from interface:Viewable
Removes a viewer.- Specified by:
removeViewer
in interfaceViewable
- 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
Description copied from interface:Viewable
Gets all the viewers of this viewable element.- Specified by:
getViewers
in interfaceViewable
- Returns:
- A Set containing all the element's viewers
-
tagHandler
- Specified by:
tagHandler
in interfaceTaggable
-
unload
protected void unload()Sets the chunk as "unloaded". -
invalidate
public abstract void invalidate()Invalidate the chunk caches
-