Folia/patches/api/0005-Add-API-for-checking-ownership-of-region-by-position.patch
Spottedleaf 7e948a6179 Add global region scheduler
This will allow plugins to safely execute commands or perform
other global tick thread data modification.
2023-03-19 16:43:34 -07:00

206 lines
11 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
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 4ae25cb199c6e20b51c4a1e7ac4ff55b23ae724f..b8c62f431bf802a80fa097f1e5f8e6247badd0ea 100644
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
@@ -2487,6 +2487,100 @@ 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 org.bukkit.util.Vector 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 <i>not</i> a <i>squared</i>
+ * radius, but rather a <i>Chebyshev Distance</i>.
+ */
+ public static boolean isOwnedByCurrentRegion(@NotNull World world, @NotNull org.bukkit.util.Vector 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 <i>not</i> a <i>squared</i>
+ * radius, but rather a <i>Chebyshev Distance</i>.
+ */
+ 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);
+ }
+
+ /**
+ * 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 <i>not</i> a <i>squared</i>
+ * radius, but rather a <i>Chebyshev Distance</i>.
+ */
+ 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);
+ }
// 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 1092ceef77bad421df647d349d997d02b2ba80a9..3a17ccd041ca64c8ab0cd3edf4ce6fc1c6f2d459 100644
--- a/src/main/java/org/bukkit/Server.java
+++ b/src/main/java/org/bukkit/Server.java
@@ -2163,5 +2163,83 @@ 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 org.bukkit.util.Vector 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 <i>not</i> a <i>squared</i>
+ * radius, but rather a <i>Chebyshev Distance</i>.
+ */
+ public boolean isOwnedByCurrentRegion(@NotNull World world, @NotNull org.bukkit.util.Vector 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 <i>not</i> a <i>squared</i>
+ * radius, but rather a <i>Chebyshev Distance</i>.
+ */
+ 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.
+ */
+ public boolean isOwnedByCurrentRegion(@NotNull org.bukkit.block.Block block);
+
+ /**
+ * 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 <i>not</i> a <i>squared</i>
+ * radius, but rather a <i>Chebyshev Distance</i>.
+ */
+ 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);
// Folia end - region threading API
}