Class ChunkManager

java.lang.Object
me.tayebyassine.homestead.managers.ChunkManager

public final class ChunkManager extends Object
A utility class that manages RegionChunk.
  • 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
    • 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
    • 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
    • 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
    • getAll

      public static List<RegionChunk> getAll()
      Returns an immutable view of every loaded chunk.
      Returns:
      list of all 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, RegionChunk 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
    • 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
    • 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
    • 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
    • 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
    • reregisterForceLoadedChunks

      public static void reregisterForceLoadedChunks()
      Re-registers force-loaded chunks after a server restart.
    • 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
    • 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