Add license header

This commit is contained in:
Paldiu 2022-12-14 23:04:43 -06:00
parent 3fd93e101f
commit e3773b24f0
16 changed files with 530 additions and 52 deletions

View File

@ -25,7 +25,7 @@ dependencies {
} }
shadowJar { shadowJar {
relocate('reactor', "io.github.simplex.reactor") relocate('reactor', "io.github.simplexdevelopment.reactor")
exclude('io.github.simplexdevelopment.impl') exclude('io.github.simplexdevelopment.impl')
} }

View File

@ -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; package io.github.simplexdevelopment.api;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -8,6 +32,12 @@ import reactor.core.publisher.Mono;
import java.util.Collection; import java.util.Collection;
import java.util.Objects; import java.util.Objects;
/**
* A glorified wrapper class which collects an element and abstracts it behind the Reactor API.
*
* @param <S> Any object type to wrap with this class.
* @author SimplexDevelopment
*/
public interface Context<S> { public interface Context<S> {
/** /**
* @return A Mono object which contains a single element represented by the definer of this Context class. * @return A Mono object which contains a single element represented by the definer of this Context class.

View File

@ -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; package io.github.simplexdevelopment.api;
import io.github.simplexdevelopment.scheduler.SchedulingSystem;
import io.github.simplexdevelopment.scheduler.ServiceManager; import io.github.simplexdevelopment.scheduler.ServiceManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import reactor.core.Disposable; import reactor.core.Disposable;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
/**
* This interface contains surface level methods for the {@link SchedulingSystem} to use.
*
* @author SimplexDevelopment
*/
public interface ISchedule { public interface ISchedule {
/** /**

View File

@ -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; package io.github.simplexdevelopment.api;
import io.github.simplexdevelopment.scheduler.ServicePool; import io.github.simplexdevelopment.scheduler.ServicePool;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
/**
* Represents a service that can be registered to a {@link ServicePool}.
* <p>
* 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 { public interface IService extends Runnable, Identifier {
/** /**

View File

@ -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; package io.github.simplexdevelopment.api;
import java.util.UUID; 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 { public interface Identifier {
/** /**
* @return The name of the identifiable object in a readable format. * @return The name of the identifiable object in a readable format.

View File

@ -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; package io.github.simplexdevelopment.api;
import java.util.function.Supplier; 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 { public class ServiceException extends RuntimeException {
/** /**
* Constructs a new service exception which states the service is not present within any service pools. * Constructs a new service exception which states the service is not present within any service pools.

View File

@ -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; package io.github.simplexdevelopment.api;
import io.github.simplexdevelopment.scheduler.ServicePool;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.function.Supplier; 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 { public class ServicePoolException extends RuntimeException {
/** /**
* Constructs a new generic service pool exception. * Constructs a new generic service pool exception.

View File

@ -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; package io.github.simplexdevelopment.impl;
import io.github.simplexdevelopment.scheduler.SchedulingSystem; import io.github.simplexdevelopment.scheduler.SchedulingSystem;
import io.github.simplexdevelopment.scheduler.ServicePool;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import reactor.core.Disposable; import reactor.core.Disposable;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@ -19,8 +43,15 @@ public class Main extends JavaPlugin {
// Plugin startup logic // Plugin startup logic
// Create a new instance of the scheduling system. // Create a new instance of the scheduling system.
this.scheduler = new SchedulingSystem<>(this); 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<Disposable> object above. // This will register all the services and set our Flux<Disposable> object above.
registerServices(new PoolHolder(this)); registerServices("main_pool");
} }
@Override @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<Disposable> object // This set will be used to set the Flux<Disposable> object
// that will be used to stop the services when the plugin is disabled. // that will be used to stop the services when the plugin is disabled.
Set<Disposable> dispos = new HashSet<>(); Set<Disposable> 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 // 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. // This will register the service to the service pool.
dispos.add(scheduler.getMainScheduler().schedule(impl)); dispos.add(scheduler.getMainScheduler().schedule(impl));
@ -48,8 +88,9 @@ public class Main extends JavaPlugin {
scheduler.queue(impl).subscribe(dispos::add); scheduler.queue(impl).subscribe(dispos::add);
// OR // OR
scheduler.getServiceManager() scheduler.getServiceManager()
.flatMap(manager -> manager.createServicePool("newPool", impl)) .flatMap(manager -> manager.emptyBukkitServicePool("backup", this))
.subscribe(p -> p.queueService(impl).subscribe(dispos::add)); .doOnNext(pool_a -> pool_a.getAssociatedServices().add(impl))
.subscribe(pool_b -> pool_b.queueService(impl).subscribe(dispos::add));
// This will set the Flux<Disposable> object on our previously made set, so that we can use it later on. // This will set the Flux<Disposable> object on our previously made set, so that we can use it later on.
disposables = Flux.fromIterable(dispos); disposables = Flux.fromIterable(dispos);

View File

@ -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<ServicePool> {
private final Collection<ServicePool> 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<ServicePool> getContext() {
return Mono.just(selectedPool);
}
@Override
public @NotNull Mono<Void> setContext(ServicePool context) {
return Mono.just(context).doOnNext(pool -> this.selectedPool = pool).then();
}
@Override
public Collection<ServicePool> contextCollection() {
return servicePoolCollection;
}
}

View File

@ -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; package io.github.simplexdevelopment.impl;
import io.github.simplexdevelopment.scheduler.ExecutableService; import io.github.simplexdevelopment.scheduler.ExecutableService;

View File

@ -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; package io.github.simplexdevelopment.scheduler;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import reactor.core.Disposable; 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 { public record BukkitDisposable(BukkitTask task) implements Disposable {
/** /**
* Disposes of the task upstream on the Bukkit scheduler. * Disposes of the task upstream on the Bukkit scheduler.

View File

@ -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; package io.github.simplexdevelopment.scheduler;
import io.github.simplexdevelopment.api.IService; import io.github.simplexdevelopment.api.IService;
@ -8,6 +32,11 @@ import reactor.core.publisher.Mono;
import java.util.Objects; import java.util.Objects;
/**
* An abstract service class meant for easy implementation and creation of services.
*
* @author SimplexDevelopment
*/
public abstract class ExecutableService implements IService { public abstract class ExecutableService implements IService {
/** /**
* The name of the service. * The name of the service.

View File

@ -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; package io.github.simplexdevelopment.scheduler;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -9,9 +33,21 @@ import reactor.core.scheduler.Scheduler;
import java.util.concurrent.TimeUnit; 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 public final class ReactorBukkitScheduler
implements Scheduler, Scheduler.Worker { implements Scheduler, Scheduler.Worker {
/**
* The plugin instance.
*/
private final JavaPlugin plugin; private final JavaPlugin plugin;
/**
* The bukkit scheduler.
*/
private final BukkitScheduler scheduler; private final BukkitScheduler scheduler;
public ReactorBukkitScheduler(JavaPlugin plugin) { public ReactorBukkitScheduler(JavaPlugin plugin) {

View File

@ -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; package io.github.simplexdevelopment.scheduler;
import io.github.simplexdevelopment.api.ISchedule; import io.github.simplexdevelopment.api.ISchedule;
@ -7,8 +32,25 @@ import org.bukkit.scheduler.BukkitScheduler;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import reactor.core.Disposable; import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; 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 { public final class SchedulingSystem<T extends JavaPlugin> implements ISchedule {
/** /**
* A denominator to use when registering default service pool names. * 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)); .flatMap(pool -> pool.queueService(service));
} }
public Flux<Disposable> queueAll() {
return getServiceManager()
.flatMapMany(ServiceManager::getServicePools)
.flatMap(ServicePool::queueServices);
}
@Override @Override
public @NotNull Mono<Void> runOnce(IService service) { public @NotNull Mono<Void> runOnce(IService service) {
return Mono.just(service) return Mono.just(service)

View File

@ -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; package io.github.simplexdevelopment.scheduler;
import io.github.simplexdevelopment.api.IService; import io.github.simplexdevelopment.api.IService;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@ -10,6 +35,15 @@ import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; 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 { public final class ServiceManager {
/** /**
* A set of {@link ServicePool}s which are currently active. * 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) { public @NotNull Mono<ServicePool> createServicePool(String poolName, IService... services) {
ServicePool pool = new ServicePool(poolName, false); ServicePool pool = new ServicePool(poolName, false);
Flux.fromIterable(Arrays.asList(services)).doOnEach(s -> pool.addService(s.get())); 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); return Mono.just(pool);
} }

View File

@ -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; package io.github.simplexdevelopment.scheduler;
import io.github.simplexdevelopment.api.IService; import io.github.simplexdevelopment.api.IService;
import io.github.simplexdevelopment.api.Identifier;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -15,7 +41,17 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; 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. * 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); 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. * @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. * @param service The service to check against this pool.
* @return Whether the service is registered with 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. * @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, * If you name a service that is stored within another service pool,
* this method will throw an error. * 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, * @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)}. * 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<>(); Set<Disposable> disposables = new HashSet<>();
return Flux.fromIterable(getAssociatedServices()) return Flux.fromIterable(getAssociatedServices())
.doOnEach(service -> disposables.add(queueService(service.get()).block())) .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.. * @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. * @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 service_name The name of the service to stop.
* @param disposable A {@link Disposable} object which contains the service that should be disposed. * @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. * @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. * @param service_name The name of the service to get.
* @return A {@link Mono} object which contains the service. * @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. * @param service The service to remove from the pool's service collection.
*/ */
void removeService(IService service) { 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. * @return This service pool after being cleared of all services.
* You will need to register services with this pool again before using it. * You will need to register services with this pool again before using it.
*/ */
@ -180,4 +233,9 @@ public final class ServicePool {
public Scheduler getScheduler() { public Scheduler getScheduler() {
return scheduler; return scheduler;
} }
@Override
public String getName() {
return name;
}
} }