Class BlocksManager


  • public class BlocksManager
    extends java.lang.Object
    Manages the loading, generating and mesh construction of chunk objects in a thread safe way. An internal cache is used to store and retrieve chunks from memory. The BlocksManager can be setup to delegate the loading, generation and mesh creation to worker threads when a pool size of > 0 is specified for the task.

    The BlocksManager needs to be initialized before it can be used and should always be cleaned up to properly shutdown the worker thread pools. It is a good practice to handle the lifecycle of the BlocksManager in an AppState to properly initialize, update and cleanup the class.

    A chunk can be retrieved by using the getChunk(Vec3i) method. When the chunk isn't available it can be requested with the requestChunk(Vec3i) method. The BlocksManager will first try to load the requested chunk using the ChunkRepository. When this is not successful it will try to generate the chunk using the ChunkGenerator. When this also fails, an empty chunk will be created. The requested chunk is placed in the cache and can be retrieved with the getChunk(Vec3i) method.

    When the node of a chunk in the cache is not set, the generation of the mesh is still in progress. Try to retrieve the node at a later time. Applications can register a BlocksManagerListener to be notified when the mesh of a chunk is updated.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Block addBlock​(@NonNull com.simsilica.mathd.Vec3i blockWorldLocation, Block block)
      Place a block at the given world location.
      void cleanup()  
      Chunk getChunk​(@NonNull com.simsilica.mathd.Vec3i location)
      Retrieves the chunk at the location from the cache.
      static com.simsilica.mathd.Vec3i getChunkLocation​(@NonNull com.jme3.math.Vector3f worldLocation)
      Calculate the location of the chunk that contains the passed world location.
      static com.simsilica.mathd.Vec3i getPickedBlockLocation​(@NonNull com.jme3.math.Vector3f contactPoint, @NonNull com.jme3.math.Vector3f contactNormal, boolean pickNeighbour)
      Calculate the picked block location based on the contact point and contact normals of a contact collision.
      boolean hasChunk​(@NonNull com.simsilica.mathd.Vec3i location)  
      void initialize()  
      void invalidateChunk​(@NonNull com.simsilica.mathd.Vec3i location)
      Discards the chunk associated with the location from the cache.
      void registerListener​(@NonNull BlocksManagerListener listener)
      Add a listener to the list of listeners that gets notified.
      Block removeBlock​(@NonNull com.simsilica.mathd.Vec3i blockWorldLocation)
      Removes and returns the block at the given world location.
      void removeListener​(@NonNull BlocksManagerListener listener)
      Remove a listener from the list of listeners that gets notified.
      boolean requestChunk​(@NonNull com.simsilica.mathd.Vec3i location)
      Request the chunk to be loaded into the cache.
      void saveChunks()
      Store all chunks that have changed data using the ChunkRepository
      void update()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BlocksManager

        public BlocksManager()
      • BlocksManager

        public BlocksManager​(int cacheSize)
    • Method Detail

      • initialize

        public void initialize()
      • update

        public void update()
      • cleanup

        public void cleanup()
      • getChunk

        public Chunk getChunk​(@NonNull
                              @NonNull com.simsilica.mathd.Vec3i location)
        Retrieves the chunk at the location from the cache.
        Parameters:
        location - of the chunk
        Returns:
        chunk or null when not found
      • hasChunk

        public boolean hasChunk​(@NonNull
                                @NonNull com.simsilica.mathd.Vec3i location)
        Parameters:
        location - of the chunk
        Returns:
        true when the chunk is in the cache, false otherwise
      • requestChunk

        public boolean requestChunk​(@NonNull
                                    @NonNull com.simsilica.mathd.Vec3i location)
        Request the chunk to be loaded into the cache. When the chunk cannot be loaded, it will be added to the generation queue.
        Parameters:
        location - of the chunk
        Returns:
        true when the chunk is successfully requested, false otherwise
      • invalidateChunk

        public void invalidateChunk​(@NonNull
                                    @NonNull com.simsilica.mathd.Vec3i location)
        Discards the chunk associated with the location from the cache.
        Parameters:
        location - of the chunk
      • saveChunks

        public void saveChunks()
        Store all chunks that have changed data using the ChunkRepository
      • registerListener

        public void registerListener​(@NonNull
                                     @NonNull BlocksManagerListener listener)
        Add a listener to the list of listeners that gets notified.
        Parameters:
        listener -
      • removeListener

        public void removeListener​(@NonNull
                                   @NonNull BlocksManagerListener listener)
        Remove a listener from the list of listeners that gets notified.
        Parameters:
        listener -
      • addBlock

        public Block addBlock​(@NonNull
                              @NonNull com.simsilica.mathd.Vec3i blockWorldLocation,
                              Block block)
        Place a block at the given world location. When the chunk to add the block to doesn't exist, a new one is created. If there was already a block on the location it will be replace by the new block.
        Parameters:
        blockWorldLocation - location in the world
        block - to add
        Returns:
        the previous block at the location or null
      • removeBlock

        public Block removeBlock​(@NonNull
                                 @NonNull com.simsilica.mathd.Vec3i blockWorldLocation)
        Removes and returns the block at the given world location.
        Parameters:
        blockWorldLocation - location in the world
        Returns:
        the block at the given location or null
      • getPickedBlockLocation

        public static com.simsilica.mathd.Vec3i getPickedBlockLocation​(@NonNull
                                                                       @NonNull com.jme3.math.Vector3f contactPoint,
                                                                       @NonNull
                                                                       @NonNull com.jme3.math.Vector3f contactNormal,
                                                                       boolean pickNeighbour)
        Calculate the picked block location based on the contact point and contact normals of a contact collision. By setting the pickNeighbour flag, the neighbour of the picked block will be returned.
        Parameters:
        contactPoint - collision contact point
        contactNormal - collision contact normal
        pickNeighbour - if the neighbour block should be returned
        Returns:
        the picked block or the neighbour of the picked block
      • getChunkLocation

        public static com.simsilica.mathd.Vec3i getChunkLocation​(@NonNull
                                                                 @NonNull com.jme3.math.Vector3f worldLocation)
        Calculate the location of the chunk that contains the passed world location.
        Parameters:
        worldLocation - location in the world
        Returns:
        the chunk location