diff --git a/src/main/java/io/github/simplexdevelopment/scheduler/ReactorBukkitScheduler.java b/src/main/java/io/github/simplexdevelopment/scheduler/ReactorBukkitScheduler.java index eda8f56..b0904e8 100644 --- a/src/main/java/io/github/simplexdevelopment/scheduler/ReactorBukkitScheduler.java +++ b/src/main/java/io/github/simplexdevelopment/scheduler/ReactorBukkitScheduler.java @@ -9,8 +9,15 @@ import reactor.core.scheduler.Scheduler; import java.util.concurrent.TimeUnit; -public record ReactorBukkitScheduler(JavaPlugin plugin, BukkitScheduler scheduler) +public final class ReactorBukkitScheduler implements Scheduler, Scheduler.Worker { + private final JavaPlugin plugin; + private final BukkitScheduler scheduler; + + public ReactorBukkitScheduler(JavaPlugin plugin) { + this.plugin = plugin; + this.scheduler = plugin.getServer().getScheduler(); + } /** * Delegates to the {@link BukkitScheduler}. @@ -58,9 +65,9 @@ public record ReactorBukkitScheduler(JavaPlugin plugin, BukkitScheduler schedule } /** - * A new {@link Scheduler.Worker}. + * A new {@link Worker}. * - * @return This class instance, as it implements {@link Scheduler.Worker}. + * @return This class instance, as it implements {@link Worker}. */ @Override public @NotNull Worker createWorker() { @@ -72,6 +79,5 @@ public record ReactorBukkitScheduler(JavaPlugin plugin, BukkitScheduler schedule */ @Override public void dispose() { - } } diff --git a/src/main/java/io/github/simplexdevelopment/scheduler/SchedulingSystem.java b/src/main/java/io/github/simplexdevelopment/scheduler/SchedulingSystem.java index debe445..61416a7 100644 --- a/src/main/java/io/github/simplexdevelopment/scheduler/SchedulingSystem.java +++ b/src/main/java/io/github/simplexdevelopment/scheduler/SchedulingSystem.java @@ -29,7 +29,7 @@ public final class SchedulingSystem implements ISchedule { this.serviceManager = new ServiceManager(); this.plugin = plugin; this.repeatingPools = new HashSet<>(); - this.mainScheduler = new ReactorBukkitScheduler(plugin, plugin.getServer().getScheduler()); + this.mainScheduler = new ReactorBukkitScheduler(plugin); } /** diff --git a/src/main/java/io/github/simplexdevelopment/scheduler/ServicePool.java b/src/main/java/io/github/simplexdevelopment/scheduler/ServicePool.java index 190b2bf..c939e98 100644 --- a/src/main/java/io/github/simplexdevelopment/scheduler/ServicePool.java +++ b/src/main/java/io/github/simplexdevelopment/scheduler/ServicePool.java @@ -1,7 +1,6 @@ package io.github.simplexdevelopment.scheduler; import io.github.simplexdevelopment.api.IService; -import io.github.simplexdevelopment.api.InvalidServicePoolException; import org.bukkit.NamespacedKey; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.Contract; @@ -34,7 +33,6 @@ public final class ServicePool { * The key used to identify this service pool. */ private final NamespacedKey name; - private final ReactorBukkitScheduler rbScheduler; /** * This will create a new instance of a Service Pool with a {@link Scheduler} as its main scheduler. @@ -51,7 +49,6 @@ public final class ServicePool { } else { this.scheduler = Schedulers.single(); } - this.rbScheduler = null; } /** @@ -63,8 +60,7 @@ public final class ServicePool { public ServicePool(NamespacedKey name, JavaPlugin plugin) { this.name = name; this.associatedServices = new HashSet<>(); - this.scheduler = null; - this.rbScheduler = new ReactorBukkitScheduler(plugin, plugin.getServer().getScheduler()); + this.scheduler = new ReactorBukkitScheduler(plugin); } /** @@ -100,38 +96,21 @@ public final class ServicePool { /** * @param service The name of the service to queue. This should be a service that is located within this service pool. - * If you name a service that is stored within another service pool, - * this method will throw an error. + * If you name a service that is stored within another service pool, + * this method will throw an error. * @return A {@link Mono} object which contains a {@link Disposable} element which can be used to destroy the registered service. */ public @NotNull Mono queueService(IService service) { return Mono.just(service).map(s -> { if (s.isPeriodic()) { - if (scheduler != null) { - return scheduler.schedulePeriodically(s, - s.getDelay() * 50, - s.getPeriod() * 50, - TimeUnit.MILLISECONDS); - } else if (rbScheduler != null) { - return rbScheduler.schedulePeriodically(s, - s.getDelay() * 50, - s.getPeriod() * 50, - TimeUnit.MILLISECONDS); - } else { - throw new InvalidServicePoolException("Service pool is not initialized properly."); - } + return scheduler.schedulePeriodically(s, + s.getDelay() * 50, + s.getPeriod() * 50, + TimeUnit.MILLISECONDS); } else { - if (scheduler != null) { - return scheduler.schedule(s, - s.getDelay() * 50, - TimeUnit.MILLISECONDS); - } else if (rbScheduler != null) { - return rbScheduler.schedule(s, - s.getDelay() * 50, - TimeUnit.MILLISECONDS); - } else { - throw new InvalidServicePoolException("Service pool is not initialized properly."); - } + return scheduler.schedule(s, + s.getDelay() * 50, + TimeUnit.MILLISECONDS); } }); }