diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/PsHubCore.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/PsHubCore.java index 3c45bf0..fba9672 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/PsHubCore.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/PsHubCore.java @@ -121,10 +121,7 @@ public class PsHubCore extends JavaPlugin { // Initialize ServerCache serverCache = new ServerCache(serversController); - - // Create ServerCache refreshing task - ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); - executor.scheduleAtFixedRate(serverCache, 1, getConfig().getInt("cache.cache-time"), TimeUnit.SECONDS); + serverCache.runTaskTimerAsynchronously(this, 0, getConfig().getLong("cache.cache-time") * 20); } /** diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/cache/ServerCache.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/cache/ServerCache.java index 2f79c13..3ff27cf 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/cache/ServerCache.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/cache/ServerCache.java @@ -1,20 +1,23 @@ package os.arcadiadevs.playerservers.hubcore.cache; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import lombok.Getter; +import org.bukkit.scheduler.BukkitRunnable; import os.arcadiadevs.playerservers.hubcore.controllers.ServersController; import os.arcadiadevs.playerservers.hubcore.models.CachedServer; -public class ServerCache implements Runnable { +public class ServerCache extends BukkitRunnable { @Getter - private final HashSet servers; + private final List servers; private final ServersController serversController; public ServerCache(ServersController serversController) { this.serversController = serversController; - this.servers = new HashSet<>(); + this.servers = new ArrayList<>(); } /** diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/guis/SelectorGui.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/guis/SelectorGui.java index 52386af..9878bbe 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/guis/SelectorGui.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/guis/SelectorGui.java @@ -38,7 +38,7 @@ public class SelectorGui { menu.setAutomaticPaginationEnabled(true); menu.setBlockDefaultInteractions(true); - final var servers = useCache + var servers = useCache ? PsHubCore.getInstance() .getServerCache() @@ -48,9 +48,7 @@ public class SelectorGui { .getServersController() .getServers(); - List filteredServers = new ArrayList<>(servers); - - filteredServers.sort(Comparator.comparing(s -> { + servers.sort(Comparator.comparing(s -> { final ServerPinger.PingResult info = s.getInfo(); if (info == null || info.status() != ServerStatus.ONLINE) { return 1; @@ -58,15 +56,11 @@ public class SelectorGui { return 0; })); - List filteredServersByPlayers = new ArrayList<>(filteredServers); + if (instance.getConfig().getBoolean("gui.selector.menu.sort-by-players")) { + servers.sort(Comparator.comparing(s -> s.getInfo().players() != null ? -s.getInfo().players() : 0)); + } - filteredServersByPlayers.sort(Comparator.comparing(s -> s.getInfo().players() != null ? -s.getInfo().players() : 0)); - - final var serversPage = instance.getConfig().getBoolean("gui.selector.menu.sort-by-players") - ? filteredServersByPlayers - : filteredServers; - - serversPage.forEach(server -> { + servers.forEach(server -> { final var onlinexMaterial = XMaterial.matchXMaterial(instance.getConfig().getString("gui.selector.menu.online.block")) .orElse(XMaterial.PLAYER_HEAD).parseItem();