Add Server#isGlobalTickThread

This method should be present in Paper, not just in Folia, given
that the GlobalRegionScheduler is present.

Additonally, add Server#isOwnedByCurrentRegion(World, int, int, int, int)
for checking of a rectangle of chunks is owned by the current region.
This commit is contained in:
Spottedleaf 2024-11-25 10:43:28 -08:00
parent c9731fc93d
commit 22dea6efbd
3 changed files with 61 additions and 7 deletions

View file

@ -10,7 +10,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+++ b/src/main/java/org/bukkit/Server.java
@@ -0,0 +0,0 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
*/
boolean isOwnedByCurrentRegion(@NotNull Entity entity);
public boolean isGlobalTickThread();
// Paper end - Folia region threading API
+
+ // Paper start - API to check if the server is sleeping

View file

@ -52,7 +52,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ * @return The {@link ScheduledTask} that represents the scheduled task.
+ */
+ @NotNull ScheduledTask runDelayed(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task, long delay,
+ @NotNull TimeUnit unit);
+ @NotNull TimeUnit unit);
+
+ /**
+ * Schedules the specified task to be executed asynchronously after the initial delay has passed,
@ -65,7 +65,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ * @return The {@link ScheduledTask} that represents the scheduled task.
+ */
+ @NotNull ScheduledTask runAtFixedRate(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task,
+ long initialDelay, long period, @NotNull TimeUnit unit);
+ long initialDelay, long period, @NotNull TimeUnit unit);
+
+ /**
+ * Attempts to cancel all tasks scheduled by the specified plugin.
@ -142,7 +142,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ * @return The {@link ScheduledTask} that represents the scheduled task, or {@code null} if the entity has been removed.
+ */
+ @Nullable ScheduledTask run(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task,
+ @Nullable Runnable retired);
+ @Nullable Runnable retired);
+
+ /**
+ * Schedules a task with the given delay. If the task failed to schedule because the scheduler is retired (entity
@ -161,7 +161,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ * @return The {@link ScheduledTask} that represents the scheduled task, or {@code null} if the entity has been removed.
+ */
+ @Nullable ScheduledTask runDelayed(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task,
+ @Nullable Runnable retired, long delayTicks);
+ @Nullable Runnable retired, long delayTicks);
+
+ /**
+ * Schedules a repeating task with the given delay and period. If the task failed to schedule because the scheduler
@ -181,7 +181,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ * @return The {@link ScheduledTask} that represents the scheduled task, or {@code null} if the entity has been removed.
+ */
+ @Nullable ScheduledTask runAtFixedRate(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task,
+ @Nullable Runnable retired, long initialDelayTicks, long periodTicks);
+ @Nullable Runnable retired, long initialDelayTicks, long periodTicks);
+}
diff --git a/src/main/java/io/papermc/paper/threadedregions/scheduler/GlobalRegionScheduler.java b/src/main/java/io/papermc/paper/threadedregions/scheduler/GlobalRegionScheduler.java
new file mode 100644
@ -239,7 +239,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ * @return The {@link ScheduledTask} that represents the scheduled task.
+ */
+ @NotNull ScheduledTask runAtFixedRate(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task,
+ long initialDelayTicks, long periodTicks);
+ long initialDelayTicks, long periodTicks);
+
+ /**
+ * Attempts to cancel all tasks scheduled by the specified plugin.
@ -631,6 +631,21 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+ /**
+ * Returns whether the current thread is ticking a region and that the region being ticked
+ * owns the chunks in the rectangle specified by the min and max parameters.
+ * Specifically, this function checks that every chunk with position x in [minChunkX, maxChunkX] and
+ * position z in [minChunkZ, maxChunkZ] is owned by the current ticking region.
+ * @param world Specified world.
+ * @param minChunkX Specified x-coordinate of the minimum chunk position.
+ * @param minChunkZ Specified z-coordinate of the minimum chunk position.
+ * @param maxChunkX Specified x-coordinate of the maximum chunk position.
+ * @param maxChunkZ Specified z-coordinate of the maximum chunk position.
+ */
+ public static boolean isOwnedByCurrentRegion(@NotNull World world, int minChunkX, int minChunkZ, int maxChunkX, int maxChunkZ) {
+ return server.isOwnedByCurrentRegion(world, minChunkX, minChunkZ, maxChunkX, maxChunkZ);
+ }
+
+ /**
+ * 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.
@ -639,6 +654,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ public static boolean isOwnedByCurrentRegion(@NotNull Entity entity) {
+ return server.isOwnedByCurrentRegion(entity);
+ }
+
+ /**
+ * Returns whether the current thread is ticking the global region.
+ * @see io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler
+ */
+ public static boolean isGlobalTickThread() {
+ return server.isGlobalTickThread();
+ }
+ // Paper end - Folia region threading API
+
@NotNull
@ -760,12 +783,31 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+ /**
+ * Returns whether the current thread is ticking a region and that the region being ticked
+ * owns the chunks in the rectangle specified by the min and max parameters.
+ * Specifically, this function checks that every chunk with position x in [minChunkX, maxChunkX] and
+ * position z in [minChunkZ, maxChunkZ] is owned by the current ticking region.
+ * @param world Specified world.
+ * @param minChunkX Specified x-coordinate of the minimum chunk position.
+ * @param minChunkZ Specified z-coordinate of the minimum chunk position.
+ * @param maxChunkX Specified x-coordinate of the maximum chunk position.
+ * @param maxChunkZ Specified z-coordinate of the maximum chunk position.
+ */
+ boolean isOwnedByCurrentRegion(@NotNull World world, int minChunkX, int minChunkZ, int maxChunkX, int maxChunkZ);
+
+ /**
+ * 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.
+ */
+ boolean isOwnedByCurrentRegion(@NotNull Entity entity);
+
+ /**
+ * Returns whether the current thread is ticking the global region.
+ * @see io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler
+ */
+ public boolean isGlobalTickThread();
+ // Paper end - Folia region threading API
}
diff --git a/src/main/java/org/bukkit/entity/Entity.java b/src/main/java/org/bukkit/entity/Entity.java

View file

@ -1321,9 +1321,21 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ @Override
+ public final boolean isOwnedByCurrentRegion(World world, int minChunkX, int minChunkZ, int maxChunkX, int maxChunkZ) {
+ return ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(
+ ((CraftWorld) world).getHandle(), minChunkX, minChunkZ, maxChunkX, maxChunkZ
+ );
+ }
+
+ @Override
+ public final boolean isOwnedByCurrentRegion(Entity entity) {
+ return ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(((org.bukkit.craftbukkit.entity.CraftEntity) entity).getHandleRaw());
+ }
+
+ @Override
+ public final boolean isGlobalTickThread() {
+ return ca.spottedleaf.moonrise.common.util.TickThread.isTickThread();
+ }
+ // Paper end - Folia reagion threading API
+
static {