From 48ac7ec76b93fd7f75eaccf63c61781cf6b0e635 Mon Sep 17 00:00:00 2001 From: CorneliusMa <30781324+CorneliusMa@users.noreply.github.com> Date: Sun, 25 Aug 2024 02:21:21 +0200 Subject: [PATCH] feat: added folia support --- .../silkspawners/api/ServerPlatform.java | 14 ++++++++ PlatformBukkit/build.gradle.kts | 7 ++++ .../bukkit/PlatformImplementation.java | 17 ++++++++++ PlatformFolia/build.gradle.kts | 7 ++++ .../folia/PlatformImplementation.java | 16 +++++++++ Plugin/build.gradle.kts | 2 ++ .../silkspawners/plugin/SilkSpawners.java | 14 +++++--- .../listeners/PlayerInteractListener.java | 9 ++--- .../plugin/platform/PlatformLoader.java | 34 +++++++++++++++++++ .../silkspawners/plugin/platform/Server.java | 12 +++++++ .../silkspawners/plugin/spawner/Spawner.java | 2 +- Plugin/src/main/resources/plugin.yml | 1 + settings.gradle.kts | 4 ++- 13 files changed, 129 insertions(+), 10 deletions(-) create mode 100644 API/src/main/java/de/corneliusmay/silkspawners/api/ServerPlatform.java create mode 100644 PlatformBukkit/build.gradle.kts create mode 100644 PlatformBukkit/src/main/java/de/corneliusmay/silkspawners/platform/bukkit/PlatformImplementation.java create mode 100644 PlatformFolia/build.gradle.kts create mode 100644 PlatformFolia/src/main/java/de/corneliusmay/silkspawners/platform/folia/PlatformImplementation.java create mode 100644 Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/platform/PlatformLoader.java create mode 100644 Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/platform/Server.java diff --git a/API/src/main/java/de/corneliusmay/silkspawners/api/ServerPlatform.java b/API/src/main/java/de/corneliusmay/silkspawners/api/ServerPlatform.java new file mode 100644 index 00000000..ed193c19 --- /dev/null +++ b/API/src/main/java/de/corneliusmay/silkspawners/api/ServerPlatform.java @@ -0,0 +1,14 @@ +package de.corneliusmay.silkspawners.api; + +import org.bukkit.Location; +import org.bukkit.plugin.java.JavaPlugin; + +public abstract class ServerPlatform { + protected final JavaPlugin plugin; + + public ServerPlatform(JavaPlugin plugin) { + this.plugin = plugin; + } + + public abstract void runTaskLater(Location location, Runnable runnable, long delay); +} diff --git a/PlatformBukkit/build.gradle.kts b/PlatformBukkit/build.gradle.kts new file mode 100644 index 00000000..7bbced3f --- /dev/null +++ b/PlatformBukkit/build.gradle.kts @@ -0,0 +1,7 @@ +group = "de.corneliusmay.silkspawners" + +dependencies { + implementation(project(":API")) + + compileOnly("org.bukkit:bukkit:1.13-R0.1-SNAPSHOT") +} \ No newline at end of file diff --git a/PlatformBukkit/src/main/java/de/corneliusmay/silkspawners/platform/bukkit/PlatformImplementation.java b/PlatformBukkit/src/main/java/de/corneliusmay/silkspawners/platform/bukkit/PlatformImplementation.java new file mode 100644 index 00000000..121d29b8 --- /dev/null +++ b/PlatformBukkit/src/main/java/de/corneliusmay/silkspawners/platform/bukkit/PlatformImplementation.java @@ -0,0 +1,17 @@ +package de.corneliusmay.silkspawners.platform.bukkit; + +import de.corneliusmay.silkspawners.api.ServerPlatform; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.plugin.java.JavaPlugin; + +public class PlatformImplementation extends ServerPlatform { + public PlatformImplementation(JavaPlugin plugin) { + super(plugin); + } + + @Override + public void runTaskLater(Location location, Runnable runnable, long delay) { + Bukkit.getScheduler().runTaskLater(this.plugin, runnable, delay); + } +} diff --git a/PlatformFolia/build.gradle.kts b/PlatformFolia/build.gradle.kts new file mode 100644 index 00000000..bb35657c --- /dev/null +++ b/PlatformFolia/build.gradle.kts @@ -0,0 +1,7 @@ +group = "de.corneliusmay.silkspawners" + +dependencies { + implementation(project(":API")) + + compileOnly("dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT") +} \ No newline at end of file diff --git a/PlatformFolia/src/main/java/de/corneliusmay/silkspawners/platform/folia/PlatformImplementation.java b/PlatformFolia/src/main/java/de/corneliusmay/silkspawners/platform/folia/PlatformImplementation.java new file mode 100644 index 00000000..bd44edb8 --- /dev/null +++ b/PlatformFolia/src/main/java/de/corneliusmay/silkspawners/platform/folia/PlatformImplementation.java @@ -0,0 +1,16 @@ +package de.corneliusmay.silkspawners.platform.folia; + +import de.corneliusmay.silkspawners.api.ServerPlatform; +import org.bukkit.Location; +import org.bukkit.plugin.java.JavaPlugin; + +public class PlatformImplementation extends ServerPlatform { + public PlatformImplementation(JavaPlugin plugin) { + super(plugin); + } + + @Override + public void runTaskLater(Location location, Runnable runnable, long delay) { + this.plugin.getServer().getRegionScheduler().runDelayed(this.plugin, location, task -> runnable.run(), delay); + } +} diff --git a/Plugin/build.gradle.kts b/Plugin/build.gradle.kts index c8c37e2e..a6bd9cc5 100644 --- a/Plugin/build.gradle.kts +++ b/Plugin/build.gradle.kts @@ -13,6 +13,8 @@ dependencies { implementation("org.bstats:bstats-bukkit:3.0.0") implementation(project(":API")) + implementation(project(":PlatformBukkit")) + implementation(project(":PlatformFolia")) implementation(project(":v1_8_4")) implementation(project(":v1_9_4")) implementation(project(":v1_12_0")) diff --git a/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/SilkSpawners.java b/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/SilkSpawners.java index bce78c24..a62302ca 100644 --- a/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/SilkSpawners.java +++ b/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/SilkSpawners.java @@ -1,6 +1,7 @@ package de.corneliusmay.silkspawners.plugin; import de.corneliusmay.silkspawners.api.Bukkit; +import de.corneliusmay.silkspawners.api.ServerPlatform; import de.corneliusmay.silkspawners.plugin.commands.*; import de.corneliusmay.silkspawners.plugin.commands.handler.SilkSpawnersCommandHandler; import de.corneliusmay.silkspawners.plugin.config.PluginConfig; @@ -12,6 +13,7 @@ import de.corneliusmay.silkspawners.plugin.listeners.SpawnerBreakListener; import de.corneliusmay.silkspawners.plugin.listeners.handler.SilkSpawnersEventHandler; import de.corneliusmay.silkspawners.plugin.locale.LocaleHandler; +import de.corneliusmay.silkspawners.plugin.platform.PlatformLoader; import de.corneliusmay.silkspawners.plugin.utils.Logger; import de.corneliusmay.silkspawners.plugin.version.VersionChecker; import de.corneliusmay.silkspawners.plugin.version.CrossVersionHandler; @@ -25,18 +27,17 @@ import java.util.List; import java.util.Locale; +@Getter public class SilkSpawners extends JavaPlugin { - @Getter private Logger log; - @Getter + private ServerPlatform platform; + private Bukkit bukkitHandler; - @Getter private LocaleHandler locale; - @Getter private VersionChecker versionChecker; @Override @@ -51,6 +52,11 @@ public void onEnable() { log.info("Starting SilkSpawners v" + versionChecker.getInstalledVersion()); + log.info("Loading server platform"); + PlatformLoader platformLoader = new PlatformLoader(this); + platformLoader.load(); + platform = platformLoader.getServerPlatform(); + log.info("Loading Cross-Version support"); CrossVersionHandler versionHandler = new CrossVersionHandler(this); if(!versionHandler.load()) return; diff --git a/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/listeners/PlayerInteractListener.java b/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/listeners/PlayerInteractListener.java index 04ad03eb..38981424 100644 --- a/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/listeners/PlayerInteractListener.java +++ b/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/listeners/PlayerInteractListener.java @@ -4,7 +4,7 @@ import de.corneliusmay.silkspawners.plugin.config.PluginConfig; import de.corneliusmay.silkspawners.plugin.listeners.handler.SilkSpawnersListener; import de.corneliusmay.silkspawners.plugin.spawner.Spawner; -import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -26,17 +26,18 @@ protected void onCall(PlayerInteractEvent e) { if(e.getAction() != Action.RIGHT_CLICK_BLOCK) return; Block block = e.getClickedBlock(); + Location blockLocation = block.getLocation(); Spawner spawner = new Spawner(plugin, block); if(!spawner.isValid()) return; - if(editedSpawners.stream().anyMatch(b -> b.getLocation().equals(block.getLocation()))) { + if(editedSpawners.stream().anyMatch(b -> b.getLocation().equals(blockLocation))) { e.setCancelled(true); return; } editedSpawners.add(block); - Bukkit.getScheduler().runTaskLater(plugin, () -> { - Spawner newSpawner = new Spawner(plugin, block.getWorld().getBlockAt(block.getLocation())); + this.plugin.getPlatform().runTaskLater(blockLocation, () -> { + Spawner newSpawner = new Spawner(plugin, block.getWorld().getBlockAt(blockLocation)); if(!e.getPlayer().hasPermission("silkspawners.change." + newSpawner.serializedEntityType()) && !e.getPlayer().hasPermission("silkspawners.change.*") diff --git a/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/platform/PlatformLoader.java b/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/platform/PlatformLoader.java new file mode 100644 index 00000000..08e6276a --- /dev/null +++ b/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/platform/PlatformLoader.java @@ -0,0 +1,34 @@ +package de.corneliusmay.silkspawners.plugin.platform; + +import de.corneliusmay.silkspawners.api.ServerPlatform; +import de.corneliusmay.silkspawners.plugin.SilkSpawners; +import lombok.Getter; +import org.bukkit.plugin.java.JavaPlugin; + +public class PlatformLoader { + + private final SilkSpawners plugin; + + @Getter + private ServerPlatform serverPlatform; + + public PlatformLoader(SilkSpawners plugin) { + this.plugin = plugin; + } + + private Class loadClass(String platformName) throws ClassNotFoundException { + return Class.forName("de.corneliusmay.silkspawners.platform." + platformName + ".PlatformImplementation"); + } + + public void load() { + String platform = Server.isFolia() ? "folia" : "bukkit"; + try { + Class clazz = loadClass(platform); + this.serverPlatform = (ServerPlatform) clazz.getConstructor(JavaPlugin.class).newInstance(this.plugin); + } catch (Exception e) { + throw new RuntimeException(e); + } + + plugin.getLog().info("Initialized plugin for " + platform + " server"); + } +} diff --git a/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/platform/Server.java b/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/platform/Server.java new file mode 100644 index 00000000..6d3e593c --- /dev/null +++ b/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/platform/Server.java @@ -0,0 +1,12 @@ +package de.corneliusmay.silkspawners.plugin.platform; + +class Server { + static boolean isFolia() { + try { + Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } +} diff --git a/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/spawner/Spawner.java b/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/spawner/Spawner.java index 8695140f..d174c558 100644 --- a/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/spawner/Spawner.java +++ b/Plugin/src/main/java/de/corneliusmay/silkspawners/plugin/spawner/Spawner.java @@ -61,7 +61,7 @@ public void setSpawnerBlockType(Block block, List editedList) { editedList.remove(block); return; } - Bukkit.getScheduler().runTaskLater(this.plugin, () -> { + this.plugin.getPlatform().runTaskLater(block.getLocation(), () -> { BlockState blockState = block.getState(); if(!(blockState instanceof CreatureSpawner)) return; CreatureSpawner creatureSpawner = (CreatureSpawner) blockState; diff --git a/Plugin/src/main/resources/plugin.yml b/Plugin/src/main/resources/plugin.yml index 515d78b4..ab3160d5 100644 --- a/Plugin/src/main/resources/plugin.yml +++ b/Plugin/src/main/resources/plugin.yml @@ -2,6 +2,7 @@ name: SilkSpawners_v2 version: ${project.version} main: de.corneliusmay.silkspawners.plugin.SilkSpawners api-version: 1.13 +folia-supported: true commands: silkspawners: diff --git a/settings.gradle.kts b/settings.gradle.kts index 097caffc..53fd718e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -4,9 +4,11 @@ include( "API", "Plugin", "Publication", + "PlatformBukkit", + "PlatformFolia", "v1_8_4", "v1_9_4", "v1_12_0", "v1_13_1", "v1_20_5" -) \ No newline at end of file +)