mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 22:16:34 +00:00
Use UUIDs for task tracking
This commit is contained in:
8 files changed
+47
-45
No files matched your search
@@ -81,7 +81,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
|||||||
* Metrics ID for <a href="https://bstats.org/plugin/bukkit/HuskHomes/8430">HuskHomes on Bukkit</a>.
|
* Metrics ID for <a href="https://bstats.org/plugin/bukkit/HuskHomes/8430">HuskHomes on Bukkit</a>.
|
||||||
*/
|
*/
|
||||||
private static final int METRICS_ID = 8430;
|
private static final int METRICS_ID = 8430;
|
||||||
private ConcurrentHashMap<Integer, ScheduledTask> tasks;
|
private ConcurrentHashMap<UUID, ScheduledTask> tasks;
|
||||||
private Set<SavedUser> savedUsers;
|
private Set<SavedUser> savedUsers;
|
||||||
private Settings settings;
|
private Settings settings;
|
||||||
private Locales locales;
|
private Locales locales;
|
||||||
@@ -493,7 +493,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
public ConcurrentHashMap<Integer, ScheduledTask> getTasks() {
|
public ConcurrentHashMap<UUID, ScheduledTask> getTasks() {
|
||||||
return tasks;
|
return tasks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import space.arim.morepaperlib.scheduling.ScheduledTask;
|
|||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
@@ -48,9 +49,10 @@ public interface BukkitTaskRunner extends TaskRunner {
|
|||||||
getScheduler().globalRegionalScheduler().run(runnable);
|
getScheduler().globalRegionalScheduler().run(runnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
default int runAsyncRepeating(@NotNull Runnable runnable, long period) {
|
default UUID runAsyncRepeating(@NotNull Runnable runnable, long period) {
|
||||||
final int taskId = getNextTaskId();
|
final UUID taskId = UUID.randomUUID();
|
||||||
getTasks().put(taskId, getScheduler().asyncScheduler().runAtFixedRate(
|
getTasks().put(taskId, getScheduler().asyncScheduler().runAtFixedRate(
|
||||||
runnable, Duration.ZERO, getDurationTicks(period))
|
runnable, Duration.ZERO, getDurationTicks(period))
|
||||||
);
|
);
|
||||||
@@ -63,11 +65,12 @@ public interface BukkitTaskRunner extends TaskRunner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default void cancelTask(int taskId) {
|
default void cancelTask(@NotNull UUID taskId) {
|
||||||
if (getTasks().containsKey(taskId)) {
|
getTasks().computeIfPresent(taskId, (id, task) -> {
|
||||||
getTasks().get(taskId).cancel();
|
task.cancel();
|
||||||
getTasks().remove(taskId);
|
return null;
|
||||||
}
|
});
|
||||||
|
getTasks().remove(taskId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -82,7 +85,7 @@ public interface BukkitTaskRunner extends TaskRunner {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
ConcurrentHashMap<Integer, ScheduledTask> getTasks();
|
ConcurrentHashMap<UUID, ScheduledTask> getTasks();
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
default Duration getDurationTicks(long ticks) {
|
default Duration getDurationTicks(long ticks) {
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ import net.william278.huskhomes.user.OnlineUser;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a {@link Teleport} that has an associated warmup time; the teleport will not be performed until the
|
* Represents a {@link Teleport} that has an associated warmup time; the teleport will not be performed until the
|
||||||
@@ -87,7 +88,7 @@ public class TimedTeleport extends Teleport {
|
|||||||
.ifPresent(teleporter::sendMessage);
|
.ifPresent(teleporter::sendMessage);
|
||||||
|
|
||||||
// Run the warmup
|
// Run the warmup
|
||||||
final AtomicInteger warmupTaskId = new AtomicInteger();
|
final AtomicReference<UUID> warmupTaskId = new AtomicReference<>();
|
||||||
final Runnable countdownRunnable = (() -> {
|
final Runnable countdownRunnable = (() -> {
|
||||||
// Display a countdown action bar message
|
// Display a countdown action bar message
|
||||||
if (timeLeft > 0) {
|
if (timeLeft > 0) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ package net.william278.huskhomes.util;
|
|||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
@@ -34,25 +35,17 @@ public interface TaskRunner {
|
|||||||
|
|
||||||
void runSync(@NotNull Runnable runnable);
|
void runSync(@NotNull Runnable runnable);
|
||||||
|
|
||||||
int runAsyncRepeating(@NotNull Runnable runnable, long delay);
|
@NotNull
|
||||||
|
UUID runAsyncRepeating(@NotNull Runnable runnable, long delay);
|
||||||
|
|
||||||
void runLater(@NotNull Runnable runnable, long delay);
|
void runLater(@NotNull Runnable runnable, long delay);
|
||||||
|
|
||||||
void cancelTask(int taskId);
|
void cancelTask(@NotNull UUID taskId);
|
||||||
|
|
||||||
void cancelAllTasks();
|
void cancelAllTasks();
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
ConcurrentHashMap<Integer, ?> getTasks();
|
ConcurrentHashMap<UUID, ?> getTasks();
|
||||||
|
|
||||||
default int getNextTaskId() {
|
|
||||||
int taskId = 0;
|
|
||||||
while (getTasks().containsKey(taskId)) {
|
|
||||||
taskId++;
|
|
||||||
}
|
|
||||||
return taskId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
HuskHomes getPlugin();
|
HuskHomes getPlugin();
|
||||||
|
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
|
|||||||
private final ModContainer modContainer = FabricLoader.getInstance()
|
private final ModContainer modContainer = FabricLoader.getInstance()
|
||||||
.getModContainer("huskhomes").orElseThrow(() -> new RuntimeException("Failed to get Mod Container"));
|
.getModContainer("huskhomes").orElseThrow(() -> new RuntimeException("Failed to get Mod Container"));
|
||||||
private MinecraftServer minecraftServer;
|
private MinecraftServer minecraftServer;
|
||||||
private ConcurrentHashMap<Integer, CompletableFuture<?>> tasks;
|
private ConcurrentHashMap<UUID, CompletableFuture<?>> tasks;
|
||||||
private Map<String, Boolean> permissions;
|
private Map<String, Boolean> permissions;
|
||||||
private Set<SavedUser> savedUsers;
|
private Set<SavedUser> savedUsers;
|
||||||
private Settings settings;
|
private Settings settings;
|
||||||
@@ -481,7 +481,7 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
public ConcurrentHashMap<Integer, CompletableFuture<?>> getTasks() {
|
public ConcurrentHashMap<UUID, CompletableFuture<?>> getTasks() {
|
||||||
return tasks;
|
return tasks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,12 +22,12 @@ package net.william278.huskhomes.util;
|
|||||||
import net.william278.huskhomes.FabricHuskHomes;
|
import net.william278.huskhomes.FabricHuskHomes;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public interface FabricTaskRunner extends TaskRunner {
|
public interface FabricTaskRunner extends TaskRunner {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default void runAsync(@NotNull Runnable runnable) {
|
default void runAsync(@NotNull Runnable runnable) {
|
||||||
CompletableFuture.runAsync(runnable, getPlugin().getMinecraftServer());
|
CompletableFuture.runAsync(runnable, getPlugin().getMinecraftServer());
|
||||||
@@ -43,9 +43,10 @@ public interface FabricTaskRunner extends TaskRunner {
|
|||||||
getPlugin().getMinecraftServer().executeSync(runnable);
|
getPlugin().getMinecraftServer().executeSync(runnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
default int runAsyncRepeating(@NotNull Runnable runnable, long delay) {
|
default UUID runAsyncRepeating(@NotNull Runnable runnable, long delay) {
|
||||||
final int taskId = getNextTaskId();
|
final UUID taskId = UUID.randomUUID();
|
||||||
final CompletableFuture<?> future = new CompletableFuture<>();
|
final CompletableFuture<?> future = new CompletableFuture<>();
|
||||||
|
|
||||||
final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||||
@@ -75,11 +76,12 @@ public interface FabricTaskRunner extends TaskRunner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default void cancelTask(int taskId) {
|
default void cancelTask(@NotNull UUID taskId) {
|
||||||
if (getTasks().containsKey(taskId)) {
|
getTasks().computeIfPresent(taskId, (uuid, completableFuture) -> {
|
||||||
getTasks().get(taskId).cancel(true);
|
completableFuture.cancel(true);
|
||||||
getTasks().remove(taskId);
|
return null;
|
||||||
}
|
});
|
||||||
|
getTasks().remove(taskId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -90,7 +92,7 @@ public interface FabricTaskRunner extends TaskRunner {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
ConcurrentHashMap<Integer, CompletableFuture<?>> getTasks();
|
ConcurrentHashMap<UUID, CompletableFuture<?>> getTasks();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ public class SpongeHuskHomes implements HuskHomes, SpongeTaskRunner, SpongeSafet
|
|||||||
@Inject
|
@Inject
|
||||||
private Metrics.Factory metricsFactory;
|
private Metrics.Factory metricsFactory;
|
||||||
|
|
||||||
private ConcurrentHashMap<Integer, CancellableRunnable> tasks;
|
private ConcurrentHashMap<UUID, CancellableRunnable> tasks;
|
||||||
private Set<SavedUser> savedUsers;
|
private Set<SavedUser> savedUsers;
|
||||||
private Settings settings;
|
private Settings settings;
|
||||||
private Locales locales;
|
private Locales locales;
|
||||||
@@ -518,7 +518,7 @@ public class SpongeHuskHomes implements HuskHomes, SpongeTaskRunner, SpongeSafet
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public ConcurrentHashMap<Integer, CancellableRunnable> getTasks() {
|
public ConcurrentHashMap<UUID, CancellableRunnable> getTasks() {
|
||||||
return tasks;
|
return tasks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.spongepowered.api.scheduler.Task;
|
import org.spongepowered.api.scheduler.Task;
|
||||||
import org.spongepowered.api.util.Ticks;
|
import org.spongepowered.api.util.Ticks;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
@@ -60,9 +61,10 @@ public interface SpongeTaskRunner extends TaskRunner {
|
|||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
default int runAsyncRepeating(@NotNull Runnable runnable, long delay) {
|
default UUID runAsyncRepeating(@NotNull Runnable runnable, long delay) {
|
||||||
final int taskId = getNextTaskId();
|
final UUID taskId = UUID.randomUUID();
|
||||||
final CancellableRunnable task = wrap(runnable);
|
final CancellableRunnable task = wrap(runnable);
|
||||||
getTasks().put(taskId, task);
|
getTasks().put(taskId, task);
|
||||||
getPlugin().getGame().asyncScheduler()
|
getPlugin().getGame().asyncScheduler()
|
||||||
@@ -86,11 +88,12 @@ public interface SpongeTaskRunner extends TaskRunner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default void cancelTask(int taskId) {
|
default void cancelTask(@NotNull UUID taskId) {
|
||||||
if (getTasks().containsKey(taskId)) {
|
getTasks().computeIfPresent(taskId, (id, task) -> {
|
||||||
getTasks().get(taskId).cancel();
|
task.cancel();
|
||||||
getTasks().remove(taskId);
|
return null;
|
||||||
}
|
});
|
||||||
|
getTasks().remove(taskId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -106,7 +109,7 @@ public interface SpongeTaskRunner extends TaskRunner {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
ConcurrentHashMap<Integer, CancellableRunnable> getTasks();
|
ConcurrentHashMap<UUID, CancellableRunnable> getTasks();
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in new issue
Block a user