Skip to content

Commit

Permalink
chore: make scheduler methods return BukkitTask and add NonNull Annot…
Browse files Browse the repository at this point in the history
…ation (#254)
  • Loading branch information
Snabeldier authored Dec 22, 2024
1 parent bb5b1f8 commit d8b23f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
50 changes: 16 additions & 34 deletions src/main/java/minevalley/core/api/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.bukkit.metadata.MetadataValue;
import org.bukkit.metadata.Metadatable;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;

import javax.annotation.Nonnull;
Expand All @@ -72,7 +73,7 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
@SuppressWarnings({"unused", "UnusedReturnValue"})
public final class Core {

private static CoreServer server;
Expand All @@ -81,56 +82,36 @@ public static JavaPlugin getInstance() {
return server.getInstance();
}

public static int runSyncTaskNow(Runnable runnable) {
@Nonnull
public static BukkitTask runSyncTaskNow(@Nonnull Runnable runnable) {
return server.runSyncTaskLater(0, runnable);
}

public static int runAsyncTaskNow(Runnable runnable) {
@Nonnull
public static BukkitTask runAsyncTaskNow(@Nonnull Runnable runnable) {
return server.runAsyncTaskLater(0, runnable);
}

public static int runSyncTaskLater(long delay, Runnable runnable) {
@Nonnull
public static BukkitTask runSyncTaskLater(long delay, @Nonnull Runnable runnable) {
return server.runSyncTaskLater(delay, runnable);
}

public static int runAsyncTaskLater(long delay, Runnable runnable) {
@Nonnull
public static BukkitTask runAsyncTaskLater(long delay, @Nonnull Runnable runnable) {
return server.runAsyncTaskLater(delay, runnable);
}

public static int runSyncTaskPeriodically(long delay, long period, Runnable runnable) {
@Nonnull
public static BukkitTask runSyncTaskPeriodically(long delay, long period, @Nonnull Runnable runnable) {
return server.runSyncTaskPeriodically(delay, period, runnable);
}

public static int runAsyncTaskPeriodically(long delay, long period, Runnable runnable) {
@Nonnull
public static BukkitTask runAsyncTaskPeriodically(long delay, long period, @Nonnull Runnable runnable) {
return server.runAsyncTaskPeriodically(delay, period, runnable);
}

/**
* Check if the task currently running.
* <p>
* A repeating task might not be running currently, but will be running in
* the future. A task that has finished, and does not repeat, will not be
* running ever again.
* <p>
* Explicitly, a task is running if there exists a thread for it, and that
* thread is alive.
*
* @param taskId The task to check.
* @return If the task is currently running.
*/
public static boolean isCurrentlyRunning(int taskId) {
return server.isCurrentlyRunning(taskId);
}

/**
* Removes task from scheduler.
*
* @param taskId Id number of task to be removed
*/
public static void cancelTask(int taskId) {
server.cancelTask(taskId);
}

public static void registerListener(Class<? extends Event> cls, EventListener<? extends Event> listener) {
server.registerListener(cls, listener);
}
Expand Down Expand Up @@ -198,11 +179,12 @@ public static String getName(@Nonnull UUID uniqueId) {
* <br>
* <strong>Note:</strong> If no player is found, this method returns null.
* <p>
* <strong>Runtime Optimization</strong>
* <strong>Runtime Optimization</strong>
* <ul>
* <li>This method uses an internal cache</li>
* <li>If the player is online, no use of the mojang api is taken</li>
* </p>
*
* @param name name of the player
* @return unique id as UUID
*/
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/minevalley/core/api/CoreServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.bukkit.metadata.MetadataValue;
import org.bukkit.metadata.Metadatable;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;

import java.time.DayOfWeek;
Expand All @@ -69,17 +70,17 @@ public interface CoreServer {

JavaPlugin getInstance();

int runSyncTaskLater(long delay, Runnable runnable);
@NonNull
BukkitTask runSyncTaskLater(long delay, @NonNull Runnable runnable);

int runAsyncTaskLater(long delay, Runnable runnable);
@NonNull
BukkitTask runAsyncTaskLater(long delay, @NonNull Runnable runnable);

int runSyncTaskPeriodically(long delay, long period, Runnable runnable);
@NonNull
BukkitTask runSyncTaskPeriodically(long delay, long period, @NonNull Runnable runnable);

int runAsyncTaskPeriodically(long delay, long period, Runnable runnable);

boolean isCurrentlyRunning(int taskId);

void cancelTask(int taskId);
@NonNull
BukkitTask runAsyncTaskPeriodically(long delay, long period, @NonNull Runnable runnable);

void registerListener(Class<? extends Event> cls, EventListener<? extends Event> listener);

Expand Down

0 comments on commit d8b23f9

Please sign in to comment.