diff --git a/src/main/java/io/github/simplex/api/ExecutableService.java b/src/main/java/io/github/simplex/api/ExecutableService.java index f4f9c91..e20bcf9 100644 --- a/src/main/java/io/github/simplex/api/ExecutableService.java +++ b/src/main/java/io/github/simplex/api/ExecutableService.java @@ -2,13 +2,21 @@ package io.github.simplex.api; import org.bukkit.plugin.Plugin; -public abstract class ExecutableService implements Service { +public abstract class ExecutableService implements IService { private final int serviceID; private final Plugin plugin; + private final long delay; + private final long period; + private final boolean delayed; + private final boolean repeating; public ExecutableService(Plugin plugin, int serviceID, long delay, long period, boolean delayed, boolean repeating) { - this.serviceID = serviceID; this.plugin = plugin; + this.serviceID = serviceID; + this.repeating = repeating; + this.delay = delay; + this.period = period; + this.delayed = delayed; } @Override @@ -20,4 +28,24 @@ public abstract class ExecutableService implements Service { public Plugin getProvidingPlugin() { return plugin; } + + @Override + public long getDelay() { + return delay; + } + + @Override + public long getPeriod() { + return period; + } + + @Override + public boolean isDelayed() { + return delayed; + } + + @Override + public boolean isRepeating() { + return repeating; + } } diff --git a/src/main/java/io/github/simplex/api/Scheduler.java b/src/main/java/io/github/simplex/api/ISchedule.java similarity index 51% rename from src/main/java/io/github/simplex/api/Scheduler.java rename to src/main/java/io/github/simplex/api/ISchedule.java index 259a366..7c24efd 100644 --- a/src/main/java/io/github/simplex/api/Scheduler.java +++ b/src/main/java/io/github/simplex/api/ISchedule.java @@ -4,14 +4,14 @@ import io.github.simplex.simplexss.ServiceManager; import io.github.simplex.simplexss.ServicePool; import reactor.core.publisher.Mono; -public interface Scheduler { +public interface ISchedule { Mono getServiceManager(); - Mono queue(Service service); + Mono queue(IService service); - Mono runOnce(Service service); + Mono runOnce(IService service); - Mono forceStop(Service service); + Mono forceStop(IService service); - Mono forceStart(Service service); + Mono forceStart(IService service); } diff --git a/src/main/java/io/github/simplex/api/Service.java b/src/main/java/io/github/simplex/api/IService.java similarity index 79% rename from src/main/java/io/github/simplex/api/Service.java rename to src/main/java/io/github/simplex/api/IService.java index 346fddc..7b8014d 100644 --- a/src/main/java/io/github/simplex/api/Service.java +++ b/src/main/java/io/github/simplex/api/IService.java @@ -1,12 +1,11 @@ package io.github.simplex.api; -import io.github.simplex.simplexss.ServicePool; import org.bukkit.plugin.Plugin; import reactor.core.publisher.Mono; import java.util.concurrent.RunnableScheduledFuture; -public interface Service extends RunnableScheduledFuture { +public interface IService extends RunnableScheduledFuture { int getServiceID(); boolean isDelayed(); diff --git a/src/main/java/io/github/simplex/simplexss/SchedulingSystem.java b/src/main/java/io/github/simplex/simplexss/SchedulingSystem.java index bb5a01e..e07ef1b 100644 --- a/src/main/java/io/github/simplex/simplexss/SchedulingSystem.java +++ b/src/main/java/io/github/simplex/simplexss/SchedulingSystem.java @@ -1,20 +1,22 @@ package io.github.simplex.simplexss; -import io.github.simplex.api.Scheduler; -import io.github.simplex.api.Service; +import io.github.simplex.api.ISchedule; +import io.github.simplex.api.IService; import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; import reactor.core.publisher.Mono; import java.util.HashSet; +import java.util.Objects; import java.util.Set; -public final class SchedulingSystem implements Scheduler { +public final class SchedulingSystem implements ISchedule { private final ServiceManager serviceManager; private final Plugin plugin; private final Set repeatingPools; private final Set delayedPools; - public SchedulingSystem(ServiceManager serviceManager, Plugin plugin) { + public SchedulingSystem(@NotNull ServiceManager serviceManager, Plugin plugin) { this.serviceManager = serviceManager; this.plugin = plugin; this.repeatingPools = new HashSet<>(); @@ -35,27 +37,29 @@ public final class SchedulingSystem implements Scheduler { } @Override - public Mono queue(Service service) { + @NotNull + public Mono queue(@NotNull IService service) { return getServiceManager().flatMap(serviceManager -> { - Mono pool = Mono.justOrEmpty(serviceManager.getAssociatedServicePool(service)); - return pool.defaultIfEmpty(serviceManager.createServicePool(service)); + Mono pool = serviceManager.getAssociatedServicePool(service); + return pool.defaultIfEmpty(Objects.requireNonNull(serviceManager.createServicePool(service).block())); }); } @Override - public Mono runOnce(Service service) { - service.start().block(); - service.stop().block(); - return Mono.empty(); + public Mono runOnce(IService service) { + return Mono.just(service).doOnNext(s -> { + s.start(); + s.stop(); + }).then(); } @Override - public Mono forceStop(Service service) { + public Mono forceStop(IService service) { return service.stop(); } @Override - public Mono forceStart(Service service) { + public Mono forceStart(IService service) { return service.start(); } diff --git a/src/main/java/io/github/simplex/simplexss/ServiceManager.java b/src/main/java/io/github/simplex/simplexss/ServiceManager.java index 44eb889..c973351 100644 --- a/src/main/java/io/github/simplex/simplexss/ServiceManager.java +++ b/src/main/java/io/github/simplex/simplexss/ServiceManager.java @@ -1,10 +1,11 @@ package io.github.simplex.simplexss; -import io.github.simplex.api.Service; +import io.github.simplex.api.IService; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; import java.util.HashSet; import java.util.Set; @@ -13,49 +14,46 @@ import java.util.stream.Stream; public final class ServiceManager { private final Set servicePools; private final Plugin plugin; - + public ServiceManager(Plugin plugin) { this.plugin = plugin; servicePools = new HashSet<>(); } - + @Contract(pure = true, value = "_ -> new") - public @NotNull ServicePool createServicePool(Service... services) { + public @NotNull Mono createServicePool(IService... services) { ServicePool pool = new ServicePool(); Stream.of(services).forEach(pool::addService); - return pool; - } - - @Contract("_, _ -> param1") - public ServicePool addToExistingPool(@NotNull ServicePool pool, Service... services) { - Stream.of(services).forEach(pool::addService); - return pool; - } - - @Contract("_, _ -> param1") - public ServicePool takeFromExistingPool(@NotNull ServicePool pool, Service... services) { - Stream.of(services).forEach(pool::removeService); - return pool; - } - - public Set getServicePools() { - return servicePools; + return Mono.just(pool); } - public boolean locateServiceWithinPools(Service service) { + @Contract("_, _ -> param1") + public Mono addToExistingPool(@NotNull ServicePool pool, IService... services) { + Stream.of(services).forEach(pool::addService); + return Mono.just(pool); + } + + @Contract("_, _ -> param1") + public Mono takeFromExistingPool(@NotNull ServicePool pool, IService... services) { + Stream.of(services).forEach(pool::removeService); + return Mono.just(pool); + } + + public Flux getServicePools() { + return Flux.fromIterable(servicePools); + } + + public boolean locateServiceWithinPools(IService service) { return servicePools.stream().map(p -> p.isValidService(service)).findFirst().orElseGet(() -> false); } - public @Nullable ServicePool getAssociatedServicePool(Service service) { - if (!locateServiceWithinPools(service)) return null; - + public @NotNull Mono getAssociatedServicePool(IService service) { + if (!locateServiceWithinPools(service)) return Mono.empty(); return getServicePools() - .stream() .filter(p -> p.getAssociatedServices().contains(service)) - .findFirst() - .orElseGet(() -> null); + .next(); } - + public Plugin getProvidingPlugin() { return plugin; } diff --git a/src/main/java/io/github/simplex/simplexss/ServicePool.java b/src/main/java/io/github/simplex/simplexss/ServicePool.java index 2f1e418..fb15260 100644 --- a/src/main/java/io/github/simplex/simplexss/ServicePool.java +++ b/src/main/java/io/github/simplex/simplexss/ServicePool.java @@ -1,7 +1,9 @@ package io.github.simplex.simplexss; -import io.github.simplex.api.Service; +import io.github.simplex.api.IService; import org.jetbrains.annotations.NotNull; +import reactor.core.Disposable; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.core.scheduler.Scheduler; import reactor.core.scheduler.Schedulers; @@ -13,11 +15,9 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public final class ServicePool { - private final Set associatedServices; + private final Set associatedServices; private final Scheduler scheduler; private final ExecutorService executor; - private boolean delayed = false; - private boolean repeating = false; public ServicePool() { this.associatedServices = new HashSet<>(); @@ -25,55 +25,61 @@ public final class ServicePool { this.scheduler = Schedulers.fromExecutorService(executor); } - void addService(Service service) { + void addService(IService service) { getAssociatedServices().add(service); } - boolean isValidService(Service service) { + boolean isValidService(IService service) { return getAssociatedServices().contains(service); } @NotNull - public Set getAssociatedServices() { + public Set getAssociatedServices() { return associatedServices; } - public Mono startServices() { - return Mono.just(getAssociatedServices()).doOnNext(services -> { - for (Service service : services) { + public Flux startServices() { + return Mono.just(getAssociatedServices()).flatMapMany(services -> { + Set disposables = new HashSet<>(); + for (IService service : services) { if (service.isRepeating()) { - scheduler.schedulePeriodically(service, service.getDelay() * 5, service.getPeriod() * 5, TimeUnit.MILLISECONDS); + disposables.add(scheduler.schedulePeriodically(service, + service.getDelay() * 5, + service.getPeriod() * 5, + TimeUnit.MILLISECONDS)); } else if (service.isDelayed()) { - scheduler.schedule(service, service.getDelay() * 5, TimeUnit.MILLISECONDS); + disposables.add(scheduler.schedule(service, + service.getDelay() * 5, + TimeUnit.MILLISECONDS)); + } else { + disposables.add(scheduler.schedule(service)); } } - }).then(); + ; + return Flux.fromIterable(disposables); + }); } - public Mono stopServices() { + public Mono stopServices(Flux disposableThread) { return Mono.just(getAssociatedServices()).doOnNext(services -> { - for (Service service : services) { - service.stop(); + for (IService service : services) { + disposableThread.doOnNext(Disposable::dispose); } }).then(); } - public Service getService(int serviceID) { - return getAssociatedServices() - .stream() - .filter(s -> s.getServiceID() == serviceID) - .findFirst() - .orElse(null); + public Mono getService(int serviceID) { + return Flux.fromIterable(getAssociatedServices()) + .filter(service -> service.getServiceID() == serviceID) + .next(); } - public void removeService(Service service) { + void removeService(IService service) { getAssociatedServices().remove(service); } public ServicePool recycle() { this.getAssociatedServices().clear(); - this.repeating = false; - this.delayed = false; return this; } } diff --git a/src/main/java/io/github/simplex/simplexss/SimplexSS.java b/src/main/java/io/github/simplex/simplexss/SimplexSS.java deleted file mode 100644 index ee106e0..0000000 --- a/src/main/java/io/github/simplex/simplexss/SimplexSS.java +++ /dev/null @@ -1,17 +0,0 @@ -package io.github.simplex.simplexss; - -import org.bukkit.plugin.java.JavaPlugin; - -public final class SimplexSS extends JavaPlugin { - - @Override - public void onEnable() { - // Plugin startup logic - - } - - @Override - public void onDisable() { - // Plugin shutdown logic - } -}