Folia/patches/api/0005-Add-API-for-checking-ownership-of-region-by-position.patch
Spottedleaf 50ad6c3131 New scheduler API
Now, entity/global/location schedulers implement a generic run,
runDelayed, and runAtFixedRate methods that provide a ScheduledTask
value that can be used to interact with the scheduled task.

Add also an async task scheduler that implements the same methods,
except the delays/periods are in time and not ticks, as the scheduler
is independent of the server tick process.

Additionally, throw on some unimplemented APIs now.
2023-03-23 02:51:04 -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 22952628c894e29bfdb94897bd9970103730b898..62b7f94de5ffacd3919f9843e64a40f6f329fadd 100644
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
@@ -2496,6 +2496,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 ea4d93680066295de9fd447eda58b93014eac635..78cd452e804310eb8ed954833f2b7431ad9101b0 100644
--- a/src/main/java/org/bukkit/Server.java
+++ b/src/main/java/org/bukkit/Server.java
@@ -2170,5 +2170,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
}