From 77dc95ae291166054fa4f18cc3d1adc087da04b4 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Fri, 21 Jul 2023 20:01:59 -0500 Subject: [PATCH] Add Folia support minus world generation --- server/build.gradle | 5 +-- server/src/main/java/dev/plex/Plex.java | 15 ++++++--- .../dev/plex/services/AbstractService.java | 6 ---- .../main/java/dev/plex/services/IService.java | 4 ++- .../dev/plex/services/ServiceManager.java | 32 +++++++++---------- .../plex/services/impl/AutoWipeService.java | 28 +++++++++++++--- .../dev/plex/services/impl/BanService.java | 3 +- .../services/impl/CommandBlockerService.java | 3 +- .../plex/services/impl/GameRuleService.java | 3 +- .../dev/plex/services/impl/TimingService.java | 3 +- .../services/impl/UpdateCheckerService.java | 3 +- .../main/java/dev/plex/util/PlexUtils.java | 11 +++++++ 12 files changed, 77 insertions(+), 39 deletions(-) diff --git a/server/build.gradle b/server/build.gradle index 986c687..bbf8808 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -12,7 +12,7 @@ dependencies { library "org.mariadb.jdbc:mariadb-java-client:3.1.4" library "com.zaxxer:HikariCP:5.0.1" library "org.apache.maven.resolver:maven-resolver-transport-http:1.9.13" - compileOnly "io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT" + compileOnly "dev.folia:folia-api:1.20.1-R0.1-SNAPSHOT" compileOnly("com.github.MilkBowl:VaultAPI:1.7.1") { exclude group: "org.bukkit", module: "bukkit" } @@ -38,9 +38,10 @@ paper { loader = "dev.plex.PlexLibraryManager" website = "https://plex.us.org" authors = ["Telesphoreo", "taahanis", "supernt"] - // Load BukkitTelnet and LibsDisguises before Plex so the modules register properly apiVersion = "1.19" + foliaSupported = true generateLibrariesJson = true + // Load BukkitTelnet and LibsDisguises before Plex so the modules register properly serverDependencies { 'BukkitTelnet' { required = false diff --git a/server/src/main/java/dev/plex/Plex.java b/server/src/main/java/dev/plex/Plex.java index 35a6417..7423766 100644 --- a/server/src/main/java/dev/plex/Plex.java +++ b/server/src/main/java/dev/plex/Plex.java @@ -21,9 +21,14 @@ import dev.plex.storage.player.MongoPlayerData; import dev.plex.storage.player.SQLPlayerData; import dev.plex.storage.punishment.SQLNotes; import dev.plex.storage.punishment.SQLPunishment; -import dev.plex.util.*; +import dev.plex.util.BuildInfo; +import dev.plex.util.BungeeUtil; +import dev.plex.util.PlexLog; +import dev.plex.util.PlexUtils; +import dev.plex.util.UpdateChecker; import dev.plex.util.redis.MessageUtil; import dev.plex.world.CustomWorld; +import java.io.File; import lombok.Getter; import lombok.Setter; import net.milkbowl.vault.chat.Chat; @@ -33,8 +38,6 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; -import java.io.File; - @Getter @Setter public class Plex extends JavaPlugin @@ -189,7 +192,11 @@ public class Plex extends JavaPlugin punishmentManager.mergeIndefiniteBans(); PlexLog.log("Punishment System initialized"); - generateWorlds(); + if (!PlexUtils.isFolia()) + { + // World generation is not supported on Folia yet + generateWorlds(); + } serviceManager = new ServiceManager(); PlexLog.log("Service Manager initialized"); diff --git a/server/src/main/java/dev/plex/services/AbstractService.java b/server/src/main/java/dev/plex/services/AbstractService.java index ddd180b..f579545 100644 --- a/server/src/main/java/dev/plex/services/AbstractService.java +++ b/server/src/main/java/dev/plex/services/AbstractService.java @@ -2,7 +2,6 @@ package dev.plex.services; import dev.plex.PlexBase; import lombok.Getter; -import lombok.Setter; @Getter public abstract class AbstractService implements IService, PlexBase @@ -10,9 +9,6 @@ public abstract class AbstractService implements IService, PlexBase private final boolean asynchronous; private final boolean repeating; - @Setter - private int taskId; - public AbstractService(boolean repeating, boolean async) { this.repeating = repeating; @@ -21,11 +17,9 @@ public abstract class AbstractService implements IService, PlexBase public void onStart() { - } public void onEnd() { - } } diff --git a/server/src/main/java/dev/plex/services/IService.java b/server/src/main/java/dev/plex/services/IService.java index b7d8e65..9e72e9a 100644 --- a/server/src/main/java/dev/plex/services/IService.java +++ b/server/src/main/java/dev/plex/services/IService.java @@ -1,8 +1,10 @@ package dev.plex.services; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; + public interface IService { - void run(); + void run(ScheduledTask scheduledTask); int repeatInSeconds(); } diff --git a/server/src/main/java/dev/plex/services/ServiceManager.java b/server/src/main/java/dev/plex/services/ServiceManager.java index 076c50b..61a2d54 100644 --- a/server/src/main/java/dev/plex/services/ServiceManager.java +++ b/server/src/main/java/dev/plex/services/ServiceManager.java @@ -2,11 +2,15 @@ package dev.plex.services; import com.google.common.collect.Lists; import dev.plex.Plex; -import dev.plex.services.impl.*; -import org.bukkit.Bukkit; -import org.bukkit.scheduler.BukkitTask; - +import dev.plex.services.impl.AutoWipeService; +import dev.plex.services.impl.BanService; +import dev.plex.services.impl.CommandBlockerService; +import dev.plex.services.impl.GameRuleService; +import dev.plex.services.impl.TimingService; +import dev.plex.services.impl.UpdateCheckerService; import java.util.List; +import java.util.concurrent.TimeUnit; +import org.bukkit.Bukkit; public class ServiceManager { @@ -24,10 +28,7 @@ public class ServiceManager public void startServices() { - for (AbstractService service : services) - { - startService(service); - } + services.forEach(this::startService); } public void endServices() @@ -45,26 +46,22 @@ public class ServiceManager if (!service.isRepeating()) { int time = service.repeatInSeconds(); - BukkitTask task; if (time == 0) { - task = Bukkit.getScheduler().runTask(Plex.get(), service::run); + Bukkit.getGlobalRegionScheduler().run(Plex.get(), service::run); } else { - task = Bukkit.getScheduler().runTaskLater(Plex.get(), service::run, time); + Bukkit.getAsyncScheduler().runDelayed(Plex.get(), service::run, time, TimeUnit.SECONDS); } - service.setTaskId(task.getTaskId()); } else if (service.isRepeating() && service.isAsynchronous()) { - BukkitTask task = Bukkit.getScheduler().runTaskTimerAsynchronously(Plex.get(), service::run, 0, 20L * service.repeatInSeconds()); - service.setTaskId(task.getTaskId()); + Bukkit.getAsyncScheduler().runAtFixedRate(Plex.get(), service::run, 1, service.repeatInSeconds(), TimeUnit.SECONDS); } else if (service.isRepeating() && !service.isAsynchronous()) { - BukkitTask task = Bukkit.getScheduler().runTaskTimer(Plex.get(), service::run, 0, 20L * service.repeatInSeconds()); - service.setTaskId(task.getTaskId()); + Bukkit.getGlobalRegionScheduler().runAtFixedRate(Plex.get(), service::run, 1, 20L * service.repeatInSeconds()); } if (!services.contains(service)) { @@ -75,7 +72,8 @@ public class ServiceManager public void endService(AbstractService service, boolean remove) { - Bukkit.getScheduler().cancelTask(service.getTaskId()); + Bukkit.getGlobalRegionScheduler().cancelTasks(Plex.get()); + Bukkit.getAsyncScheduler().cancelTasks(Plex.get()); service.onEnd(); if (remove) { diff --git a/server/src/main/java/dev/plex/services/impl/AutoWipeService.java b/server/src/main/java/dev/plex/services/impl/AutoWipeService.java index 92e5659..dfcfa7e 100644 --- a/server/src/main/java/dev/plex/services/impl/AutoWipeService.java +++ b/server/src/main/java/dev/plex/services/impl/AutoWipeService.java @@ -2,12 +2,13 @@ package dev.plex.services.impl; import dev.plex.Plex; import dev.plex.services.AbstractService; +import dev.plex.util.PlexLog; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; +import java.util.List; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.entity.Entity; -import java.util.List; - public class AutoWipeService extends AbstractService { public AutoWipeService() @@ -16,7 +17,7 @@ public class AutoWipeService extends AbstractService } @Override - public void run() + public void run(ScheduledTask task) { if (Plex.get().config.getBoolean("autowipe.enabled")) { @@ -28,13 +29,32 @@ public class AutoWipeService extends AbstractService { if (entities.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(entity.getType().name()))) { - entity.remove(); + Bukkit.getRegionScheduler().run(Plex.get(), entity.getLocation(), this::entityRun); + PlexLog.debug("Started entity scheduler"); } } } } } + private void entityRun(ScheduledTask task) + { + List entities = plugin.config.getStringList("autowipe.entities"); + + for (World world : Bukkit.getWorlds()) + { + for (Entity entity : world.getEntities()) + { + if (entities.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(entity.getType().name()))) + { + PlexLog.debug("Removed entity " + entity.getName()); + entity.remove(); + task.cancel(); + } + } + } + } + @Override public int repeatInSeconds() { diff --git a/server/src/main/java/dev/plex/services/impl/BanService.java b/server/src/main/java/dev/plex/services/impl/BanService.java index 2903837..d76f8b3 100644 --- a/server/src/main/java/dev/plex/services/impl/BanService.java +++ b/server/src/main/java/dev/plex/services/impl/BanService.java @@ -3,6 +3,7 @@ package dev.plex.services.impl; import dev.plex.Plex; import dev.plex.services.AbstractService; import dev.plex.util.TimeUtils; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; @@ -17,7 +18,7 @@ public class BanService extends AbstractService } @Override - public void run() + public void run(ScheduledTask task) { Plex.get().getPunishmentManager().getActiveBans().whenComplete((punishments, throwable) -> { diff --git a/server/src/main/java/dev/plex/services/impl/CommandBlockerService.java b/server/src/main/java/dev/plex/services/impl/CommandBlockerService.java index 58476d4..2d7d589 100644 --- a/server/src/main/java/dev/plex/services/impl/CommandBlockerService.java +++ b/server/src/main/java/dev/plex/services/impl/CommandBlockerService.java @@ -5,6 +5,7 @@ import dev.plex.command.blocking.BlockedCommand; import dev.plex.services.AbstractService; import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import lombok.Getter; import org.bukkit.command.Command; @@ -22,7 +23,7 @@ public class CommandBlockerService extends AbstractService } @Override - public void run() + public void run(ScheduledTask task) { BLOCKED_COMMANDS.clear(); plugin.commands.getStringList("commands").forEach(s -> diff --git a/server/src/main/java/dev/plex/services/impl/GameRuleService.java b/server/src/main/java/dev/plex/services/impl/GameRuleService.java index 23425b3..c7fe33e 100644 --- a/server/src/main/java/dev/plex/services/impl/GameRuleService.java +++ b/server/src/main/java/dev/plex/services/impl/GameRuleService.java @@ -3,6 +3,7 @@ package dev.plex.services.impl; import dev.plex.services.AbstractService; import dev.plex.util.GameRuleUtil; import dev.plex.util.PlexLog; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import org.bukkit.Bukkit; import org.bukkit.World; @@ -16,7 +17,7 @@ public class GameRuleService extends AbstractService } @Override - public void run() + public void run(ScheduledTask task) { for (World world : Bukkit.getWorlds()) { diff --git a/server/src/main/java/dev/plex/services/impl/TimingService.java b/server/src/main/java/dev/plex/services/impl/TimingService.java index 58c6e8f..226e10d 100644 --- a/server/src/main/java/dev/plex/services/impl/TimingService.java +++ b/server/src/main/java/dev/plex/services/impl/TimingService.java @@ -7,6 +7,7 @@ import dev.plex.punishment.PunishmentType; import dev.plex.services.AbstractService; import dev.plex.util.PlexLog; import dev.plex.util.TimeUtils; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -26,7 +27,7 @@ public class TimingService extends AbstractService } @Override - public void run() + public void run(ScheduledTask task) { spamCooldown.clear(); nukerCooldown.clear(); diff --git a/server/src/main/java/dev/plex/services/impl/UpdateCheckerService.java b/server/src/main/java/dev/plex/services/impl/UpdateCheckerService.java index 418de7e..2fd9378 100644 --- a/server/src/main/java/dev/plex/services/impl/UpdateCheckerService.java +++ b/server/src/main/java/dev/plex/services/impl/UpdateCheckerService.java @@ -1,6 +1,7 @@ package dev.plex.services.impl; import dev.plex.services.AbstractService; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import org.bukkit.Bukkit; public class UpdateCheckerService extends AbstractService @@ -13,7 +14,7 @@ public class UpdateCheckerService extends AbstractService } @Override - public void run() + public void run(ScheduledTask task) { if (!newVersion) { diff --git a/server/src/main/java/dev/plex/util/PlexUtils.java b/server/src/main/java/dev/plex/util/PlexUtils.java index 8c09f6a..68e2af8 100644 --- a/server/src/main/java/dev/plex/util/PlexUtils.java +++ b/server/src/main/java/dev/plex/util/PlexUtils.java @@ -105,6 +105,17 @@ public class PlexUtils implements PlexBase } } + public static boolean isFolia() { + try { + Class.forName("io.papermc.paper.threadedregions.ThreadedRegionizer"); + } + catch (Exception e) { + return false; + } + + return true; + } + public static boolean isPluginCMD(String cmd, String pluginName) { Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginName);