From d0308a1bbf73fb15576976678ff42bc734d6c542 Mon Sep 17 00:00:00 2001 From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> Date: Thu, 5 Dec 2024 04:02:05 -0800 Subject: [PATCH 1/6] Folia support --- .../api/concurrent/ChunkTeleport.java | 38 +++++----- .../api/concurrent/count/TickResetCount.java | 17 ++--- .../notifications/BossBarNotification.java | 11 ++- .../api/listeners/InsightsListener.java | 15 ++-- .../insights/api/tasks/ScanTask.java | 16 +++-- .../insights/api/utils/ClassUtils.java | 15 ++++ .../insights/api/utils/SchedulingUtils.java | 61 ++++++++++++++++ Insights/build.gradle.kts | 1 + .../dev/frankheijden/insights/Insights.java | 15 ++-- .../insights/listeners/BlockListener.java | 2 +- .../insights/tasks/PlayerTrackerTask.java | 72 ++++++++++--------- 11 files changed, 183 insertions(+), 80 deletions(-) create mode 100644 Insights-API/src/main/java/dev/frankheijden/insights/api/utils/ClassUtils.java create mode 100644 Insights-API/src/main/java/dev/frankheijden/insights/api/utils/SchedulingUtils.java diff --git a/Insights-API/src/main/java/dev/frankheijden/insights/api/concurrent/ChunkTeleport.java b/Insights-API/src/main/java/dev/frankheijden/insights/api/concurrent/ChunkTeleport.java index 4ab382e4..0a27009f 100644 --- a/Insights-API/src/main/java/dev/frankheijden/insights/api/concurrent/ChunkTeleport.java +++ b/Insights-API/src/main/java/dev/frankheijden/insights/api/concurrent/ChunkTeleport.java @@ -1,6 +1,7 @@ package dev.frankheijden.insights.api.concurrent; import dev.frankheijden.insights.api.InsightsPlugin; +import dev.frankheijden.insights.api.utils.SchedulingUtils; import io.papermc.lib.PaperLib; import java.util.concurrent.CompletableFuture; import org.bukkit.HeightMap; @@ -21,28 +22,29 @@ public ChunkTeleport(InsightsPlugin plugin) { */ public CompletableFuture teleportPlayerToChunk(Player player, World world, int x, int z, boolean gen) { CompletableFuture resultFuture = new CompletableFuture<>(); - PaperLib.getChunkAtAsync(world, x, z, gen).whenComplete((chunk, chunkErr) -> { - if (chunkErr != null) { - resultFuture.completeExceptionally(chunkErr); - return; - } else if (chunk == null) { - resultFuture.complete(Result.NOT_GENERATED); - return; - } - - plugin.getServer().getScheduler().runTask(plugin, () -> { + SchedulingUtils.runImmediatelyAtChunk(plugin, world, x, z, () -> { + PaperLib.getChunkAtAsync(world, x, z, gen).whenComplete((chunk, chunkErr) -> { + if (chunkErr != null) { + resultFuture.completeExceptionally(chunkErr); + return; + } else if (chunk == null) { + resultFuture.complete(Result.NOT_GENERATED); + return; + } int blockX = (x << 4) + 8; int blockZ = (z << 4) + 8; int blockY = world.getHighestBlockYAt(blockX, blockZ, HeightMap.MOTION_BLOCKING) + 1; var loc = new Location(world, blockX + .5, blockY, blockZ + .5); - PaperLib.teleportAsync(player, loc).whenComplete((success, tpErr) -> { - if (tpErr != null) { - resultFuture.completeExceptionally(tpErr); - } else if (Boolean.FALSE.equals(success)) { - resultFuture.complete(Result.FAILED); - } else { - resultFuture.complete(Result.SUCCESS); - } + SchedulingUtils.runImmediatelyAtEntityIfFolia(plugin, player, () -> { + PaperLib.teleportAsync(player, loc).whenComplete((success, tpErr) -> { + if (tpErr != null) { + resultFuture.completeExceptionally(tpErr); + } else if (Boolean.FALSE.equals(success)) { + resultFuture.complete(Result.FAILED); + } else { + resultFuture.complete(Result.SUCCESS); + } + }); }); }); }); diff --git a/Insights-API/src/main/java/dev/frankheijden/insights/api/concurrent/count/TickResetCount.java b/Insights-API/src/main/java/dev/frankheijden/insights/api/concurrent/count/TickResetCount.java index 7a7dc278..6a25b7a8 100644 --- a/Insights-API/src/main/java/dev/frankheijden/insights/api/concurrent/count/TickResetCount.java +++ b/Insights-API/src/main/java/dev/frankheijden/insights/api/concurrent/count/TickResetCount.java @@ -1,7 +1,8 @@ package dev.frankheijden.insights.api.concurrent.count; import dev.frankheijden.insights.api.InsightsPlugin; -import org.bukkit.scheduler.BukkitTask; +import dev.frankheijden.insights.api.utils.SchedulingUtils; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -12,7 +13,7 @@ public class TickResetCount { private final int size; private final Map counts; private final ResetTask resetTask; - private BukkitTask bukkitTask = null; + private ScheduledTask scheduledTask = null; /** * Constructs a new TickResetCount. @@ -30,14 +31,14 @@ public TickResetCount(InsightsPlugin plugin, int intervalTicks, int size) { * @throws IllegalStateException If the task is already running. */ public void start() { - if (bukkitTask != null) { + if (scheduledTask != null) { throw new IllegalStateException("ResetTask is already running!"); } - this.bukkitTask = plugin.getServer().getScheduler().runTaskTimerAsynchronously( + this.scheduledTask = SchedulingUtils.runTaskTimerAsynchronously( plugin, resetTask, - 0, + 1L, intervalTicks ); } @@ -47,12 +48,12 @@ public void start() { * @throws IllegalStateException If the task is not running. */ public void stop() { - if (bukkitTask == null) { + if (scheduledTask == null) { throw new IllegalStateException("ResetTask is not running!"); } - this.bukkitTask.cancel(); - this.bukkitTask = null; + this.scheduledTask.cancel(); + this.scheduledTask = null; } public int getCurrentTick() { diff --git a/Insights-API/src/main/java/dev/frankheijden/insights/api/config/notifications/BossBarNotification.java b/Insights-API/src/main/java/dev/frankheijden/insights/api/config/notifications/BossBarNotification.java index 0ad58232..81d320b4 100644 --- a/Insights-API/src/main/java/dev/frankheijden/insights/api/config/notifications/BossBarNotification.java +++ b/Insights-API/src/main/java/dev/frankheijden/insights/api/config/notifications/BossBarNotification.java @@ -2,12 +2,13 @@ import dev.frankheijden.insights.api.InsightsPlugin; import dev.frankheijden.insights.api.config.Messages; +import dev.frankheijden.insights.api.utils.SchedulingUtils; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.bossbar.BossBar; import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitTask; import java.util.LinkedList; import java.util.Queue; @@ -20,7 +21,7 @@ public class BossBarNotification implements Notification { protected final Queue viewers = new LinkedList<>(); protected final int ticks; protected final Runnable bossBarClearer; - protected BukkitTask task; + protected ScheduledTask task; protected BossBarNotification(InsightsPlugin plugin, BossBar bossBar, Messages.Message content, int ticks) { this.plugin = plugin; @@ -55,7 +56,11 @@ public void send() { audience.showBossBar(bossBar); viewers.add(audience); } - task = Bukkit.getScheduler().runTaskLater(plugin, bossBarClearer, ticks); + task = Bukkit.getGlobalRegionScheduler().runDelayed( + plugin, + SchedulingUtils.wrapRunnable(bossBarClearer), + ticks + ); } }; } diff --git a/Insights-API/src/main/java/dev/frankheijden/insights/api/listeners/InsightsListener.java b/Insights-API/src/main/java/dev/frankheijden/insights/api/listeners/InsightsListener.java index 2477a29e..05c30133 100644 --- a/Insights-API/src/main/java/dev/frankheijden/insights/api/listeners/InsightsListener.java +++ b/Insights-API/src/main/java/dev/frankheijden/insights/api/listeners/InsightsListener.java @@ -17,6 +17,7 @@ import dev.frankheijden.insights.api.objects.wrappers.ScanObject; import dev.frankheijden.insights.api.tasks.ScanTask; import dev.frankheijden.insights.api.utils.ChunkUtils; +import dev.frankheijden.insights.api.utils.SchedulingUtils; import dev.frankheijden.insights.api.utils.StringUtils; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.Chunk; @@ -57,12 +58,14 @@ protected void handleModification(Location location, Material material, int amou } protected void handleModification(Location location, Consumer storageConsumer) { - UUID worldUid = location.getWorld().getUID(); - long chunkKey = ChunkUtils.getKey(location); - plugin.getWorldStorage().getWorld(worldUid).get(chunkKey).ifPresent(storageConsumer); - plugin.getAddonManager().getRegion(location) - .flatMap(region -> plugin.getAddonStorage().get(region.getKey())) - .ifPresent(storageConsumer); + SchedulingUtils.runImmediatelyAtLocationIfFolia(plugin, location, () -> { + UUID worldUid = location.getWorld().getUID(); + long chunkKey = ChunkUtils.getKey(location); + plugin.getWorldStorage().getWorld(worldUid).get(chunkKey).ifPresent(storageConsumer); + plugin.getAddonManager().getRegion(location) + .flatMap(region -> plugin.getAddonStorage().get(region.getKey())) + .ifPresent(storageConsumer); + }); } protected void handleModification(Location location, Material from, Material to, int amount) { diff --git a/Insights-API/src/main/java/dev/frankheijden/insights/api/tasks/ScanTask.java b/Insights-API/src/main/java/dev/frankheijden/insights/api/tasks/ScanTask.java index fa769195..d45513d0 100644 --- a/Insights-API/src/main/java/dev/frankheijden/insights/api/tasks/ScanTask.java +++ b/Insights-API/src/main/java/dev/frankheijden/insights/api/tasks/ScanTask.java @@ -12,11 +12,12 @@ import dev.frankheijden.insights.api.objects.wrappers.ScanObject; import dev.frankheijden.insights.api.util.TriConsumer; import dev.frankheijden.insights.api.utils.EnumUtils; +import dev.frankheijden.insights.api.utils.SchedulingUtils; import dev.frankheijden.insights.api.utils.StringUtils; +import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import net.kyori.adventure.text.Component; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitScheduler; -import org.bukkit.scheduler.BukkitTask; import java.time.Duration; import java.util.Comparator; import java.util.Iterator; @@ -51,7 +52,7 @@ public class ScanTask implements Runnable { private final AtomicBoolean completedExceptionally = new AtomicBoolean(); private final int chunkCount; private long lastInfo = 0; - private BukkitTask task; + private ScheduledTask task; /** * Creates a new ScanTask to scan a collection of ChunkPart's. @@ -370,8 +371,13 @@ public static void scanAndDisplayGroupedByChunk( } private void start() { - BukkitScheduler scheduler = plugin.getServer().getScheduler(); - task = scheduler.runTaskTimer(plugin, this, 0, plugin.getSettings().SCANS_ITERATION_INTERVAL_TICKS); + GlobalRegionScheduler scheduler = plugin.getServer().getGlobalRegionScheduler(); + task = scheduler.runAtFixedRate( + plugin, + SchedulingUtils.wrapRunnable(this), + 1L, + plugin.getSettings().SCANS_ITERATION_INTERVAL_TICKS + ); } private void cancel() { diff --git a/Insights-API/src/main/java/dev/frankheijden/insights/api/utils/ClassUtils.java b/Insights-API/src/main/java/dev/frankheijden/insights/api/utils/ClassUtils.java new file mode 100644 index 00000000..5d68bb61 --- /dev/null +++ b/Insights-API/src/main/java/dev/frankheijden/insights/api/utils/ClassUtils.java @@ -0,0 +1,15 @@ +package dev.frankheijden.insights.api.utils; + +public class ClassUtils { + + private ClassUtils() {} + + public static boolean isClassLoaded(String className) { + try { + Class.forName(className); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } +} diff --git a/Insights-API/src/main/java/dev/frankheijden/insights/api/utils/SchedulingUtils.java b/Insights-API/src/main/java/dev/frankheijden/insights/api/utils/SchedulingUtils.java new file mode 100644 index 00000000..65e25418 --- /dev/null +++ b/Insights-API/src/main/java/dev/frankheijden/insights/api/utils/SchedulingUtils.java @@ -0,0 +1,61 @@ +package dev.frankheijden.insights.api.utils; + +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.Plugin; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +public class SchedulingUtils { + public static final boolean IS_FOLIA = ClassUtils.isClassLoaded( + "io.papermc.paper.threadedregions.RegionizedServer" + ); + public static final long TICK_TO_MILLISECONDS = 50L; + private static final long FOLIA_MINIMUM_DELAY = 1L; + + public static void runImmediatelyAtEntityIfFolia(Plugin plugin, Entity entity, Runnable task) { + if (IS_FOLIA) { + entity.getScheduler().execute(plugin, task, null, FOLIA_MINIMUM_DELAY); + } else { + task.run(); + } + } + + public static void runImmediatelyAtChunkIfFolia(Plugin plugin, World world, int chunkX, int chunkZ, Runnable task) { + if (IS_FOLIA) { + plugin.getServer().getRegionScheduler().execute(plugin, world, chunkX, chunkZ, task); + } else { + task.run(); + } + } + + public static void runImmediatelyAtChunk(Plugin plugin, World world, int chunkX, int chunkZ, Runnable task) { + plugin.getServer().getRegionScheduler().execute(plugin, world, chunkX, chunkZ, task); + } + + public static void runImmediatelyAtLocationIfFolia(Plugin plugin, Location location, Runnable task) { + if (IS_FOLIA) { + plugin.getServer().getRegionScheduler().execute(plugin, location, task); + } else { + task.run(); + } + } + + public static ScheduledTask runTaskTimerAsynchronously( + Plugin plugin, Runnable runnable, long delayTicks, long periodTicks + ) { + return plugin.getServer().getAsyncScheduler().runAtFixedRate( + plugin, + wrapRunnable(runnable), + delayTicks * TICK_TO_MILLISECONDS, + periodTicks * TICK_TO_MILLISECONDS, + TimeUnit.MILLISECONDS + ); + } + + public static Consumer wrapRunnable(Runnable runnable) { + return task -> runnable.run(); + } +} diff --git a/Insights/build.gradle.kts b/Insights/build.gradle.kts index a4387ce3..bfe41c8f 100644 --- a/Insights/build.gradle.kts +++ b/Insights/build.gradle.kts @@ -36,6 +36,7 @@ bukkit { website = "https://github.com/InsightsPlugin/Insights" softDepend = listOf("PlaceholderAPI") authors = listOf("FrankHeijden") + foliaSupported = true permissions { register("insights.info") { description = "Allows you to see information about insights" diff --git a/Insights/src/main/java/dev/frankheijden/insights/Insights.java b/Insights/src/main/java/dev/frankheijden/insights/Insights.java index 5cd57c09..2805053e 100644 --- a/Insights/src/main/java/dev/frankheijden/insights/Insights.java +++ b/Insights/src/main/java/dev/frankheijden/insights/Insights.java @@ -23,6 +23,7 @@ import dev.frankheijden.insights.api.objects.wrappers.ScanObject; import dev.frankheijden.insights.api.tasks.UpdateCheckerTask; import dev.frankheijden.insights.api.utils.IOUtils; +import dev.frankheijden.insights.api.utils.SchedulingUtils; import dev.frankheijden.insights.commands.CommandCancelScan; import dev.frankheijden.insights.commands.CommandInsights; import dev.frankheijden.insights.commands.CommandScan; @@ -43,11 +44,11 @@ import dev.frankheijden.insights.tasks.PlayerTrackerTask; import io.leangen.geantyref.TypeToken; import io.papermc.lib.PaperLib; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.command.CommandSender; -import org.bukkit.scheduler.BukkitTask; import org.incendo.cloud.annotations.AnnotationParser; import org.incendo.cloud.execution.ExecutionCoordinator; import org.incendo.cloud.paper.PaperCommandManager; @@ -83,8 +84,8 @@ public class Insights extends InsightsPlugin { private ScanHistory scanHistory; private ListenerManager listenerManager; private InsightsPlaceholderExpansion placeholderExpansion; - private BukkitTask playerTracker = null; - private BukkitTask updateChecker = null; + private ScheduledTask playerTracker = null; + private ScheduledTask updateChecker = null; private BukkitAudiences audiences = null; private RedstoneUpdateCount redstoneUpdateCount = null; private ChunkTeleport chunkTeleport; @@ -117,13 +118,13 @@ public void onEnable() { ex.printStackTrace(); } - getServer().getScheduler().runTaskLater(this, () -> { + getServer().getGlobalRegionScheduler().runDelayed(this, task -> { try { addonManager.loadAddons(); } catch (IOException ex) { ex.printStackTrace(); } - }, 1); + }, 1L); playerList = new PlayerList(Bukkit.getOnlinePlayers()); worldStorage = new WorldStorage(); @@ -379,7 +380,7 @@ public void reload() { } if (settings.CHUNK_SCANS_MODE == Settings.ChunkScanMode.ALWAYS) { - playerTracker = getServer().getScheduler().runTaskTimerAsynchronously( + playerTracker = SchedulingUtils.runTaskTimerAsynchronously( this, new PlayerTrackerTask(this), 0, @@ -392,7 +393,7 @@ public void reload() { } if (settings.UPDATE_CHECKER_ENABLED) { - updateChecker = getServer().getScheduler().runTaskTimerAsynchronously( + updateChecker = SchedulingUtils.runTaskTimerAsynchronously( this, new UpdateCheckerTask(this), 20, diff --git a/Insights/src/main/java/dev/frankheijden/insights/listeners/BlockListener.java b/Insights/src/main/java/dev/frankheijden/insights/listeners/BlockListener.java index efd8624b..01b36d9a 100644 --- a/Insights/src/main/java/dev/frankheijden/insights/listeners/BlockListener.java +++ b/Insights/src/main/java/dev/frankheijden/insights/listeners/BlockListener.java @@ -338,7 +338,7 @@ private void handlePistonEvent(BlockPistonEvent event, List blocks) { materials[i] = material; } - plugin.getServer().getScheduler().runTaskLater(plugin, () -> { + plugin.getServer().getRegionScheduler().runDelayed(plugin, event.getBlock().getLocation(), task -> { for (var i = 0; i < blocks.size(); i++) { var relative = blocks.get(i).getRelative(event.getDirection()); var material = relative.getType(); diff --git a/Insights/src/main/java/dev/frankheijden/insights/tasks/PlayerTrackerTask.java b/Insights/src/main/java/dev/frankheijden/insights/tasks/PlayerTrackerTask.java index 9603f13a..12861442 100644 --- a/Insights/src/main/java/dev/frankheijden/insights/tasks/PlayerTrackerTask.java +++ b/Insights/src/main/java/dev/frankheijden/insights/tasks/PlayerTrackerTask.java @@ -4,6 +4,7 @@ import dev.frankheijden.insights.api.concurrent.ScanOptions; import dev.frankheijden.insights.api.objects.chunk.ChunkLocation; import dev.frankheijden.insights.api.tasks.InsightsAsyncTask; +import dev.frankheijden.insights.api.utils.SchedulingUtils; import org.bukkit.entity.Player; import java.util.HashSet; import java.util.Map; @@ -26,51 +27,58 @@ public void run() { var worldStorage = plugin.getWorldStorage(); Set locations = new HashSet<>(); for (Map.Entry entry : plugin.getPlayerList()) { - var location = entry.getValue().getLocation(); - var world = location.getWorld(); - Set loadedChunks = worldStorage.getWorld(world.getUID()).getChunks(); + var player = entry.getValue(); + SchedulingUtils.runImmediatelyAtEntityIfFolia(plugin, player, () -> { + var location = player.getLocation(); + SchedulingUtils.runImmediatelyAtLocationIfFolia(plugin, location, () -> { + var world = location.getWorld(); + Set loadedChunks = worldStorage.getWorld(world.getUID()).getChunks(); - int chunkX = location.getBlockX() >> 4; - int chunkZ = location.getBlockZ() >> 4; - for (int x = -1; x <= 1; x++) { - for (int z = -1; z <= 1; z++) { - var loc = new ChunkLocation(world, chunkX + x, chunkZ + z); - if (!loadedChunks.contains(loc.getKey()) && !this.scanLocations.containsKey(loc)) { - locations.add(loc); + int chunkX = location.getBlockX() >> 4; + int chunkZ = location.getBlockZ() >> 4; + for (int x = -1; x <= 1; x++) { + for (int z = -1; z <= 1; z++) { + var loc = new ChunkLocation(world, chunkX + x, chunkZ + z); + if (!loadedChunks.contains(loc.getKey()) && !this.scanLocations.containsKey(loc)) { + locations.add(loc); + } + } } - } - } + }); + }); } if (locations.isEmpty()) { return; } - plugin.getServer().getScheduler().runTask(plugin, () -> { + plugin.getServer().getGlobalRegionScheduler().execute(plugin, () -> { long now = System.nanoTime(); for (ChunkLocation loc : locations) { var world = loc.getWorld(); - if (world.isChunkLoaded(loc.getX(), loc.getZ())) { - this.scanLocations.put(loc, now); + SchedulingUtils.runImmediatelyAtChunkIfFolia(plugin, world, loc.getX(), loc.getZ(), () -> { + if (world.isChunkLoaded(loc.getX(), loc.getZ())) { + this.scanLocations.put(loc, now); - var chunk = world.getChunkAt(loc.getX(), loc.getZ()); - plugin.getChunkContainerExecutor().submit(chunk, ScanOptions.all()).whenComplete((s, e) -> { - if (s == null) { - int hash = e.getStackTrace()[0].hashCode(); - if (!knownErrorStackTraceHashes.contains(hash)) { - knownErrorStackTraceHashes.add(hash); - plugin.getLogger().log( - Level.SEVERE, - "Error occurred while scanning " - + loc - + " (future errors with the same stacktrace are suppressed)", - e - ); + var chunk = world.getChunkAt(loc.getX(), loc.getZ()); + plugin.getChunkContainerExecutor().submit(chunk, ScanOptions.all()).whenComplete((s, e) -> { + if (s == null) { + int hash = e.getStackTrace()[0].hashCode(); + if (!knownErrorStackTraceHashes.contains(hash)) { + knownErrorStackTraceHashes.add(hash); + plugin.getLogger().log( + Level.SEVERE, + "Error occurred while scanning " + + loc + + " (future errors with the same stacktrace are suppressed)", + e + ); + } } - } - this.scanLocations.remove(loc); - }); - } + this.scanLocations.remove(loc); + }); + } + }); } }); } From 4773346f610b4af0f9de4011c4fab565b01e6475 Mon Sep 17 00:00:00 2001 From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> Date: Thu, 5 Dec 2024 04:42:54 -0800 Subject: [PATCH 2/6] Fix scan command --- .../insights/api/tasks/ScanTask.java | 60 ++++++++++--------- Insights-NMS/Current/build.gradle.kts | 2 + .../insights/nms/impl/InsightsNMSImpl.java | 25 +++++--- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/Insights-API/src/main/java/dev/frankheijden/insights/api/tasks/ScanTask.java b/Insights-API/src/main/java/dev/frankheijden/insights/api/tasks/ScanTask.java index d45513d0..5c7d21eb 100644 --- a/Insights-API/src/main/java/dev/frankheijden/insights/api/tasks/ScanTask.java +++ b/Insights-API/src/main/java/dev/frankheijden/insights/api/tasks/ScanTask.java @@ -422,35 +422,39 @@ public void run() { var loc = chunkPart.getChunkLocation(); var world = loc.getWorld(); - CompletableFuture storageFuture; - if (world.isChunkLoaded(loc.getX(), loc.getZ())) { - storageFuture = executor.submit( - world.getChunkAt(loc.getX(), loc.getZ()), - chunkPart.getChunkCuboid(), - options - ); - } else { - storageFuture = executor.submit( - loc.getWorld(), - loc.getX(), - loc.getZ(), - chunkPart.getChunkCuboid(), - options - ); - } + int chunkX = loc.getX(); + int chunkZ = loc.getZ(); + SchedulingUtils.runImmediatelyAtChunkIfFolia(plugin, world, chunkX, chunkZ, () -> { + CompletableFuture storageFuture; + if (world.isChunkLoaded(loc.getX(), loc.getZ())) { + storageFuture = executor.submit( + world.getChunkAt(loc.getX(), loc.getZ()), + chunkPart.getChunkCuboid(), + options + ); + } else { + storageFuture = executor.submit( + loc.getWorld(), + loc.getX(), + loc.getZ(), + chunkPart.getChunkCuboid(), + options + ); + } - storageFuture - .thenAccept(storage -> resultMerger.accept(storage, loc, result)) - .thenRun(() -> { - iterationChunks.incrementAndGet(); - chunks.incrementAndGet(); - }) - .exceptionally(th -> { - if (!completedExceptionally.getAndSet(true)) { - plugin.getLogger().log(Level.SEVERE, th, th::getMessage); - } - return null; - }); + storageFuture + .thenAccept(storage -> resultMerger.accept(storage, loc, result)) + .thenRun(() -> { + iterationChunks.incrementAndGet(); + chunks.incrementAndGet(); + }) + .exceptionally(th -> { + if (!completedExceptionally.getAndSet(true)) { + plugin.getLogger().log(Level.SEVERE, th, th::getMessage); + } + return null; + }); + }); } } diff --git a/Insights-NMS/Current/build.gradle.kts b/Insights-NMS/Current/build.gradle.kts index e1273724..a638b8ef 100644 --- a/Insights-NMS/Current/build.gradle.kts +++ b/Insights-NMS/Current/build.gradle.kts @@ -1,3 +1,5 @@ dependencies { paperweight.paperDevBundle("1.21.3-R0.1-SNAPSHOT") + compileOnly(project(":Insights-API")) + compileOnly(project(":Insights")) } diff --git a/Insights-NMS/Current/src/main/java/dev/frankheijden/insights/nms/impl/InsightsNMSImpl.java b/Insights-NMS/Current/src/main/java/dev/frankheijden/insights/nms/impl/InsightsNMSImpl.java index 855c8eb2..5fe67423 100644 --- a/Insights-NMS/Current/src/main/java/dev/frankheijden/insights/nms/impl/InsightsNMSImpl.java +++ b/Insights-NMS/Current/src/main/java/dev/frankheijden/insights/nms/impl/InsightsNMSImpl.java @@ -4,6 +4,8 @@ import ca.spottedleaf.moonrise.patches.chunk_system.io.MoonriseRegionFileIO; import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; +import dev.frankheijden.insights.api.InsightsPlugin; +import dev.frankheijden.insights.api.utils.SchedulingUtils; import dev.frankheijden.insights.nms.core.ChunkEntity; import dev.frankheijden.insights.nms.core.ChunkSection; import dev.frankheijden.insights.nms.core.InsightsNMS; @@ -106,15 +108,20 @@ public void getUnloadedChunkSections(World world, int chunkX, int chunkZ, Consum @Override public void getLoadedChunkEntities(Chunk chunk, Consumer entityConsumer) { - for (org.bukkit.entity.Entity bukkitEntity : chunk.getEntities()) { - Entity entity = ((CraftEntity) bukkitEntity).getHandle(); - entityConsumer.accept(new ChunkEntity( - bukkitEntity.getType(), - entity.getBlockX(), - entity.getBlockY(), - entity.getBlockZ() - )); - } + var plugin = InsightsPlugin.getInstance(); + SchedulingUtils.runImmediatelyAtChunkIfFolia(plugin, chunk.getWorld(), chunk.getX(), chunk.getZ(), () -> { + for (org.bukkit.entity.Entity bukkitEntity : chunk.getEntities()) { + SchedulingUtils.runImmediatelyAtEntityIfFolia(plugin, bukkitEntity, () -> { + Entity entity = ((CraftEntity) bukkitEntity).getHandle(); + entityConsumer.accept(new ChunkEntity( + bukkitEntity.getType(), + entity.getBlockX(), + entity.getBlockY(), + entity.getBlockZ() + )); + }); + } + }); } @Override From d2e5483a3a3d525b4271d18665443bc245f269a4 Mon Sep 17 00:00:00 2001 From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> Date: Thu, 5 Dec 2024 05:22:16 -0800 Subject: [PATCH 3/6] Scan command --- .../insights/commands/CommandScan.java | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/Insights/src/main/java/dev/frankheijden/insights/commands/CommandScan.java b/Insights/src/main/java/dev/frankheijden/insights/commands/CommandScan.java index ea1ad925..10d6ffb2 100644 --- a/Insights/src/main/java/dev/frankheijden/insights/commands/CommandScan.java +++ b/Insights/src/main/java/dev/frankheijden/insights/commands/CommandScan.java @@ -9,7 +9,9 @@ import dev.frankheijden.insights.api.tasks.ScanTask; import dev.frankheijden.insights.api.util.LazyChunkPartRadiusIterator; import dev.frankheijden.insights.api.utils.Constants; +import dev.frankheijden.insights.api.utils.SchedulingUtils; import org.bukkit.Chunk; +import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; import org.incendo.cloud.annotation.specifier.Range; @@ -114,17 +116,26 @@ public void handleScan( boolean displayZeros, boolean groupByChunk ) { - Chunk chunk = player.getLocation().getChunk(); - World world = chunk.getWorld(); - int chunkX = chunk.getX(); - int chunkZ = chunk.getZ(); + SchedulingUtils.runImmediatelyAtEntityIfFolia(plugin, player, () -> { + Location loc = player.getLocation(); + SchedulingUtils.runImmediatelyAtLocationIfFolia(plugin, loc, () -> { + Chunk chunk = loc.getChunk(); + World world = chunk.getWorld(); + int chunkX = chunk.getX(); + int chunkZ = chunk.getZ(); - LazyChunkPartRadiusIterator it = new LazyChunkPartRadiusIterator(world, chunkX, chunkZ, radius); + LazyChunkPartRadiusIterator it = new LazyChunkPartRadiusIterator(world, chunkX, chunkZ, radius); - if (groupByChunk) { - ScanTask.scanAndDisplayGroupedByChunk(plugin, player, it, it.getChunkCount(), options, items, false); - } else { - ScanTask.scanAndDisplay(plugin, player, it, it.getChunkCount(), options, items, displayZeros); - } + if (groupByChunk) { + ScanTask.scanAndDisplayGroupedByChunk( + plugin, player, it, it.getChunkCount(), options, items, false + ); + } else { + ScanTask.scanAndDisplay( + plugin, player, it, it.getChunkCount(), options, items, displayZeros + ); + } + }); + }); } } From becff95daf687190074751748c838d67475b08ce Mon Sep 17 00:00:00 2001 From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> Date: Thu, 5 Dec 2024 05:53:38 -0800 Subject: [PATCH 4/6] [ci skip] Add comment --- .../main/java/dev/frankheijden/insights/api/tasks/ScanTask.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Insights-API/src/main/java/dev/frankheijden/insights/api/tasks/ScanTask.java b/Insights-API/src/main/java/dev/frankheijden/insights/api/tasks/ScanTask.java index 5c7d21eb..5f97cab0 100644 --- a/Insights-API/src/main/java/dev/frankheijden/insights/api/tasks/ScanTask.java +++ b/Insights-API/src/main/java/dev/frankheijden/insights/api/tasks/ScanTask.java @@ -375,7 +375,7 @@ private void start() { task = scheduler.runAtFixedRate( plugin, SchedulingUtils.wrapRunnable(this), - 1L, + 1L, // At least 1 tick for Folia schedulers plugin.getSettings().SCANS_ITERATION_INTERVAL_TICKS ); } From 5ec7701db9b835b6f092bd4866f2f7a3221b705f Mon Sep 17 00:00:00 2001 From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> Date: Thu, 5 Dec 2024 06:20:13 -0800 Subject: [PATCH 5/6] Only depend on api --- Insights-NMS/Current/build.gradle.kts | 1 - 1 file changed, 1 deletion(-) diff --git a/Insights-NMS/Current/build.gradle.kts b/Insights-NMS/Current/build.gradle.kts index a638b8ef..6d634324 100644 --- a/Insights-NMS/Current/build.gradle.kts +++ b/Insights-NMS/Current/build.gradle.kts @@ -1,5 +1,4 @@ dependencies { paperweight.paperDevBundle("1.21.3-R0.1-SNAPSHOT") compileOnly(project(":Insights-API")) - compileOnly(project(":Insights")) } From 0f3288fec0ff3356e60f5faefcc392e39e37ab89 Mon Sep 17 00:00:00 2001 From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> Date: Tue, 14 Jan 2025 20:51:16 -0800 Subject: [PATCH 6/6] Update Paper repository --- build.gradle.kts | 2 +- settings.gradle.kts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index d722f44d..63beab52 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -46,7 +46,7 @@ subprojects { maven("https://repo.codemc.io/repository/maven-public") maven("https://oss.sonatype.org/content/repositories/snapshots/") maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") - maven("https://papermc.io/repo/repository/maven-public/") + maven("https://repo.papermc.io/repository/maven-public/") maven("https://libraries.minecraft.net") } diff --git a/settings.gradle.kts b/settings.gradle.kts index c36a94ff..bd9922a9 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,7 +3,7 @@ import java.nio.file.Files pluginManagement { repositories { gradlePluginPortal() - maven("https://papermc.io/repo/repository/maven-public/") + maven("https://repo.papermc.io/repository/maven-public/") } }