mirror of
https://github.com/SimplexDevelopment/SimplexSS.git
synced 2025-07-07 08:43:04 +00:00
Add license header
This commit is contained in:
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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) {
|
||||
|
@ -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 <T> Your plugin class, which extends {@link JavaPlugin}.
|
||||
* @author SimplexDevelopment
|
||||
* @see ServiceManager
|
||||
* @see ServicePool
|
||||
* @see ReactorBukkitScheduler
|
||||
*/
|
||||
public final class SchedulingSystem<T extends JavaPlugin> implements ISchedule {
|
||||
/**
|
||||
* A denominator to use when registering default service pool names.
|
||||
@ -52,6 +94,12 @@ public final class SchedulingSystem<T extends JavaPlugin> implements ISchedule {
|
||||
.flatMap(pool -> pool.queueService(service));
|
||||
}
|
||||
|
||||
public Flux<Disposable> queueAll() {
|
||||
return getServiceManager()
|
||||
.flatMapMany(ServiceManager::getServicePools)
|
||||
.flatMap(ServicePool::queueServices);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Mono<Void> runOnce(IService service) {
|
||||
return Mono.just(service)
|
||||
|
@ -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<ServicePool> 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<ServicePool> 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<ServicePool> 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);
|
||||
}
|
||||
|
||||
|
@ -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<Disposable> startServices() {
|
||||
public @NotNull Flux<Disposable> queueServices() {
|
||||
Set<Disposable> 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<Disposable>} which contains all the services that should be disposed..
|
||||
* @return A {@link Mono<Void>} 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<Void>} 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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user