From 50a0b435f8fd462ab223f062f186819cbc188035 Mon Sep 17 00:00:00 2001 From: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> Date: Mon, 22 May 2023 11:26:43 +0100 Subject: [PATCH 1/7] feat: Folia support Signed-off-by: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> --- .github/workflows/build.yml | 4 +- .github/workflows/pullrequest.yml | 4 +- AdvancedTeleport-Bukkit/build.gradle.kts | 42 ++++- .../advancedteleport/CoreClass.java | 41 ++-- .../advancedteleport/api/ATPlayer.java | 20 +- .../api/AdvancedTeleportAPI.java | 4 +- .../advancedteleport/api/TeleportRequest.java | 10 +- .../advancedteleport/api/signs/WarpSign.java | 4 +- .../commands/core/ClearCacheCommand.java | 22 +-- .../commands/core/ExportCommand.java | 44 ++--- .../commands/core/ImportCommand.java | 2 +- .../commands/home/HomeCommand.java | 26 +-- .../commands/teleport/Back.java | 25 ++- .../commands/teleport/TpAll.java | 19 +- .../commands/teleport/TpHereOffline.java | 37 ++-- .../commands/teleport/TpLoc.java | 3 +- .../commands/teleport/TpOffline.java | 36 ++-- .../commands/teleport/Tpa.java | 50 +++-- .../commands/teleport/TpaHere.java | 39 ++-- .../config/CustomMessages.java | 67 ++++--- .../advancedteleport/config/MainConfig.java | 13 +- .../folia/CancellableRunnable.java | 28 +++ .../folia/RunnableManager.java | 140 ++++++++++++++ .../folia/schedulers/FoliaRunnable.java | 32 ++++ .../schedulers/NormalBukkitRunnable.java | 32 ++++ .../managers/CommandManager.java | 51 ++--- .../managers/CooldownManager.java | 14 +- .../managers/MovementManager.java | 27 +-- .../advancedteleport/managers/RTPManager.java | 175 +++++++++++------- .../managers/TeleportTrackingManager.java | 20 +- .../sql/BlocklistManager.java | 59 +++--- .../advancedteleport/sql/DataFailManager.java | 20 +- .../advancedteleport/sql/HomeSQLManager.java | 83 ++++----- .../sql/MetadataSQLManager.java | 62 +++---- .../sql/PlayerSQLManager.java | 74 ++++---- .../advancedteleport/sql/SQLManager.java | 4 +- .../advancedteleport/sql/SpawnSQLManager.java | 65 +++---- .../advancedteleport/sql/WarpSQLManager.java | 75 ++++---- .../advancedteleport/utilities/IconMenu.java | 3 +- .../utilities/NMSUtilities.java | 41 ++++ .../utilities/nbt/NBTReader.java | 66 +++---- README.md | 2 +- gradle.properties | 2 +- gradle/libs.versions.toml | 5 + settings.gradle.kts | 1 + 45 files changed, 916 insertions(+), 677 deletions(-) create mode 100644 AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/CancellableRunnable.java create mode 100644 AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/RunnableManager.java create mode 100644 AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/schedulers/FoliaRunnable.java create mode 100644 AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/schedulers/NormalBukkitRunnable.java create mode 100644 AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/NMSUtilities.java diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index eb911bad..fd5c27a8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -93,7 +93,7 @@ jobs: # If this step fails, then you should remove it and run the build manually (see below) - name: Setup Gradle uses: gradle/gradle-build-action@v2 - + - name: Build with Gradle run: ./gradlew shadowJar @@ -101,7 +101,7 @@ jobs: uses: github/codeql-action/analyze@v2 with: category: "/language:${{matrix.language}}" - + - name: Archive Artifact uses: actions/upload-artifact@v4 if: success() diff --git a/.github/workflows/pullrequest.yml b/.github/workflows/pullrequest.yml index 9361bc49..e22c788c 100644 --- a/.github/workflows/pullrequest.yml +++ b/.github/workflows/pullrequest.yml @@ -93,7 +93,7 @@ jobs: uses: gradle/gradle-build-action@v2 - name: Build with Gradle - run: ./gradlew shadowJar + run: ./gradlew reobfJar - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@v2 @@ -105,5 +105,5 @@ jobs: if: success() with: name: AdvancedTeleport - path: build/advancedteleport-bukkit/libs/AdvancedTeleport-Bukkit-*-all.jar + path: build/advancedteleport-bukkit/libs/AdvancedTeleport-Bukkit-*.jar if-no-files-found: error diff --git a/AdvancedTeleport-Bukkit/build.gradle.kts b/AdvancedTeleport-Bukkit/build.gradle.kts index f8f661a6..4b31c4c0 100644 --- a/AdvancedTeleport-Bukkit/build.gradle.kts +++ b/AdvancedTeleport-Bukkit/build.gradle.kts @@ -1,4 +1,5 @@ import net.minecrell.pluginyml.bukkit.BukkitPluginDescription +import xyz.jpenilla.runpaper.task.RunServer import java.io.BufferedReader import java.text.SimpleDateFormat import java.util.Date @@ -9,6 +10,7 @@ plugins { id("java-library") id("maven-publish") id("com.modrinth.minotaur") + id("io.papermc.paperweight.userdev") version "1.5.5" alias(libs.plugins.shadow) alias(libs.plugins.minecraft.pluginYML) alias(libs.plugins.minecraft.runPaper) @@ -90,7 +92,10 @@ repositories { } dependencies { - compileOnly("io.papermc.paper:paper-api:1.20.2-R0.1-SNAPSHOT") + paperweight.foliaDevBundle("1.20.4-R0.1-SNAPSHOT") + + compileOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT") + compileOnly(libs.folia) implementation(libs.hook.slimjar) @@ -121,6 +126,8 @@ dependencies { } } +runPaper.folia.registerTask() + publishing { publications.create("maven") { from(components["java"]) @@ -132,26 +139,35 @@ tasks { options.encoding = "UTF-8" } + withType().getByName("runFolia") { + // Wait for slimJar to go through first + dependsOn(reobfJar) + + // Set the version to 1.20.4 + minecraftVersion("1.20.4") + + runDirectory.set(rootDir.resolve(".run")) + } + runServer { // Wait for slimJar to go through first - dependsOn(slimJar) + dependsOn(reobfJar) - // Set the version to 1.20.1 + // Set the version to 1.20.4 minecraftVersion("1.20.4") // Get the dev server folder val devServer = file(findProperty("devServer") ?: "${System.getProperty("user.home")}/Documents/Minecraft/Dev") val pluginsFolder = devServer.resolve("plugins") runDirectory.set(rootDir.resolve(".run")) - pluginJars(pluginsFolder.resolve("Vault.jar")) - pluginJars(pluginsFolder.resolve("Spark.jar")) - pluginJars(pluginsFolder.resolve("Spoofer.jar")) - pluginJars(pluginsFolder.resolve("squaremap.jar")) - pluginJars(pluginsFolder.resolve("PlayerParticles.jar")) - pluginJars(pluginsFolder.resolve("dynmap.jar")) + // pluginJars(pluginsFolder.resolve("Vault.jar")) + // pluginJars(pluginsFolder.resolve("Spark.jar")) + // pluginJars(pluginsFolder.resolve("Spoofer.jar")) + // pluginJars(pluginsFolder.resolve("squaremap.jar")) + // pluginJars(pluginsFolder.resolve("PlayerParticles.jar")) + // pluginJars(pluginsFolder.resolve("dynmap.jar")) pluginJars(getJarFile()) - } withType { @@ -190,6 +206,10 @@ tasks { } } +tasks.shadowJar { + from(tasks.slimJar.get().outputDirectory) +} + // Lead development use only. modrinth { token.set(System.getenv("MODRINTH_TOKEN")) @@ -246,6 +266,8 @@ bukkit { main = "io.github.niestrat99.advancedteleport.CoreClass" load = BukkitPluginDescription.PluginLoadOrder.POSTWORLD + foliaSupported = true + softDepend = listOf("Vault", "Ultimate_Economy", "ConfigurationMaster", "WorldBorder", "ChunkyBorder", "floodgate", "Lands", "WorldGuard", "GriefProtection", "dynmap", "squaremap", "PlayerParticles") loadBefore = listOf("Essentials", "EssentialsSpawn") diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/CoreClass.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/CoreClass.java index 12701f8e..3ee702b0 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/CoreClass.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/CoreClass.java @@ -4,6 +4,7 @@ import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.GUIConfig; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.listeners.*; import io.github.niestrat99.advancedteleport.listeners.paper.PaperLegacySignListener; import io.github.niestrat99.advancedteleport.listeners.paper.PaperSignChangeListener; @@ -46,10 +47,8 @@ public final class CoreClass extends JavaPlugin { private static CoreClass instance; - public static final Executor async = - task -> Bukkit.getScheduler().runTaskAsynchronously(CoreClass.getInstance(), task); - public static final Executor sync = - task -> Bukkit.getScheduler().runTask(CoreClass.getInstance(), task); + public static final Executor async = RunnableManager::setupRunnerAsync; + public static final Executor sync = RunnableManager::setupRunner; private static Permission perms; private Object[] updateInfo; @@ -149,23 +148,22 @@ public void onEnable() { RandomTPAlgorithms.init(); new Metrics(this, 5146); - Bukkit.getScheduler() - .runTaskAsynchronously( - this, - () -> { - RTPManager.init(); - if (MainConfig.get().CHECK_FOR_UPDATES.get()) { - updateInfo = UpdateChecker.getUpdate(); - if (updateInfo != null) { - getLogger().info("A new version is available!"); - getLogger().info("Current version you're using: " + getDescription().getVersion()); - getLogger().info("Latest version available: " + updateInfo[0]); - getLogger().info("Download link: https://www.spigotmc.org/resources/advancedteleport.64139/"); - } else { - getLogger().info("Plugin is up to date!"); - } - } - }); + RunnableManager.setupRunnerAsync(() -> { + if (RunnableManager.isFolia()) { + RunnableManager.setupRunnerDelayedAsync(task -> RTPManager.init(), 60); + } + if (MainConfig.get().CHECK_FOR_UPDATES.get()) { + updateInfo = UpdateChecker.getUpdate(); + if (updateInfo != null) { + getLogger().info("A new version is available!"); + getLogger().info("Current version you're using: " + getDescription().getVersion()); + getLogger().info("Latest version available: " + updateInfo[0]); + getLogger().info("Download link: https://www.spigotmc.org/resources/advancedteleport.64139/"); + } else { + getLogger().info("Plugin is up to date!"); + } + } + }); } private void checkVersion() { @@ -238,6 +236,7 @@ private void registerSensitiveEvents(final @NotNull NewListener listener) { */ private void hackTheMainFrame() throws NoSuchFieldException, IllegalAccessException { if (!PaperLib.isPaper()) return; + if (RunnableManager.isFolia()) return; final var scheduler = Bukkit.getScheduler(); diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/ATPlayer.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/ATPlayer.java index b9efce6c..fdb3035a 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/ATPlayer.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/ATPlayer.java @@ -12,11 +12,17 @@ import io.github.niestrat99.advancedteleport.api.events.players.ToggleTeleportationEvent; import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; +import io.github.niestrat99.advancedteleport.managers.CooldownManager; +import io.github.niestrat99.advancedteleport.managers.MovementManager; +import io.github.niestrat99.advancedteleport.managers.ParticleManager; +import io.github.niestrat99.advancedteleport.managers.PluginHookManager; import io.github.niestrat99.advancedteleport.managers.*; import io.github.niestrat99.advancedteleport.payments.PaymentManager; import io.github.niestrat99.advancedteleport.sql.BlocklistManager; import io.github.niestrat99.advancedteleport.sql.HomeSQLManager; import io.github.niestrat99.advancedteleport.sql.PlayerSQLManager; +import io.github.niestrat99.advancedteleport.utilities.NMSUtilities; import io.github.thatsmusic99.configurationmaster.api.ConfigSection; import io.papermc.lib.PaperLib; import io.papermc.paper.entity.TeleportFlag; @@ -251,7 +257,7 @@ public static CompletableFuture teleportWithOptions( } // - if (!flags.isEmpty()) { + if (!flags.isEmpty() && !RunnableManager.isFolia()) { return CompletableFuture.completedFuture( player.teleport(location, cause, flags.toArray(new TeleportFlag[0]))); } @@ -441,8 +447,12 @@ public boolean hasBlocked(@NotNull final UUID otherPlayer) { @Contract(pure = true) public @NotNull ImmutableMap getHomes(boolean withBed) { final var map = homes.data == null ? new HashMap() : homes.data; - final var bedSpawn = getBedSpawn(); - if (withBed && bedSpawn != null && MainConfig.get().ADD_BED_TO_HOMES.get()) map.put("bed", bedSpawn); + + // Only include the bed if it's requested + if (withBed && MainConfig.get().ADD_BED_TO_HOMES.get()) { + final var bedSpawn = getBedSpawn(); + if (bedSpawn != null) map.put("bed", getBedSpawn()); + } return ImmutableMap.copyOf(map); } @@ -628,8 +638,8 @@ public boolean hasHome(@NotNull final String name) { */ @Contract(pure = true) public @Nullable Home getBedSpawn() { - if (getOfflinePlayer().getBedSpawnLocation() != null) { - return new Home(uuid, "bed", getOfflinePlayer().getBedSpawnLocation(), -1, -1); + if (NMSUtilities.getRespawnLocation(getOfflinePlayer()) != null) { + return new Home(uuid, "bed", NMSUtilities.getRespawnLocation(getOfflinePlayer()), -1, -1); } return null; diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/AdvancedTeleportAPI.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/AdvancedTeleportAPI.java index b7e1b398..8035661a 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/AdvancedTeleportAPI.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/AdvancedTeleportAPI.java @@ -8,6 +8,7 @@ import io.github.niestrat99.advancedteleport.api.events.warps.WarpCreateEvent; import io.github.niestrat99.advancedteleport.api.events.warps.WarpPostCreateEvent; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.managers.NamedLocationManager; import io.github.niestrat99.advancedteleport.managers.RTPManager; import io.github.niestrat99.advancedteleport.managers.SignManager; @@ -184,8 +185,7 @@ public static boolean isWarpSet(@NotNull String name) { // If there is no main spawn yet, make it this one too if (NamedLocationManager.get().getMainSpawn() == null) { - Bukkit.getScheduler().runTask(CoreClass.getInstance(), - () -> AdvancedTeleportAPI.setMainSpawn(spawn, sender)); + RunnableManager.setupRunner(() -> AdvancedTeleportAPI.setMainSpawn(spawn, sender)); } // Add it to the database. diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/TeleportRequest.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/TeleportRequest.java index 1a008ac7..8c193a90 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/TeleportRequest.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/TeleportRequest.java @@ -2,9 +2,9 @@ import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.CancellableRunnable; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -17,10 +17,10 @@ * @param responder The player receiving it. */ public record TeleportRequest( - @NotNull Player requester, - @NotNull Player responder, - @NotNull BukkitRunnable timer, - @NotNull TeleportRequestType type) { + @NotNull Player requester, + @NotNull Player responder, + @NotNull CancellableRunnable timer, + @NotNull TeleportRequestType type) { private static final List requestList = new ArrayList<>(); diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/signs/WarpSign.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/signs/WarpSign.java index 234db5a3..35fdec14 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/signs/WarpSign.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/signs/WarpSign.java @@ -22,9 +22,9 @@ public WarpSign() { @Override public void onInteract(@NotNull Sign sign, @NotNull Player player) { - + boolean contains = AdvancedTeleportAPI.getWarps().containsKey(sign.getLine(1)); - CoreClass.debug("Warp " + sign.getLine(1) + " exists: " + contains); + CoreClass.debug("Warp " + sign.getLine(1) + " exists: " + contains); if (!contains) return; WarpCommand.warp(AdvancedTeleportAPI.getWarps().get(sign.getLine(1)), player, true); diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ClearCacheCommand.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ClearCacheCommand.java index f4e0fe63..0043f50d 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ClearCacheCommand.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ClearCacheCommand.java @@ -1,8 +1,8 @@ package io.github.niestrat99.advancedteleport.commands.core; -import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.commands.SubATCommand; import io.github.niestrat99.advancedteleport.config.CustomMessages; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.managers.RTPManager; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; @@ -38,14 +38,11 @@ public boolean onCommand( RTPManager.clearEverything(); // Reload the data - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), - () -> { - for (World world : Bukkit.getWorlds()) { - RTPManager.loadWorldData(world); - } - }); + RunnableManager.setupRunnerAsync(task -> { + for (World world : Bukkit.getWorlds()) { + RTPManager.loadWorldData(world); + } + }); CustomMessages.sendMessage(sender, "Info.clearEverything"); return true; @@ -62,11 +59,8 @@ public boolean onCommand( // Reset the data RTPManager.unloadWorldData(world); - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), () -> RTPManager.loadWorldData(world)); - CustomMessages.sendMessage( - sender, "Info.clearWorld", Placeholder.unparsed("world", args[0])); + RunnableManager.setupRunnerAsync(task -> RTPManager.loadWorldData(world)); + CustomMessages.sendMessage(sender, "Info.clearWorld", Placeholder.unparsed("world", args[0])); return true; } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ExportCommand.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ExportCommand.java index e0ec9fb0..703dc3d2 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ExportCommand.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ExportCommand.java @@ -1,14 +1,13 @@ package io.github.niestrat99.advancedteleport.commands.core; -import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.commands.SubATCommand; import io.github.niestrat99.advancedteleport.config.CustomMessages; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.hooks.ImportExportPlugin; import io.github.niestrat99.advancedteleport.managers.PluginHookManager; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; -import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.util.StringUtil; @@ -47,30 +46,23 @@ public boolean onCommand( } // Start the export with the specified section. - CustomMessages.sendMessage( - sender, "Info.exportStarted", Placeholder.unparsed("plugin", args[0])); - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), - () -> { - final var arg = args.length == 1 ? "all" : args[1]; - switch (arg.toLowerCase()) { - case "homes" -> pluginHook.exportHomes(); - case "warps" -> pluginHook.exportWarps(); - case "lastlocs" -> pluginHook.exportLastLocations(); - case "spawns" -> pluginHook.exportSpawn(); - case "players" -> pluginHook.exportPlayerInformation(); - case "all" -> pluginHook.exportAll(); - default -> { - CustomMessages.sendMessage(sender, "Error.invalidOption"); - return; - } - } - CustomMessages.sendMessage( - sender, - "Info.exportFinished", - Placeholder.unparsed("plugin", args[0])); - }); + CustomMessages.sendMessage(sender, "Info.exportStarted", Placeholder.unparsed("plugin", args[0])); + RunnableManager.setupRunnerAsync(() -> { + final var arg = args.length == 1 ? "all" : args[1]; + switch (arg.toLowerCase()) { + case "homes" -> pluginHook.exportHomes(); + case "warps" -> pluginHook.exportWarps(); + case "lastlocs" -> pluginHook.exportLastLocations(); + case "spawns" -> pluginHook.exportSpawn(); + case "players" -> pluginHook.exportPlayerInformation(); + case "all" -> pluginHook.exportAll(); + default -> { + CustomMessages.sendMessage(sender, "Error.invalidOption"); + return; + } + } + CustomMessages.sendMessage(sender, "Info.exportFinished", Placeholder.unparsed("plugin", args[0])); + }); return true; } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ImportCommand.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ImportCommand.java index f735f575..fc6d8226 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ImportCommand.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ImportCommand.java @@ -75,8 +75,8 @@ public boolean onCommand( sender, "Info.importFinished", Placeholder.unparsed("plugin", args[0])); - return true; + } @Override diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/home/HomeCommand.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/home/HomeCommand.java index 69251f7e..a6ce648d 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/home/HomeCommand.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/home/HomeCommand.java @@ -7,6 +7,7 @@ import io.github.niestrat99.advancedteleport.commands.TimedATCommand; import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; @@ -92,14 +93,8 @@ public boolean onCommand( // If we're requesting a list, just throw it if (args[1].equalsIgnoreCase("list")) { - Bukkit.getScheduler() - .runTask( - CoreClass.getInstance(), - () -> - Bukkit - .dispatchCommand( - sender, - "advancedteleport:homes " + args[0])); + RunnableManager.setupRunner(() -> + Bukkit.dispatchCommand(sender, "advancedteleport:homes " + args[0])); return; } @@ -116,10 +111,7 @@ public boolean onCommand( if (MainConfig.get().SHOW_HOMES_WITH_NO_INPUT.get() && !(atPlayer.hasMainHome() && MainConfig.get().PRIORITISE_MAIN_HOME.get())) { - Bukkit.getScheduler() - .runTask( - CoreClass.getInstance(), - () -> Bukkit.dispatchCommand(sender, "advancedteleport:homes")); + RunnableManager.setupRunner(() -> Bukkit.dispatchCommand(sender, "advancedteleport:homes")); } // Try getting the main home - if it exists, teleport there @@ -179,12 +171,10 @@ public boolean onCommand( } if (args[0].equalsIgnoreCase("list")) { - Bukkit.getScheduler() - .runTask( - CoreClass.getInstance(), - () -> - Bukkit.dispatchCommand( - sender, "advancedteleport:homes " + args[0])); + RunnableManager.setupRunner(() -> Bukkit.dispatchCommand( + sender, + "advancedteleport:homes " + args[0] + )); return true; } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/Back.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/Back.java index 2d8e1cd1..63ebeb69 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/Back.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/Back.java @@ -6,6 +6,7 @@ import io.github.niestrat99.advancedteleport.commands.TimedATCommand; import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.utilities.DistanceLimiter; import org.bukkit.Bukkit; @@ -56,6 +57,24 @@ public boolean onCommand( return true; } + // If we're on Folia, run through that location + if (RunnableManager.isFolia()) { + final Player finalPlayer = player; + final Location finalLoc = loc; + RunnableManager.setupRunner(loc, () -> processLocation(finalPlayer, finalLoc, sender)); + return true; + } else { + return processLocation(player, loc, sender); + } + } + + private boolean processLocation(@NotNull Player player, + @NotNull Location loc, + @NotNull CommandSender sender) { + + // Get the ATPlayer object + ATPlayer atPlayer = ATPlayer.getPlayer(player); + // Get the original previous location double originalY = loc.getY(); double originalX = loc.getX(); @@ -141,9 +160,11 @@ public boolean onCommand( // Teleport the target player if (sender != player) { CustomMessages.sendMessage(player, "Teleport.teleportingToLastLoc"); - player.teleport(loc); + final Location finalLoc = loc; + RunnableManager.setupRunner(player, () -> player.teleport(finalLoc), () -> {}) ; } else { - atPlayer.teleport(event, "back", "Teleport.teleportingToLastLoc"); + RunnableManager.setupRunner(player, () -> + atPlayer.teleport(event, "back", "Teleport.teleportingToLastLoc"), () -> {}); } return true; } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpAll.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpAll.java index dfcac5f4..25c79821 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpAll.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpAll.java @@ -1,12 +1,13 @@ package io.github.niestrat99.advancedteleport.commands.teleport; -import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.api.TeleportRequest; import io.github.niestrat99.advancedteleport.api.TeleportRequestType; import io.github.niestrat99.advancedteleport.commands.PlayerCommand; import io.github.niestrat99.advancedteleport.commands.TeleportATCommand; import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.CancellableRunnable; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.managers.CooldownManager; import io.github.niestrat99.advancedteleport.utilities.ConditionChecker; @@ -16,7 +17,6 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -56,17 +56,10 @@ public boolean onCommand( Placeholder.unparsed("lifetime", String.valueOf(requestLifetime)), Placeholder.component("lifetime-formatted", CustomMessages.toTime(requestLifetime))); - BukkitRunnable run = - new BukkitRunnable() { - @Override - public void run() { - TeleportRequest.removeRequest( - TeleportRequest.getRequestByReqAndResponder(target, player)); - } - }; - run.runTaskLater(CoreClass.getInstance(), requestLifetime * 20L); // 60 seconds - TeleportRequest request = - new TeleportRequest(player, target, run, TeleportRequestType.TPAHERE); + CancellableRunnable run = RunnableManager.setupRunnerDelayed(() -> + TeleportRequest.removeRequest(TeleportRequest.getRequestByReqAndResponder(target, player)), requestLifetime * 20L); + + TeleportRequest request = new TeleportRequest(player, target, run, TeleportRequestType.TPAHERE); // Creates a new teleport request. TeleportRequest.addRequest(request); // Cooldown for tpall is always applied after request diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpHereOffline.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpHereOffline.java index d6de1a61..cc6743bd 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpHereOffline.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpHereOffline.java @@ -1,9 +1,9 @@ package io.github.niestrat99.advancedteleport.commands.teleport; -import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.commands.PlayerCommand; import io.github.niestrat99.advancedteleport.commands.TeleportATCommand; import io.github.niestrat99.advancedteleport.config.CustomMessages; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.utilities.nbt.NBTReader; import net.kyori.adventure.text.Component; @@ -33,28 +33,19 @@ public boolean onCommand( } Player target = Bukkit.getPlayer(args[0]); if (target == null) { - NBTReader.setLocation( - args[0], - player.getLocation(), - new NBTReader.NBTCallback<>() { - @Override - public void onSuccess(Boolean data) { - Bukkit.getScheduler() - .runTask( - CoreClass.getInstance(), - () -> - CustomMessages.sendMessage( - sender, - "Teleport.teleportedOfflinePlayerHere", - Placeholder.unparsed( - "player", args[0]))); - } - - @Override - public void onFail(@NotNull final Component message) { - CustomMessages.sendMessage(sender, message); - } - }); + NBTReader.setLocation(args[0], player.getLocation(), new NBTReader.NBTCallback<>() { + @Override + public void onSuccess(Boolean data) { + RunnableManager.setupRunner(() -> + CustomMessages.sendMessage(sender, "Teleport.teleportedOfflinePlayerHere", + Placeholder.unparsed("player", args[0]))); + } + + @Override + public void onFail(@NotNull final Component message) { + CustomMessages.sendMessage(sender, message); + } + }); return true; } else { Bukkit.getServer().dispatchCommand(sender, "tpohere " + args[0]); diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpLoc.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpLoc.java index 31c90868..62fdd5a6 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpLoc.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpLoc.java @@ -5,6 +5,7 @@ import io.github.niestrat99.advancedteleport.api.events.ATTeleportEvent; import io.github.niestrat99.advancedteleport.commands.TeleportATCommand; import io.github.niestrat99.advancedteleport.config.CustomMessages; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.utilities.ConditionChecker; import io.github.niestrat99.advancedteleport.utilities.Pair; @@ -91,7 +92,7 @@ public boolean onCommand( if (allowFlight && target.getAllowFlight() && target.hasPermission("at.admin.tploc.safe-teleport") - && blockBelow.getBlock().getType() == Material.AIR) { + && (RunnableManager.isFolia() || blockBelow.getBlock().getType() == Material.AIR)) { target.setFlying(true); } ATPlayer.teleportWithOptions( diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpOffline.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpOffline.java index 82295528..66b30239 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpOffline.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpOffline.java @@ -1,9 +1,9 @@ package io.github.niestrat99.advancedteleport.commands.teleport; -import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.commands.PlayerCommand; import io.github.niestrat99.advancedteleport.commands.TeleportATCommand; import io.github.niestrat99.advancedteleport.config.CustomMessages; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.utilities.nbt.NBTReader; import io.papermc.lib.PaperLib; @@ -35,28 +35,20 @@ public boolean onCommand( Bukkit.getServer().dispatchCommand(sender, "tpo " + args[0]); return true; } - NBTReader.getLocation( - args[0], - new NBTReader.NBTCallback<>() { - @Override - public void onSuccess(Location data) { - Bukkit.getScheduler() - .runTask( - CoreClass.getInstance(), - () -> { - PaperLib.teleportAsync((Player) sender, data); - CustomMessages.sendMessage( - sender, - "Teleport.teleportedToOfflinePlayer", - Placeholder.unparsed("player", args[0])); - }); - } - - @Override - public void onFail(@NotNull final Component message) { - CustomMessages.sendMessage(sender, message); - } + NBTReader.getLocation(args[0], new NBTReader.NBTCallback<>() { + @Override + public void onSuccess(Location data) { + RunnableManager.setupRunner(() -> { + PaperLib.teleportAsync((Player) sender, data); + CustomMessages.sendMessage(sender, "Teleport.teleportedToOfflinePlayer", Placeholder.unparsed("player", args[0])); }); + } + + @Override + public void onFail(@NotNull final Component message) { + CustomMessages.sendMessage(sender, message); + } + }); return true; } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/Tpa.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/Tpa.java index ac9da861..e3ef224e 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/Tpa.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/Tpa.java @@ -10,6 +10,8 @@ import io.github.niestrat99.advancedteleport.commands.TimedATCommand; import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.CancellableRunnable; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.managers.CooldownManager; import io.github.niestrat99.advancedteleport.payments.PaymentManager; import io.github.niestrat99.advancedteleport.utilities.ConditionChecker; @@ -21,7 +23,6 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; public final class Tpa extends TeleportATCommand implements TimedATCommand { @@ -100,34 +101,25 @@ public boolean onCommand( CoreClass.playSound("tpa", "received", target); - BukkitRunnable run = - new BukkitRunnable() { - @Override - public void run() { - - // Get the teleport request - TeleportRequest request = - TeleportRequest.getRequestByReqAndResponder(target, player); - if (request == null) return; - - if (MainConfig.get().NOTIFY_ON_EXPIRE.get()) { - CustomMessages.sendMessage( - sender, - "Error.requestExpired", - Placeholder.unparsed("player", target.getName())); - } - TeleportRequest.removeRequest(request); - } - }; - run.runTaskLater(CoreClass.getInstance(), requestLifetime * 20L); // 60 seconds - TeleportRequest request = - new TeleportRequest(player, target, run, TeleportRequestType.TPA); - // Creates a new teleport request. - TeleportRequest.addRequest(request); - // If the cooldown is to be applied after request, apply it now - if (MainConfig.get().APPLY_COOLDOWN_AFTER.get().equalsIgnoreCase("request")) { - CooldownManager.addToCooldown("tpa", player, target.getWorld()); - } + CancellableRunnable run = RunnableManager.setupRunnerDelayed(() -> { + // Get the teleport request + TeleportRequest request = TeleportRequest.getRequestByReqAndResponder(target, player); + if (request == null) return; + + if (MainConfig.get().NOTIFY_ON_EXPIRE.get()) { + CustomMessages.sendMessage(sender, "Error.requestExpired", + Placeholder.unparsed("player", target.getName())); + } + TeleportRequest.removeRequest(request); + }, requestLifetime * 20L); + + TeleportRequest request = new TeleportRequest(player, target, run, TeleportRequestType.TPA); + // Creates a new teleport request. + TeleportRequest.addRequest(request); + // If the cooldown is to be applied after request, apply it now + if (MainConfig.get().APPLY_COOLDOWN_AFTER.get().equalsIgnoreCase("request")) { + CooldownManager.addToCooldown("tpa", player, target.getWorld()); + } } return true; } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpaHere.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpaHere.java index c6affd9c..e0662132 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpaHere.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpaHere.java @@ -9,6 +9,8 @@ import io.github.niestrat99.advancedteleport.commands.TimedATCommand; import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.CancellableRunnable; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.managers.CooldownManager; import io.github.niestrat99.advancedteleport.payments.PaymentManager; import io.github.niestrat99.advancedteleport.utilities.ConditionChecker; @@ -20,7 +22,6 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; public final class TpaHere extends TeleportATCommand implements TimedATCommand { @@ -87,33 +88,21 @@ public boolean onCommand( } CoreClass.playSound("tpahere", "received", target); - BukkitRunnable run = - new BukkitRunnable() { - @Override - public void run() { - if (MainConfig.get().NOTIFY_ON_EXPIRE.get()) { - CustomMessages.sendMessage( - sender, - "Error.requestExpired", - Placeholder.unparsed("player", target.getName())); + CancellableRunnable run = RunnableManager.setupRunnerDelayed(task -> { + if (MainConfig.get().NOTIFY_ON_EXPIRE.get()) { + CustomMessages.sendMessage(sender, "Error.requestExpired", + Placeholder.unparsed("player", target.getName())); + } - TeleportRequest.removeRequest( - TeleportRequest.getRequestByReqAndResponder( - target, player)); - } - } - }; - run.runTaskLater(CoreClass.getInstance(), requestLifetime * 20L); // 60 seconds - TeleportRequest request = - new TeleportRequest( - player, - target, - run, - TeleportRequestType.TPAHERE); // Creates a new teleport request. + TeleportRequest.removeRequest(TeleportRequest.getRequestByReqAndResponder( + target, + player + )); + }, requestLifetime * 20L); + TeleportRequest request = new TeleportRequest(player, target, run, TeleportRequestType.TPAHERE); // Creates a new teleport request. TeleportRequest.addRequest(request); // If the cooldown is to be applied after request or accept (they are the same in the - // case of - // /spawn), apply it now + // case of /spawn), apply it now if (MainConfig.get().APPLY_COOLDOWN_AFTER.get().equalsIgnoreCase("request")) { CooldownManager.addToCooldown("tpahere", player, player.getWorld()); } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/CustomMessages.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/CustomMessages.java index e03a69ee..6d8b1daa 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/CustomMessages.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/CustomMessages.java @@ -7,6 +7,10 @@ import io.github.niestrat99.advancedteleport.api.ATPlayer; import io.github.niestrat99.advancedteleport.api.NamedLocation; import io.github.niestrat99.advancedteleport.api.data.ATException; +import io.github.niestrat99.advancedteleport.data.PartialComponent; +import io.github.niestrat99.advancedteleport.extensions.ExPermission; +import io.github.niestrat99.advancedteleport.folia.CancellableRunnable; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.utilities.minimessage.PartialComponent; import io.github.niestrat99.advancedteleport.managers.PluginHookManager; import io.github.niestrat99.advancedteleport.utilities.PagedLists; @@ -34,7 +38,6 @@ import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; import org.geysermc.floodgate.api.FloodgateApi; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Contract; @@ -52,9 +55,9 @@ public final class CustomMessages extends ATConfig { public static CustomMessages config; - private static HashMap titleManager; - private static HashMap actionBarManager; - private static HashMap soundManager; + private static HashMap titleManager; + private static HashMap actionBarManager; + private static HashMap soundManager; @NotNull private static ImmutableMap messageCache = ImmutableMap.of(); @NotNull private static ImmutableSortedSet prefixes = ImmutableSortedSet.of(); @Nullable private static BukkitAudiences audience; @@ -931,20 +934,19 @@ public static void sendMessage( titleInfo[2] = titles.getInteger("fade-out"); } - // Handle t - BukkitRunnable titleRunnable = - new BukkitRunnable() { + Consumer runnable = new Consumer<>() { - private int current = 0; - private @Nullable Component previousTitle = null; - private @Nullable Component previousSubtitle = null; + private int current = 0; + private @Nullable Component previousTitle = null; + + private @Nullable Component previousSubtitle = null; - @Override - public void run() { - if (current == titleInfo[1] || titleManager.get(player) != this) { - cancel(); - return; - } + @Override + public void accept(CancellableRunnable runnable) { + if (current == titleInfo[1] || titleManager.get(player) != runnable) { + runnable.cancel(); + return; + } String title = null; String subtitle = null; @@ -982,12 +984,10 @@ public void run() { Duration.ofMillis( titleInfo[2] * 50L)))); - current++; - } - }; - - titleManager.put(player, titleRunnable); - titleRunnable.runTaskTimer(CoreClass.getInstance(), 1, 1); + current++; + } + }; + titleManager.put(player, RunnableManager.setupRunnerPeriod(player, runnable, () -> {}, 1, 1)); } } else { String raw = config.getString(path); @@ -1232,7 +1232,8 @@ private static void handleSpecialMessage( @NotNull Player player, @NotNull String id, @NotNull Consumer consumer, - @NotNull HashMap runnableTracker) { + @NotNull HashMap runnableTracker + ) { // If the section does not exist, stop there if (!config.contains(id)) return; @@ -1248,20 +1249,19 @@ private static void handleSpecialMessage( ConfigSection section = config.getConfigSection(id); // Create the BukkitRunnable - BukkitRunnable runnable = - new BukkitRunnable() { + Consumer runnable = new Consumer<>() { private final Queue times = new ArrayDeque<>(section.getKeys(false)); private int current = 0; - @Override - public void run() { + @Override + public void accept(CancellableRunnable cancellableRunnable) { - // If the times queue is empty stop there - if (times.isEmpty() || runnableTracker.get(player) != this) { - cancel(); - return; - } + // If the times queue is empty stop there + if (times.isEmpty() || runnableTracker.get(player) != cancellableRunnable) { + cancellableRunnable.cancel(); + return; + } // If the next element is equal to the current timer, use that if (!String.valueOf(current++).equals(times.peek())) return; @@ -1274,8 +1274,7 @@ public void run() { }; // Add it to the hashmap and run it - runnableTracker.put(player, runnable); - runnable.runTaskTimer(CoreClass.getInstance(), 1, 1); + runnableTracker.put(player, RunnableManager.setupRunnerPeriod(player, runnable, () -> {}, 1, 1)); } @Contract(pure = true) diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/MainConfig.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/MainConfig.java index d7ca74b6..70b0690c 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/MainConfig.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/MainConfig.java @@ -1231,10 +1231,10 @@ public void postSave() { if (!permission.startsWith("at")) continue; if (permission.startsWith("at.admin")) { if (!warned) { - CoreClass.getInstance() - .getLogger() - .warning( - "WARNING: You've given an admin permission by default to all users."); + CoreClass.getInstance() + .getLogger() + .warning( + "WARNING: You've given an admin permission by default to all users."); if (!ALLOW_ADMIN_PERMS.get() || CoreClass.getPerms() != null) { CoreClass.getInstance() .getLogger() @@ -1254,9 +1254,8 @@ public void postSave() { } else { CoreClass.getInstance() .getLogger() - .warning( - "This can potentially be destructive, so if this " - + "is not your doing, please check your configuration."); + .warning("This can potentially be destructive, so if this " + + "is not your doing, please check your configuration."); CoreClass.getInstance() .getLogger() .warning( diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/CancellableRunnable.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/CancellableRunnable.java new file mode 100644 index 00000000..e75c995d --- /dev/null +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/CancellableRunnable.java @@ -0,0 +1,28 @@ +package io.github.niestrat99.advancedteleport.folia; + +import org.jetbrains.annotations.NotNull; + +import java.util.function.Consumer; + +public class CancellableRunnable implements Runnable { + + private final @NotNull Consumer consumer; + protected boolean cancelled = false; + + public CancellableRunnable(final @NotNull Consumer consumer) { + this.consumer = consumer; + } + + public CancellableRunnable(final @NotNull Runnable runnable) { + this(task -> runnable.run()); + } + + public void cancel() { + cancelled = true; + } + + @Override + public void run() { + this.consumer.accept(this); + } +} diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/RunnableManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/RunnableManager.java new file mode 100644 index 00000000..cfa8e6fa --- /dev/null +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/RunnableManager.java @@ -0,0 +1,140 @@ +package io.github.niestrat99.advancedteleport.folia; + +import io.github.niestrat99.advancedteleport.CoreClass; +import io.github.niestrat99.advancedteleport.folia.schedulers.FoliaRunnable; +import io.github.niestrat99.advancedteleport.folia.schedulers.NormalBukkitRunnable; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +public class RunnableManager { + + public static CancellableRunnable setupRunnerPeriod(Entity entity, Consumer runnable, Runnable retired, long delay, long period) { + + return run(runnable, + (run) -> Bukkit.getScheduler().runTaskTimer(CoreClass.getInstance(), run::start, delay, period), + (run) -> entity.getScheduler().runAtFixedRate(CoreClass.getInstance(), run::start, retired, delay, period)); + } + + public static CancellableRunnable setupRunnerDelayed(Entity entity, Consumer runnable, Runnable retired, long delay) { + + return run(runnable, + (run) -> Bukkit.getScheduler().runTaskLater(CoreClass.getInstance(), run::start, delay), + (run) -> entity.getScheduler().runDelayed(CoreClass.getInstance(), run::start, retired, delay)); + } + + public static CancellableRunnable setupRunnerDelayed(Runnable runnable, long delay) { + + return setupRunnerDelayed(task -> runnable.run(), delay); + } + + public static CancellableRunnable setupRunnerDelayed(Consumer runnable, long delay) { + + return run(runnable, + (run) -> Bukkit.getScheduler().runTaskLater(CoreClass.getInstance(), run::start, delay), + (run) -> Bukkit.getGlobalRegionScheduler().runDelayed(CoreClass.getInstance(), run::start, delay)); + } + + public static CancellableRunnable setupRunnerDelayedAsync(Consumer runnable, long delay) { + + return run(runnable, + (run) -> Bukkit.getScheduler().runTaskLaterAsynchronously(CoreClass.getInstance(), run::start, delay), + (run) -> Bukkit.getAsyncScheduler().runDelayed(CoreClass.getInstance(), run::start, delay * 50, TimeUnit.MILLISECONDS)); + } + + public static CancellableRunnable setupRunnerPeriod(Consumer runnable, long delay, long period) { + + return run(runnable, + (run) -> Bukkit.getScheduler().runTaskTimer(CoreClass.getInstance(), run::start, delay, period), + (run) -> Bukkit.getGlobalRegionScheduler().runAtFixedRate(CoreClass.getInstance(), run::start, delay, period)); + } + + public static CancellableRunnable setupRunnerPeriodAsync(Runnable runnable, long delay, long period) { + + return setupRunnerPeriodAsync(task -> runnable.run(), delay, period); + } + + public static CancellableRunnable setupRunnerPeriodAsync(Consumer runnable, long delay, long period) { + + return run(runnable, + (run) -> Bukkit.getScheduler().runTaskTimerAsynchronously(CoreClass.getInstance(), run::start, delay, period), + (run) -> Bukkit.getAsyncScheduler().runAtFixedRate(CoreClass.getInstance(), run::start, delay * 50, period * 50, TimeUnit.MILLISECONDS)); + } + + public static CancellableRunnable setupRunnerAsync(Runnable runnable) { + + return setupRunnerAsync(task -> runnable.run()); + } + + public static CancellableRunnable setupRunnerAsync(Consumer runnable) { + + return run(runnable, + (run) -> Bukkit.getScheduler().runTaskAsynchronously(CoreClass.getInstance(), run::start), + (run) -> Bukkit.getAsyncScheduler().runNow(CoreClass.getInstance(), run::start)); + } + + public static CancellableRunnable setupRunner(Runnable runnable) { + + return setupRunner(task -> runnable.run()); + } + + public static CancellableRunnable setupRunner(Location location, Runnable runnable) { + return setupRunner(location, task -> runnable.run()); + } + + public static CancellableRunnable setupRunner(Entity entity, Runnable runnable, Runnable retired) { + return setupRunner(entity, task -> runnable.run(), retired); + } + + public static CancellableRunnable setupRunner(Consumer runnable) { + + return run(runnable, + (run) -> Bukkit.getScheduler().runTask(CoreClass.getInstance(), run::start), + (run) -> Bukkit.getGlobalRegionScheduler().run(CoreClass.getInstance(), run::start)); + } + + public static CancellableRunnable setupRunner(Location location, Consumer runnable) { + + return run(runnable, + (run) -> Bukkit.getScheduler().runTask(CoreClass.getInstance(), run::start), + (run) -> Bukkit.getRegionScheduler().execute(CoreClass.getInstance(), location, run)); + } + + public static CancellableRunnable setupRunner(Entity entity, Consumer runnable, Runnable retired) { + + return run(runnable, + (run) -> Bukkit.getScheduler().runTask(CoreClass.getInstance(), run::start), + (run) -> entity.getScheduler().run(CoreClass.getInstance(), run::start, retired)); + } + + private static CancellableRunnable run( + @NotNull Consumer runnable, + @NotNull Consumer bukkitRunnable, + @NotNull Consumer foliaRunnable + ) { + + if (!isFolia()) { + NormalBukkitRunnable coolRunnable = new NormalBukkitRunnable(runnable); + bukkitRunnable.accept(coolRunnable); + return coolRunnable; + } + + FoliaRunnable coolRunnable = new FoliaRunnable(runnable); + foliaRunnable.accept(coolRunnable); + return coolRunnable; + } + + public static boolean isFolia() { + + try { + Class.forName("io.papermc.paper.threadedregions.scheduler.EntityScheduler"); + return true; + } catch (NoClassDefFoundError | ClassNotFoundException ignored) { + return false; + } + } +} diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/schedulers/FoliaRunnable.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/schedulers/FoliaRunnable.java new file mode 100644 index 00000000..0f6310f3 --- /dev/null +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/schedulers/FoliaRunnable.java @@ -0,0 +1,32 @@ +package io.github.niestrat99.advancedteleport.folia.schedulers; + +import io.github.niestrat99.advancedteleport.folia.CancellableRunnable; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.function.Consumer; + +public class FoliaRunnable extends CancellableRunnable { + + private @Nullable ScheduledTask task; + + public FoliaRunnable(@NotNull Consumer runnable) { + super(runnable); + } + + public FoliaRunnable(@NotNull Runnable runnable) { + super(runnable); + } + + public void start(@NotNull ScheduledTask task) { + this.task = task; + if (!cancelled) run(); + } + + @Override + public void cancel() { + super.cancel(); + if (task != null) this.task.cancel(); + } +} diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/schedulers/NormalBukkitRunnable.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/schedulers/NormalBukkitRunnable.java new file mode 100644 index 00000000..385075e6 --- /dev/null +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/schedulers/NormalBukkitRunnable.java @@ -0,0 +1,32 @@ +package io.github.niestrat99.advancedteleport.folia.schedulers; + +import io.github.niestrat99.advancedteleport.folia.CancellableRunnable; +import org.bukkit.scheduler.BukkitTask; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.function.Consumer; + +public class NormalBukkitRunnable extends CancellableRunnable { + + private @Nullable BukkitTask task; + + public NormalBukkitRunnable(@NotNull Consumer runnable) { + super(runnable); + } + + public NormalBukkitRunnable(@NotNull Runnable runnable) { + super(runnable); + } + + public void start(@NotNull BukkitTask task) { + this.task = task; + if (!cancelled) run(); + } + + @Override + public void cancel() { + super.cancel(); + if (task != null) this.task.cancel(); + } +} diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/CommandManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/CommandManager.java index 4d0642ee..f66921cd 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/CommandManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/CommandManager.java @@ -11,6 +11,7 @@ import io.github.niestrat99.advancedteleport.commands.warp.*; import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import org.bukkit.Bukkit; @@ -96,7 +97,7 @@ private static void register(String name, ATCommand atCommand) { CommandMap map = getMap(); if (map == null) return; - HashMap commands = getCommands(map); + Map commands = getCommands(map); if (commands == null) return; List aliases = new ArrayList<>(command.getAliases()); @@ -122,28 +123,24 @@ private static void register(String name, ATCommand atCommand) { CoreClass.debug("Removed " + alias + "."); // Let another plugin take over - Bukkit.getScheduler() - .runTaskLater( - CoreClass.getInstance(), - () -> { - Iterator commandIterator = commands.keySet().iterator(); - HashMap pendingChanges = new HashMap<>(); - - // Ignore warning, can yield CME - while (commandIterator.hasNext()) { - String otherCmd = commandIterator.next(); - String[] parts = otherCmd.split(":"); - if (parts.length < 2) continue; - if (parts[1].equals(alias)) { - if (parts[0].equals("advancedteleport")) continue; - CoreClass.debug("Letting " + parts[0] + "'s " + alias + " take over..."); - pendingChanges.put(alias, commands.get(otherCmd)); - break; - } - } - commands.putAll(pendingChanges); - }, - 100); + RunnableManager.setupRunnerDelayed((run) -> { + Iterator commandIterator = commands.keySet().iterator(); + HashMap pendingChanges = new HashMap<>(); + + // Ignore warning, can yield CME + while (commandIterator.hasNext()) { + String otherCmd = commandIterator.next(); + String[] parts = otherCmd.split(":"); + if (parts.length < 2) continue; + if (parts[1].equals(alias)) { + if (parts[0].equals("advancedteleport")) continue; + CoreClass.debug("Letting " + parts[0] + "'s " + alias + " take over..."); + pendingChanges.put(alias, commands.get(otherCmd)); + break; + } + } + commands.putAll(pendingChanges); + }, 100); } return; } @@ -188,11 +185,15 @@ private static CommandMap getMap() { return null; } - private static HashMap getCommands(CommandMap map) { + private static Map getCommands(CommandMap map) { + try { + return map.getKnownCommands(); + } catch (NoSuchMethodError ignored) { + } try { Field commands = SimpleCommandMap.class.getDeclaredField("knownCommands"); commands.setAccessible(true); - return (HashMap) commands.get(map); + return (Map) commands.get(map); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/CooldownManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/CooldownManager.java index 012e9f29..3b153bfe 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/CooldownManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/CooldownManager.java @@ -1,15 +1,12 @@ package io.github.niestrat99.advancedteleport.managers; -import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.api.ATPlayer; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitRunnable; -import org.bukkit.scheduler.BukkitTask; import java.util.ArrayList; import java.util.Arrays; @@ -63,7 +60,7 @@ public static void init() { } } - public static class ATRunnable extends BukkitRunnable { + public static class ATRunnable implements Runnable { private final UUID uuid; private final long startingTime; private final String command; @@ -78,12 +75,7 @@ public ATRunnable(UUID uuid, long waitingTime, String command) { } this.command = getKey(command); startingTime = System.currentTimeMillis(); - runTaskLater(CoreClass.getInstance()); - } - - public synchronized BukkitTask runTaskLater(Plugin plugin) - throws IllegalArgumentException, IllegalStateException { - return super.runTaskLater(plugin, ms * 20); + RunnableManager.setupRunnerDelayed(t -> this.run(), ms * 20); } @Override diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/MovementManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/MovementManager.java index 51872f3d..e3805b1c 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/MovementManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/MovementManager.java @@ -1,9 +1,10 @@ package io.github.niestrat99.advancedteleport.managers; -import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.api.ATPlayer; import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.CancellableRunnable; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.payments.PaymentManager; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; @@ -18,7 +19,7 @@ import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -import org.bukkit.scheduler.BukkitRunnable; +import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.UUID; @@ -35,7 +36,7 @@ public void onMovement(PlayerMoveEvent event) { UUID uuid = event.getPlayer().getUniqueId(); if (cancelled && movement.containsKey(uuid)) { ImprovedRunnable timer = movement.get(uuid); - timer.cancel(); + timer.runnable.cancel(); CustomMessages.sendMessage(event.getPlayer(), "Teleport.eventMovement"); ParticleManager.removeParticles(event.getPlayer(), timer.command); movement.remove(uuid); @@ -145,10 +146,8 @@ public static void createMovementTimer( ParticleManager.applyParticles(teleportingPlayer, command); // Starts the movement checker. - ImprovedRunnable movementTimer = - new ImprovedRunnable(command) { - @Override - public void run() { + ImprovedRunnable runnable = new ImprovedRunnable(command, + RunnableManager.setupRunnerDelayed(teleportingPlayer, task -> { // If the player can't pay for the if (!PaymentManager.getInstance() @@ -164,6 +163,7 @@ public void run() { // If we didn't succeed, let the player know. if (!result) { CustomMessages.sendMessage(teleportingPlayer, "Error.teleportFailed"); + movement.remove(uuid); return; } @@ -189,10 +189,9 @@ public void run() { }); movement.remove(uuid); - } - }; - movement.put(uuid, movementTimer); - movementTimer.runTaskLater(CoreClass.getInstance(), warmUp * 20L); + + }, () -> {}, warmUp * 20L)); + movement.put(uuid, runnable); if ((MainConfig.get().CANCEL_WARM_UP_ON_MOVEMENT.get() && !teleportingPlayer.hasPermission("at.admin.bypass.movement")) || (MainConfig.get().CANCEL_WARM_UP_ON_ROTATION.get() @@ -211,12 +210,14 @@ public void run() { } } - public abstract static class ImprovedRunnable extends BukkitRunnable { + public static class ImprovedRunnable { private final String command; + private final @NotNull CancellableRunnable runnable; - ImprovedRunnable(String command) { + ImprovedRunnable(String command, @NotNull CancellableRunnable runnable) { this.command = command; + this.runnable = runnable; } public String getCommand() { diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/RTPManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/RTPManager.java index 48826fa7..a0f68650 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/RTPManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/RTPManager.java @@ -4,6 +4,7 @@ import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.utilities.RandomCoords; import io.papermc.lib.PaperLib; @@ -22,6 +23,7 @@ import java.io.IOException; import java.util.*; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; public class RTPManager { @@ -63,17 +65,33 @@ public static boolean isInitialised() { return locQueue != null; } + /** + * Used to fetch the next available random location in the world. + * + * @param world the world to check. + * @return + */ public static CompletableFuture getNextAvailableLocation(World world) { + + // Fetch from the queue. final Queue queue = locQueue.get(world.getUID()); - addLocation(world, false, 0) - .thenAccept( - location -> { - if (location == null) return; - queue.add(location); - locQueue.put(world.getUID(), queue); - }); + + // Proactively add a location to the queue in case we remove the location. + addLocation(world, false).whenComplete((location, err) -> { + if (err != null) { + err.printStackTrace(); + return; + } + + // If a location was not found due to being exhausted, stop there. + if (location == null) return; + queue.add(location); + locQueue.put(world.getUID(), queue); + }); + + // If there's nothing found, then fetch the location directly and mark it as urgent, otherwise just fetch it from the queue if (queue == null || queue.isEmpty()) { - return addLocation(world, true, 0); + return addLocation(world, true); } else { return CompletableFuture.completedFuture(queue.poll()); } @@ -81,13 +99,15 @@ public static CompletableFuture getNextAvailableLocation(World world) public static Location getLocationUrgently(World world) { Queue queue = locQueue.get(world.getUID()); - addLocation(world, false, 0) - .thenAccept( - location -> { - if (location == null) return; - queue.add(location); - locQueue.put(world.getUID(), queue); - }); + addLocation(world, false).whenComplete((location, err) -> { + if (err != null) { + err.printStackTrace(); + return; + } + if (location == null) return; + queue.add(location); + locQueue.put(world.getUID(), queue); + }); if (queue == null || queue.isEmpty()) { return null; } else { @@ -96,21 +116,15 @@ public static Location getLocationUrgently(World world) { } public static CompletableFuture<@Nullable Location> addLocation( - final World world, final boolean urgent, int tries) { + final World world, final boolean urgent) { CoreClass.debug("Attempting to either add a location or return it for /rtp."); // If it's not a Paper server, stop there. if (!PaperLib.isPaper()) return CompletableFuture.completedFuture(null); - // Increment the number of attempts so we don't exhaust the server. - tries++; - - // If there are too many locations for a world, just return the first one and remove it from - // the queue. - if (locQueue.get(world.getUID()) != null - && locQueue.get(world.getUID()).size() - > MainConfig.get().PREPARED_LOCATIONS_LIMIT.get()) { + // If there are too many locations for a world, just return the first one and remove it from the queue. + if (locQueue.get(world.getUID()) != null && locQueue.get(world.getUID()).size() > MainConfig.get().PREPARED_LOCATIONS_LIMIT.get()) { Location loc = locQueue.get(world.getUID()).poll(); if (!PluginHookManager.get().isClaimed(loc)) { CoreClass.debug("Area is not claimed - returning as valid location"); @@ -118,47 +132,68 @@ public static Location getLocationUrgently(World world) { } } - // Generate the coordinates. - Location location = RandomCoords.generateCoords(world); + // Generate initial coordinates for the world + RandomCoords.generateCoords(world); + + // Alright baby let's go + return CompletableFuture.supplyAsync(() -> { + + // Declare initial variables + int tries = 0; + + // Wait for the chunk to load + while (tries < 5 || urgent) { + + // Fetch location + Location location = RandomCoords.generateCoords(world); + if (location == null) return null; + int[] coords = new int[]{location.getBlockX(), location.getBlockZ()}; + + try { + tries++; + + // If we're on Folia, then do some tomfoolery and handle chunk-related tasks on the scheduler for the chunk + if (RunnableManager.isFolia()) { + + // Let it do what it needs to do and wait on it. + return CompletableFuture.supplyAsync(() -> { - if (location == null) { - return CompletableFuture.completedFuture(null); + // Get the block + Block block = world.getEnvironment().equals(World.Environment.NETHER) ? doBinaryJump(world, coords) : world.getHighestBlockAt(coords[0], coords[1]); + + // If it's valid, then return it + return isValidLocation(block) ? block.getLocation().add(0.5, 1, 0.5) : null; + + }, task -> Bukkit.getRegionScheduler().execute(CoreClass.getInstance(), world, coords[0] >> 4, coords[1] >> 4, task)).get(); + } + + Block block = world.getEnvironment().equals(World.Environment.NETHER) ? doBinaryJump(world, coords) : world.getHighestBlockAt(coords[0], coords[1]); + if (isValidLocation(block)) return block.getLocation().add(0.5, 1, 0.5); + + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } + + return null; + }, CoreClass.async); + } + + private static Block getHighestBlock(World world, int x, int z) { + + // If we're on Folia, then hop onto the region thread briefly + if (RunnableManager.isFolia()) { + return CompletableFuture.supplyAsync(() -> world.getHighestBlockAt(x, z), + task -> Bukkit.getGlobalRegionScheduler().execute(CoreClass.getInstance(), task)).join(); } - int[] coords = new int[] {location.getBlockX(), location.getBlockZ()}; - int finalTries = tries; - CoreClass.debug("Attempting coordinates: " + Arrays.toString(coords) + " (try " + finalTries + ")"); - - // Attempt to fetch the chunk to be loaded. - return PaperLib.getChunkAtAsync(world, coords[0] >> 4, coords[1] >> 4, true, urgent) - .thenApplyAsync( - chunk -> { - - // If we're in the Nether, do a binary jump, otherwise get the highest - // block. - Block block = - world.getEnvironment().equals(World.Environment.NETHER) - ? doBinaryJump(world, coords) - : world.getHighestBlockAt(coords[0], coords[1]); - - // If it's a valid location, return it. If not, try again unless the - // plugin has exhausted its attempts. - if (isValidLocation(block)) { - CoreClass.debug("Valid location found - returning it"); - return block.getLocation().add(0.5, 1, 0.5); - } else if (finalTries < 10) { - CoreClass.debug("Invalid location, trying again..."); - return addLocation(world, urgent, finalTries).join(); - } else { - CoreClass.debug("Invalid location - not going to try again"); - return null; - } - }, - CoreClass.async) - .thenApplyAsync(loc -> loc, CoreClass.sync); + return world.getHighestBlockAt(x, z); } - private static Block doBinaryJump(World world, int[] coords) { + private static Block doBinaryJump( + World world, + int[] coords + ) { Location location = new Location(world, coords[0], 128, coords[1]); // This is how much we'll jump by at first @@ -210,8 +245,7 @@ private static Block doBinaryJump(World world, int[] coords) { if (currentMat != Material.AIR) { if (subTempLocation.add(0, 1, 0).getBlock().getType() == Material.AIR - && subTempLocation.clone().add(0, 1, 0).getBlock().getType() - == Material.AIR) { + && subTempLocation.clone().add(0, 1, 0).getBlock().getType() == Material.AIR) { return subTempLocation.add(0.5, -1, 0.5).getBlock(); } else { up = true; @@ -240,14 +274,15 @@ public static void loadWorldData(World world) { int size = locQueue.getOrDefault(world.getUID(), new ArrayDeque<>()).size(); for (int i = size; i < MainConfig.get().PREPARED_LOCATIONS_LIMIT.get(); i++) { - addLocation(world, false, 0) - .thenAccept( - location -> { - Queue queue = - locQueue.getOrDefault(world.getUID(), new ArrayDeque<>()); - queue.add(location); - locQueue.put(world.getUID(), queue); - }); + addLocation(world, false).whenComplete((location, err) -> { + if (err != null) { + err.printStackTrace(); + return; + } + Queue queue = locQueue.getOrDefault(world.getUID(), new ArrayDeque<>()); + queue.add(location); + locQueue.put(world.getUID(), queue); + }); } } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/TeleportTrackingManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/TeleportTrackingManager.java index 49631fd6..e52ee1af 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/TeleportTrackingManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/TeleportTrackingManager.java @@ -3,11 +3,13 @@ import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.api.ATPlayer; import io.github.niestrat99.advancedteleport.api.AdvancedTeleportAPI; +import io.github.niestrat99.advancedteleport.api.Spawn; import io.github.niestrat99.advancedteleport.api.Warp; import io.github.niestrat99.advancedteleport.api.events.ATTeleportEvent; -import io.github.niestrat99.advancedteleport.api.Spawn; import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.utilities.ConditionChecker; import io.github.thatsmusic99.configurationmaster.api.ConfigSection; @@ -104,14 +106,11 @@ public void onJoin(PlayerJoinEvent e) { } } - private void spawn(Player player, Location spawn) { - Bukkit.getScheduler() - .runTaskLater( - CoreClass.getInstance(), - () -> + private void spawn(Player player, Spawn spawn) { + RunnableManager.setupRunnerDelayed(t -> ATPlayer.teleportWithOptions( player, - spawn, + spawn.getLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN) .whenComplete( (result, err) -> { @@ -174,6 +173,13 @@ public void onTeleport(ATTeleportEvent e) { Placeholder.unparsed("world", e.getToLocation().getWorld().getName())); e.setCancelled(true); } + + // Temporary fix for Folia - set the previous location + if (MainConfig.get().USE_BASIC_TELEPORT_FEATURES.get() + && MainConfig.get().BACK_TELEPORT_CAUSES.get().contains("COMMAND") + && RunnableManager.isFolia()) { + ATPlayer.getPlayer(e.getPlayer()).setPreviousLocation(e.getFromLocation()); + } } @EventHandler diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/BlocklistManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/BlocklistManager.java index 70f760e7..80aa6ba4 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/BlocklistManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/BlocklistManager.java @@ -2,8 +2,8 @@ import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.api.BlockInfo; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; -import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.jetbrains.annotations.NotNull; @@ -33,39 +33,30 @@ public static BlocklistManager get() { @Override public void createTable() { - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), - () -> { - CoreClass.debug( - "Creating table data for the block list manager if it is not already set up."); - - // Attempt to create the table. - try (Connection connection = implementConnection()) { - PreparedStatement createTable = - prepareStatement( - connection, - "CREATE TABLE IF NOT EXISTS " - + tablePrefix - + "_blocklist " - + "(id INTEGER PRIMARY KEY " - + getStupidAutoIncrementThing() - + ", " - + "uuid_receiver VARCHAR(256) NOT NULL, " - + "uuid_blocked VARCHAR(256) NOT NULL," - + "timestamp BIGINT NOT NULL," - + "reason TEXT)"); - executeUpdate(createTable); - } catch (SQLException exception) { - CoreClass.getInstance() - .getLogger() - .severe("Failed to create the blocklist table."); - exception.printStackTrace(); - } - - // Transfer old data. - transferOldData(); - }); + RunnableManager.setupRunnerAsync(() -> { + + CoreClass.debug("Creating table data for the block list manager if it is not already set up."); + + // Attempt to create the table. + try (Connection connection = implementConnection()) { + PreparedStatement createTable = prepareStatement( + connection, + "CREATE TABLE IF NOT EXISTS " + tablePrefix + "_blocklist " + + "(id INTEGER PRIMARY KEY " + getStupidAutoIncrementThing() + ", " + + "uuid_receiver VARCHAR(256) NOT NULL, " + + "uuid_blocked VARCHAR(256) NOT NULL," + + "timestamp BIGINT NOT NULL," + + "reason TEXT)" + ); + executeUpdate(createTable); + } catch (SQLException exception) { + CoreClass.getInstance().getLogger().severe("Failed to create the blocklist table."); + exception.printStackTrace(); + } + + // Transfer old data. + transferOldData(); + }); } @Override diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/DataFailManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/DataFailManager.java index da9367d7..8f8db037 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/DataFailManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/DataFailManager.java @@ -4,6 +4,7 @@ import io.github.niestrat99.advancedteleport.api.AdvancedTeleportAPI; import io.github.niestrat99.advancedteleport.api.Warp; import io.github.niestrat99.advancedteleport.api.WorldlessLocation; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.jetbrains.annotations.NotNull; @@ -51,19 +52,12 @@ public DataFailManager() { } } - Bukkit.getScheduler() - .runTaskTimerAsynchronously( - CoreClass.getInstance(), - () -> { - for (Fail fail : pendingFails.keySet()) { - CoreClass.getInstance() - .getLogger() - .warning("Handling failure " + fail.operation.name() + "."); - handleFailure(fail); - } - }, - 1200, - 1200); + RunnableManager.setupRunnerPeriodAsync(() -> { + for (Fail fail : pendingFails.keySet()) { + CoreClass.getInstance().getLogger().warning("Handling failure " + fail.operation.name() + "."); + handleFailure(fail); + } + }, 1200, 1200); } public void addFailure(Operation operation, String... data) { diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/HomeSQLManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/HomeSQLManager.java index 3dd52057..2bfc396a 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/HomeSQLManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/HomeSQLManager.java @@ -4,6 +4,8 @@ import io.github.niestrat99.advancedteleport.api.ATPlayer; import io.github.niestrat99.advancedteleport.api.Home; import io.github.niestrat99.advancedteleport.api.WorldlessLocation; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; + import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.OfflinePlayer; @@ -36,43 +38,41 @@ public static HomeSQLManager get() { @Override public void createTable() { - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), - () -> { - CoreClass.debug( - "Creating table data for the home manager if it is not already set up."); - - try (Connection connection = implementConnection()) { - PreparedStatement createTable = - prepareStatement( - connection, - "CREATE TABLE IF NOT EXISTS " - + tablePrefix - + "_homes " - + "(id INTEGER PRIMARY KEY " - + getStupidAutoIncrementThing() - + ", " - + "uuid_owner VARCHAR(256) NOT NULL, " - + "home VARCHAR(256) NOT NULL," - + "x DOUBLE NOT NULL," - + "y DOUBLE NOT NULL," - + "z DOUBLE NOT NULL," - + "yaw FLOAT NOT NULL," - + "pitch FLOAT NOT NULL," - + "world VARCHAR(256) NOT NULL," - + "icon VARCHAR(256) DEFAULT 'GRASS_BLOCK' NOT NULL," - + "timestamp_created BIGINT NOT NULL," - + "timestamp_updated BIGINT NOT NULL)"); - executeUpdate(createTable); - } catch (SQLException exception) { - CoreClass.getInstance() - .getLogger() - .severe("Failed to create the homes table."); - exception.printStackTrace(); - } - transferOldData(); - }); + RunnableManager.setupRunnerAsync( + () -> { + CoreClass.debug( + "Creating table data for the home manager if it is not already set up."); + + try (Connection connection = implementConnection()) { + PreparedStatement createTable = + prepareStatement( + connection, + "CREATE TABLE IF NOT EXISTS " + + tablePrefix + + "_homes " + + "(id INTEGER PRIMARY KEY " + + getStupidAutoIncrementThing() + + ", " + + "uuid_owner VARCHAR(256) NOT NULL, " + + "home VARCHAR(256) NOT NULL," + + "x DOUBLE NOT NULL," + + "y DOUBLE NOT NULL," + + "z DOUBLE NOT NULL," + + "yaw FLOAT NOT NULL," + + "pitch FLOAT NOT NULL," + + "world VARCHAR(256) NOT NULL," + + "icon VARCHAR(256) DEFAULT 'GRASS_BLOCK' NOT NULL," + + "timestamp_created BIGINT NOT NULL," + + "timestamp_updated BIGINT NOT NULL)"); + executeUpdate(createTable); + } catch (SQLException exception) { + CoreClass.getInstance() + .getLogger() + .severe("Failed to create the homes table."); + exception.printStackTrace(); + } + transferOldData(); + }); } @Override @@ -128,9 +128,7 @@ public void addHome(Location location, UUID owner, String name) { public void addHome(Location location, UUID owner, String name, boolean async) { if (async) { - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), () -> addHomePrivate(location, owner, name)); + RunnableManager.setupRunnerAsync(() -> addHomePrivate(location, owner, name)); } else { addHomePrivate(location, owner, name); } @@ -225,10 +223,7 @@ public void moveHome(Location newLocation, UUID owner, String name) { public void moveHome(Location newLocation, UUID owner, String name, boolean async) { if (async) { - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), - () -> moveHomePrivate(newLocation, owner, name)); + RunnableManager.setupRunnerAsync(() -> moveHomePrivate(newLocation, owner, name)); } else { moveHomePrivate(newLocation, owner, name); } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/MetadataSQLManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/MetadataSQLManager.java index e740daba..d3c8b852 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/MetadataSQLManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/MetadataSQLManager.java @@ -3,6 +3,8 @@ import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.api.AdvancedTeleportAPI; import io.github.niestrat99.advancedteleport.api.Spawn; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; + import org.bukkit.Bukkit; import org.bukkit.World; import org.jetbrains.annotations.NotNull; @@ -31,33 +33,26 @@ public static MetadataSQLManager get() { @Override public void createTable() { - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), - () -> { - CoreClass.debug( - "Creating table data for the metadata manager if it is not already set up."); - - try (Connection connection = implementConnection()) { - PreparedStatement createTable = - prepareStatement( - connection, - "CREATE TABLE IF NOT EXISTS " - + tablePrefix - + "_metadata " - + "(data_id VARCHAR(256) NOT NULL, " - + "type VARCHAR(256) NOT NULL," - + "`key` VARCHAR(256) NOT NULL, " - + "value TEXT NOT NULL)"); - executeUpdate(createTable); - } catch (SQLException exception) { - CoreClass.getInstance() - .getLogger() - .severe("Failed to create the metadata table."); - exception.printStackTrace(); - } - transferOldData(); - }); + RunnableManager.setupRunnerAsync(() -> { + + CoreClass.debug("Creating table data for the metadata manager if it is not already set up."); + + try (Connection connection = implementConnection()) { + PreparedStatement createTable = prepareStatement( + connection, + "CREATE TABLE IF NOT EXISTS " + tablePrefix + "_metadata " + + "(data_id VARCHAR(256) NOT NULL, " + + "type VARCHAR(256) NOT NULL," + + "key VARCHAR(256) NOT NULL, " + + "value TEXT NOT NULL)" + ); + executeUpdate(createTable); + } catch (SQLException exception) { + CoreClass.getInstance().getLogger().severe("Failed to create the metadata table."); + exception.printStackTrace(); + } + transferOldData(); + }); } @Override @@ -383,14 +378,11 @@ public CompletableFuture loadMirroredSpawns() { Spawn main = getSpawnFromId(connection, dataId); Spawn mirror = getSpawnFromId(connection, mirrorId); - // If the main is not null, then mirror it - if (main != null) { - Bukkit.getScheduler() - .runTask( - CoreClass.getInstance(), - () -> main.setMirroringSpawn(mirror, null)); - } - } + // If the main is not null, then mirror it + if (main != null) { + RunnableManager.setupRunnerAsync(() -> main.setMirroringSpawn(mirror, null)); + } + } } catch (SQLException exception) { throw new RuntimeException(exception); diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/PlayerSQLManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/PlayerSQLManager.java index fc369cfb..0fc1282b 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/PlayerSQLManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/PlayerSQLManager.java @@ -2,6 +2,7 @@ import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.api.ATPlayer; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -36,44 +37,41 @@ public PlayerSQLManager() { @Override public void createTable() { - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), - () -> { - CoreClass.debug( - "Creating table data for the players manager if it is not already set up."); - - try (Connection connection = implementConnection()) { - PreparedStatement createTable = - prepareStatement( - connection, - "CREATE TABLE IF NOT EXISTS " - + tablePrefix - + "_players " - + "(id INTEGER PRIMARY KEY " - + getStupidAutoIncrementThing() - + ", " - + "uuid VARCHAR(256) NOT NULL, " - + "name VARCHAR(256) NOT NULL," - + "timestamp_last_joined BIGINT NOT NULL," - + "main_home VARCHAR(256)," - + "teleportation_on BIT DEFAULT 1 NOT NULL, " - + "x DOUBLE, " - + "y DOUBLE, " - + "z DOUBLE, " - + "yaw FLOAT, " - + "pitch FLOAT, " - + "world VARCHAR(256))"); - - executeUpdate(createTable); - } catch (SQLException exception) { - CoreClass.getInstance() - .getLogger() - .severe("Failed to create the players table."); - exception.printStackTrace(); - } - transferOldData(); - }); + RunnableManager.setupRunnerAsync(() -> { + + CoreClass.debug("Creating table data for the players manager if it is not already set up."); + + try (Connection connection = implementConnection()) { + PreparedStatement createTable = + prepareStatement( + connection, + "CREATE TABLE IF NOT EXISTS " + + tablePrefix + + "_players " + + "(id INTEGER PRIMARY KEY " + + getStupidAutoIncrementThing() + + ", " + + "uuid VARCHAR(256) NOT NULL, " + + "name VARCHAR(256) NOT NULL," + + "timestamp_last_joined BIGINT NOT NULL," + + "main_home VARCHAR(256)," + + "teleportation_on BIT DEFAULT 1 NOT NULL, " + + "x DOUBLE, " + + "y DOUBLE, " + + "z DOUBLE, " + + "yaw FLOAT, " + + "pitch FLOAT, " + + "world VARCHAR(256))"); + + executeUpdate(createTable); + } catch (SQLException exception) { + CoreClass.getInstance() + .getLogger() + .severe("Failed to create the players table."); + exception.printStackTrace(); + } + transferOldData(); + }); } @Override diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/SQLManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/SQLManager.java index fc7dee55..12ec7791 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/SQLManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/SQLManager.java @@ -4,8 +4,8 @@ import io.github.niestrat99.advancedteleport.api.WorldlessLocation; import io.github.niestrat99.advancedteleport.api.data.UnloadedWorldException; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.jetbrains.annotations.NotNull; @@ -88,7 +88,7 @@ private Connection loadSqlite() { } public static void runAsync(Runnable runnable) { - Bukkit.getScheduler().runTaskAsynchronously(CoreClass.getInstance(), runnable); + RunnableManager.setupRunnerAsync(runnable); } public static String getTablePrefix() { diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/SpawnSQLManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/SpawnSQLManager.java index 8f652604..a06c2801 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/SpawnSQLManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/SpawnSQLManager.java @@ -3,9 +3,9 @@ import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.api.data.UnloadedWorldException; import io.github.niestrat99.advancedteleport.api.Spawn; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.managers.NamedLocationManager; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; @@ -37,44 +37,33 @@ public static SpawnSQLManager get() { @Override public void createTable() { - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), - () -> { - CoreClass.debug( - "Creating table data for the spawns manager if it is not already set up."); - - try (Connection connection = implementConnection()) { - PreparedStatement createTable = - prepareStatement( - connection, - "CREATE TABLE IF NOT EXISTS " - + tablePrefix - + "_spawns " - + "(id INTEGER PRIMARY KEY " - + getStupidAutoIncrementThing() - + ", " - + "spawn VARCHAR(256) NOT NULL, " - + "uuid_creator VARCHAR(256), " - + "x DOUBLE NOT NULL, " - + "y DOUBLE NOT NULL, " - + "z DOUBLE NOT NULL, " - + "yaw FLOAT NOT NULL, " - + "pitch FLOAT NOT NULL, " - + "world VARCHAR(256) NOT NULL, " - + "timestamp_created BIGINT NOT NULL, " - + "timestamp_updated BIGINT NOT NULL)"); - executeUpdate(createTable); - } catch (SQLException exception) { - CoreClass.getInstance() - .getLogger() - .severe("Failed to create the spawns table."); - exception.printStackTrace(); - } - transferOldData(); + RunnableManager.setupRunnerAsync(() -> { + + CoreClass.debug("Creating table data for the spawns manager if it is not already set up."); + + try (Connection connection = implementConnection()) { + PreparedStatement createTable = prepareStatement(connection, + "CREATE TABLE IF NOT EXISTS " + tablePrefix + "_spawns " + + "(id INTEGER PRIMARY KEY " + getStupidAutoIncrementThing() + ", " + + "spawn VARCHAR(256) NOT NULL, " + + "uuid_creator VARCHAR(256), " + + "x DOUBLE NOT NULL, " + + "y DOUBLE NOT NULL, " + + "z DOUBLE NOT NULL, " + + "yaw FLOAT NOT NULL, " + + "pitch FLOAT NOT NULL, " + + "world VARCHAR(256) NOT NULL, " + + "timestamp_created BIGINT NOT NULL, " + + "timestamp_updated BIGINT NOT NULL)"); + executeUpdate(createTable); + } catch (SQLException exception) { + CoreClass.getInstance().getLogger().severe("Failed to create the spawns table."); + exception.printStackTrace(); + } + transferOldData(); - NamedLocationManager.get().loadSpawnData(); - }); + NamedLocationManager.get().loadSpawnData(); + }); } @Override diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/WarpSQLManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/WarpSQLManager.java index 0f9f9591..dee0a462 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/WarpSQLManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/sql/WarpSQLManager.java @@ -3,10 +3,10 @@ import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.api.AdvancedTeleportAPI; import io.github.niestrat99.advancedteleport.api.Warp; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.api.WorldlessLocation; import io.github.niestrat99.advancedteleport.managers.NamedLocationManager; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; @@ -36,44 +36,41 @@ public static WarpSQLManager get() { @Override public void createTable() { - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), - () -> { - try (Connection connection = implementConnection()) { - - CoreClass.debug( - "Creating table data for the warps manager if it is not already set up."); - - PreparedStatement createTable = - prepareStatement( - connection, - "CREATE TABLE IF NOT EXISTS " - + tablePrefix - + "_warps " - + "(id INTEGER PRIMARY KEY " - + getStupidAutoIncrementThing() - + ", " - + "warp VARCHAR(256) NOT NULL," - + "uuid_creator VARCHAR(256), " - + "x DOUBLE NOT NULL," - + "y DOUBLE NOT NULL," - + "z DOUBLE NOT NULL," - + "yaw FLOAT NOT NULL," - + "pitch FLOAT NOT NULL," - + "world VARCHAR(256) NOT NULL," - + "price VARCHAR(256)," - + "timestamp_created BIGINT NOT NULL," - + "timestamp_updated BIGINT NOT NULL)"); - executeUpdate(createTable); - } catch (SQLException exception) { - CoreClass.getInstance() - .getLogger() - .severe("Failed to create the warps table."); - exception.printStackTrace(); - } - transferOldData(); - }); + RunnableManager.setupRunnerAsync(() -> { + try (Connection connection = implementConnection()) { + + CoreClass.debug( + "Creating table data for the warps manager if it is not already set up."); + + PreparedStatement createTable = + prepareStatement( + connection, + "CREATE TABLE IF NOT EXISTS " + + tablePrefix + + "_warps " + + "(id INTEGER PRIMARY KEY " + + getStupidAutoIncrementThing() + + ", " + + "warp VARCHAR(256) NOT NULL," + + "uuid_creator VARCHAR(256), " + + "x DOUBLE NOT NULL," + + "y DOUBLE NOT NULL," + + "z DOUBLE NOT NULL," + + "yaw FLOAT NOT NULL," + + "pitch FLOAT NOT NULL," + + "world VARCHAR(256) NOT NULL," + + "price VARCHAR(256)," + + "timestamp_created BIGINT NOT NULL," + + "timestamp_updated BIGINT NOT NULL)"); + executeUpdate(createTable); + } catch (SQLException exception) { + CoreClass.getInstance() + .getLogger() + .severe("Failed to create the warps table."); + exception.printStackTrace(); + } + transferOldData(); + }); } @Override diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/IconMenu.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/IconMenu.java index 0ce0d3d8..f9660a86 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/IconMenu.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/IconMenu.java @@ -9,6 +9,7 @@ import com.mojang.authlib.properties.Property; import io.github.niestrat99.advancedteleport.CoreClass; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -169,7 +170,7 @@ public void onInventoryClick(InventoryClickEvent event) { final Player p = (Player) event.getWhoClicked(); icon.activate(p, e); if (e.willClose()) { - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, p::closeInventory, 1); + RunnableManager.setupRunnerDelayed(() -> p.closeInventory(), 1); } if (e.willDestroy()) { destroy(); diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/NMSUtilities.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/NMSUtilities.java new file mode 100644 index 00000000..d213b167 --- /dev/null +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/NMSUtilities.java @@ -0,0 +1,41 @@ +package io.github.niestrat99.advancedteleport.utilities; + +import io.github.niestrat99.advancedteleport.folia.RunnableManager; +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class NMSUtilities { + + // Temporary hotfix for world mismatching on Folia + public static @Nullable Location getRespawnLocation(@NotNull OfflinePlayer player) { + if (!RunnableManager.isFolia() || !(player instanceof Player onlinePlayer)) { + return player.getBedSpawnLocation(); + } + + ServerPlayer serverPlayer; + try { + Method method = onlinePlayer.getClass().getMethod("getHandleRaw"); + serverPlayer = (ServerPlayer) method.invoke(onlinePlayer); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + + ServerLevel world = serverPlayer.server.getLevel(serverPlayer.getRespawnDimension()); + BlockPos pos = serverPlayer.getRespawnPosition(); + float yaw = serverPlayer.getRespawnAngle(); + + // If world is null, stop there + if (world == null || pos == null) return null; + + return new Location(world.getWorld(), pos.getX(), pos.getY(), pos.getZ(), yaw, 0); + } +} diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/nbt/NBTReader.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/nbt/NBTReader.java index 5cedaa1c..6326bc89 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/nbt/NBTReader.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/nbt/NBTReader.java @@ -1,8 +1,7 @@ package io.github.niestrat99.advancedteleport.utilities.nbt; -import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.config.CustomMessages; - +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import net.kyori.adventure.nbt.*; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; @@ -20,29 +19,20 @@ public class NBTReader { public static void getLocation(String name, NBTCallback callback) { - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), - () -> { - OfflinePlayer player = Bukkit.getOfflinePlayer(name); - try { - Location location = getLocation(player); - if (location == null) { - callback.onFail( - CustomMessages.getComponent( - "Error.noOfflineLocation", - Placeholder.unparsed("player", name))); - return; - } - callback.onSuccess(location); - } catch (IOException e) { - callback.onFail( - CustomMessages.getComponent( - "Error.failedOfflineTeleport", - Placeholder.unparsed("player", name))); - e.printStackTrace(); - } - }); + RunnableManager.setupRunnerAsync(() -> { + OfflinePlayer player = Bukkit.getOfflinePlayer(name); + try { + Location location = getLocation(player); + if (location == null) { + callback.onFail(CustomMessages.getComponent("Error.noOfflineLocation", Placeholder.unparsed("player", name))); + return; + } + callback.onSuccess(location); + } catch (IOException e) { + callback.onFail(CustomMessages.getComponent("Error.failedOfflineTeleport", Placeholder.unparsed("player", name))); + e.printStackTrace(); + } + }); } private static Location getLocation(OfflinePlayer player) throws IOException { @@ -92,22 +82,16 @@ private static File getPlayerFile(File playerDataFolder, UUID uuid) { } public static void setLocation(String name, Location newLoc, NBTCallback callback) { - Bukkit.getScheduler() - .runTaskAsynchronously( - CoreClass.getInstance(), - () -> { - OfflinePlayer player = Bukkit.getOfflinePlayer(name); - try { - setLocation(player, newLoc); - callback.onSuccess(true); - } catch (IOException e) { - e.printStackTrace(); - callback.onFail( - CustomMessages.getComponent( - "Error.failedOfflineTeleportHere", - Placeholder.unparsed("player", name))); - } - }); + RunnableManager.setupRunnerAsync(() -> { + OfflinePlayer player = Bukkit.getOfflinePlayer(name); + try { + setLocation(player, newLoc); + callback.onSuccess(true); + } catch (IOException e) { + e.printStackTrace(); + callback.onFail(CustomMessages.getComponent("Error.failedOfflineTeleportHere", Placeholder.unparsed("player", name))); + } + }); } private static void setLocation(OfflinePlayer player, Location location) throws IOException { diff --git a/README.md b/README.md index 1eab5685..669a9d77 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Advanced Teleport is a rapidly growing plugin that is not only increasing sharpl - A spawn feature. - A per-world respawning mechanism. -## Installation/Cloning +### Installation/Cloning As of currently, Advanced Teleport uses Gradle to manage its dependencies. The Gradle command used to build the plugin is `gradle shadowJar` and is done under the AdvancedTeleport-Bukkit module. diff --git a/gradle.properties b/gradle.properties index c57d6961..180a7022 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ # suppress inspection "UnusedProperty" for whole file group=io.github.niestrat99 -version=6.2.0 +version=6.2.0-alpha+folia.3 # Keep up to date with https://github.com/DaRacci/Minix-Conventions kotlinVersion=1.8.0 build=7 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 903eac43..0ccae2e2 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -26,6 +26,8 @@ slimjar = "1.6.1" shadow = "7.1.2" pluginYML = "0.6.0" +folia = "1.20.4-R0.1-SNAPSHOT" + [libraries] paperlib = { module = "io.papermc:paperlib", version.ref = "paperlib" } configuration = { module = "com.github.thatsmusic99:ConfigurationMaster-API", version.ref = "configuration" } @@ -54,6 +56,9 @@ hook-squaremap = { module = "xyz.jpenilla:squaremap-api", version.ref = "squarem hook-dynmap = { module = "us.dynmap:dynmap-api", version.ref = "dynmap" } hook-slimjar = { module = "dev.racci.slimjar:slimjar", version.ref = "slimjar" } +folia = { module = "dev.folia:folia-api", version.ref = "folia" } + + [plugins] hangar = { id = "io.papermc.hangar-publish-plugin", version.ref = "hangar" } slimjar = { id = "dev.racci.slimjar", version.ref = "slimjar" } diff --git a/settings.gradle.kts b/settings.gradle.kts index 505ef66f..3ac90da3 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -8,6 +8,7 @@ include(":AdvancedTP-Core") pluginManagement { repositories { mavenCentral() + mavenLocal() gradlePluginPortal() maven("https://papermc.io/repo/repository/maven-public/") maven("https://repo.bsdevelopment.org/releases") From 002d9b9d74cf0c9210fdd0c2c61ac71ba4b924c1 Mon Sep 17 00:00:00 2001 From: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> Date: Sun, 20 Oct 2024 23:19:59 +0100 Subject: [PATCH 2/7] chore: oi, build the damn pr Signed-off-by: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> --- .../advancedteleport/utilities/nbt/NBTReader.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/nbt/NBTReader.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/nbt/NBTReader.java index 6326bc89..3df835b5 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/nbt/NBTReader.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/nbt/NBTReader.java @@ -104,19 +104,19 @@ private static void setLocation(OfflinePlayer player, Location location) throws CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder().put(rawTag); ListBinaryTag.Builder posTag = ListBinaryTag.builder(); - posTag.add(DoubleBinaryTag.of(location.getX())); - posTag.add(DoubleBinaryTag.of(location.getY())); - posTag.add(DoubleBinaryTag.of(location.getZ())); + posTag.add(DoubleBinaryTag.doubleBinaryTag(location.getX())); + posTag.add(DoubleBinaryTag.doubleBinaryTag(location.getY())); + posTag.add(DoubleBinaryTag.doubleBinaryTag(location.getZ())); ListBinaryTag.Builder rotTag = ListBinaryTag.builder(); - rotTag.add(FloatBinaryTag.of(location.getYaw())); - rotTag.add(FloatBinaryTag.of(location.getPitch())); + rotTag.add(FloatBinaryTag.floatBinaryTag(location.getYaw())); + rotTag.add(FloatBinaryTag.floatBinaryTag(location.getPitch())); builder.put("Pos", posTag.build()); builder.put("Rotation", rotTag.build()); - builder.put("WorldUUIDMost", LongBinaryTag.of(location.getWorld().getUID().getMostSignificantBits())); - builder.put("WorldUUIDLeast", LongBinaryTag.of(location.getWorld().getUID().getLeastSignificantBits())); + builder.put("WorldUUIDMost", LongBinaryTag.longBinaryTag(location.getWorld().getUID().getMostSignificantBits())); + builder.put("WorldUUIDLeast", LongBinaryTag.longBinaryTag(location.getWorld().getUID().getLeastSignificantBits())); BinaryTagIO.writer() .write(builder.build(), dataFile.toPath(), BinaryTagIO.Compression.GZIP); From 84dd8ee9c69f7b845966903293d4e99491f98d51 Mon Sep 17 00:00:00 2001 From: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> Date: Thu, 13 Feb 2025 20:26:33 +0000 Subject: [PATCH 3/7] fix: compilation issues Signed-off-by: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> --- .../hooks/maps/SquaremapHook.java | 106 ++++++------------ gradle/libs.versions.toml | 1 - 2 files changed, 32 insertions(+), 75 deletions(-) diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/hooks/maps/SquaremapHook.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/hooks/maps/SquaremapHook.java index 469bf1cc..8363e61d 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/hooks/maps/SquaremapHook.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/hooks/maps/SquaremapHook.java @@ -49,61 +49,24 @@ public void enable() { CoreClass.getInstance().getLogger().info("Found squaremap, hooking..."); // Get the API provider - this.provider() - .ifPresent( - squaremap -> { - this.provider = squaremap; - for (final var world : Bukkit.getWorlds()) { - provider.getWorldIfEnabled(BukkitAdapter.worldIdentifier(world)) - .ifPresent( - mapWorld -> { - final var key = - Key.of("advancedteleport_warps"); - mapWorld.layerRegistry() - .register( - key, - createLayerProvider( - MainConfig.get() - .MAP_WARPS)); - CoreClass.getInstance() - .getLogger() - .info( - "Added the warp layer for " - + world.getName() - + "."); - - final var homesKey = - Key.of("advancedteleport_homes"); - mapWorld.layerRegistry() - .register( - homesKey, - createLayerProvider( - MainConfig.get() - .MAP_HOMES)); - CoreClass.getInstance() - .getLogger() - .info( - "Added the homes layer for " - + world.getName() - + "."); - - final var spawnsKey = - Key.of("advancedteleport_spawns"); - mapWorld.layerRegistry() - .register( - spawnsKey, - createLayerProvider( - MainConfig.get() - .MAP_SPAWNS)); - CoreClass.getInstance() - .getLogger() - .info( - "Added the spawns layer for " - + world.getName() - + "."); - }); - } - }); + this.provider().ifPresent(squaremap -> { + this.provider = squaremap; + for (final var world : Bukkit.getWorlds()) { + provider.getWorldIfEnabled(BukkitAdapter.worldIdentifier(world)).ifPresent(mapWorld -> { + final var warpsKey = Key.of("advancedteleport_warps"); + mapWorld.layerRegistry().register(warpsKey, createLayerProvider(MainConfig.get().MAP_WARPS)); + CoreClass.getInstance().getLogger().info("Added the warp layer for " + world.getName() + "."); + + final var homesKey = Key.of("advancedteleport_homes"); + mapWorld.layerRegistry().register(homesKey, createLayerProvider(MainConfig.get().MAP_HOMES)); + CoreClass.getInstance().getLogger().info("Added the homes layer for " + world.getName() + "."); + + final var spawnsKey = Key.of("advancedteleport_spawns"); + mapWorld.layerRegistry().register(spawnsKey, createLayerProvider(MainConfig.get().MAP_SPAWNS)); + CoreClass.getInstance().getLogger().info("Added the spawns layer for " + world.getName() + "."); + }); + } + }); } @Override @@ -264,25 +227,20 @@ private void removeMarker( @NotNull final MapAssetManager.IconType type, @NotNull final World world) { Objects.requireNonNull(world, "The world for " + name + " is not loaded."); - provider.getWorldIfEnabled(BukkitAdapter.worldIdentifier(world)) - .ifPresent( - mapWorld -> { - // Get the key - Key layerKey = - Key.of("advancedteleport_" + type.name().toLowerCase() + "s"); - // Get the layer provider associated - SimpleLayerProvider layer = - (SimpleLayerProvider) mapWorld.layerRegistry().get(layerKey); - // Get the icon key - Key key = - Key.of( - "advancedteleport_" - + type.name().toLowerCase() - + "_" - + name); - // Remove the icon - layer.removeMarker(key); - }); + provider.getWorldIfEnabled(BukkitAdapter.worldIdentifier(world)).ifPresent(mapWorld -> { + + // Get the key + Key layerKey = Key.of("advancedteleport_" + type.name().toLowerCase() + "s"); + + // Get the layer provider associated + SimpleLayerProvider layer = (SimpleLayerProvider) mapWorld.layerRegistry().get(layerKey); + + // Get the icon key + Key key = Key.of("advancedteleport_" + type.name().toLowerCase() + "_" + name); + + // Remove the icon + layer.removeMarker(key); + }); } private void moveMarker( diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0ccae2e2..b1271283 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -58,7 +58,6 @@ hook-slimjar = { module = "dev.racci.slimjar:slimjar", version.ref = "slimjar" } folia = { module = "dev.folia:folia-api", version.ref = "folia" } - [plugins] hangar = { id = "io.papermc.hangar-publish-plugin", version.ref = "hangar" } slimjar = { id = "dev.racci.slimjar", version.ref = "slimjar" } From 619578d7a4035180ed2f1ca35b6eb6dd674a99dc Mon Sep 17 00:00:00 2001 From: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> Date: Thu, 13 Feb 2025 21:05:52 +0000 Subject: [PATCH 4/7] fix: folia telling me off for completely unreasonable expectations (i am upset) Signed-off-by: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> --- .../niestrat99/advancedteleport/folia/RunnableManager.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/RunnableManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/RunnableManager.java index cfa8e6fa..72360eac 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/RunnableManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/folia/RunnableManager.java @@ -33,6 +33,7 @@ public static CancellableRunnable setupRunnerDelayed(Runnable runnable, long del } public static CancellableRunnable setupRunnerDelayed(Consumer runnable, long delay) { + if (delay == 0) return setupRunner(runnable); return run(runnable, (run) -> Bukkit.getScheduler().runTaskLater(CoreClass.getInstance(), run::start, delay), @@ -40,6 +41,7 @@ public static CancellableRunnable setupRunnerDelayed(Consumer runnable, long delay) { + if (delay == 0) return setupRunnerAsync(runnable); return run(runnable, (run) -> Bukkit.getScheduler().runTaskLaterAsynchronously(CoreClass.getInstance(), run::start, delay), From fbb85e452649084004f524cbc5cd7dcd5f220463 Mon Sep 17 00:00:00 2001 From: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> Date: Thu, 13 Feb 2025 21:31:13 +0000 Subject: [PATCH 5/7] fix: run particles on same thread as player Signed-off-by: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> --- .../io/github/niestrat99/advancedteleport/api/ATPlayer.java | 3 ++- .../advancedteleport/managers/ParticleManager.java | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/ATPlayer.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/ATPlayer.java index fdb3035a..7eae40a3 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/ATPlayer.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/ATPlayer.java @@ -188,7 +188,8 @@ public void teleport( Placeholder.unparsed("home", event.getLocName()), Placeholder.unparsed("warp", event.getLocName())); } else { - ParticleManager.onPreTeleport(player, command); + + RunnableManager.setupRunner(this.getPlayer(), () -> ParticleManager.onPreTeleport(player, command), () -> {}); // teleportWithOptions( diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/ParticleManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/ParticleManager.java index c1be482b..3f935ebb 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/ParticleManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/ParticleManager.java @@ -1,7 +1,7 @@ package io.github.niestrat99.advancedteleport.managers; -import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.hooks.ParticlesPlugin; import org.bukkit.*; @@ -53,11 +53,11 @@ private static void onTeleport(@NotNull final Player player, @NotNull final Stri final var particle = particlesOption.valueOf(command).get(); if (particle.equals("spark")) { doSpark(player.getLocation()); - return; } + // TODO - fix teleporting particles on main branch applyParticles(player, command); - Bukkit.getServer().getScheduler().runTask(CoreClass.getInstance(), () -> removeParticles(player, command)); + RunnableManager.setupRunner(player, () -> removeParticles(player, command), () -> {}); } public static @Nullable String getData(@NotNull final Player player) { From aa677279a72a9b2770b1336618a4b0a983882012 Mon Sep 17 00:00:00 2001 From: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> Date: Sat, 22 Mar 2025 22:11:18 +0000 Subject: [PATCH 6/7] fix: only return null if the retries expire Signed-off-by: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> --- .../advancedteleport/managers/RTPManager.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/RTPManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/RTPManager.java index a0f68650..88e7b2f7 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/RTPManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/RTPManager.java @@ -156,7 +156,7 @@ public static Location getLocationUrgently(World world) { if (RunnableManager.isFolia()) { // Let it do what it needs to do and wait on it. - return CompletableFuture.supplyAsync(() -> { + Location loc = CompletableFuture.supplyAsync(() -> { // Get the block Block block = world.getEnvironment().equals(World.Environment.NETHER) ? doBinaryJump(world, coords) : world.getHighestBlockAt(coords[0], coords[1]); @@ -165,11 +165,12 @@ public static Location getLocationUrgently(World world) { return isValidLocation(block) ? block.getLocation().add(0.5, 1, 0.5) : null; }, task -> Bukkit.getRegionScheduler().execute(CoreClass.getInstance(), world, coords[0] >> 4, coords[1] >> 4, task)).get(); - } - - Block block = world.getEnvironment().equals(World.Environment.NETHER) ? doBinaryJump(world, coords) : world.getHighestBlockAt(coords[0], coords[1]); - if (isValidLocation(block)) return block.getLocation().add(0.5, 1, 0.5); + if (loc != null) return loc; + } else { + Block block = world.getEnvironment().equals(World.Environment.NETHER) ? doBinaryJump(world, coords) : world.getHighestBlockAt(coords[0], coords[1]); + if (isValidLocation(block)) return block.getLocation().add(0.5, 1, 0.5); + } } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } From e5f25ea17b570189e0ef0a22de7f09518be9188b Mon Sep 17 00:00:00 2001 From: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> Date: Wed, 14 Jan 2026 22:04:37 +0000 Subject: [PATCH 7/7] fix: compilation errors Signed-off-by: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> --- .../niestrat99/advancedteleport/config/CustomMessages.java | 2 -- .../advancedteleport/managers/MovementManager.java | 2 +- .../advancedteleport/managers/TeleportTrackingManager.java | 5 ++--- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/CustomMessages.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/CustomMessages.java index 6d8b1daa..2037bfef 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/CustomMessages.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/CustomMessages.java @@ -7,8 +7,6 @@ import io.github.niestrat99.advancedteleport.api.ATPlayer; import io.github.niestrat99.advancedteleport.api.NamedLocation; import io.github.niestrat99.advancedteleport.api.data.ATException; -import io.github.niestrat99.advancedteleport.data.PartialComponent; -import io.github.niestrat99.advancedteleport.extensions.ExPermission; import io.github.niestrat99.advancedteleport.folia.CancellableRunnable; import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.utilities.minimessage.PartialComponent; diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/MovementManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/MovementManager.java index e3805b1c..4f881ef6 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/MovementManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/MovementManager.java @@ -55,7 +55,7 @@ public void onDamage(EntityDamageEvent event) { // Cancel the timer final var timer = movement.get(player.getUniqueId()); - timer.cancel(); + timer.runnable.cancel(); CustomMessages.sendMessage(player, "Teleport.eventDamage"); ParticleManager.removeParticles(player, timer.command); movement.remove(player.getUniqueId()); diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/TeleportTrackingManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/TeleportTrackingManager.java index e52ee1af..b6730f0f 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/TeleportTrackingManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/TeleportTrackingManager.java @@ -9,7 +9,6 @@ import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.MainConfig; import io.github.niestrat99.advancedteleport.folia.RunnableManager; -import io.github.niestrat99.advancedteleport.folia.RunnableManager; import io.github.niestrat99.advancedteleport.utilities.ConditionChecker; import io.github.thatsmusic99.configurationmaster.api.ConfigSection; @@ -106,11 +105,11 @@ public void onJoin(PlayerJoinEvent e) { } } - private void spawn(Player player, Spawn spawn) { + private void spawn(Player player, Location location) { RunnableManager.setupRunnerDelayed(t -> ATPlayer.teleportWithOptions( player, - spawn.getLocation(), + location, PlayerTeleportEvent.TeleportCause.PLUGIN) .whenComplete( (result, err) -> {