From 32a83295acb76faa8e052ed6c1c0cc2e0be75fd5 Mon Sep 17 00:00:00 2001 From: Collin Barber Date: Wed, 19 Jun 2024 16:19:08 -0400 Subject: [PATCH 1/3] add isOwnedByCurrentRegion methods --- .../folialib/impl/ServerImplementation.java | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java b/platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java index 216c1c2..51cabd0 100644 --- a/platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java +++ b/platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java @@ -2,7 +2,10 @@ import com.tcoded.folialib.enums.EntityTaskResult; import com.tcoded.folialib.wrapper.task.WrappedTask; +import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -21,6 +24,117 @@ @Deprecated public interface ServerImplementation { + // ----- Check thread ----- + + /** + * Folia: 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. + * Paper: Returns {@link Bukkit#isPrimaryThread()} + * Spigot: Returns {@link Bukkit#isPrimaryThread()} + * + * @param location Specified location, must have a non-null world + * @return true if the current thread is ticking the region that owns the chunk at the specified location + */ + default boolean isOwnedByCurrentRegion(@NotNull Location location) { + return Bukkit.isPrimaryThread(); + } + + /** + * Folia: 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. + * Paper: Returns {@link Bukkit#isPrimaryThread()} + * Spigot: Returns {@link Bukkit#isPrimaryThread()} + * + * @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 + * @return true if the current thread is ticking the region that owns the chunks centered at the specified location within the specified square radius + */ + default boolean isOwnedByCurrentRegion(@NotNull Location location, int squareRadiusChunks) { + return Bukkit.isPrimaryThread(); + } + + /** + * Folia: Returns whether the current thread is ticking a region and that + * the region being ticked owns the chunk at the specified block position. + * Paper: Returns {@link Bukkit#isPrimaryThread()} + * Spigot: Returns {@link Bukkit#isPrimaryThread()} + * + * @param block Specified block position + * @return true if the current thread is ticking the region that owns the chunk at the specified block position + */ + default boolean isOwnedByCurrentRegion(@NotNull Block block) { + return Bukkit.isPrimaryThread(); + } + + /** + * Folia: 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. + * Paper: Returns {@link Bukkit#isPrimaryThread()} + * Spigot: Returns {@link Bukkit#isPrimaryThread()} + * + * @param world Specified world + * @param chunkX Specified x-coordinate of the chunk position + * @param chunkZ Specified z-coordinate of the chunk position + * @return true if the current thread is ticking the region that owns the chunk at the specified world and chunk position + */ + default boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ) { + return Bukkit.isPrimaryThread(); + } + + /** + * Folia: 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. + * Paper: Returns {@link Bukkit#isPrimaryThread()} + * Spigot: Returns {@link Bukkit#isPrimaryThread()} + * + * @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. + * @return true if the current thread is ticking the region that owns the chunks centered at the specified world and chunk position within the specified square radius + */ + default boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ, int squareRadiusChunks) { + return Bukkit.isPrimaryThread(); + } + + /** + * Folia: 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. + * Paper: Returns {@link Bukkit#isPrimaryThread()} + * Spigot: Returns {@link Bukkit#isPrimaryThread()} + * + * @param entity Specified entity + * @return true if the current thread is ticking the region that owns the specified entity + */ + default boolean isOwnedByCurrentRegion(@NotNull Entity entity) { + return Bukkit.isPrimaryThread(); + } + + /** + * Folia: Returns whether the current thread is ticking the global region. + * Paper: Returns {@link Bukkit#isPrimaryThread()} + * Spigot: Returns {@link Bukkit#isPrimaryThread()} + * + * @return true if the current thread is ticking the global region + */ + default boolean isGlobalTickThread() { + return Bukkit.isPrimaryThread(); + } + // ----- Run now ----- /** From 5ebe84047479fb5330ac566eb15af63cdf9f5949 Mon Sep 17 00:00:00 2001 From: Collin Barber Date: Wed, 19 Jun 2024 16:21:52 -0400 Subject: [PATCH 2/3] implement isOwnedByCurrentRegion methods --- .../folialib/impl/FoliaImplementation.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java b/platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java index 7c02493..87885c7 100644 --- a/platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java +++ b/platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java @@ -10,7 +10,10 @@ import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler; import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; +import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -40,7 +43,42 @@ public FoliaImplementation(FoliaLib foliaLib) { this.asyncScheduler = plugin.getServer().getAsyncScheduler(); } - @Override + @Override + public boolean isOwnedByCurrentRegion(@NotNull Location location) { + return Bukkit.isOwnedByCurrentRegion(location); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull Location location, int squareRadiusChunks) { + return Bukkit.isOwnedByCurrentRegion(location, squareRadiusChunks); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull Block block) { + return Bukkit.isOwnedByCurrentRegion(block); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ) { + return Bukkit.isOwnedByCurrentRegion(world, chunkX, chunkZ); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ, int squareRadiusChunks) { + return Bukkit.isOwnedByCurrentRegion(world, chunkX, chunkZ, squareRadiusChunks); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull Entity entity) { + return Bukkit.isOwnedByCurrentRegion(entity); + } + + @Override + public boolean isGlobalTickThread() { + return Bukkit.isGlobalTickThread(); + } + + @Override public CompletableFuture runNextTick(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); From 7d54d95f62b13075bcf2a7d6a436cf15ca2eb95b Mon Sep 17 00:00:00 2001 From: TechnicallyCoded Date: Wed, 3 Jul 2024 11:43:45 +0200 Subject: [PATCH 3/3] Adjust implementation details --- .../folialib/impl/ServerImplementation.java | 28 ++++---------- .../folialib/impl/FoliaImplementation.java | 14 +++---- .../impl/LegacySpigotImplementation.java | 37 +++++++++++++++++++ .../folialib/impl/SpigotImplementation.java | 37 +++++++++++++++++++ 4 files changed, 88 insertions(+), 28 deletions(-) diff --git a/platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java b/platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java index 8fdbdf1..b8a2776 100644 --- a/platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java +++ b/platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java @@ -36,9 +36,7 @@ public interface ServerImplementation { * @param location Specified location, must have a non-null world * @return true if the current thread is ticking the region that owns the chunk at the specified location */ - default boolean isOwnedByCurrentRegion(@NotNull Location location) { - return Bukkit.isPrimaryThread(); - } + boolean isOwnedByCurrentRegion(@NotNull Location location); /** * Folia: Returns whether the current thread is ticking a region and that @@ -55,9 +53,7 @@ default boolean isOwnedByCurrentRegion(@NotNull Location location) { * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is not a squared radius, but rather a Chebyshev Distance * @return true if the current thread is ticking the region that owns the chunks centered at the specified location within the specified square radius */ - default boolean isOwnedByCurrentRegion(@NotNull Location location, int squareRadiusChunks) { - return Bukkit.isPrimaryThread(); - } + boolean isOwnedByCurrentRegion(@NotNull Location location, int squareRadiusChunks); /** * Folia: Returns whether the current thread is ticking a region and that @@ -68,9 +64,7 @@ default boolean isOwnedByCurrentRegion(@NotNull Location location, int squareRad * @param block Specified block position * @return true if the current thread is ticking the region that owns the chunk at the specified block position */ - default boolean isOwnedByCurrentRegion(@NotNull Block block) { - return Bukkit.isPrimaryThread(); - } + boolean isOwnedByCurrentRegion(@NotNull Block block); /** * Folia: Returns whether the current thread is ticking a region and that @@ -84,9 +78,7 @@ default boolean isOwnedByCurrentRegion(@NotNull Block block) { * @param chunkZ Specified z-coordinate of the chunk position * @return true if the current thread is ticking the region that owns the chunk at the specified world and chunk position */ - default boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ) { - return Bukkit.isPrimaryThread(); - } + boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ); /** * Folia: Returns whether the current thread is ticking a region and that @@ -104,9 +96,7 @@ default boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chu * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is not a squared radius, but rather a Chebyshev Distance. * @return true if the current thread is ticking the region that owns the chunks centered at the specified world and chunk position within the specified square radius */ - default boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ, int squareRadiusChunks) { - return Bukkit.isPrimaryThread(); - } + boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ, int squareRadiusChunks); /** * Folia: Returns whether the current thread is ticking a region and that @@ -120,9 +110,7 @@ default boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chu * @param entity Specified entity * @return true if the current thread is ticking the region that owns the specified entity */ - default boolean isOwnedByCurrentRegion(@NotNull Entity entity) { - return Bukkit.isPrimaryThread(); - } + boolean isOwnedByCurrentRegion(@NotNull Entity entity); /** * Folia: Returns whether the current thread is ticking the global region. @@ -131,9 +119,7 @@ default boolean isOwnedByCurrentRegion(@NotNull Entity entity) { * * @return true if the current thread is ticking the global region */ - default boolean isGlobalTickThread() { - return Bukkit.isPrimaryThread(); - } + boolean isGlobalTickThread(); // ----- Run now ----- diff --git a/platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java b/platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java index d6b2b71..1d8ddee 100644 --- a/platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java +++ b/platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java @@ -45,37 +45,37 @@ public FoliaImplementation(FoliaLib foliaLib) { @Override public boolean isOwnedByCurrentRegion(@NotNull Location location) { - return Bukkit.isOwnedByCurrentRegion(location); + return this.plugin.getServer().isOwnedByCurrentRegion(location); } @Override public boolean isOwnedByCurrentRegion(@NotNull Location location, int squareRadiusChunks) { - return Bukkit.isOwnedByCurrentRegion(location, squareRadiusChunks); + return this.plugin.getServer().isOwnedByCurrentRegion(location, squareRadiusChunks); } @Override public boolean isOwnedByCurrentRegion(@NotNull Block block) { - return Bukkit.isOwnedByCurrentRegion(block); + return this.plugin.getServer().isOwnedByCurrentRegion(block); } @Override public boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ) { - return Bukkit.isOwnedByCurrentRegion(world, chunkX, chunkZ); + return this.plugin.getServer().isOwnedByCurrentRegion(world, chunkX, chunkZ); } @Override public boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ, int squareRadiusChunks) { - return Bukkit.isOwnedByCurrentRegion(world, chunkX, chunkZ, squareRadiusChunks); + return this.plugin.getServer().isOwnedByCurrentRegion(world, chunkX, chunkZ, squareRadiusChunks); } @Override public boolean isOwnedByCurrentRegion(@NotNull Entity entity) { - return Bukkit.isOwnedByCurrentRegion(entity); + return this.plugin.getServer().isOwnedByCurrentRegion(entity); } @Override public boolean isGlobalTickThread() { - return Bukkit.isGlobalTickThread(); + return this.plugin.getServer().isGlobalTickThread(); } @Override diff --git a/platform/legacy-spigot/src/main/java/com/tcoded/folialib/impl/LegacySpigotImplementation.java b/platform/legacy-spigot/src/main/java/com/tcoded/folialib/impl/LegacySpigotImplementation.java index 599c775..6ec5311 100644 --- a/platform/legacy-spigot/src/main/java/com/tcoded/folialib/impl/LegacySpigotImplementation.java +++ b/platform/legacy-spigot/src/main/java/com/tcoded/folialib/impl/LegacySpigotImplementation.java @@ -8,6 +8,8 @@ import com.tcoded.folialib.wrapper.task.WrappedTask; import com.tcoded.folialib.wrapper.task.WrappedLegacyBukkitTask; import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -36,6 +38,41 @@ public LegacySpigotImplementation(FoliaLib foliaLib) { this.scheduler = plugin.getServer().getScheduler(); } + @Override + public boolean isOwnedByCurrentRegion(@NotNull Location location) { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull Location location, int squareRadiusChunks) { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull Block block) { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ) { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ, int squareRadiusChunks) { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull Entity entity) { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public boolean isGlobalTickThread() { + return this.plugin.getServer().isPrimaryThread(); + } + @Override public @NotNull CompletableFuture runNextTick(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>(); diff --git a/platform/spigot/src/main/java/com/tcoded/folialib/impl/SpigotImplementation.java b/platform/spigot/src/main/java/com/tcoded/folialib/impl/SpigotImplementation.java index a76f929..3f83186 100644 --- a/platform/spigot/src/main/java/com/tcoded/folialib/impl/SpigotImplementation.java +++ b/platform/spigot/src/main/java/com/tcoded/folialib/impl/SpigotImplementation.java @@ -6,6 +6,8 @@ import com.tcoded.folialib.wrapper.task.WrappedTask; import com.tcoded.folialib.wrapper.task.WrappedBukkitTask; import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -34,6 +36,41 @@ public SpigotImplementation(FoliaLib foliaLib) { this.scheduler = plugin.getServer().getScheduler(); } + @Override + public boolean isOwnedByCurrentRegion(@NotNull Location location) { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull Location location, int squareRadiusChunks) { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull Block block) { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ) { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ, int squareRadiusChunks) { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public boolean isOwnedByCurrentRegion(@NotNull Entity entity) { + return this.plugin.getServer().isPrimaryThread(); + } + + @Override + public boolean isGlobalTickThread() { + return this.plugin.getServer().isPrimaryThread(); + } + @Override public @NotNull CompletableFuture runNextTick(@NotNull Consumer consumer) { CompletableFuture future = new CompletableFuture<>();