This commit is contained in:
OpenSource 2023-07-23 20:44:52 +02:00
parent 3a938c8e61
commit 9ed2083928
3 changed files with 13 additions and 19 deletions

View File

@ -121,10 +121,7 @@ public class PsHubCore extends JavaPlugin {
// Initialize ServerCache // Initialize ServerCache
serverCache = new ServerCache(serversController); serverCache = new ServerCache(serversController);
serverCache.runTaskTimerAsynchronously(this, 0, getConfig().getLong("cache.cache-time") * 20);
// Create ServerCache refreshing task
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(serverCache, 1, getConfig().getInt("cache.cache-time"), TimeUnit.SECONDS);
} }
/** /**

View File

@ -1,20 +1,23 @@
package os.arcadiadevs.playerservers.hubcore.cache; package os.arcadiadevs.playerservers.hubcore.cache;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import lombok.Getter; import lombok.Getter;
import org.bukkit.scheduler.BukkitRunnable;
import os.arcadiadevs.playerservers.hubcore.controllers.ServersController; import os.arcadiadevs.playerservers.hubcore.controllers.ServersController;
import os.arcadiadevs.playerservers.hubcore.models.CachedServer; import os.arcadiadevs.playerservers.hubcore.models.CachedServer;
public class ServerCache implements Runnable { public class ServerCache extends BukkitRunnable {
@Getter @Getter
private final HashSet<CachedServer> servers; private final List<CachedServer> servers;
private final ServersController serversController; private final ServersController serversController;
public ServerCache(ServersController serversController) { public ServerCache(ServersController serversController) {
this.serversController = serversController; this.serversController = serversController;
this.servers = new HashSet<>(); this.servers = new ArrayList<>();
} }
/** /**

View File

@ -38,7 +38,7 @@ public class SelectorGui {
menu.setAutomaticPaginationEnabled(true); menu.setAutomaticPaginationEnabled(true);
menu.setBlockDefaultInteractions(true); menu.setBlockDefaultInteractions(true);
final var servers = useCache var servers = useCache
? ?
PsHubCore.getInstance() PsHubCore.getInstance()
.getServerCache() .getServerCache()
@ -48,9 +48,7 @@ public class SelectorGui {
.getServersController() .getServersController()
.getServers(); .getServers();
List<Server> filteredServers = new ArrayList<>(servers); servers.sort(Comparator.comparing(s -> {
filteredServers.sort(Comparator.comparing(s -> {
final ServerPinger.PingResult info = s.getInfo(); final ServerPinger.PingResult info = s.getInfo();
if (info == null || info.status() != ServerStatus.ONLINE) { if (info == null || info.status() != ServerStatus.ONLINE) {
return 1; return 1;
@ -58,15 +56,11 @@ public class SelectorGui {
return 0; return 0;
})); }));
List<Server> 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)); servers.forEach(server -> {
final var serversPage = instance.getConfig().getBoolean("gui.selector.menu.sort-by-players")
? filteredServersByPlayers
: filteredServers;
serversPage.forEach(server -> {
final var onlinexMaterial = final var onlinexMaterial =
XMaterial.matchXMaterial(instance.getConfig().getString("gui.selector.menu.online.block")) XMaterial.matchXMaterial(instance.getConfig().getString("gui.selector.menu.online.block"))
.orElse(XMaterial.PLAYER_HEAD).parseItem(); .orElse(XMaterial.PLAYER_HEAD).parseItem();