Class ChunkManager
java.lang.Object
tfagaming.projects.minecraft.homestead.managers.ChunkManager
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.
This is a utility class that helps manage chunks more easily.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic ChunkManager.ErrorclaimChunk(long regionId, org.bukkit.Chunk chunk) Claims a chunk for a specific region with normal protection checks.static ChunkManager.ErrorclaimChunk(Region region, org.bukkit.Chunk chunk) Claims a chunk for a specific region with normal protection checks.static intRemoves all claimed chunks with invalid references:
- Worlds that no longer exist
- Regions that no longer exist
Also cleans orphaned force-loaded chunks.static intClean up force loaded chunks that are not claimed by any region.static RegionChunkfindChunk(long id) Retrieves the chunk with the exact ID, or null if none exists.static RegionChunkFinds a chunk by its world and coordinates.static RegionChunkfindChunk(org.bukkit.Chunk chunk) Finds a chunk by its Bukkit chunk.static org.bukkit.ChunkfindNearbyUnclaimedChunk(org.bukkit.entity.Player player) Finds a nearby unclaimed chunk around the player's current chunk within a radius up to 30.static ChunkManager.ErrorforceUnclaimChunk(long regionId, org.bukkit.Chunk chunk) Unclaims a chunk bypassing split/topology protection and ownership checks.static ChunkManager.ErrorforceUnclaimChunk(Region region, org.bukkit.Chunk chunk) Unclaims a chunk bypassing split/topology protection and ownership checks.static List<RegionChunk> getAll()Returns an immutable view of every loaded chunk.static List<RegionChunk> getBorderChunks(long regionId) Returns all chunks on the border of a region.static List<RegionChunk> getBorderChunks(Region 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[]getBoundingChunks(Region region) 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[]getChunkCentroid(Region region) Calculates the centroid (average center) of all chunks in a region.static intReturns the number of chunks in the server.static intgetChunkCount(long regionId) Returns the number of chunks owned by a region.static intgetChunkCount(Region region) Returns the number of chunks owned by a region.static intgetChunkDistanceToRegion(org.bukkit.Chunk chunk, long regionId) Calculates the Manhattan distance from a chunk to the nearest chunk owned by a region.static intgetChunkDistanceToRegion(org.bukkit.Chunk chunk, Region region) Calculates the Manhattan distance from a chunk to the nearest chunk owned by a region.static List<RegionChunk> getChunksInWorld(UUID worldId) Returns all claimed chunks in a specific world.static List<RegionChunk> getChunksInWorld(org.bukkit.World world) Returns all claimed chunks in a specific world.static List<RegionChunk> getChunksOfRegion(long regionId) Returns all chunks belonging to a region.static List<RegionChunk> getChunksOfRegion(Region region) Returns all chunks belonging to a region.static doublegetClaimedPercentage(org.bukkit.World world) Returns the percentage of a world's chunks that are claimed.static List<RegionChunk> getForceLoadedChunks(long regionId) Returns all force-loaded chunks belonging to a region.static List<RegionChunk> getForceLoadedChunks(Region region) Returns all force-loaded chunks belonging to a region.static org.bukkit.ChunkgetFromLocation(org.bukkit.World world, int x, int z) Returns the chunk object for the given world and chunk coordinates.getNeighborsOfRegion(long regionId) Returns all unique regions that are adjacent (share a chunk border) to the given region.getNeighborsOfRegion(Region region) Returns all unique regions that are adjacent (share a chunk border) to the given region.static intgetRegionChunkArea(long regionId) Returns the total chunk area (width * depth) of a region's bounding box.static intgetRegionChunkArea(Region region) Returns the total chunk area (width * depth) of a region's bounding box.static RegiongetRegionOwnsTheChunk(org.bukkit.Chunk chunk) Returns the region that owns a given chunk, or null if it is unclaimed.static intReturns the total number of claimed chunks across all regions.static booleanhasAdjacentOwnedChunk(long regionId, org.bukkit.Chunk chunk) Returns true if the provided chunk is adjacent to any chunk owned by the same region.static booleanhasNeighbor(org.bukkit.entity.Player player) Returnstrueif the player has any neighboring claimed chunks that belong to another region.static booleanisChunkClaimed(org.bukkit.Chunk chunk) Returns whether a given chunk is already claimed by any region.static booleanisChunkClaimedByRegion(long regionId, org.bukkit.Chunk chunk) Returnstrueif the region owns a given chunk,falseotherwise.static booleanisChunkClaimedByRegion(Region region, org.bukkit.Chunk chunk) Returnstrueif the region owns a given chunk,falseotherwise.static booleanisChunkInDisabledWorld(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 booleanisChunkOnBorder(long regionId, org.bukkit.Chunk chunk) Checks if a chunk is on the border (edge) of a region.static booleanisRegionSurrounded(long regionId) Checks if a region is completely surrounded by other regions (no unclaimed neighbors).static booleanisRegionSurrounded(Region region) Checks if a region is completely surrounded by other regions (no unclaimed neighbors).static intmergeRegions(long fromRegionId, long toRegionId) Merges all chunks from one region into another.static voidremoveRandomChunk(long regionId) Removes a random chunk from the region.static voidremoveRandomChunk(Region region) Removes a random chunk from the region.static voidRe-registers force-loaded chunks after a server restart.static booleantransferChunk(org.bukkit.Chunk chunk, long fromRegionId, long toRegionId) Transfers a chunk from one region to another.static ChunkManager.ErrorunclaimChunk(long regionId, org.bukkit.Chunk chunk) Unclaims a chunk with normal protection checks.static ChunkManager.ErrorunclaimChunk(Region region, org.bukkit.Chunk chunk) Unclaims a chunk with normal protection checks.static booleanwouldSplitRegion(long regionId, org.bukkit.Chunk chunkToRemove) Determines whether removing the specified chunk would split the region into multiple disconnected areas.
-
Method Details
-
claimChunk
Claims a chunk for a specific region with normal protection checks.- Parameters:
region- The regionchunk- The chunk- Returns:
ChunkManager.Errorif there is an error,nullotherwise.
-
claimChunk
Claims a chunk for a specific region with normal protection checks.- Parameters:
regionId- The region IDchunk- The chunk- Returns:
ChunkManager.Errorif there is an error,nullotherwise.
-
unclaimChunk
Unclaims a chunk with normal protection checks.- Parameters:
region- The regionchunk- The chunk- Returns:
ChunkManager.Errorif there is an error,nullotherwise.
-
unclaimChunk
Unclaims a chunk with normal protection checks.- Parameters:
regionId- The region IDchunk- The chunk- Returns:
ChunkManager.Errorif there is an error,nullotherwise.
-
forceUnclaimChunk
Unclaims a chunk bypassing split/topology protection and ownership checks.- Parameters:
region- The regionchunk- The chunk- Returns:
ChunkManager.Errorif there is an error,nullotherwise.
-
forceUnclaimChunk
Unclaims a chunk bypassing split/topology protection and ownership checks.- Parameters:
regionId- The region IDchunk- The chunk- Returns:
ChunkManager.Errorif there is an error,nullotherwise.
-
getChunksOfRegion
Returns all chunks belonging to a region.- Parameters:
region- The region- Returns:
- List of region chunks.
-
getChunksOfRegion
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
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
Returns all claimed chunks in a specific world.- Parameters:
world- The world- Returns:
- List of chunks in the world.
-
getChunksInWorld
Returns all claimed chunks in a specific world.- Parameters:
worldId- The world UUID- Returns:
- List of chunks in the world.
-
getForceLoadedChunks
Returns all force-loaded chunks belonging to a region.- Parameters:
region- The region- Returns:
- List of force-loaded chunks.
-
getForceLoadedChunks
Returns all force-loaded chunks belonging to a region.- Parameters:
regionId- The region ID- Returns:
- List of force-loaded chunks.
-
getChunkCentroid
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), ornullif 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), ornullif no chunks.
-
getBoundingChunks
Returns the bounding box of a region's chunks in chunk coordinates.- Parameters:
region- The region- Returns:
int[4]as {minX, minZ, maxX, maxZ}, ornullif 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}, ornullif no chunks.
-
getRegionChunkArea
Returns the total chunk area (width * depth) of a region's bounding box.- Parameters:
region- The region- Returns:
- The area in chunks, or
0if 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
0if 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 IDchunk- The chunk to check- Returns:
trueif the chunk touches the region edge.
-
getBorderChunks
Returns all chunks on the border of a region.- Parameters:
region- The region- Returns:
- List of border chunks.
-
getBorderChunks
Returns all chunks on the border of a region.- Parameters:
regionId- The region ID- Returns:
- List of border chunks.
-
getNeighborsOfRegion
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
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 transferfromRegionId- The current owning regiontoRegionId- The target region- Returns:
trueif 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 regiontoRegionId- The destination region- Returns:
- The number of chunks transferred.
-
getChunkDistanceToRegion
Calculates the Manhattan distance from a chunk to the nearest chunk owned by a region.- Parameters:
chunk- The starting chunkregion- The target region- Returns:
- The distance, or
-1if 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 chunkregionId- The target region ID- Returns:
- The distance, or
-1if 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.0if world has no border.
-
isRegionSurrounded
Checks if a region is completely surrounded by other regions (no unclaimed neighbors).- Parameters:
region- The region- Returns:
trueif 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:
trueif every border chunk neighbor is claimed by another region.
-
getAll
Returns an immutable view of every loaded chunk. -
findChunk
Retrieves the chunk with the exact ID, or null if none exists.- Parameters:
id- The chunk ID- Returns:
- The RegionChunk, or
null.
-
findChunk
Finds a chunk by its world and coordinates.- Parameters:
worldId- The world UUIDx- The chunk Xz- The chunk Z- Returns:
- The RegionChunk, or
null.
-
findChunk
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 IDchunkToRemove- The chunk that will be removed- Returns:
trueif 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:
trueif 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:
trueif claimed.
-
getRegionOwnsTheChunk
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
Returnstrueif the region owns a given chunk,falseotherwise.- Parameters:
region- The regionchunk- The chunk- Returns:
trueif owned by the region.
-
isChunkClaimedByRegion
public static boolean isChunkClaimedByRegion(long regionId, org.bukkit.Chunk chunk) Returnstrueif the region owns a given chunk,falseotherwise.- Parameters:
regionId- The region IDchunk- The chunk- Returns:
trueif 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 IDchunk- The chunk- Returns:
trueif 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) Returnstrueif the player has any neighboring claimed chunks that belong to another region.- Parameters:
player- The player- Returns:
trueif 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 worldx- The chunk coordinates (X axis)z- The chunk coordinates (Z axis)- Returns:
- The Bukkit Chunk.
-
removeRandomChunk
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.
-