java.lang.Object
tfagaming.projects.minecraft.homestead.managers.ChunkManager

public final class ChunkManager extends Object
Handles claiming, unclaiming, and managing chunks in regions. Includes adjacency enforcement, anti-split protection, and data handling.
This is a utility class that helps manage chunks more easily.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
     
  • Method Summary

    Modifier and Type
    Method
    Description
    claimChunk(long regionId, org.bukkit.Chunk chunk)
    Claims a chunk for a specific region with normal protection checks.
    claimChunk(Region region, org.bukkit.Chunk chunk)
    Claims a chunk for a specific region with normal protection checks.
    static int
    Removes all claimed chunks with invalid references:
    - Worlds that no longer exist
    - Regions that no longer exist

    Also cleans orphaned force-loaded chunks.
    static int
    Clean up force loaded chunks that are not claimed by any region.
    findChunk(long id)
    Retrieves the chunk with the exact ID, or null if none exists.
    findChunk(UUID worldId, int x, int z)
    Finds a chunk by its world and coordinates.
    findChunk(org.bukkit.Chunk chunk)
    Finds a chunk by its Bukkit chunk.
    static org.bukkit.Chunk
    findNearbyUnclaimedChunk(org.bukkit.entity.Player player)
    Finds a nearby unclaimed chunk around the player's current chunk within a radius up to 30.
    forceUnclaimChunk(long regionId, org.bukkit.Chunk chunk)
    Unclaims a chunk bypassing split/topology protection and ownership checks.
    forceUnclaimChunk(Region region, org.bukkit.Chunk chunk)
    Unclaims a chunk bypassing split/topology protection and ownership checks.
    Returns an immutable view of every loaded chunk.
    getBorderChunks(long regionId)
    Returns all chunks on the border of a region.
    Returns all chunks on the border of a region.
    static int[]
    getBoundingChunks(long regionId)
    Returns the bounding box of a region's chunks in chunk coordinates.
    static int[]
    Returns the bounding box of a region's chunks in chunk coordinates.
    static int[]
    getChunkCentroid(long regionId)
    Calculates the centroid (average center) of all chunks in a region.
    static int[]
    Calculates the centroid (average center) of all chunks in a region.
    static int
    Returns the number of chunks in the server.
    static int
    getChunkCount(long regionId)
    Returns the number of chunks owned by a region.
    static int
    Returns the number of chunks owned by a region.
    static int
    getChunkDistanceToRegion(org.bukkit.Chunk chunk, long regionId)
    Calculates the Manhattan distance from a chunk to the nearest chunk owned by a region.
    static int
    getChunkDistanceToRegion(org.bukkit.Chunk chunk, Region region)
    Calculates the Manhattan distance from a chunk to the nearest chunk owned by a region.
    Returns all claimed chunks in a specific world.
    getChunksInWorld(org.bukkit.World world)
    Returns all claimed chunks in a specific world.
    getChunksOfRegion(long regionId)
    Returns all chunks belonging to a region.
    Returns all chunks belonging to a region.
    static double
    getClaimedPercentage(org.bukkit.World world)
    Returns the percentage of a world's chunks that are claimed.
    getForceLoadedChunks(long regionId)
    Returns all force-loaded chunks belonging to a region.
    Returns all force-loaded chunks belonging to a region.
    static org.bukkit.Chunk
    getFromLocation(org.bukkit.World world, int x, int z)
    Returns the chunk object for the given world and chunk coordinates.
    static List<Region>
    getNeighborsOfRegion(long regionId)
    Returns all unique regions that are adjacent (share a chunk border) to the given region.
    static List<Region>
    Returns all unique regions that are adjacent (share a chunk border) to the given region.
    static int
    getRegionChunkArea(long regionId)
    Returns the total chunk area (width * depth) of a region's bounding box.
    static int
    Returns the total chunk area (width * depth) of a region's bounding box.
    static Region
    getRegionOwnsTheChunk(org.bukkit.Chunk chunk)
    Returns the region that owns a given chunk, or null if it is unclaimed.
    static int
    Returns the total number of claimed chunks across all regions.
    static boolean
    hasAdjacentOwnedChunk(long regionId, org.bukkit.Chunk chunk)
    Returns true if the provided chunk is adjacent to any chunk owned by the same region.
    static boolean
    hasNeighbor(org.bukkit.entity.Player player)
    Returns true if the player has any neighboring claimed chunks that belong to another region.
    static boolean
    isChunkClaimed(org.bukkit.Chunk chunk)
    Returns whether a given chunk is already claimed by any region.
    static boolean
    isChunkClaimedByRegion(long regionId, org.bukkit.Chunk chunk)
    Returns true if the region owns a given chunk, false otherwise.
    static boolean
    isChunkClaimedByRegion(Region region, org.bukkit.Chunk chunk)
    Returns true if the region owns a given chunk, false otherwise.
    static boolean
    isChunkInDisabledWorld(org.bukkit.Chunk chunk)
    Returns true if the chunk's world is on the exact list OR matches any configured glob-style pattern (supports * and ?).
    static boolean
    isChunkOnBorder(long regionId, org.bukkit.Chunk chunk)
    Checks if a chunk is on the border (edge) of a region.
    static boolean
    isRegionSurrounded(long regionId)
    Checks if a region is completely surrounded by other regions (no unclaimed neighbors).
    static boolean
    Checks if a region is completely surrounded by other regions (no unclaimed neighbors).
    static int
    mergeRegions(long fromRegionId, long toRegionId)
    Merges all chunks from one region into another.
    static void
    removeRandomChunk(long regionId)
    Removes a random chunk from the region.
    static void
    Removes a random chunk from the region.
    static void
    Re-registers force-loaded chunks after a server restart.
    static boolean
    transferChunk(org.bukkit.Chunk chunk, long fromRegionId, long toRegionId)
    Transfers a chunk from one region to another.
    unclaimChunk(long regionId, org.bukkit.Chunk chunk)
    Unclaims a chunk with normal protection checks.
    unclaimChunk(Region region, org.bukkit.Chunk chunk)
    Unclaims a chunk with normal protection checks.
    static boolean
    wouldSplitRegion(long regionId, org.bukkit.Chunk chunkToRemove)
    Determines whether removing the specified chunk would split the region into multiple disconnected areas.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • claimChunk

      public static ChunkManager.Error claimChunk(Region region, org.bukkit.Chunk chunk)
      Claims a chunk for a specific region with normal protection checks.
      Parameters:
      region - The region
      chunk - The chunk
      Returns:
      ChunkManager.Error if there is an error, null otherwise.
    • claimChunk

      public static ChunkManager.Error claimChunk(long regionId, org.bukkit.Chunk chunk)
      Claims a chunk for a specific region with normal protection checks.
      Parameters:
      regionId - The region ID
      chunk - The chunk
      Returns:
      ChunkManager.Error if there is an error, null otherwise.
    • unclaimChunk

      public static ChunkManager.Error unclaimChunk(Region region, org.bukkit.Chunk chunk)
      Unclaims a chunk with normal protection checks.
      Parameters:
      region - The region
      chunk - The chunk
      Returns:
      ChunkManager.Error if there is an error, null otherwise.
    • unclaimChunk

      public static ChunkManager.Error unclaimChunk(long regionId, org.bukkit.Chunk chunk)
      Unclaims a chunk with normal protection checks.
      Parameters:
      regionId - The region ID
      chunk - The chunk
      Returns:
      ChunkManager.Error if there is an error, null otherwise.
    • forceUnclaimChunk

      public static ChunkManager.Error forceUnclaimChunk(Region region, org.bukkit.Chunk chunk)
      Unclaims a chunk bypassing split/topology protection and ownership checks.
      Parameters:
      region - The region
      chunk - The chunk
      Returns:
      ChunkManager.Error if there is an error, null otherwise.
    • forceUnclaimChunk

      public static ChunkManager.Error forceUnclaimChunk(long regionId, org.bukkit.Chunk chunk)
      Unclaims a chunk bypassing split/topology protection and ownership checks.
      Parameters:
      regionId - The region ID
      chunk - The chunk
      Returns:
      ChunkManager.Error if there is an error, null otherwise.
    • getChunksOfRegion

      public static List<RegionChunk> getChunksOfRegion(Region region)
      Returns all chunks belonging to a region.
      Parameters:
      region - The region
      Returns:
      List of region chunks.
    • getChunksOfRegion

      public static List<RegionChunk> getChunksOfRegion(long regionId)
      Returns all chunks belonging to a region.
      Parameters:
      regionId - The region ID
      Returns:
      List of region chunks.
    • getChunkCount

      public static int getChunkCount()
      Returns the number of chunks in the server.
      Returns:
      The chunk count.
    • getChunkCount

      public static int getChunkCount(Region region)
      Returns the number of chunks owned by a region.
      Parameters:
      region - The region
      Returns:
      The chunk count.
    • getChunkCount

      public static int getChunkCount(long regionId)
      Returns the number of chunks owned by a region.
      Parameters:
      regionId - The region ID
      Returns:
      The chunk count.
    • getChunksInWorld

      public static List<RegionChunk> getChunksInWorld(org.bukkit.World world)
      Returns all claimed chunks in a specific world.
      Parameters:
      world - The world
      Returns:
      List of chunks in the world.
    • getChunksInWorld

      public static List<RegionChunk> getChunksInWorld(UUID worldId)
      Returns all claimed chunks in a specific world.
      Parameters:
      worldId - The world UUID
      Returns:
      List of chunks in the world.
    • getForceLoadedChunks

      public static List<RegionChunk> getForceLoadedChunks(Region region)
      Returns all force-loaded chunks belonging to a region.
      Parameters:
      region - The region
      Returns:
      List of force-loaded chunks.
    • getForceLoadedChunks

      public static List<RegionChunk> getForceLoadedChunks(long regionId)
      Returns all force-loaded chunks belonging to a region.
      Parameters:
      regionId - The region ID
      Returns:
      List of force-loaded chunks.
    • getChunkCentroid

      public static int[] getChunkCentroid(Region region)
      Calculates the centroid (average center) of all chunks in a region.
      Parameters:
      region - The region
      Returns:
      The centroid chunk coordinates as int[2] (x, z), or null if no chunks.
    • getChunkCentroid

      public static int[] getChunkCentroid(long regionId)
      Calculates the centroid (average center) of all chunks in a region.
      Parameters:
      regionId - The region ID
      Returns:
      The centroid chunk coordinates as int[2] (x, z), or null if no chunks.
    • getBoundingChunks

      public static int[] getBoundingChunks(Region region)
      Returns the bounding box of a region's chunks in chunk coordinates.
      Parameters:
      region - The region
      Returns:
      int[4] as {minX, minZ, maxX, maxZ}, or null if no chunks.
    • getBoundingChunks

      public static int[] getBoundingChunks(long regionId)
      Returns the bounding box of a region's chunks in chunk coordinates.
      Parameters:
      regionId - The region ID
      Returns:
      int[4] as {minX, minZ, maxX, maxZ}, or null if no chunks.
    • getRegionChunkArea

      public static int getRegionChunkArea(Region region)
      Returns the total chunk area (width * depth) of a region's bounding box.
      Parameters:
      region - The region
      Returns:
      The area in chunks, or 0 if no chunks.
    • getRegionChunkArea

      public static int getRegionChunkArea(long regionId)
      Returns the total chunk area (width * depth) of a region's bounding box.
      Parameters:
      regionId - The region ID
      Returns:
      The area in chunks, or 0 if no chunks.
    • isChunkOnBorder

      public static boolean isChunkOnBorder(long regionId, org.bukkit.Chunk chunk)
      Checks if a chunk is on the border (edge) of a region.
      Parameters:
      regionId - The region ID
      chunk - The chunk to check
      Returns:
      true if the chunk touches the region edge.
    • getBorderChunks

      public static List<RegionChunk> getBorderChunks(Region region)
      Returns all chunks on the border of a region.
      Parameters:
      region - The region
      Returns:
      List of border chunks.
    • getBorderChunks

      public static List<RegionChunk> getBorderChunks(long regionId)
      Returns all chunks on the border of a region.
      Parameters:
      regionId - The region ID
      Returns:
      List of border chunks.
    • getNeighborsOfRegion

      public static List<Region> getNeighborsOfRegion(Region region)
      Returns all unique regions that are adjacent (share a chunk border) to the given region.
      Parameters:
      region - The region
      Returns:
      List of neighboring regions.
    • getNeighborsOfRegion

      public static List<Region> getNeighborsOfRegion(long regionId)
      Returns all unique regions that are adjacent (share a chunk border) to the given region.
      Parameters:
      regionId - The region ID
      Returns:
      List of neighboring regions.
    • transferChunk

      public static boolean transferChunk(org.bukkit.Chunk chunk, long fromRegionId, long toRegionId)
      Transfers a chunk from one region to another.
      Parameters:
      chunk - The chunk to transfer
      fromRegionId - The current owning region
      toRegionId - The target region
      Returns:
      true if the transfer was successful.
    • mergeRegions

      public static int mergeRegions(long fromRegionId, long toRegionId)
      Merges all chunks from one region into another. The source region will have no chunks left.
      Parameters:
      fromRegionId - The source region
      toRegionId - The destination region
      Returns:
      The number of chunks transferred.
    • getChunkDistanceToRegion

      public static int getChunkDistanceToRegion(org.bukkit.Chunk chunk, Region region)
      Calculates the Manhattan distance from a chunk to the nearest chunk owned by a region.
      Parameters:
      chunk - The starting chunk
      region - The target region
      Returns:
      The distance, or -1 if the region has no chunks.
    • getChunkDistanceToRegion

      public static int getChunkDistanceToRegion(org.bukkit.Chunk chunk, long regionId)
      Calculates the Manhattan distance from a chunk to the nearest chunk owned by a region.
      Parameters:
      chunk - The starting chunk
      regionId - The target region ID
      Returns:
      The distance, or -1 if the region has no chunks.
    • getTotalClaimedChunks

      public static int getTotalClaimedChunks()
      Returns the total number of claimed chunks across all regions.
      Returns:
      The total claimed chunk count.
    • getClaimedPercentage

      public static double getClaimedPercentage(org.bukkit.World world)
      Returns the percentage of a world's chunks that are claimed.
      Parameters:
      world - The world
      Returns:
      Percentage from 0.0 to 100.0, or 0.0 if world has no border.
    • isRegionSurrounded

      public static boolean isRegionSurrounded(Region region)
      Checks if a region is completely surrounded by other regions (no unclaimed neighbors).
      Parameters:
      region - The region
      Returns:
      true if every border chunk neighbor is claimed by another region.
    • isRegionSurrounded

      public static boolean isRegionSurrounded(long regionId)
      Checks if a region is completely surrounded by other regions (no unclaimed neighbors).
      Parameters:
      regionId - The region ID
      Returns:
      true if every border chunk neighbor is claimed by another region.
    • getAll

      public static List<RegionChunk> getAll()
      Returns an immutable view of every loaded chunk.
    • findChunk

      public static RegionChunk findChunk(long id)
      Retrieves the chunk with the exact ID, or null if none exists.
      Parameters:
      id - The chunk ID
      Returns:
      The RegionChunk, or null.
    • findChunk

      public static RegionChunk findChunk(UUID worldId, int x, int z)
      Finds a chunk by its world and coordinates.
      Parameters:
      worldId - The world UUID
      x - The chunk X
      z - The chunk Z
      Returns:
      The RegionChunk, or null.
    • findChunk

      public static RegionChunk findChunk(org.bukkit.Chunk chunk)
      Finds a chunk by its Bukkit chunk.
      Parameters:
      chunk - The Bukkit chunk
      Returns:
      The RegionChunk, or null.
    • wouldSplitRegion

      public static boolean wouldSplitRegion(long regionId, org.bukkit.Chunk chunkToRemove)
      Determines whether removing the specified chunk would split the region into multiple disconnected areas.
      Parameters:
      regionId - The region ID
      chunkToRemove - The chunk that will be removed
      Returns:
      true if removal would split the region.
    • isChunkInDisabledWorld

      public static boolean isChunkInDisabledWorld(org.bukkit.Chunk chunk)
      Returns true if the chunk's world is on the exact list OR matches any configured glob-style pattern (supports * and ?).
      Parameters:
      chunk - The chunk
      Returns:
      true if the world is disabled.
    • isChunkClaimed

      public static boolean isChunkClaimed(org.bukkit.Chunk chunk)
      Returns whether a given chunk is already claimed by any region.
      Parameters:
      chunk - The chunk
      Returns:
      true if claimed.
    • getRegionOwnsTheChunk

      public static Region getRegionOwnsTheChunk(org.bukkit.Chunk chunk)
      Returns the region that owns a given chunk, or null if it is unclaimed.
      Parameters:
      chunk - The chunk
      Returns:
      The owning region, or null.
    • isChunkClaimedByRegion

      public static boolean isChunkClaimedByRegion(Region region, org.bukkit.Chunk chunk)
      Returns true if the region owns a given chunk, false otherwise.
      Parameters:
      region - The region
      chunk - The chunk
      Returns:
      true if owned by the region.
    • isChunkClaimedByRegion

      public static boolean isChunkClaimedByRegion(long regionId, org.bukkit.Chunk chunk)
      Returns true if the region owns a given chunk, false otherwise.
      Parameters:
      regionId - The region ID
      chunk - The chunk
      Returns:
      true if owned by the region.
    • hasAdjacentOwnedChunk

      public static boolean hasAdjacentOwnedChunk(long regionId, org.bukkit.Chunk chunk)
      Returns true if the provided chunk is adjacent to any chunk owned by the same region.
      Parameters:
      regionId - The region ID
      chunk - The chunk
      Returns:
      true if an adjacent owned chunk exists.
    • findNearbyUnclaimedChunk

      public static org.bukkit.Chunk findNearbyUnclaimedChunk(org.bukkit.entity.Player player)
      Finds a nearby unclaimed chunk around the player's current chunk within a radius up to 30.
      Parameters:
      player - The player
      Returns:
      An unclaimed chunk, or null.
    • hasNeighbor

      public static boolean hasNeighbor(org.bukkit.entity.Player player)
      Returns true if the player has any neighboring claimed chunks that belong to another region.
      Parameters:
      player - The player
      Returns:
      true if a foreign neighbor exists.
    • getFromLocation

      public static org.bukkit.Chunk getFromLocation(org.bukkit.World world, int x, int z)
      Returns the chunk object for the given world and chunk coordinates.
      Parameters:
      world - The world
      x - The chunk coordinates (X axis)
      z - The chunk coordinates (Z axis)
      Returns:
      The Bukkit Chunk.
    • removeRandomChunk

      public static void removeRandomChunk(Region region)
      Removes a random chunk from the region.
      Parameters:
      region - The region
    • removeRandomChunk

      public static void removeRandomChunk(long regionId)
      Removes a random chunk from the region.
      Parameters:
      regionId - The region ID
    • reregisterForceLoadedChunks

      public static void reregisterForceLoadedChunks()
      Re-registers force-loaded chunks after a server restart.
    • cleanupOrphanedForceLoadedChunks

      public static int cleanupOrphanedForceLoadedChunks()
      Clean up force loaded chunks that are not claimed by any region.
      Returns:
      The number of orphaned chunks cleaned up.
    • cleanupInvalidChunks

      public static int cleanupInvalidChunks()
      Removes all claimed chunks with invalid references:
      - Worlds that no longer exist
      - Regions that no longer exist

      Also cleans orphaned force-loaded chunks.
      Returns:
      Number of corrupted chunks removed.