EternalCore icon indicating copy to clipboard operation
EternalCore copied to clipboard

Folia support.

Open vLuckyyy opened this issue 1 year ago • 6 comments

vLuckyyy avatar Dec 24 '23 22:12 vLuckyyy

I am want to check and analyze what changes will need to be made before implementing the folia.

vLuckyyy avatar Feb 17 '24 20:02 vLuckyyy

If you test the folia, remember to activate minimum two folia regions.

Rollczi avatar Feb 17 '24 21:02 Rollczi

The first question is how to implement the Scheduler well so as not to lose compatibility of the current code.

vLuckyyy avatar Feb 28 '24 16:02 vLuckyyy

I will probably address this as soon as #652 is completed.

vLuckyyy avatar Mar 20 '24 15:03 vLuckyyy

@coderabbitai full review

vLuckyyy avatar Oct 09 '24 17:10 vLuckyyy

[!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:

  1. Abstract Scheduler Interface: Create an abstraction layer for your Scheduler. By defining a Scheduler interface, 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
    }
    
  2. 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
    }
    
  3. 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
    }
    
  4. 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;
            }
        }
    }
    
  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

coderabbitai[bot] avatar Oct 09 '24 17:10 coderabbitai[bot]