From 271109a72655f73e1850c5baf5d36871d4cc0599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Wed, 8 Apr 2020 21:49:41 +0200 Subject: [PATCH] Fix dumdum async getLoadedChunks call, but only because Aikar broke everything. --- .../bukkit/util/BukkitChunkManager.java | 27 ++++++++++++++++--- .../plotsquared/plot/commands/Debug.java | 11 +++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java index 7ea1a62f1..83ee0aaf3 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java @@ -1,5 +1,6 @@ package com.github.intellectualsites.plotsquared.bukkit.util; +import com.github.intellectualsites.plotsquared.bukkit.BukkitMain; import com.github.intellectualsites.plotsquared.bukkit.object.entity.EntityWrapper; import com.github.intellectualsites.plotsquared.bukkit.object.entity.ReplicatingEntityWrapper; import com.github.intellectualsites.plotsquared.plot.PlotSquared; @@ -43,6 +44,7 @@ import java.util.Map.Entry; import java.util.Objects; import java.util.Set; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Semaphore; import static com.google.common.base.Preconditions.checkNotNull; @@ -114,9 +116,28 @@ public class BukkitChunkManager extends ChunkManager { @Override public Set getChunkChunks(String world) { Set chunks = super.getChunkChunks(world); - for (Chunk chunk : Objects.requireNonNull(Bukkit.getWorld(world)).getLoadedChunks()) { - BlockVector2 loc = BlockVector2.at(chunk.getX() >> 5, chunk.getZ() >> 5); - chunks.add(loc); + if (Bukkit.isPrimaryThread()) { + for (Chunk chunk : Objects.requireNonNull(Bukkit.getWorld(world)).getLoadedChunks()) { + BlockVector2 loc = BlockVector2.at(chunk.getX() >> 5, chunk.getZ() >> 5); + chunks.add(loc); + } + } else { + final Semaphore semaphore = new Semaphore(1); + try { + PlotSquared.debug("Attempting to make an asynchronous call to getLoadedChunks." + + " Will halt the calling thread until completed."); + semaphore.acquire(); + Bukkit.getScheduler().runTask(BukkitMain.getPlugin(BukkitMain.class), () -> { + for (Chunk chunk : Objects.requireNonNull(Bukkit.getWorld(world)).getLoadedChunks()) { + BlockVector2 loc = BlockVector2.at(chunk.getX() >> 5, chunk.getZ() >> 5); + chunks.add(loc); + } + semaphore.release(); + }); + semaphore.acquireUninterruptibly(); + } catch (final Exception e) { + e.printStackTrace(); + } } return chunks; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Debug.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Debug.java index ad1fc6d2e..339d33c5c 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Debug.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Debug.java @@ -4,8 +4,10 @@ import com.github.intellectualsites.plotsquared.commands.CommandDeclaration; import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; +import com.github.intellectualsites.plotsquared.plot.util.ChunkManager; import com.github.intellectualsites.plotsquared.plot.util.MainUtil; import com.github.intellectualsites.plotsquared.plot.util.StringMan; +import com.github.intellectualsites.plotsquared.plot.util.TaskManager; import java.util.Map; @@ -23,9 +25,16 @@ public class Debug extends SubCommand { MainUtil.sendMessage(player, "Key: " + meta.getKey() + " Value: " + meta.getValue().toString() + " , "); } - ; } } + if (args.length > 0 && "loadedchunks".equalsIgnoreCase(args[0])) { + final long start = System.currentTimeMillis(); + MainUtil.sendMessage(player, "Fetching loaded chunks..."); + TaskManager.runTaskAsync(() -> MainUtil.sendMessage(player,"Loaded chunks: " + + ChunkManager.manager.getChunkChunks(player.getLocation().getWorld()).size() + "(" + (System.currentTimeMillis() - start) + "ms) using thread: " + + Thread.currentThread().getName())); + return true; + } if ((args.length > 0) && args[0].equalsIgnoreCase("msg")) { StringBuilder msg = new StringBuilder(); for (Captions caption : Captions.values()) {