From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Tue, 7 Mar 2023 14:34:09 -0800 Subject: [PATCH] Add API for checking ownership of region by position/entity This may be useful for plugins which want to perform operations over large areas outside of the buffer zone provided by the regionaliser, as it is not guaranteed that anything outside of the buffer zone is owned. Then, the plugins may use the schedulers depending on the result of the ownership check. diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java index aa2d0af0b0a783000e993fa622dd14e32b1914e3..92678097e5255a8c7ff7b6b302ccb99f13c6f2e6 100644 --- a/src/main/java/org/bukkit/Bukkit.java +++ b/src/main/java/org/bukkit/Bukkit.java @@ -2496,6 +2496,107 @@ public final class Bukkit { public static @NotNull io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler getGlobalRegionScheduler() { return server.getGlobalRegionScheduler(); } + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunk at the specified world and block position. + * @param world Specified world. + * @param position Specified block position. + */ + public static boolean isOwnedByCurrentRegion(@NotNull World world, @NotNull io.papermc.paper.math.Position position) { + return server.isOwnedByCurrentRegion(world, position); + } + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunks centered at the specified block position within the specified square radius. + * Specifically, this function checks that every chunk with position x in [centerX - radius, centerX + radius] and + * position z in [centerZ - radius, centerZ + radius] is owned by the current ticking region. + * @param world Specified world. + * @param position Specified block position. + * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is not a squared + * radius, but rather a Chebyshev Distance. + */ + public static boolean isOwnedByCurrentRegion(@NotNull World world, @NotNull io.papermc.paper.math.Position position, int squareRadiusChunks) { + return server.isOwnedByCurrentRegion(world, position, squareRadiusChunks); + } + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunk at the specified world and block position as included in the specified location. + * @param location Specified location, must have a non-null world. + */ + public static boolean isOwnedByCurrentRegion(@NotNull Location location) { + return server.isOwnedByCurrentRegion(location); + } + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunks centered at the specified world and block position as included in the specified location + * within the specified square radius. + * Specifically, this function checks that every chunk with position x in [centerX - radius, centerX + radius] and + * position z in [centerZ - radius, centerZ + radius] is owned by the current ticking region. + * @param location Specified location, must have a non-null world. + * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is not a squared + * radius, but rather a Chebyshev Distance. + */ + public static boolean isOwnedByCurrentRegion(@NotNull Location location, int squareRadiusChunks) { + return server.isOwnedByCurrentRegion(location, squareRadiusChunks); + } + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunk at the specified block position. + * @param block Specified block position. + */ + public static boolean isOwnedByCurrentRegion(@NotNull org.bukkit.block.Block block) { + return server.isOwnedByCurrentRegion(block.getLocation()); + } + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunk at the specified world and chunk position. + * @param world Specified world. + * @param chunkX Specified x-coordinate of the chunk position. + * @param chunkZ Specified z-coordinate of the chunk position. + */ + public static boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ) { + return server.isOwnedByCurrentRegion(world, chunkX, chunkZ); + } + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunks centered at the specified world and chunk position within the specified + * square radius. + * Specifically, this function checks that every chunk with position x in [centerX - radius, centerX + radius] and + * position z in [centerZ - radius, centerZ + radius] is owned by the current ticking region. + * @param world Specified world. + * @param chunkX Specified x-coordinate of the chunk position. + * @param chunkZ Specified z-coordinate of the chunk position. + * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is not a squared + * radius, but rather a Chebyshev Distance. + */ + public static boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ, int squareRadiusChunks) { + return server.isOwnedByCurrentRegion(world, chunkX, chunkZ, squareRadiusChunks); + } + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the specified entity. Note that this function is the only appropriate method of checking + * for ownership of an entity, as retrieving the entity's location is undefined unless the entity is owned + * by the current region. + * @param entity Specified entity. + */ + public static boolean isOwnedByCurrentRegion(@NotNull Entity entity) { + return server.isOwnedByCurrentRegion(entity); + } + + /** + * Returns whether the current thread is ticking the global region. + */ + public static boolean isGlobalTickThread() { + return server.isGlobalTickThread(); + } // Folia end - region threading API @NotNull diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java index 2f1d1edbe802f66962e2763d49a025fbc657c99f..717635388aa449fd2e24692b14ceccc8a0f762c1 100644 --- a/src/main/java/org/bukkit/Server.java +++ b/src/main/java/org/bukkit/Server.java @@ -2170,5 +2170,90 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi * @return the global region scheduler */ public @NotNull io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler getGlobalRegionScheduler(); + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunk at the specified world and block position. + * @param world Specified world. + * @param position Specified block position. + */ + public boolean isOwnedByCurrentRegion(@NotNull World world, @NotNull io.papermc.paper.math.Position position); + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunks centered at the specified block position within the specified square radius. + * Specifically, this function checks that every chunk with position x in [centerX - radius, centerX + radius] and + * position z in [centerZ - radius, centerZ + radius] is owned by the current ticking region. + * @param world Specified world. + * @param position Specified block position. + * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is not a squared + * radius, but rather a Chebyshev Distance. + */ + public boolean isOwnedByCurrentRegion(@NotNull World world, @NotNull io.papermc.paper.math.Position position, int squareRadiusChunks); + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunk at the specified world and block position as included in the specified location. + * @param location Specified location, must have a non-null world. + */ + public boolean isOwnedByCurrentRegion(@NotNull Location location); + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunks centered at the specified world and block position as included in the specified location + * within the specified square radius. + * Specifically, this function checks that every chunk with position x in [centerX - radius, centerX + radius] and + * position z in [centerZ - radius, centerZ + radius] is owned by the current ticking region. + * @param location Specified location, must have a non-null world. + * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is not a squared + * radius, but rather a Chebyshev Distance. + */ + public boolean isOwnedByCurrentRegion(@NotNull Location location, int squareRadiusChunks); + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunk at the specified block position. + * @param block Specified block position. + */ + default boolean isOwnedByCurrentRegion(@NotNull org.bukkit.block.Block block) { + return isOwnedByCurrentRegion(block.getLocation()); + } + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunk at the specified world and chunk position. + * @param world Specified world. + * @param chunkX Specified x-coordinate of the chunk position. + * @param chunkZ Specified z-coordinate of the chunk position. + */ + public boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ); + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the chunks centered at the specified world and chunk position within the specified + * square radius. + * Specifically, this function checks that every chunk with position x in [centerX - radius, centerX + radius] and + * position z in [centerZ - radius, centerZ + radius] is owned by the current ticking region. + * @param world Specified world. + * @param chunkX Specified x-coordinate of the chunk position. + * @param chunkZ Specified z-coordinate of the chunk position. + * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is not a squared + * radius, but rather a Chebyshev Distance. + */ + public boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ, int squareRadiusChunks); + + /** + * Returns whether the current thread is ticking a region and that the region being ticked + * owns the specified entity. Note that this function is the only appropriate method of checking + * for ownership of an entity, as retrieving the entity's location is undefined unless the entity is owned + * by the current region. + * @param entity Specified entity. + */ + public boolean isOwnedByCurrentRegion(@NotNull Entity entity); + + /** + * Returns whether the current thread is ticking the global region. + */ + public boolean isGlobalTickThread(); // Folia end - region threading API }