Class InstanceContainer

java.lang.Object
net.minestom.server.instance.Instance
net.minestom.server.instance.InstanceContainer
All Implemented Interfaces:
Audience, ForwardingAudience, Pointered, PacketGroupingAudience, EventHandler<InstanceEvent>, Block.Getter, Block.Setter, Snapshotable, Taggable, TagReadable, TagWritable, Tickable, Schedulable

public class InstanceContainer extends Instance
InstanceContainer is an instance that contains chunks in contrary to SharedInstance.
  • Field Details

  • Constructor Details

    • InstanceContainer

      public InstanceContainer(@NotNull @NotNull UUID uniqueId, @NotNull @NotNull DimensionType dimensionType)
    • InstanceContainer

      public InstanceContainer(@NotNull @NotNull UUID uniqueId, @NotNull @NotNull DimensionType dimensionType, @NotNull @NotNull NamespaceID dimensionName)
    • InstanceContainer

      @Experimental public InstanceContainer(@NotNull @NotNull UUID uniqueId, @NotNull @NotNull DimensionType dimensionType, @Nullable @Nullable IChunkLoader loader)
    • InstanceContainer

      @Experimental public InstanceContainer(@NotNull @NotNull UUID uniqueId, @NotNull @NotNull DimensionType dimensionType, @Nullable @Nullable IChunkLoader loader, @NotNull @NotNull NamespaceID dimensionName)
  • Method Details

    • setBlock

      public void setBlock(int x, int y, int z, @NotNull @NotNull Block block, boolean doBlockUpdates)
      Specified by:
      setBlock in class Instance
    • placeBlock

      public boolean placeBlock(@NotNull BlockHandler.Placement placement, boolean doBlockUpdates)
      Specified by:
      placeBlock in class Instance
    • breakBlock

      public boolean breakBlock(@NotNull @NotNull Player player, @NotNull @NotNull Point blockPosition, @NotNull @NotNull BlockFace blockFace, boolean doBlockUpdates)
      Description copied from class: Instance
      Does call PlayerBlockBreakEvent and send particle packets
      Specified by:
      breakBlock in class Instance
      Parameters:
      player - the Player who break the block
      blockPosition - the position of the broken block
      doBlockUpdates - true to do block updates, false otherwise
      Returns:
      true if the block has been broken, false if it has been cancelled
    • loadChunk

      @NotNull public @NotNull CompletableFuture<Chunk> loadChunk(int chunkX, int chunkZ)
      Description copied from class: Instance
      Forces the generation of a Chunk, even if no file and ChunkGenerator are defined.
      Specified by:
      loadChunk in class Instance
      Parameters:
      chunkX - the chunk X
      chunkZ - the chunk Z
      Returns:
      a CompletableFuture completed once the chunk has been loaded
    • loadOptionalChunk

      @NotNull public @NotNull CompletableFuture<Chunk> loadOptionalChunk(int chunkX, int chunkZ)
      Description copied from class: Instance
      Loads the chunk if the chunk is already loaded or if Instance.hasEnabledAutoChunkLoad() returns true.
      Specified by:
      loadOptionalChunk in class Instance
      Parameters:
      chunkX - the chunk X
      chunkZ - the chunk Z
      Returns:
      a CompletableFuture completed once the chunk has been processed, can be null if not loaded
    • unloadChunk

      public void unloadChunk(@NotNull @NotNull Chunk chunk)
      Description copied from class: Instance
      Schedules the removal of a Chunk, this method does not promise when it will be done.

      WARNING: during unloading, all entities other than Player will be removed.

      Specified by:
      unloadChunk in class Instance
      Parameters:
      chunk - the chunk to unload
    • getChunk

      public Chunk getChunk(int chunkX, int chunkZ)
      Description copied from class: Instance
      Gets the loaded Chunk at a position.

      WARNING: this should only return already-loaded chunk, use Instance.loadChunk(int, int) or similar to load one instead.

      Specified by:
      getChunk in class Instance
      Parameters:
      chunkX - the chunk X
      chunkZ - the chunk Z
      Returns:
      the chunk at the specified position, null if not loaded
    • saveInstance

      @NotNull public @NotNull CompletableFuture<Void> saveInstance()
      Description copied from class: Instance
      Saves the current instance tags.

      Warning: only the global instance data will be saved, not chunks. You would need to call Instance.saveChunksToStorage() too.

      Specified by:
      saveInstance in class Instance
      Returns:
      the future called once the instance data has been saved
    • saveChunkToStorage

      @NotNull public @NotNull CompletableFuture<Void> saveChunkToStorage(@NotNull @NotNull Chunk chunk)
      Description copied from class: Instance
      Saves a Chunk to permanent storage.
      Specified by:
      saveChunkToStorage in class Instance
      Parameters:
      chunk - the Chunk to save
      Returns:
      future called when the chunk is done saving
    • saveChunksToStorage

      @NotNull public @NotNull CompletableFuture<Void> saveChunksToStorage()
      Description copied from class: Instance
      Saves multiple chunks to permanent storage.
      Specified by:
      saveChunksToStorage in class Instance
      Returns:
      future called when the chunks are done saving
    • retrieveChunk

      @NotNull protected @NotNull CompletableFuture<@NotNull Chunk> retrieveChunk(int chunkX, int chunkZ)
    • createChunk

      @NotNull protected @NotNull CompletableFuture<@NotNull Chunk> createChunk(int chunkX, int chunkZ)
    • enableAutoChunkLoad

      public void enableAutoChunkLoad(boolean enable)
      Description copied from class: Instance
      When set to true, chunks will load automatically when requested. Otherwise using Instance.loadChunk(int, int) will be required to even spawn a player
      Specified by:
      enableAutoChunkLoad in class Instance
      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 class Instance
      Returns:
      true if auto chunk load is enabled, false otherwise
    • isInVoid

      public boolean isInVoid(@NotNull @NotNull Point point)
      Description copied from class: Instance
      Determines whether a position in the void.
      Specified by:
      isInVoid in class Instance
      Parameters:
      point - the point in the world
      Returns:
      true if the point is inside the void
    • setChunkSupplier

      public void setChunkSupplier(@NotNull @NotNull ChunkSupplier chunkSupplier)
      Changes which type of Chunk 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) to create the correct type of Chunk. tl;dr: Need chunk save = no random type.

      Specified by:
      setChunkSupplier in class Instance
      Parameters:
      chunkSupplier - the new ChunkSupplier of this instance, chunks need to be non-null
      Throws:
      NullPointerException - if chunkSupplier is null
    • getChunkSupplier

      public ChunkSupplier getChunkSupplier()
      Gets the current ChunkSupplier.

      You shouldn't use it to generate a new chunk, but as a way to view which one is currently in use.

      Specified by:
      getChunkSupplier in class Instance
      Returns:
      the current ChunkSupplier
    • getSharedInstances

      public List<SharedInstance> getSharedInstances()
      Gets all the SharedInstance linked to this container.
      Returns:
      an unmodifiable List containing all the SharedInstance linked to this container
    • hasSharedInstances

      public boolean hasSharedInstances()
      Gets if this instance has SharedInstance linked to it.
      Returns:
      true if getSharedInstances() is not empty
    • addSharedInstance

      protected void addSharedInstance(SharedInstance sharedInstance)
      Assigns a SharedInstance to this container.

      Only used by InstanceManager, mostly unsafe.

      Parameters:
      sharedInstance - the shared instance to assign to this container
    • copy

      public InstanceContainer copy()
      Copies all the chunks of this instance and create a new instance container with all of them.

      Chunks are copied with Chunk.copy(Instance, int, int), UUID is randomized and DimensionType is passed over.

      Returns:
      an InstanceContainer with the exact same chunks as 'this'
      See Also:
    • getSrcInstance

      @Nullable public @Nullable InstanceContainer 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:
    • 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.

    • generator

      @Nullable public @Nullable Generator generator()
      Description copied from class: Instance
      Gets the generator associated with the instance
      Specified by:
      generator in class Instance
      Returns:
      the generator if any
    • setGenerator

      public void setGenerator(@Nullable @Nullable Generator generator)
      Description copied from class: Instance
      Changes the generator of the instance
      Specified by:
      setGenerator in class Instance
      Parameters:
      generator - the new generator, or null to disable generation
    • getChunks

      @NotNull public @NotNull Collection<@NotNull Chunk> getChunks()
      Gets all the instance chunks.
      Specified by:
      getChunks in class Instance
      Returns:
      the chunks of this instance
    • getChunkLoader

      public IChunkLoader getChunkLoader()
      Gets the IChunkLoader of this instance.
      Returns:
      the IChunkLoader of this instance
    • setChunkLoader

      public void setChunkLoader(IChunkLoader chunkLoader)
      Changes the IChunkLoader of this instance (to change how chunks are retrieved when not already loaded).
      Parameters:
      chunkLoader - the new IChunkLoader
    • tick

      public void tick(long time)
      Description copied from class: Instance
      Performs a single tick in the instance, including scheduled tasks from Instance.scheduleNextTick(Consumer).

      Warning: this does not update chunks and entities.

      Specified by:
      tick in interface Tickable
      Overrides:
      tick in class Instance
      Parameters:
      time - the tick time in milliseconds