Minor update

This commit is contained in:
Paldiu 2022-12-14 11:31:49 -06:00
parent 262115a52e
commit 99c78e320f
3 changed files with 21 additions and 36 deletions

View File

@ -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() {
}
}

View File

@ -29,7 +29,7 @@ public final class SchedulingSystem<T extends JavaPlugin> 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);
}
/**

View File

@ -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<Disposable> 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);
}
});
}