Skip to content

Commit

Permalink
feat: added folia support
Browse files Browse the repository at this point in the history
  • Loading branch information
CorneliusMa committed Aug 25, 2024
1 parent eff1d88 commit 48ac7ec
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
7 changes: 7 additions & 0 deletions PlatformBukkit/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
group = "de.corneliusmay.silkspawners"

dependencies {
implementation(project(":API"))

compileOnly("org.bukkit:bukkit:1.13-R0.1-SNAPSHOT")
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 7 additions & 0 deletions PlatformFolia/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
group = "de.corneliusmay.silkspawners"

dependencies {
implementation(project(":API"))

compileOnly("dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT")
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions Plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*")
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void setSpawnerBlockType(Block block, List<Block> 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;
Expand Down
1 change: 1 addition & 0 deletions Plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
)

0 comments on commit 48ac7ec

Please sign in to comment.