Skip to content

Commit

Permalink
Use SingleThreadScheduler in Folia (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
turikhay authored Jan 13, 2024
1 parent 63d0adb commit f9f6404
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.turikhay.mc.mapmodcompanion.spigot;

import org.bukkit.plugin.Plugin;

import java.util.logging.Level;

public class BukkitScheduler implements PluginScheduler {
private final Plugin plugin;

public BukkitScheduler(Plugin plugin) {
this.plugin = plugin;
}

@Override
public void cleanUp() {
}

@Override
public void schedule(Runnable r) {
if (plugin.getServer().isPrimaryThread()) {
executeTask(r);
} else {
plugin.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, () -> executeTask(r));
}
}

private void executeTask(Runnable r) {
try {
r.run();
} catch (Throwable t) {
plugin.getLogger().log(Level.SEVERE, "Failed to execute the task", t);
}
}

@Override
public String toString() {
return "BukkitScheduler{}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.turikhay.mc.mapmodcompanion.spigot;

public class FoliaSupport {
private static Boolean IS_FOLIA_SERVER;

public static boolean isFoliaServer() {
if (IS_FOLIA_SERVER == null) {
boolean isIt = true;
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
} catch (Throwable t) {
isIt = false;
}
IS_FOLIA_SERVER = isIt;
}
return IS_FOLIA_SERVER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class MapModCompanion extends JavaPlugin {
);

private VerboseLogger logger;
private PluginScheduler scheduler;
private ScheduledExecutorService fileChangeWatchdogScheduler;
private IdRegistry registry;
private @Nullable ProtocolLib protocolLib;
Expand All @@ -63,16 +64,18 @@ public void onLoad() {

@Override
public void onEnable() {
scheduler = initScheduler();
fileChangeWatchdogScheduler = FileChangeWatchdog.createScheduler(ILogger.ofJava(logger));
new Metrics(this, BSTATS_ID);
saveDefaultConfig();
load();
scheduler.schedule(this::load);
}

@Override
public void onDisable() {
unload();
scheduler.schedule(this::unload);
fileChangeWatchdogScheduler.shutdown();
scheduler.cleanUp();
}

private void load() {
Expand All @@ -90,7 +93,7 @@ private void load() {
logger,
fileChangeWatchdogScheduler,
getDataFolder().toPath().resolve("config.yml"),
() -> getServer().getScheduler().scheduleSyncDelayedTask(this, this::reload)
() -> scheduler.schedule(this::reload)
);
fileChangeWatchdog.start();
}
Expand All @@ -108,6 +111,18 @@ private void reload() {
load();
}

private PluginScheduler initScheduler() {
PluginScheduler selected;
if (FoliaSupport.isFoliaServer()) {
logger.info("Folia server support enabled");
selected = new SingleThreadScheduler(ILogger.ofJava(logger));
} else {
selected = new BukkitScheduler(this);
}
logger.fine("Scheduler: " + selected);
return selected;
}

private IdRegistry initRegistry() {
World world = null;
if (getConfig().getBoolean("preferDefaultWorld", true)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.turikhay.mc.mapmodcompanion.spigot;

import com.turikhay.mc.mapmodcompanion.Disposable;

public interface PluginScheduler extends Disposable {
void schedule(Runnable r);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.turikhay.mc.mapmodcompanion.spigot;

import com.turikhay.mc.mapmodcompanion.DaemonThreadFactory;
import com.turikhay.mc.mapmodcompanion.ILogger;

import java.util.concurrent.ScheduledThreadPoolExecutor;

public class SingleThreadScheduler implements PluginScheduler {

private final ScheduledThreadPoolExecutor service;

public SingleThreadScheduler(ILogger logger) {
this.service = new ScheduledThreadPoolExecutor(
1,
new DaemonThreadFactory(logger, SingleThreadScheduler.class)
);
service.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
}

@Override
public void cleanUp() {
service.shutdown();
}

@Override
public void schedule(Runnable r) {
service.submit(r);
}

@Override
public String toString() {
return "SingleThreadScheduler{" +
"service=" + service +
'}';
}
}

0 comments on commit f9f6404

Please sign in to comment.