Improved Scheduler API (#696)

* Improved Scheduler API

- Added `Scheduler#builder(plugin)`
This method allows a more simplified builder while maintaining the main requirement of the executor plugin
- Added `Scheduler#taskByPlugin(plugin)`
Allows to obtain the tasks that a plugin has sent to execute and that are currently active
- Added `TaskBuilder#task(Consumer<SchuledTask>)`
Allows to specify a task with access to the task itself with the ability to cancel itself

* Applied requested changes

- Removed tasks builder method
- Added `Scheduler#buildTask(plugin, Consumer<ScheduledTask>)`

* Removed some unused imports

* Applied suggested change

* Fix possible test bug

* Applied more suggested changes

* Fixed tests inside tasks
This commit is contained in:
4drian3d
2022-06-09 02:27:06 -05:00
committed by GitHub
parent f088bd4443
commit e45ca5f357
9 changed files with 129 additions and 21 deletions

View File

@@ -7,11 +7,7 @@
package com.velocitypowered.api.command;
import com.google.common.collect.ImmutableList;
import com.velocitypowered.api.proxy.Player;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* Represents a command that can be executed by a {@link CommandSource}

View File

@@ -8,7 +8,6 @@
package com.velocitypowered.api.permission;
import net.kyori.adventure.permission.PermissionChecker;
import net.kyori.adventure.util.TriState;
/**
* Represents a object that has a set of queryable permissions.

View File

@@ -23,7 +23,6 @@ import java.util.Collection;
import java.util.Optional;
import java.util.UUID;
import net.kyori.adventure.audience.Audience;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* Provides an interface to a Minecraft server proxy.

View File

@@ -7,6 +7,8 @@
package com.velocitypowered.api.scheduler;
import org.jetbrains.annotations.NotNull;
/**
* Represents a task that is scheduled to run on the proxy.
*/
@@ -17,7 +19,7 @@ public interface ScheduledTask {
*
* @return the plugin that scheduled this task
*/
Object plugin();
@NotNull Object plugin();
/**
* Returns the current status of this task.

View File

@@ -8,8 +8,12 @@
package com.velocitypowered.api.scheduler;
import java.time.Duration;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.checkerframework.common.value.qual.IntRange;
import org.jetbrains.annotations.NotNull;
/**
* Represents a scheduler to execute tasks on the proxy.
@@ -23,7 +27,24 @@ public interface Scheduler {
* @param runnable the task to run when scheduled
* @return the task builder
*/
TaskBuilder buildTask(Object plugin, Runnable runnable);
TaskBuilder buildTask(@NotNull Object plugin, @NotNull Runnable runnable);
/**
* Initializes a new {@link TaskBuilder} for creating a task on the proxy.
*
* @param plugin the plugin to request the task for
* @param consumer the task to be run when scheduled with the capacity to cancel itself
* @return the task builder
*/
TaskBuilder buildTask(@NotNull Object plugin, @NotNull Consumer<ScheduledTask> consumer);
/**
* Get the {@link ScheduledTask} for a specific plugin.
*
* @param plugin the plugin object
* @return the list of {@link ScheduledTask} corresponding to a specific plugin
*/
@NotNull Collection<ScheduledTask> tasksByPlugin(@NotNull Object plugin);
/**
* Represents a fluent interface to schedule tasks on the proxy.
@@ -37,7 +58,7 @@ public interface Scheduler {
* @param unit the unit of time for {@code time}
* @return this builder, for chaining
*/
TaskBuilder delay(@IntRange(from = 0) long time, TimeUnit unit);
TaskBuilder delay(@IntRange(from = 0) long time, @NotNull TimeUnit unit);
/**
* Specifies that the task should delay its execution by the specified amount of time.
@@ -45,7 +66,7 @@ public interface Scheduler {
* @param duration the duration of the delay
* @return this builder, for chaining
*/
default TaskBuilder delay(Duration duration) {
default TaskBuilder delay(@NotNull Duration duration) {
return delay(duration.toMillis(), TimeUnit.MILLISECONDS);
}
@@ -57,7 +78,7 @@ public interface Scheduler {
* @param unit the unit of time for {@code time}
* @return this builder, for chaining
*/
TaskBuilder repeat(@IntRange(from = 0) long time, TimeUnit unit);
TaskBuilder repeat(@IntRange(from = 0) long time, @NotNull TimeUnit unit);
/**
* Specifies that the task should continue running after waiting for the specified amount, until
@@ -66,7 +87,7 @@ public interface Scheduler {
* @param duration the duration of the delay
* @return this builder, for chaining
*/
default TaskBuilder repeat(Duration duration) {
default TaskBuilder repeat(@NotNull Duration duration) {
return repeat(duration.toMillis(), TimeUnit.MILLISECONDS);
}