diff --git a/build.gradle b/build.gradle index 17259a9..0230544 100644 --- a/build.gradle +++ b/build.gradle @@ -25,7 +25,7 @@ dependencies { } shadowJar { - relocate('reactor', "io.github.simplex.reactor") + relocate('reactor', "io.github.simplexdevelopment.reactor") exclude('io.github.simplexdevelopment.impl') } diff --git a/src/main/java/io/github/simplexdevelopment/api/Context.java b/src/main/java/io/github/simplexdevelopment/api/Context.java index 3d9e688..abeb727 100644 --- a/src/main/java/io/github/simplexdevelopment/api/Context.java +++ b/src/main/java/io/github/simplexdevelopment/api/Context.java @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.api; import org.jetbrains.annotations.NotNull; @@ -8,6 +32,12 @@ import reactor.core.publisher.Mono; import java.util.Collection; import java.util.Objects; +/** + * A glorified wrapper class which collects an element and abstracts it behind the Reactor API. + * + * @param Any object type to wrap with this class. + * @author SimplexDevelopment + */ public interface Context { /** * @return A Mono object which contains a single element represented by the definer of this Context class. diff --git a/src/main/java/io/github/simplexdevelopment/api/ISchedule.java b/src/main/java/io/github/simplexdevelopment/api/ISchedule.java index 2c597a9..715e180 100644 --- a/src/main/java/io/github/simplexdevelopment/api/ISchedule.java +++ b/src/main/java/io/github/simplexdevelopment/api/ISchedule.java @@ -1,10 +1,40 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.api; +import io.github.simplexdevelopment.scheduler.SchedulingSystem; import io.github.simplexdevelopment.scheduler.ServiceManager; import org.jetbrains.annotations.NotNull; import reactor.core.Disposable; import reactor.core.publisher.Mono; +/** + * This interface contains surface level methods for the {@link SchedulingSystem} to use. + * + * @author SimplexDevelopment + */ public interface ISchedule { /** diff --git a/src/main/java/io/github/simplexdevelopment/api/IService.java b/src/main/java/io/github/simplexdevelopment/api/IService.java index 4328d44..7f6b8c9 100644 --- a/src/main/java/io/github/simplexdevelopment/api/IService.java +++ b/src/main/java/io/github/simplexdevelopment/api/IService.java @@ -1,9 +1,41 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.api; import io.github.simplexdevelopment.scheduler.ServicePool; import org.bukkit.plugin.java.JavaPlugin; import reactor.core.publisher.Mono; +/** + * Represents a service that can be registered to a {@link ServicePool}. + *

+ * This interface provides surface level methods to be used by the {@link ServicePool} + * associated to each service. + * + * @author SimplexDevelopment + */ public interface IService extends Runnable, Identifier { /** diff --git a/src/main/java/io/github/simplexdevelopment/api/Identifier.java b/src/main/java/io/github/simplexdevelopment/api/Identifier.java index c29e7c4..6be16a5 100644 --- a/src/main/java/io/github/simplexdevelopment/api/Identifier.java +++ b/src/main/java/io/github/simplexdevelopment/api/Identifier.java @@ -1,7 +1,36 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.api; import java.util.UUID; +/** + * This interface provides a method for retrieving a name, unique identifier, and numerical id for a class. + * + * @author SimplexDevelopment + */ public interface Identifier { /** * @return The name of the identifiable object in a readable format. diff --git a/src/main/java/io/github/simplexdevelopment/api/ServiceException.java b/src/main/java/io/github/simplexdevelopment/api/ServiceException.java index 506b73f..60c2b36 100644 --- a/src/main/java/io/github/simplexdevelopment/api/ServiceException.java +++ b/src/main/java/io/github/simplexdevelopment/api/ServiceException.java @@ -1,7 +1,38 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.api; import java.util.function.Supplier; +/** + * This class is used to represent a service exception. + * When a {@link IService} is called that has not been registered with a {@link io.github.simplexdevelopment.scheduler.ServicePool}, + * this exception will be thrown. + * + * @author SimplexDevelopment + */ public class ServiceException extends RuntimeException { /** * Constructs a new service exception which states the service is not present within any service pools. diff --git a/src/main/java/io/github/simplexdevelopment/api/ServicePoolException.java b/src/main/java/io/github/simplexdevelopment/api/ServicePoolException.java index 6f58f4f..943b93a 100644 --- a/src/main/java/io/github/simplexdevelopment/api/ServicePoolException.java +++ b/src/main/java/io/github/simplexdevelopment/api/ServicePoolException.java @@ -1,10 +1,43 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.api; +import io.github.simplexdevelopment.scheduler.ServicePool; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import java.util.function.Supplier; +/** + * This class is used to represent a service pool exception. + * This exception is thrown when a {@link ServicePool} is not found, + * when {@link ServicePool#queueService(IService)} is called but the service is not registered with this pool, + * or if the service pool is empty and {@link ServicePool#queueServices()} is called. + * + * @author SimplexDevelopment + */ public class ServicePoolException extends RuntimeException { /** * Constructs a new generic service pool exception. diff --git a/src/main/java/io/github/simplexdevelopment/impl/Main.java b/src/main/java/io/github/simplexdevelopment/impl/Main.java index 4b628df..d0475aa 100644 --- a/src/main/java/io/github/simplexdevelopment/impl/Main.java +++ b/src/main/java/io/github/simplexdevelopment/impl/Main.java @@ -1,8 +1,32 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.impl; import io.github.simplexdevelopment.scheduler.SchedulingSystem; +import io.github.simplexdevelopment.scheduler.ServicePool; import org.bukkit.plugin.java.JavaPlugin; -import org.jetbrains.annotations.NotNull; import reactor.core.Disposable; import reactor.core.publisher.Flux; @@ -19,8 +43,15 @@ public class Main extends JavaPlugin { // Plugin startup logic // Create a new instance of the scheduling system. this.scheduler = new SchedulingSystem<>(this); + + // Creates new service pools. + getScheduler().getServiceManager().subscribe(a -> { + a.emptyBukkitServicePool("main_pool", this).subscribe(); + a.emptyServicePool("off_loader", true).subscribe(); + }); + // This will register all the services and set our Flux object above. - registerServices(new PoolHolder(this)); + registerServices("main_pool"); } @Override @@ -34,13 +65,22 @@ public class Main extends JavaPlugin { }); } - public void registerServices(@NotNull PoolHolder poolHolder) { + public void registerServices(String poolName) { // This set will be used to set the Flux object // that will be used to stop the services when the plugin is disabled. Set dispos = new HashSet<>(); + // Find the service pool we want to register our services to. + ServicePool pool = scheduler.getServiceManager().map(a -> a.getServicePools() + .filter(b -> b.getName().equalsIgnoreCase(poolName)) + .blockFirst()) + .block(); + + // Make sure the pool isn't null. + if (pool == null) pool = new ServicePool(poolName, this); + // Register services here - ServiceImpl impl = new ServiceImpl(this, poolHolder.getContext().block()); + ServiceImpl impl = new ServiceImpl(this, pool); // This will register the service to the service pool. dispos.add(scheduler.getMainScheduler().schedule(impl)); @@ -48,8 +88,9 @@ public class Main extends JavaPlugin { scheduler.queue(impl).subscribe(dispos::add); // OR scheduler.getServiceManager() - .flatMap(manager -> manager.createServicePool("newPool", impl)) - .subscribe(p -> p.queueService(impl).subscribe(dispos::add)); + .flatMap(manager -> manager.emptyBukkitServicePool("backup", this)) + .doOnNext(pool_a -> pool_a.getAssociatedServices().add(impl)) + .subscribe(pool_b -> pool_b.queueService(impl).subscribe(dispos::add)); // This will set the Flux object on our previously made set, so that we can use it later on. disposables = Flux.fromIterable(dispos); diff --git a/src/main/java/io/github/simplexdevelopment/impl/PoolHolder.java b/src/main/java/io/github/simplexdevelopment/impl/PoolHolder.java deleted file mode 100644 index c1e1c3d..0000000 --- a/src/main/java/io/github/simplexdevelopment/impl/PoolHolder.java +++ /dev/null @@ -1,36 +0,0 @@ -package io.github.simplexdevelopment.impl; - -import io.github.simplexdevelopment.api.Context; -import io.github.simplexdevelopment.scheduler.ServicePool; -import org.jetbrains.annotations.NotNull; -import reactor.core.publisher.Mono; - -import java.util.ArrayList; -import java.util.Collection; - -public class PoolHolder implements Context { - private final Collection servicePoolCollection = new ArrayList<>(); - private ServicePool selectedPool; - - public PoolHolder(Main plugin) { - this.servicePoolCollection.add(new ServicePool("Pool-One", plugin)); - this.servicePoolCollection.add(new ServicePool("Pool-Two", true)); - this.servicePoolCollection.add(new ServicePool("Pool-Three", false)); - this.selectedPool = servicePoolCollection.stream().findFirst().orElseGet(() -> new ServicePool("Default", plugin)); - } - - @Override - public @NotNull Mono getContext() { - return Mono.just(selectedPool); - } - - @Override - public @NotNull Mono setContext(ServicePool context) { - return Mono.just(context).doOnNext(pool -> this.selectedPool = pool).then(); - } - - @Override - public Collection contextCollection() { - return servicePoolCollection; - } -} diff --git a/src/main/java/io/github/simplexdevelopment/impl/ServiceImpl.java b/src/main/java/io/github/simplexdevelopment/impl/ServiceImpl.java index ab6c470..c8880e4 100644 --- a/src/main/java/io/github/simplexdevelopment/impl/ServiceImpl.java +++ b/src/main/java/io/github/simplexdevelopment/impl/ServiceImpl.java @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.impl; import io.github.simplexdevelopment.scheduler.ExecutableService; diff --git a/src/main/java/io/github/simplexdevelopment/scheduler/BukkitDisposable.java b/src/main/java/io/github/simplexdevelopment/scheduler/BukkitDisposable.java index 5ee9bbb..00d57f6 100644 --- a/src/main/java/io/github/simplexdevelopment/scheduler/BukkitDisposable.java +++ b/src/main/java/io/github/simplexdevelopment/scheduler/BukkitDisposable.java @@ -1,8 +1,37 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.scheduler; import org.bukkit.scheduler.BukkitTask; import reactor.core.Disposable; +/** + * An abstraction of the {@link BukkitTask} class which allows this object to be used with Reactor. + * + * @author SimplexDevelopment + */ public record BukkitDisposable(BukkitTask task) implements Disposable { /** * Disposes of the task upstream on the Bukkit scheduler. diff --git a/src/main/java/io/github/simplexdevelopment/scheduler/ExecutableService.java b/src/main/java/io/github/simplexdevelopment/scheduler/ExecutableService.java index 870a2c5..ef0ab67 100644 --- a/src/main/java/io/github/simplexdevelopment/scheduler/ExecutableService.java +++ b/src/main/java/io/github/simplexdevelopment/scheduler/ExecutableService.java @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.scheduler; import io.github.simplexdevelopment.api.IService; @@ -8,6 +32,11 @@ import reactor.core.publisher.Mono; import java.util.Objects; +/** + * An abstract service class meant for easy implementation and creation of services. + * + * @author SimplexDevelopment + */ public abstract class ExecutableService implements IService { /** * The name of the service. diff --git a/src/main/java/io/github/simplexdevelopment/scheduler/ReactorBukkitScheduler.java b/src/main/java/io/github/simplexdevelopment/scheduler/ReactorBukkitScheduler.java index 352e002..9003ff2 100644 --- a/src/main/java/io/github/simplexdevelopment/scheduler/ReactorBukkitScheduler.java +++ b/src/main/java/io/github/simplexdevelopment/scheduler/ReactorBukkitScheduler.java @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.scheduler; import org.bukkit.plugin.java.JavaPlugin; @@ -9,9 +33,21 @@ import reactor.core.scheduler.Scheduler; import java.util.concurrent.TimeUnit; +/** + * An abstraction layer over the {@link BukkitScheduler} to allow for the use as a {@link Scheduler}. + * This will allow us to perform non-blocking operations on the main server thread. + * + * @author SimplexDevelopment + */ public final class ReactorBukkitScheduler implements Scheduler, Scheduler.Worker { + /** + * The plugin instance. + */ private final JavaPlugin plugin; + /** + * The bukkit scheduler. + */ private final BukkitScheduler scheduler; public ReactorBukkitScheduler(JavaPlugin plugin) { diff --git a/src/main/java/io/github/simplexdevelopment/scheduler/SchedulingSystem.java b/src/main/java/io/github/simplexdevelopment/scheduler/SchedulingSystem.java index 2efabee..a8d4c9b 100644 --- a/src/main/java/io/github/simplexdevelopment/scheduler/SchedulingSystem.java +++ b/src/main/java/io/github/simplexdevelopment/scheduler/SchedulingSystem.java @@ -1,3 +1,28 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + package io.github.simplexdevelopment.scheduler; import io.github.simplexdevelopment.api.ISchedule; @@ -7,8 +32,25 @@ import org.bukkit.scheduler.BukkitScheduler; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import reactor.core.Disposable; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +/** + * This class is used to manage the scheduling of {@link IService}s, + * and the creation of {@link ServicePool}s through the {@link ServiceManager}. + * The {@link ServiceManager} should be used to create new {@link ServicePool}s on + * initialization of your plugin, and your {@link IService}s should be registered in the + * {@link JavaPlugin#onEnable()} method. You can then use the {@link #queue(IService)} method + * to then queue up your services, or call {@link #queueAll()} to queue up all services in every pool. + * {@link #forceStart(IService)} and {@link #forceStop(IService)} will forcefully start and stop the services, respectively. + * {@link #getMainScheduler()} gets the main scheduler for the scheduling system, which is a {@link ReactorBukkitScheduler} object. + * + * @param Your plugin class, which extends {@link JavaPlugin}. + * @author SimplexDevelopment + * @see ServiceManager + * @see ServicePool + * @see ReactorBukkitScheduler + */ public final class SchedulingSystem implements ISchedule { /** * A denominator to use when registering default service pool names. @@ -52,6 +94,12 @@ public final class SchedulingSystem implements ISchedule { .flatMap(pool -> pool.queueService(service)); } + public Flux queueAll() { + return getServiceManager() + .flatMapMany(ServiceManager::getServicePools) + .flatMap(ServicePool::queueServices); + } + @Override public @NotNull Mono runOnce(IService service) { return Mono.just(service) diff --git a/src/main/java/io/github/simplexdevelopment/scheduler/ServiceManager.java b/src/main/java/io/github/simplexdevelopment/scheduler/ServiceManager.java index 5cf65ef..3001969 100644 --- a/src/main/java/io/github/simplexdevelopment/scheduler/ServiceManager.java +++ b/src/main/java/io/github/simplexdevelopment/scheduler/ServiceManager.java @@ -1,6 +1,31 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.scheduler; import io.github.simplexdevelopment.api.IService; +import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import reactor.core.publisher.Flux; @@ -10,6 +35,15 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; +/** + * The ServiceManager is a factory class for managing {@link ServicePool}s. + * You can use this class for easy creation of {@link ServicePool}s, + * as well as adding and removing {@link IService}s from the pool. You can create an + * {@link #emptyServicePool(String, boolean)}, {@link #createServicePool(String, IService...)}, + * and even create a {@link #multithreadedServicePool(String, IService...)}. + * + * @author SimplexDevelopment + */ public final class ServiceManager { /** * A set of {@link ServicePool}s which are currently active. @@ -37,6 +71,36 @@ public final class ServiceManager { public @NotNull Mono createServicePool(String poolName, IService... services) { ServicePool pool = new ServicePool(poolName, false); Flux.fromIterable(Arrays.asList(services)).doOnEach(s -> pool.addService(s.get())); + servicePools.add(pool); + return Mono.just(pool); + } + + /** + * @param poolName The name of the service pool. + * @param plugin The plugin which will be used to register the service pool. + * @return A {@link Mono} object which contains a {@link ServicePool} element. + * This Service Pool will execute each service within the main server thread. + */ + @Contract(pure = true, value = "_, _ -> new") + public @NotNull Mono emptyBukkitServicePool(String poolName, JavaPlugin plugin) { + ServicePool pool = new ServicePool(poolName, plugin); + servicePools.add(pool); + return Mono.just(pool); + + } + + /** + * @param poolName The name of the service pool. + * @param plugin The plugin which will be used to register the service pool. + * @param services The services to register within the service pool. + * @return A {@link Mono} object which contains a {@link ServicePool} element. + * This Service Pool will execute each service within the main server thread. + */ + @Contract(pure = true, value = "_, _, _ -> new") + public @NotNull Mono bukkitServicePool(String poolName, JavaPlugin plugin, IService... services) { + ServicePool pool = new ServicePool(poolName, plugin); + Flux.fromIterable(Arrays.asList(services)).doOnEach(s -> pool.addService(s.get())); + servicePools.add(pool); return Mono.just(pool); } diff --git a/src/main/java/io/github/simplexdevelopment/scheduler/ServicePool.java b/src/main/java/io/github/simplexdevelopment/scheduler/ServicePool.java index d332463..e4cfa8f 100644 --- a/src/main/java/io/github/simplexdevelopment/scheduler/ServicePool.java +++ b/src/main/java/io/github/simplexdevelopment/scheduler/ServicePool.java @@ -1,7 +1,33 @@ +/* + * MIT License + * + * Copyright (c) 2022 SimplexDevelopment + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package io.github.simplexdevelopment.scheduler; import io.github.simplexdevelopment.api.IService; +import io.github.simplexdevelopment.api.Identifier; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitScheduler; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -15,7 +41,17 @@ import java.util.HashSet; import java.util.Set; import java.util.concurrent.TimeUnit; -public final class ServicePool { +/** + * A service pool is a collection of services which are managed by a single scheduler. + * The scheduler can either be an instance of {@link Scheduler} or {@link ReactorBukkitScheduler}. + * Using {@link Scheduler} allows for more flexibility, but doesn't communicate with the Main thread. + * Using {@link ReactorBukkitScheduler} allows for communication with the Main thread, but is less flexible. + * + * @author SimplexDevelopment + * @see ReactorBukkitScheduler + * @see Scheduler + */ +public final class ServicePool implements Identifier { /** * The default {@link String} used to identify unmarked services. This will cause errors if left unchecked. */ @@ -62,13 +98,6 @@ public final class ServicePool { this.scheduler = new ReactorBukkitScheduler(plugin); } - /** - * @return The default namespaced key to use if one is not assigned. - */ - static String getDefaultNamespacedKey() { - return DEFAULT; - } - /** * @param service Add a service to the pool's service collection. */ @@ -77,6 +106,8 @@ public final class ServicePool { } /** + * Checks to see if the defined service is present within this pool. + * * @param service The service to check against this pool. * @return Whether the service is registered with this pool. */ @@ -94,6 +125,11 @@ public final class ServicePool { } /** + * This method is the actual method used to schedule a service. + * This will register the service with the scheduler, and then return a {@link Disposable} object + * encapsulated within a {@link Mono}. If {@link IService#isPeriodic()} returns true, then the service + * will be scheduled to run periodically. Otherwise, it will be scheduled to run once. + * * @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. @@ -115,10 +151,13 @@ public final class ServicePool { } /** + * This method can be used to start all the services registered with this pool. + * If there are no services, this will do nothing. + * * @return A {@link Flux} object which contains a collection of {@link Disposable} elements, * which can be used to destroy the registered services using {@link ServicePool#stopServices(Flux)}. */ - public @NotNull Flux startServices() { + public @NotNull Flux queueServices() { Set disposables = new HashSet<>(); return Flux.fromIterable(getAssociatedServices()) .doOnEach(service -> disposables.add(queueService(service.get()).block())) @@ -126,6 +165,9 @@ public final class ServicePool { } /** + * This method can be used to stop all the services registered with this pool. + * If there are no services, this will do nothing. + * * @param disposableThread A {@link Flux} which contains all the services that should be disposed.. * @return A {@link Mono} object which can be used to stop the services. */ @@ -135,6 +177,10 @@ public final class ServicePool { } /** + * This is the method used to stop a service. This will call the relative {@link Disposable#dispose} method + * to the {@link Scheduler} supplied for this pool. If you are using the {@link ReactorBukkitScheduler}, + * this will cancel the task upstream on the {@link BukkitScheduler}. + * * @param service_name The name of the service to stop. * @param disposable A {@link Disposable} object which contains the service that should be disposed. * @return A {@link Mono} object which can be used to stop the service. @@ -148,6 +194,9 @@ public final class ServicePool { } /** + * Gets a service based on the name of the service defined by {@link Identifier#getName()}. + * This will search the service pool for a service with the same name, and return it. + * * @param service_name The name of the service to get. * @return A {@link Mono} object which contains the service. */ @@ -158,6 +207,8 @@ public final class ServicePool { } /** + * This method removes a service from the service pool set. + * * @param service The service to remove from the pool's service collection. */ void removeService(IService service) { @@ -165,6 +216,8 @@ public final class ServicePool { } /** + * This will clear the ServicePool of all services and return an empty pool. + * * @return This service pool after being cleared of all services. * You will need to register services with this pool again before using it. */ @@ -180,4 +233,9 @@ public final class ServicePool { public Scheduler getScheduler() { return scheduler; } + + @Override + public String getName() { + return name; + } }