Folia support.
I am want to check and analyze what changes will need to be made before implementing the folia.
If you test the folia, remember to activate minimum two folia regions.
The first question is how to implement the Scheduler well so as not to lose compatibility of the current code.
I will probably address this as soon as #652 is completed.
@coderabbitai full review
[!TIP] For best results, initiate chat on the files or code changes.
@vLuckyyy, here are some considerations for implementing Folia support while maintaining compatibility with your current code, especially regarding the Scheduler:
-
Abstract Scheduler Interface: Create an abstraction layer for your Scheduler. By defining a
Schedulerinterface, you can have different implementations for Bukkit and Folia without changing the core logic of your application.// Scheduler interface public interface Scheduler { void runTask(Runnable task); void runTaskAsynchronously(Runnable task); // Add other scheduling methods as needed } -
Bukkit Implementation:
// Bukkit Scheduler implementation public class BukkitScheduler implements Scheduler { private final Plugin plugin; public BukkitScheduler(Plugin plugin) { this.plugin = plugin; } @Override public void runTask(Runnable task) { Bukkit.getScheduler().runTask(plugin, task); } @Override public void runTaskAsynchronously(Runnable task) { Bukkit.getScheduler().runTaskAsynchronously(plugin, task); } // Implement other methods } -
Folia Implementation:
// Folia Scheduler implementation public class FoliaScheduler implements Scheduler { private final Plugin plugin; public FoliaScheduler(Plugin plugin) { this.plugin = plugin; } @Override public void runTask(Runnable task) { // Use Folia-specific scheduling plugin.getServer().getGlobalRegionScheduler().run(plugin, task); } @Override public void runTaskAsynchronously(Runnable task) { // Folia might handle async tasks differently CompletableFuture.runAsync(task); } // Implement other methods } -
Runtime Selection: Determine which Scheduler implementation to use at runtime based on the server environment.
// Scheduler factory public class SchedulerFactory { public static Scheduler getScheduler(Plugin plugin) { if (isFolia()) { return new FoliaScheduler(plugin); } else { return new BukkitScheduler(plugin); } } private static boolean isFolia() { try { Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); return true; } catch (ClassNotFoundException e) { return false; } } } -
Concurrency and Thread Safety: Folia introduces a different concurrency model. Review your code for thread safety, especially when accessing shared data. Ensure that any shared resources are appropriately synchronized or designed using thread-safe patterns.
-
API Differences: Be mindful of differences between the Bukkit and Folia APIs. Some methods may behave differently or may not be available in Folia. Testing and conditional code may be necessary.
-
Testing with Multiple Folia Regions: As @Rollczi mentioned, ensure that you activate at least two Folia regions during testing. This will help identify any region-specific issues that might not appear with a single region.
-
Maintain Compatibility: Keep the existing functionality intact by defaulting to the Bukkit Scheduler when Folia is not detected. This ensures that your plugin remains compatible with servers not running Folia.
-
Documentation and User Guidance: Update your documentation to inform users about Folia support and any configuration they might need to enable or disable it.
Feel free to let me know if you'd like to delve deeper into any of these areas or if you have specific code you'd like me to review in detail.