diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java b/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java index e3e18b04b..b558aee4f 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java @@ -64,10 +64,8 @@ import com.plotsquared.core.util.RegionManager; import com.plotsquared.core.util.SchematicHandler; import com.plotsquared.core.util.SetupUtils; import com.plotsquared.core.util.WorldUtil; -import com.plotsquared.core.util.task.TaskTime; import com.sk89q.worldedit.bukkit.WorldEditPlugin; import com.sk89q.worldedit.extension.platform.Actor; -import io.papermc.lib.PaperLib; import lombok.RequiredArgsConstructor; import org.bukkit.Bukkit; import org.bukkit.command.ConsoleCommandSender; diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ChunkListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ChunkListener.java index 7f7143758..bf756fcf0 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ChunkListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ChunkListener.java @@ -60,6 +60,7 @@ import org.slf4j.LoggerFactory; import javax.annotation.Nonnull; import java.lang.reflect.Method; import java.util.HashSet; +import java.util.Objects; import static com.plotsquared.core.util.ReflectionUtils.getRefClass; @@ -257,15 +258,13 @@ public class ChunkListener implements Listener { final Integer currentIndex = TaskManager.index.get(); PlotSquaredTask task = TaskManager.runTaskRepeat(() -> { if (!chunk.isLoaded()) { - TaskManager.tasks.get(currentIndex).cancel(); - TaskManager.tasks.remove(currentIndex); + Objects.requireNonNull(TaskManager.removeTask(currentIndex)).cancel(); chunk.unload(true); return; } BlockState[] tiles = chunk.getTileEntities(); if (tiles.length == 0) { - TaskManager.tasks.get(currentIndex).cancel(); - TaskManager.tasks.remove(currentIndex); + Objects.requireNonNull(TaskManager.removeTask(currentIndex)).cancel(); chunk.unload(true); return; } @@ -273,8 +272,7 @@ public class ChunkListener implements Listener { int i = 0; while (System.currentTimeMillis() - start < 250) { if (i >= tiles.length - Settings.Chunk_Processor.MAX_TILES) { - TaskManager.tasks.get(currentIndex).cancel(); - TaskManager.tasks.remove(currentIndex); + Objects.requireNonNull(TaskManager.removeTask(currentIndex)).cancel(); chunk.unload(true); return; } @@ -282,7 +280,7 @@ public class ChunkListener implements Listener { i++; } }, TaskTime.ticks(5L)); - TaskManager.tasks.put(currentIndex, task); + TaskManager.addTask(task, currentIndex); } public boolean processChunk(Chunk chunk, boolean unload) { diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java index 61e40742f..d85afbbef 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java @@ -842,7 +842,7 @@ import java.util.regex.Pattern; Player player = event.getPlayer(); BukkitPlayer pp = BukkitUtil.getPlayer(player); // Cancel teleport - if (TaskManager.TELEPORT_QUEUE.remove(pp.getName())) { + if (TaskManager.removeFromTeleportQueue(pp.getName())) { MainUtil.sendMessage(pp, Captions.TELEPORT_FAILED); } // Set last location @@ -904,7 +904,7 @@ import java.util.regex.Pattern; Player player = event.getPlayer(); BukkitPlayer pp = BukkitUtil.getPlayer(player); // Cancel teleport - if (TaskManager.TELEPORT_QUEUE.remove(pp.getName())) { + if (TaskManager.removeFromTeleportQueue(pp.getName())) { MainUtil.sendMessage(pp, Captions.TELEPORT_FAILED); } // Set last location @@ -2417,7 +2417,7 @@ import java.util.regex.Pattern; } @EventHandler(priority = EventPriority.MONITOR) public void onLeave(PlayerQuitEvent event) { - TaskManager.TELEPORT_QUEUE.remove(event.getPlayer().getName()); + TaskManager.removeFromTeleportQueue(event.getPlayer().getName()); BukkitPlayer pp = BukkitUtil.getPlayer(event.getPlayer()); pp.unregister(); this.logout(pp.getUUID()); diff --git a/Core/src/main/java/com/plotsquared/core/command/Trim.java b/Core/src/main/java/com/plotsquared/core/command/Trim.java index 18aa48475..3afe16c56 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Trim.java +++ b/Core/src/main/java/com/plotsquared/core/command/Trim.java @@ -121,7 +121,7 @@ public class Trim extends SubCommand { } } }).thenAccept(ignore -> - TaskManager.getPlatformImplementation().taskLater(result, TaskTime.ticks(1))); + TaskManager.getPlatformImplementation().taskLater(result, TaskTime.ticks(1L))); return true; } @@ -196,7 +196,7 @@ public class Trim extends SubCommand { queue.regenChunk(value.getX(), value.getZ()); } }).thenAccept(ignore -> TaskManager.getPlatformImplementation() - .taskLater(this, TaskTime.ticks(1))); + .taskLater(this, TaskTime.ticks(1L))); } }; } else { diff --git a/Core/src/main/java/com/plotsquared/core/plot/Plot.java b/Core/src/main/java/com/plotsquared/core/plot/Plot.java index 6b5d9d723..93084adee 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/Plot.java +++ b/Core/src/main/java/com/plotsquared/core/plot/Plot.java @@ -2996,9 +2996,9 @@ public class Plot { MainUtil .sendMessage(player, Captions.TELEPORT_IN_SECONDS, Settings.Teleport.DELAY + ""); final String name = player.getName(); - TaskManager.TELEPORT_QUEUE.add(name); + TaskManager.addToTeleportQueue(name); TaskManager.runTaskLater(() -> { - if (!TaskManager.TELEPORT_QUEUE.remove(name)) { + if (!TaskManager.removeFromTeleportQueue(name)) { return; } if (player.isOnline()) { diff --git a/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java b/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java index 0835ba8b1..68f2c4b89 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java +++ b/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java @@ -377,7 +377,7 @@ public class ExpireManager { ExpireManager.this.running = 2; runTask(expiredTask); } - }, TaskTime.ticks(86400000)); + }, TaskTime.ticks(86400000L)); } else { TaskManager.runTaskLaterAsync(task, TaskTime.seconds(10L)); } diff --git a/Core/src/main/java/com/plotsquared/core/util/RegionManager.java b/Core/src/main/java/com/plotsquared/core/util/RegionManager.java index 3afb51724..0c7d9fc9f 100644 --- a/Core/src/main/java/com/plotsquared/core/util/RegionManager.java +++ b/Core/src/main/java/com/plotsquared/core/util/RegionManager.java @@ -101,7 +101,7 @@ public abstract class RegionManager { .thenRun(() -> task.run(value)); } }).thenAccept(ignore -> - TaskManager.getPlatformImplementation().taskLater(whenDone, TaskTime.ticks(1))); + TaskManager.getPlatformImplementation().taskLater(whenDone, TaskTime.ticks(1L))); }); } diff --git a/Core/src/main/java/com/plotsquared/core/util/task/TaskManager.java b/Core/src/main/java/com/plotsquared/core/util/task/TaskManager.java index fdc04b620..9a120e297 100644 --- a/Core/src/main/java/com/plotsquared/core/util/task/TaskManager.java +++ b/Core/src/main/java/com/plotsquared/core/util/task/TaskManager.java @@ -35,6 +35,8 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.Map; +import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; @@ -51,12 +53,52 @@ import java.util.concurrent.atomic.AtomicInteger; */ public abstract class TaskManager { - public static final HashSet TELEPORT_QUEUE = new HashSet<>(); - public static final HashMap tasks = new HashMap<>(); + private static final Set teleportQueue = new HashSet<>(); + private static final Map tasks = new HashMap<>(); + public static AtomicInteger index = new AtomicInteger(0); @Getter @Setter private static TaskManager platformImplementation; + /** + * Add a string to the teleport queue + * + * @param string String to add + */ + public static void addToTeleportQueue(@Nonnull final String string) { + teleportQueue.add(string); + } + + /** + * Remove a string from the teleport queue + * + * @param string String to remove + * return {@code true} if the value was stored in the map, or {@code false} + */ + public static boolean removeFromTeleportQueue(@Nonnull final String string) { + return teleportQueue.remove(string); + } + + /** + * Add a task to the task map + * + * @param task Task + * @param id Task ID + */ + public static void addTask(@Nonnull final PlotSquaredTask task, final int id) { + tasks.put(id, task); + } + + /** + * Remove a task from the task map and return the stored value + * + * @param id Task ID + * @return Task if stored, or {@code null} + */ + @Nullable public static PlotSquaredTask removeTask(final int id) { + return tasks.get(id); + } + /** * Run a repeating synchronous task. If using a platform scheduler, * this is guaranteed to run on the server thread