diff --git a/build.gradle.kts b/build.gradle.kts index f7f0465..30e4499 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -209,7 +209,9 @@ dependencies { compileOnly("me.clip:placeholderapi:2.11.6") compileOnly("com.github.MilkBowl:VaultAPI:1.7") compileOnly("me.filoghost.holographicdisplays:holographicdisplays-api:3.0.0-SNAPSHOT") - compileOnly("org.geysermc.floodgate:api:2.0-SNAPSHOT") + // Only the API surface is needed; its transitive Geyser snapshot + // (org.geysermc:common:1.4.3-SNAPSHOT) was purged from every public repo. + compileOnly("org.geysermc.floodgate:api:2.0-SNAPSHOT") { isTransitive = false } // Error Prone + NullAway (compile-time bug/null-safety checks) errorprone("com.google.errorprone:error_prone_core:2.41.0") @@ -245,7 +247,7 @@ tasks.named("build") { } tasks.withType { - filesMatching(listOf("plugin.yml", "config.yml")) { + filesMatching(listOf("plugin.yml", "folia-plugin.yml", "config.yml")) { expand( "version" to project.version, "description" to (project.description ?: ""), diff --git a/src/main/java/dev/loki/loparkour/duels/DuelsGenerator.java b/src/main/java/dev/loki/loparkour/duels/DuelsGenerator.java index 7d3fa2c..5307e18 100644 --- a/src/main/java/dev/loki/loparkour/duels/DuelsGenerator.java +++ b/src/main/java/dev/loki/loparkour/duels/DuelsGenerator.java @@ -10,6 +10,7 @@ import dev.loki.loparkour.mode.base.Modes; import dev.loki.loparkour.player.core.ParkourPlayer; import dev.loki.loparkour.scheduler.Scheduler; import dev.loki.loparkour.session.core.Session; +import dev.loki.loparkour.util.misc.Compat; import dev.loki.loparkour.util.text.ColorUtil; import org.bukkit.Location; import org.bukkit.Material; @@ -113,7 +114,7 @@ public class DuelsGenerator extends ParkourGenerator { final Location playerSpawn = platformCenter.clone().add(SPAWN_CENTER_OFFSET, 1, SPAWN_CENTER_OFFSET); playerSpawns.put(pp, playerSpawn); playerGenerators.put(pp, gen); - pp.player.teleport(playerSpawn); + Compat.teleport(pp.player, playerSpawn); } } diff --git a/src/main/java/dev/loki/loparkour/ghost/core/GhostPlayer.java b/src/main/java/dev/loki/loparkour/ghost/core/GhostPlayer.java index ff1239e..0c71aaa 100644 --- a/src/main/java/dev/loki/loparkour/ghost/core/GhostPlayer.java +++ b/src/main/java/dev/loki/loparkour/ghost/core/GhostPlayer.java @@ -5,6 +5,7 @@ import dev.loki.loparkour.ghost.model.GhostData; import dev.loki.loparkour.scheduler.Scheduler; import dev.loki.loparkour.scheduler.ScheduledTask; +import dev.loki.loparkour.util.misc.Compat; import dev.loki.loparkour.LoParkour; import org.bukkit.Location; import org.bukkit.World; @@ -51,7 +52,7 @@ public class GhostPlayer { startTime = System.currentTimeMillis(); currentFrame = 0; - task = Scheduler.get(LoParkour.getPlugin()).runTimer(this::update, 0, 1); + task = Scheduler.get(LoParkour.getPlugin()).runTimerAtLocation(this::update, startLocation, 0, 1); } private void update() { @@ -67,7 +68,7 @@ public class GhostPlayer { if (frame.getTimestamp() > elapsed) { break; } - entity.teleport(frame.toLocation(world)); + Compat.teleport(entity, frame.toLocation(world)); currentFrame++; } diff --git a/src/main/java/dev/loki/loparkour/ghost/service/GhostReplay.java b/src/main/java/dev/loki/loparkour/ghost/service/GhostReplay.java index 29faead..cec8dea 100644 --- a/src/main/java/dev/loki/loparkour/ghost/service/GhostReplay.java +++ b/src/main/java/dev/loki/loparkour/ghost/service/GhostReplay.java @@ -7,6 +7,7 @@ import dev.loki.loparkour.ghost.replay.PlayerGhost; import dev.loki.loparkour.player.core.ParkourUser; import dev.loki.loparkour.scheduler.ScheduledTask; import dev.loki.loparkour.scheduler.Scheduler; +import dev.loki.loparkour.util.misc.Compat; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; @@ -63,14 +64,15 @@ public class GhostReplay { ParkourUser.leave(viewer); } final Location start = run.getFrames().get(FIRST_FRAME).toLocation(world); - viewer.teleport(start); + Compat.teleport(viewer, start); moved = true; ghost = new PlayerGhost(viewer, run.getPlayerName()); if (ghost.spawn(start)) { startTime = System.currentTimeMillis(); frameIndex = FIRST_FRAME; current = start; - task = Scheduler.get(LoParkour.getPlugin()).runTimer(this::tick, TICK_DELAY, TICK_PERIOD); + task = Scheduler.get(LoParkour.getPlugin()).runTimerAtLocation(this::tick, start, + TICK_DELAY, TICK_PERIOD); } else { finish(); } @@ -106,7 +108,7 @@ public class GhostReplay { ghost = null; } if (moved && viewer.isOnline()) { - viewer.teleport(returnTo); + Compat.teleport(viewer, returnTo); } onFinish.run(); } diff --git a/src/main/java/dev/loki/loparkour/player/core/ParkourUser.java b/src/main/java/dev/loki/loparkour/player/core/ParkourUser.java index 8cf170b..fcbc1ba 100644 --- a/src/main/java/dev/loki/loparkour/player/core/ParkourUser.java +++ b/src/main/java/dev/loki/loparkour/player/core/ParkourUser.java @@ -10,6 +10,7 @@ import dev.loki.loparkour.hook.floodgate.FloodgateHook; import dev.loki.loparkour.menu.core.screen.ParkourOption; import dev.loki.loparkour.player.data.PreviousData; import dev.loki.loparkour.session.core.Session; +import dev.loki.loparkour.util.misc.Compat; import dev.loki.loparkour.util.text.ColorUtil; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -131,7 +132,7 @@ public abstract class ParkourUser { } public void teleport(@NotNull Location to) { - player.teleport(to); + Compat.teleport(player, to); } public void send(String message) { diff --git a/src/main/java/dev/loki/loparkour/player/data/PreviousData.java b/src/main/java/dev/loki/loparkour/player/data/PreviousData.java index 60299f3..5a48458 100644 --- a/src/main/java/dev/loki/loparkour/player/data/PreviousData.java +++ b/src/main/java/dev/loki/loparkour/player/data/PreviousData.java @@ -4,6 +4,7 @@ import dev.loki.loparkour.LoParkour; import dev.loki.loparkour.config.core.Config; import dev.loki.loparkour.config.options.OptionGeneral; import dev.loki.loparkour.reward.core.Reward; +import dev.loki.loparkour.util.misc.Compat; import io.papermc.lib.PaperLib; import org.bukkit.GameMode; import org.bukkit.Location; @@ -61,7 +62,7 @@ public class PreviousData { if (!urgent) { PaperLib.teleportAsync(player, to).thenRun(() -> apply(player)); } else { - player.teleport(to); + Compat.teleport(player, to); apply(player); } diff --git a/src/main/java/dev/loki/loparkour/scheduler/BukkitBackend.java b/src/main/java/dev/loki/loparkour/scheduler/BukkitBackend.java index be8c3b2..b4bfac3 100644 --- a/src/main/java/dev/loki/loparkour/scheduler/BukkitBackend.java +++ b/src/main/java/dev/loki/loparkour/scheduler/BukkitBackend.java @@ -38,4 +38,20 @@ final class BukkitBackend implements Scheduler.Backend { public ScheduledTask runTimerAsync(Runnable task, long delayTicks, long periodTicks) { return Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, task, delayTicks, periodTicks)::cancel; } + + @Override + public ScheduledTask runAtLocation(Runnable task, org.bukkit.Location location, long delayTicks) { + return Bukkit.getScheduler().runTaskLater(plugin, task, delayTicks)::cancel; + } + + @Override + public ScheduledTask runTimerAtLocation(Runnable task, org.bukkit.Location location, + long delayTicks, long periodTicks) { + return Bukkit.getScheduler().runTaskTimer(plugin, task, delayTicks, periodTicks)::cancel; + } + + @Override + public ScheduledTask runAtEntity(Runnable task, org.bukkit.entity.Entity entity) { + return Bukkit.getScheduler().runTask(plugin, task)::cancel; + } } diff --git a/src/main/java/dev/loki/loparkour/scheduler/FoliaBackend.java b/src/main/java/dev/loki/loparkour/scheduler/FoliaBackend.java index 15516f2..dd3b7e8 100644 --- a/src/main/java/dev/loki/loparkour/scheduler/FoliaBackend.java +++ b/src/main/java/dev/loki/loparkour/scheduler/FoliaBackend.java @@ -20,21 +20,27 @@ final class FoliaBackend implements Scheduler.Backend { private static final String GLOBAL_SCHEDULER_CLASS = "GlobalRegionScheduler"; private static final String ASYNC_SCHEDULER_CLASS = "AsyncScheduler"; + private static final String REGION_SCHEDULER_CLASS = "RegionScheduler"; private final Plugin plugin; private final Object globalScheduler; private final Object asyncScheduler; + private final Object regionScheduler; private final Method globalRun; private final Method globalRunDelayed; private final Method globalRunAtFixedRate; private final Method asyncRunNow; private final Method asyncRunAtFixedRate; + private final Method regionRunDelayed; + private final Method regionRunAtFixedRate; + private final Method entityExecute; private final Method taskCancel; FoliaBackend(Plugin plugin) throws ReflectiveOperationException { this.plugin = plugin; this.globalScheduler = scheduler("getGlobalRegionScheduler"); this.asyncScheduler = scheduler("getAsyncScheduler"); + this.regionScheduler = scheduler("getRegionScheduler"); this.globalRun = method(GLOBAL_SCHEDULER_CLASS, "run", Plugin.class, Consumer.class); this.globalRunDelayed = method(GLOBAL_SCHEDULER_CLASS, "runDelayed", Plugin.class, Consumer.class, long.class); @@ -43,6 +49,12 @@ final class FoliaBackend implements Scheduler.Backend { this.asyncRunNow = method(ASYNC_SCHEDULER_CLASS, "runNow", Plugin.class, Consumer.class); this.asyncRunAtFixedRate = method(ASYNC_SCHEDULER_CLASS, "runAtFixedRate", Plugin.class, Consumer.class, long.class, long.class); + this.regionRunDelayed = method(REGION_SCHEDULER_CLASS, "runDelayed", + Plugin.class, org.bukkit.Location.class, Consumer.class, long.class); + this.regionRunAtFixedRate = method(REGION_SCHEDULER_CLASS, "runAtFixedRate", + Plugin.class, org.bukkit.Location.class, Consumer.class, long.class, long.class); + this.entityExecute = Class.forName(SCHEDULER_PACKAGE + "EntityScheduler") + .getMethod("execute", Plugin.class, Runnable.class); this.taskCancel = Class.forName(SCHEDULER_PACKAGE + "ScheduledTask").getMethod("cancel"); } @@ -67,6 +79,29 @@ final class FoliaBackend implements Scheduler.Backend { consumer(task), delayTicks, periodTicks)); } + @Override + public ScheduledTask runTimerAtLocation(Runnable task, org.bukkit.Location location, + long delayTicks, long periodTicks) { + return wrap(invoke(regionRunAtFixedRate, regionScheduler, plugin, location, + consumer(task), delayTicks, periodTicks)); + } + + @Override + public ScheduledTask runAtLocation(Runnable task, org.bukkit.Location location, long delayTicks) { + return wrap(invoke(regionRunDelayed, regionScheduler, plugin, location, + consumer(task), delayTicks)); + } + + @Override + public ScheduledTask runAtEntity(Runnable task, org.bukkit.entity.Entity entity) { + try { + final Object entityScheduler = entity.getClass().getMethod("getScheduler").invoke(entity); + return wrap(invoke(entityExecute, entityScheduler, plugin, task)); + } catch (ReflectiveOperationException | IllegalArgumentException cause) { + throw new IllegalStateException("Folia entity scheduler call failed", cause); + } + } + @Override public ScheduledTask runTimerAsync(Runnable task, long delayTicks, long periodTicks) { return wrap(invoke(asyncRunAtFixedRate, asyncScheduler, plugin, diff --git a/src/main/java/dev/loki/loparkour/scheduler/Scheduler.java b/src/main/java/dev/loki/loparkour/scheduler/Scheduler.java index 0282ed6..9cb2a42 100644 --- a/src/main/java/dev/loki/loparkour/scheduler/Scheduler.java +++ b/src/main/java/dev/loki/loparkour/scheduler/Scheduler.java @@ -99,6 +99,49 @@ public final class Scheduler { return backend.runTimerAsync(task, delayTicks, periodTicks); } + /** + * Runs the task once on the region thread owning the location. + * + *

On Bukkit this is a plain delayed task; on Folia it uses the region + * scheduler, so block access around the location is legal. + * + * @param task the task to run + * @param location the location whose region owns the task + * @param delayTicks delay in ticks + * @return a handle used to cancel the task + */ + public ScheduledTask runAtLocation(Runnable task, org.bukkit.Location location, long delayTicks) { + return backend.runAtLocation(task, location, delayTicks); + } + + /** + * Runs the task repeatedly on the region thread owning the location. + * + * @param task the task to run + * @param location the location whose region owns the task + * @param delayTicks first run delay in ticks + * @param periodTicks period between runs in ticks + * @return a handle used to cancel the task + */ + public ScheduledTask runTimerAtLocation(Runnable task, org.bukkit.Location location, + long delayTicks, long periodTicks) { + return backend.runTimerAtLocation(task, location, delayTicks, periodTicks); + } + + /** + * Runs the task on the entity's thread on the next tick. + * + *

On Bukkit this is a plain sync task; on Folia it uses the entity + * scheduler, so entity access (teleport, inventory) is legal. + * + * @param task the task to run + * @param entity the entity whose thread owns the task + * @return a handle used to cancel the task + */ + public ScheduledTask runAtEntity(Runnable task, org.bukkit.entity.Entity entity) { + return backend.runAtEntity(task, entity); + } + private static Backend createBackend(Plugin plugin) { if (FOLIA) { try { @@ -132,5 +175,12 @@ public final class Scheduler { ScheduledTask runTimer(Runnable task, long delayTicks, long periodTicks); ScheduledTask runTimerAsync(Runnable task, long delayTicks, long periodTicks); + + ScheduledTask runAtLocation(Runnable task, org.bukkit.Location location, long delayTicks); + + ScheduledTask runTimerAtLocation(Runnable task, org.bukkit.Location location, + long delayTicks, long periodTicks); + + ScheduledTask runAtEntity(Runnable task, org.bukkit.entity.Entity entity); } } diff --git a/src/main/java/dev/loki/loparkour/session/manager/SessionStateManager.java b/src/main/java/dev/loki/loparkour/session/manager/SessionStateManager.java index 91d86a5..cbb3bd0 100644 --- a/src/main/java/dev/loki/loparkour/session/manager/SessionStateManager.java +++ b/src/main/java/dev/loki/loparkour/session/manager/SessionStateManager.java @@ -74,7 +74,7 @@ public class SessionStateManager { return; } - tickTask = Scheduler.get(LoParkour.getPlugin()).runTimer(() -> { + tickTask = Scheduler.get(LoParkour.getPlugin()).runTimerAtLocation(() -> { if (session.generator() != null) { try { session.generator().tick(); @@ -86,7 +86,7 @@ public class SessionStateManager { p.player.sendMessage("§c[LoParkour] Tick error: " + t.getMessage())); } } - }, 0, 1); // Run every tick (1 = 50ms) + }, spawnLocation, 0, 1); // Run every tick (1 = 50ms) } /** diff --git a/src/main/java/dev/loki/loparkour/util/misc/Compat.java b/src/main/java/dev/loki/loparkour/util/misc/Compat.java index eb55a3d..edd4327 100644 --- a/src/main/java/dev/loki/loparkour/util/misc/Compat.java +++ b/src/main/java/dev/loki/loparkour/util/misc/Compat.java @@ -1,11 +1,16 @@ package dev.loki.loparkour.util.misc; +import dev.loki.loparkour.LoParkour; +import dev.loki.loparkour.scheduler.Scheduler; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.Particle; import org.bukkit.block.BlockState; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.potion.PotionEffectType; import org.bukkit.structure.StructureManager; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -106,6 +111,18 @@ public final class Compat { return null; } + /** + * Teleports an entity. Direct on Bukkit; on Folia the call is routed to the + * entity scheduler because entity access from other threads is illegal. + */ + public static void teleport(@NotNull Entity entity, @NotNull Location location) { + if (Scheduler.isFolia()) { + Scheduler.get(LoParkour.getPlugin()).runAtEntity(() -> entity.teleport(location), entity); + } else { + entity.teleport(location); + } + } + /** * Resolves a {@link PotionEffectType} by name (JUMP → JUMP_BOOST, SLOW → * SLOWNESS were renamed in 1.21), or {@code null} when none exists. diff --git a/src/main/resources/folia-plugin.yml b/src/main/resources/folia-plugin.yml new file mode 100644 index 0000000..cb5c01f --- /dev/null +++ b/src/main/resources/folia-plugin.yml @@ -0,0 +1,12 @@ +name: 'LoParkour' +description: 'BETA - Advanced infinitely generating parkour plugin.' +author: loki +version: ${version} +api-version: ${apiVersion} +main: dev.loki.loparkour.LoParkour +softdepend: [floodgate, Vault, PlaceholderAPI, Multiverse-Core] +commands: + loparkour: + aliases: [parkour, lpk] + usage: / [arguments] + description: Main command for LoParkour diff --git a/src/test/java/dev/loki/loparkour/arch/ArchitectureTest.java b/src/test/java/dev/loki/loparkour/arch/ArchitectureTest.java index ffc276e..77568f4 100644 --- a/src/test/java/dev/loki/loparkour/arch/ArchitectureTest.java +++ b/src/test/java/dev/loki/loparkour/arch/ArchitectureTest.java @@ -46,6 +46,14 @@ class ArchitectureTest { .should().callMethod(org.bukkit.Bukkit.class, "getScheduler") .because("Bukkit.getScheduler() does not exist on Folia")); + @ArchTest + static final ArchRule NO_DIRECT_TELEPORT_OUTSIDE_COMPAT = freeze( + noClasses().that().resideOutsideOfPackage(ROOT + ".util.misc..") + .should().callMethod(org.bukkit.entity.Entity.class, "teleport", + org.bukkit.Location.class) + .because("teleports must go through Compat.teleport so they run " + + "on the entity thread on Folia")); + @ArchTest static final ArchRule NO_LEGACY_LOLIB = noClasses().should().dependOnClassesThat().resideInAPackage("dev.lolib.."); diff --git a/src/test/resources/archunit_store/cea999b4-4780-4ff1-ae71-5394d5efa19e b/src/test/resources/archunit_store/cea999b4-4780-4ff1-ae71-5394d5efa19e new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/archunit_store/stored.rules b/src/test/resources/archunit_store/stored.rules index 62e7a72..d43ad0b 100644 --- a/src/test/resources/archunit_store/stored.rules +++ b/src/test/resources/archunit_store/stored.rules @@ -1,6 +1,7 @@ # -#Tue Oct 06 13:42:44 CEST 2026 +#Sat Oct 10 15:00:30 CEST 2026 no\ classes\ that\ reside\ in\ a\ package\ 'dev.loki.loparkour.api..'\ should\ depend\ on\ classes\ that\ reside\ in\ any\ package\ ['dev.loki.loparkour.menu..',\ 'dev.loki.loparkour.command..',\ 'dev.loki.loparkour.storage..',\ 'dev.loki.loparkour.listener..',\ 'dev.loki.loparkour.hook..']=d1d8a9d7-476c-4f86-adda-8531054aea1f no\ classes\ that\ reside\ outside\ of\ package\ 'dev.loki.loparkour.scheduler..'\ should\ call\ method\ Bukkit.getScheduler(),\ because\ Bukkit.getScheduler()\ does\ not\ exist\ on\ Folia=f1e3c34b-9b02-40e6-83a6-0269e173eb83 no\ classes\ that\ reside\ outside\ of\ package\ 'dev.loki.loparkour.scheduler..'\ should\ depend\ on\ classes\ that\ are\ assignable\ to\ org.bukkit.scheduler.BukkitRunnable,\ because\ all\ tasks\ go\ through\ scheduler/\ so\ they\ work\ on\ Folia=339ab3ea-28aa-4f4b-a1e4-6c69e3ff0a0a +no\ classes\ that\ reside\ outside\ of\ package\ 'dev.loki.loparkour.util.misc..'\ should\ call\ method\ Entity.teleport(Location),\ because\ teleports\ must\ go\ through\ Compat.teleport\ so\ they\ run\ on\ the\ entity\ thread\ on\ Folia=cea999b4-4780-4ff1-ae71-5394d5efa19e slices\ matching\ 'dev.loki.loparkour.(*)..'\ should\ be\ free\ of\ cycles=58784f63-3f60-4edb-9733-fa0d7e11b877