Run offlinePlayer code async to avoid blocking IO
This commit is contained in:
parent
2506d2ac9e
commit
dcd5bd1514
|
@ -1,11 +1,10 @@
|
|||
package world.bentobox.warps;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
@ -69,20 +68,23 @@ public class WarpPanelManager {
|
|||
.user(user)
|
||||
.name(user.getTranslation("warps.title") + " " + (index + 1));
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(addon.getPlugin(), () -> {
|
||||
buildPanel(panelBuilder, user, index, world);
|
||||
Bukkit.getScheduler().runTask(addon.getPlugin(), () -> panelBuilder.build());
|
||||
});
|
||||
|
||||
buildPanel(panelBuilder, user, index, world).thenRun(() -> panelBuilder.build());
|
||||
}
|
||||
|
||||
void buildPanel(PanelBuilder panelBuilder, User user, int index, World world) {
|
||||
List<UUID> warps = new ArrayList<>(addon.getWarpSignsManager().getSortedWarps(world));
|
||||
// Build the main body
|
||||
int i = buildMainBody(panelBuilder, user, index, world, warps, getRandomWarp(warps));
|
||||
// Add navigation
|
||||
addNavigation(panelBuilder, user, world, i, index, warps.size());
|
||||
CompletableFuture<Void> buildPanel(PanelBuilder panelBuilder, User user, int index, World world) {
|
||||
CompletableFuture<Void> r = new CompletableFuture<>();
|
||||
processSigns(r, panelBuilder, user, index, world);
|
||||
return r;
|
||||
}
|
||||
|
||||
void processSigns(CompletableFuture<Void> r, PanelBuilder panelBuilder, User user, int index, World world) {
|
||||
addon.getWarpSignsManager().getSortedWarps(world).thenAccept(warps -> {
|
||||
// Build the main body
|
||||
int i = buildMainBody(panelBuilder, user, index, world, warps, getRandomWarp(warps));
|
||||
// Add navigation
|
||||
addNavigation(panelBuilder, user, world, i, index, warps.size());
|
||||
r.complete(null);
|
||||
});
|
||||
}
|
||||
|
||||
int buildMainBody(PanelBuilder panelBuilder, User user, int index, World world, List<UUID> warps, boolean randomWarp) {
|
||||
|
|
|
@ -14,6 +14,7 @@ import java.util.Optional;
|
|||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
@ -143,8 +144,13 @@ public class WarpSignsManager {
|
|||
* Get sorted list of warps with most recent players listed first
|
||||
* @return UUID list
|
||||
*/
|
||||
@NonNull
|
||||
public List<UUID> getSortedWarps(@NonNull World world) {
|
||||
public CompletableFuture<List<UUID>> getSortedWarps(@NonNull World world) {
|
||||
CompletableFuture<List<UUID>> r = new CompletableFuture<>();
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> processWarpMap(r, world));
|
||||
return r;
|
||||
}
|
||||
|
||||
private void processWarpMap(CompletableFuture<List<UUID>> r, @NonNull World world) {
|
||||
// Remove any null locations - this can happen if an admin changes the name of the world and signs point to old locations
|
||||
getWarpMap(world).values().removeIf(Objects::isNull);
|
||||
// Bigger value of time means a more recent login
|
||||
|
@ -164,7 +170,9 @@ public class WarpSignsManager {
|
|||
if (list.size() > MAX_WARPS) {
|
||||
list.subList(0, MAX_WARPS).clear();
|
||||
}
|
||||
return list;
|
||||
// Return to main thread
|
||||
Bukkit.getScheduler().runTask(plugin, () -> r.complete(list));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue