From ad5749f8e7505f34a224378de9cb2d4e2f9e1882 Mon Sep 17 00:00:00 2001 From: Euphyllia Date: Sun, 12 May 2024 19:28:29 +0200 Subject: [PATCH] Add Folia Support (#40) --- build.gradle | 2 +- .../net/pl3x/map/bukkit/Pl3xMapBukkit.java | 27 ++++++++++++------- bukkit/src/main/resources/plugin.yml | 1 + 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index 035dde528..ba9e25645 100644 --- a/build.gradle +++ b/build.gradle @@ -156,7 +156,7 @@ modrinth { // rootProject.layout.buildDirectory.file("libs/${rootProject.name}-${project.version}-sources.jar").get() //]) gameVersions.addAll(["${minecraftVersion}"]) - loaders.addAll(['bukkit', 'fabric', /*'forge',*/ 'paper', 'purpur', 'quilt', 'spigot']) + loaders.addAll(['bukkit', 'fabric', /*'forge',*/ 'paper', 'purpur', 'quilt', 'spigot', 'folia']) changelog = System.getenv('COMMIT_MESSAGE') dependencies { required.project 'fabric-api' diff --git a/bukkit/src/main/java/net/pl3x/map/bukkit/Pl3xMapBukkit.java b/bukkit/src/main/java/net/pl3x/map/bukkit/Pl3xMapBukkit.java index 1837efa5d..5862ecad1 100644 --- a/bukkit/src/main/java/net/pl3x/map/bukkit/Pl3xMapBukkit.java +++ b/bukkit/src/main/java/net/pl3x/map/bukkit/Pl3xMapBukkit.java @@ -24,6 +24,7 @@ package net.pl3x.map.bukkit; import java.util.UUID; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import net.pl3x.map.bukkit.command.BukkitCommandManager; import net.pl3x.map.core.Pl3xMap; import net.pl3x.map.core.event.server.ServerLoadedEvent; @@ -31,6 +32,7 @@ import net.pl3x.map.core.player.Player; import net.pl3x.map.core.player.PlayerListener; import net.pl3x.map.core.player.PlayerRegistry; +import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.event.EventHandler; @@ -47,6 +49,8 @@ public class Pl3xMapBukkit extends JavaPlugin implements Listener { private final Pl3xMapImpl pl3xmap; private final PlayerListener playerListener = new PlayerListener(); + private boolean isFolia = false; + private ScheduledTask tickTimerTask = null; private Network network; @@ -58,13 +62,9 @@ public Pl3xMapBukkit() { @Override public void onEnable() { try { - io.papermc.paper.chunk.system.scheduling.ChunkFullTask.class.getDeclaredField("chunkLoads"); - io.papermc.paper.chunk.system.scheduling.ChunkFullTask.class.getDeclaredField("chunkGenerates"); - getLogger().severe("Pl3xMap does not support Folia"); - getLogger().severe("Pl3xMap will now disable itself"); - getServer().getPluginManager().disablePlugin(this); - return; - } catch (Throwable ignore) { + Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); + isFolia = true; + } catch (ClassNotFoundException ignored) { } this.pl3xmap.enable(); @@ -80,8 +80,13 @@ public void onEnable() { throw new RuntimeException(e); } - getServer().getScheduler().runTaskTimer(this, () -> - this.pl3xmap.getScheduler().tick(), 20, 1); + if (isFolia) { + tickTimerTask = Bukkit.getGlobalRegionScheduler().runAtFixedRate(this, timerTask -> + this.pl3xmap.getScheduler().tick(), 20, 1); + } else { + getServer().getScheduler().runTaskTimer(this, () -> + this.pl3xmap.getScheduler().tick(), 20, 1); + } } @Override @@ -91,6 +96,10 @@ public void onDisable() { this.network = null; } + if (tickTimerTask != null) { + tickTimerTask.cancel(); + } + this.pl3xmap.disable(); } diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index fe9197525..462332b7e 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -6,6 +6,7 @@ load: "POSTWORLD" authors: ${authors} description: "${description}" website: "${website}" +folia-supported: true permissions: pl3xmap.command.map: