Class InstanceContainer
- All Implemented Interfaces:
DataContainer
,EventHandler
,BlockModifier
public class InstanceContainer extends Instance
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Set<Chunk>
scheduledChunksToRemove
protected InstanceContainer
srcInstance
Fields inherited from class net.minestom.server.instance.Instance
BLOCK_MANAGER, chunkEntities, creatures, entities, experienceOrbs, nextTick, objectEntities, players, uniqueId, UPDATE_MANAGER
-
Constructor Summary
Constructors Constructor Description InstanceContainer(java.util.UUID uniqueId, DimensionType dimensionType, StorageLocation storageLocation)
Creates anInstanceContainer
. -
Method Summary
Modifier and Type Method Description protected void
addSharedInstance(SharedInstance sharedInstance)
Assigns aSharedInstance
to this container.boolean
breakBlock(Player player, BlockPosition blockPosition)
Does callPlayerBlockBreakEvent
and send particle packetsvoid
cacheChunk(Chunk chunk)
Adds aChunk
to the internal instance map.InstanceContainer
copy()
Copies all the chunks of this instance and create a new instance container with all of them.BlockBatch
createBlockBatch()
Creates a newBlockBatch
linked to this instance.protected void
createChunk(int chunkX, int chunkZ, ChunkCallback callback)
Called to generated a newChunk
from scratch.ChunkBatch
createChunkBatch(Chunk chunk)
Creates a newChunk
batch linked to this instance and the specified chunk.void
enableAutoChunkLoad(boolean enable)
When set to true, chunks will load automatically when requested.Chunk
getChunk(int chunkX, int chunkZ)
Gets the loadedChunk
at a position.ChunkGenerator
getChunkGenerator()
Gets the instanceChunkGenerator
.IChunkLoader
getChunkLoader()
Gets theIChunkLoader
of this instance.java.util.Collection<Chunk>
getChunks()
Gets all the instance chunks.ChunkSupplier
getChunkSupplier()
Gets the currentChunkSupplier
.long
getLastBlockChangeTime()
Gets the last time at which a block changed.java.util.List<SharedInstance>
getSharedInstances()
Gets all theSharedInstance
linked to this container.InstanceContainer
getSrcInstance()
Gets the instance from which this one has been copied.StorageLocation
getStorageLocation()
Gets the instanceStorageLocation
.boolean
hasEnabledAutoChunkLoad()
Gets if the instance should auto load chunks.boolean
hasSharedInstances()
Gets if this instance hasSharedInstance
linked to it.boolean
isInVoid(Position position)
Determines whether a position in the void.void
loadChunk(int chunkX, int chunkZ, ChunkCallback callback)
Forces the generation of aChunk
, even if no file andChunkGenerator
are defined.void
loadOptionalChunk(int chunkX, int chunkZ, ChunkCallback callback)
Loads the chunk if the chunk is already loaded or ifInstance.hasEnabledAutoChunkLoad()
returns true.void
refreshBlockStateId(BlockPosition blockPosition, short blockStateId)
Used to change the id of the block in a specificBlockPosition
.void
refreshLastBlockChangeTime()
Signals the instance that a block changed.protected void
retrieveChunk(int chunkX, int chunkZ, ChunkCallback callback)
Used when aChunk
is not currently loaded in memory and need to be retrieved from somewhere else.void
saveChunksToStorage(java.lang.Runnable callback)
Saves multiple chunks to permanent storage.void
saveChunkToStorage(Chunk chunk, java.lang.Runnable callback)
Saves aChunk
to permanent storage.void
saveInstance()
Save the instance without callback.void
saveInstance(java.lang.Runnable callback)
Saves the instance (Instance.getUniqueId()
Instance.getData()
) and callsaveChunksToStorage(Runnable)
.void
scheduleUpdate(int time, TimeUnit unit, BlockPosition position)
Schedules a block update at a givenBlockPosition
.void
setBlockStateId(int x, int y, int z, short blockStateId, Data data)
Sets a block at a position.void
setChunkGenerator(ChunkGenerator chunkGenerator)
Changes the instanceChunkGenerator
.void
setChunkLoader(IChunkLoader chunkLoader)
Changes theIChunkLoader
of this instance (to change how chunks are retrieved when not already loaded).void
setChunkSupplier(ChunkSupplier chunkSupplier)
Changes which type ofChunk
implementation to use once one needs to be loaded.void
setCustomBlock(int x, int y, int z, short customBlockId, Data data)
Sets aCustomBlock
at a position.void
setSeparateBlocks(int x, int y, int z, short blockStateId, short customBlockId, Data data)
Sets aCustomBlock
at a position with a custom state id.void
setStorageLocation(StorageLocation storageLocation)
Changes the instanceStorageLocation
.void
tick(long time)
Performs a single tick in the instance, including scheduled tasks fromInstance.scheduleNextTick(Consumer)
.void
unloadChunk(Chunk chunk)
Schedules the removal of aChunk
, this method does not promise when it will be done.protected void
UNSAFE_unloadChunks()
Unloads all waiting chunks.Methods inherited from class net.minestom.server.instance.Instance
addEntityToChunk, explode, explode, getBlockData, getBlockData, getBlockStateId, getBlockStateId, getBlockStateId, getChunkAt, getChunkAt, getChunkAt, getChunkEntities, getCreatures, getCustomBlock, getCustomBlock, getData, getDimensionType, getEntities, getEventCallbacksMap, getExperienceOrbs, getExplosionSupplier, getInstanceSpace, getObjectEntities, getPlayers, getTime, getTimeRate, getTimeUpdate, getUniqueId, getWorldAge, getWorldBorder, isChunkLoaded, isRegistered, loadChunk, loadChunk, loadOptionalChunk, refreshBlockId, refreshBlockId, refreshBlockStateId, removeEntityFromChunk, saveChunksToStorage, saveChunkToStorage, scheduleNextTick, sendBlockAction, setBlockData, setBlockData, setData, setExplosionSupplier, setRegistered, setTime, setTimeRate, setTimeUpdate, unloadChunk, UNSAFE_addEntity, UNSAFE_removeEntity
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.minestom.server.instance.BlockModifier
setBlock, setBlock, setBlockStateId, setBlockStateId, setCustomBlock, setCustomBlock, setCustomBlock, setCustomBlock, setSeparateBlocks, setSeparateBlocks
Methods inherited from interface net.minestom.server.event.handler.EventHandler
addEventCallback, callCancellableEvent, callEvent, getEventCallbacks, getEventCallbacks, removeEventCallback
-
Field Details
-
Constructor Details
-
InstanceContainer
public InstanceContainer(@NotNull java.util.UUID uniqueId, @NotNull DimensionType dimensionType, @Nullable StorageLocation storageLocation)Creates anInstanceContainer
.- Parameters:
uniqueId
- the unique id of the instancedimensionType
- the dimension type of the instancestorageLocation
- theStorageLocation
of the instance, can be null if you do not wish to save the instance later on
-
-
Method Details
-
setBlockStateId
Description copied from interface:BlockModifier
Sets a block at a position.You can use
BlockModifier.setBlock(int, int, int, Block)
if you want it to be more explicit.- Parameters:
x
- the block Xy
- the block Yz
- the block ZblockStateId
- the block state iddata
- the blockData
, can be null
-
setCustomBlock
Description copied from interface:BlockModifier
Sets aCustomBlock
at a position.The custom block id should be the one returned by
CustomBlock.getCustomBlockId()
.- Parameters:
x
- the block Xy
- the block Yz
- the block ZcustomBlockId
- the custom block iddata
- the blockData
, can be null
-
setSeparateBlocks
public void setSeparateBlocks(int x, int y, int z, short blockStateId, short customBlockId, @Nullable Data data)Description copied from interface:BlockModifier
Sets aCustomBlock
at a position with a custom state id.The custom block id should be the one returned by
CustomBlock.getCustomBlockId()
, and the block state id can be anything you want, state id can be retrieved usingBlock.getBlockId()
.- Parameters:
x
- the block Xy
- the block Yz
- the block ZblockStateId
- the block state idcustomBlockId
- the custom block iddata
- the blockData
, can be null
-
refreshBlockStateId
Description copied from class:Instance
Used to change the id of the block in a specificBlockPosition
.In case of a
CustomBlock
it does not remove it but only refresh its visual.- Specified by:
refreshBlockStateId
in classInstance
- Parameters:
blockPosition
- the block positionblockStateId
- the new block state
-
breakBlock
Description copied from class:Instance
Does callPlayerBlockBreakEvent
and send particle packets- Specified by:
breakBlock
in classInstance
- Parameters:
player
- thePlayer
who break the blockblockPosition
- theBlockPosition
of the broken block- Returns:
- true if the block has been broken, false if it has been cancelled
-
loadChunk
Description copied from class:Instance
Forces the generation of aChunk
, even if no file andChunkGenerator
are defined. -
loadOptionalChunk
Description copied from class:Instance
Loads the chunk if the chunk is already loaded or ifInstance.hasEnabledAutoChunkLoad()
returns true.- Specified by:
loadOptionalChunk
in classInstance
- Parameters:
chunkX
- the chunk XchunkZ
- the chunk Zcallback
- optional consumer called after the chunk has tried to be loaded, contains a chunk if it is successful, null otherwise
-
unloadChunk
Description copied from class:Instance
Schedules the removal of aChunk
, this method does not promise when it will be done.WARNING: during unloading, all entities other than
Player
will be removed.For
InstanceContainer
it is done during the nexttick(long)
.- Specified by:
unloadChunk
in classInstance
- Parameters:
chunk
- the chunk to unload
-
getChunk
Description copied from class:Instance
Gets the loadedChunk
at a position.WARNING: this should only return already-loaded chunk, use
Instance.loadChunk(int, int)
or similar to load one instead. -
saveInstance
public void saveInstance(@Nullable java.lang.Runnable callback)Saves the instance (Instance.getUniqueId()
Instance.getData()
) and callsaveChunksToStorage(Runnable)
.WARNING:
Instance.getData()
needs to be aSerializableData
in order to be saved.- Parameters:
callback
- the optional callback once the saving is done
-
saveInstance
public void saveInstance()Save the instance without callback.- See Also:
saveInstance(Runnable)
-
saveChunkToStorage
Description copied from class:Instance
Saves aChunk
to permanent storage.- Specified by:
saveChunkToStorage
in classInstance
- Parameters:
chunk
- theChunk
to savecallback
- optional callback called when theChunk
is done saving
-
saveChunksToStorage
public void saveChunksToStorage(@Nullable java.lang.Runnable callback)Description copied from class:Instance
Saves multiple chunks to permanent storage.- Specified by:
saveChunksToStorage
in classInstance
- Parameters:
callback
- optional callback called when the chunks are done saving
-
createBlockBatch
Description copied from class:Instance
Creates a newBlockBatch
linked to this instance.- Specified by:
createBlockBatch
in classInstance
- Returns:
- a
BlockBatch
linked to the instance
-
createChunkBatch
Description copied from class:Instance
Creates a newChunk
batch linked to this instance and the specified chunk.- Specified by:
createChunkBatch
in classInstance
- Parameters:
chunk
- the chunk to modify- Returns:
- a ChunkBatch linked to
chunk
-
retrieveChunk
Description copied from class:Instance
Used when aChunk
is not currently loaded in memory and need to be retrieved from somewhere else. Could be read from disk, or generated from scratch.Be sure to signal the chunk using
UpdateManager.signalChunkLoad(Instance, int, int)
and to cache that this chunk has been loaded.WARNING: it has to retrieve a chunk, this is not optional and should execute the callback in all case.
- Specified by:
retrieveChunk
in classInstance
- Parameters:
chunkX
- the chunk XchunkZ
- the chunk Xcallback
- the optional callback executed once theChunk
has been retrieved
-
createChunk
Description copied from class:Instance
Called to generated a newChunk
from scratch.Be sure to signal the chunk using
UpdateManager.signalChunkLoad(Instance, int, int)
and to cache that this chunk has been loaded.This is where you can put your chunk generation code.
- Specified by:
createChunk
in classInstance
- Parameters:
chunkX
- the chunk XchunkZ
- the chunk Zcallback
- the optional callback executed with the newly createdChunk
-
enableAutoChunkLoad
public void enableAutoChunkLoad(boolean enable)Description copied from class:Instance
When set to true, chunks will load automatically when requested. Otherwise usingInstance.loadChunk(int, int)
will be required to even spawn a player- Specified by:
enableAutoChunkLoad
in classInstance
- Parameters:
enable
- enable the auto chunk load
-
hasEnabledAutoChunkLoad
public boolean hasEnabledAutoChunkLoad()Description copied from class:Instance
Gets if the instance should auto load chunks.- Specified by:
hasEnabledAutoChunkLoad
in classInstance
- Returns:
- true if auto chunk load is enabled, false otherwise
-
isInVoid
Description copied from class:Instance
Determines whether a position in the void. If true, entities should take damage and die.Always returning false allow entities to survive in the void.
-
setChunkSupplier
Changes which type ofChunk
implementation to use once one needs to be loaded.Uses
DynamicChunk
by default.WARNING: if you need to save this instance's chunks later, the code needs to be predictable for
IChunkLoader.loadChunk(Instance, int, int, ChunkCallback)
to create the correct type ofChunk
. tl;dr: Need chunk save = no random type.- Parameters:
chunkSupplier
- the newChunkSupplier
of this instance, chunks need to be non-null- Throws:
java.lang.NullPointerException
- ifchunkSupplier
is null
-
getChunkSupplier
Gets the currentChunkSupplier
.You shouldn't use it to generate a new chunk, but as a way to view which one is currently in use.
- Returns:
- the current
ChunkSupplier
-
getSharedInstances
Gets all theSharedInstance
linked to this container.- Returns:
- an unmodifiable
List
containing all theSharedInstance
linked to this container
-
hasSharedInstances
public boolean hasSharedInstances()Gets if this instance hasSharedInstance
linked to it.- Returns:
- true if
getSharedInstances()
is not empty
-
addSharedInstance
Assigns aSharedInstance
to this container.Only used by
InstanceManager
, mostly unsafe.- Parameters:
sharedInstance
- the shared instance to assign to this container
-
copy
Copies all the chunks of this instance and create a new instance container with all of them.Chunks are copied with
Chunk.copy(int, int)
,UUID
is randomized,DimensionType
is passed over and theStorageLocation
is null.- Returns:
- an
InstanceContainer
with the exact same chunks as 'this' - See Also:
to retrieve the "creation source" of the copied instance
-
getSrcInstance
Gets the instance from which this one has been copied.Only present if this instance has been created with
copy()
.- Returns:
- the instance source, null if not created by a copy
- See Also:
to create a copy of this instance with 'this' as the source
-
getLastBlockChangeTime
public long getLastBlockChangeTime()Gets the last time at which a block changed.- Returns:
- the time at which the last block changed in milliseconds, 0 if never
-
refreshLastBlockChangeTime
public void refreshLastBlockChangeTime()Signals the instance that a block changed.Useful if you change blocks values directly using a
Chunk
object. -
cacheChunk
Adds aChunk
to the internal instance map.WARNING: the chunk will not automatically be sent to players and
UpdateManager.signalChunkLoad(Instance, int, int)
must be called manually.- Parameters:
chunk
- the chunk to cache
-
getChunkGenerator
Description copied from class:Instance
Gets the instanceChunkGenerator
.- Specified by:
getChunkGenerator
in classInstance
- Returns:
- the
ChunkGenerator
of the instance
-
setChunkGenerator
Description copied from class:Instance
Changes the instanceChunkGenerator
.- Specified by:
setChunkGenerator
in classInstance
- Parameters:
chunkGenerator
- the newChunkGenerator
of the instance
-
getChunks
Gets all the instance chunks. -
getStorageLocation
Description copied from class:Instance
Gets the instanceStorageLocation
.- Specified by:
getStorageLocation
in classInstance
- Returns:
- the
StorageLocation
of the instance
-
setStorageLocation
Description copied from class:Instance
Changes the instanceStorageLocation
.- Specified by:
setStorageLocation
in classInstance
- Parameters:
storageLocation
- the newStorageLocation
of the instance
-
getChunkLoader
Gets theIChunkLoader
of this instance.- Returns:
- the
IChunkLoader
of this instance
-
setChunkLoader
Changes theIChunkLoader
of this instance (to change how chunks are retrieved when not already loaded).- Parameters:
chunkLoader
- the newIChunkLoader
-
scheduleUpdate
Description copied from class:Instance
Schedules a block update at a givenBlockPosition
. Does nothing if noCustomBlock
is present atposition
.Cancelled if the block changes between this call and the actual update.
- Specified by:
scheduleUpdate
in classInstance
- Parameters:
time
- in how long this update must be performed?unit
- in what unit is the time expressedposition
- the location of the block to update
-
tick
public void tick(long time)Description copied from class:Instance
Performs a single tick in the instance, including scheduled tasks fromInstance.scheduleNextTick(Consumer)
.Warning: this does not update chunks and entities.
-
UNSAFE_unloadChunks
protected void UNSAFE_unloadChunks()Unloads all waiting chunks.Unsafe because it has to be done on the same thread as the instance/chunks tick update.
-