Added options to sort servers by players and to not show offline servers

This commit is contained in:
cuftica 2023-06-04 18:31:55 +02:00
parent 8c80e58757
commit 40b7604531
3 changed files with 31 additions and 9 deletions

View File

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module version="4"> <module version="4">
<component name="CheckStyle-IDEA-Module" serialisationVersion="2">
<option name="activeLocationsIds" />
</component>
<component name="FacetManager"> <component name="FacetManager">
<facet type="minecraft" name="Minecraft"> <facet type="minecraft" name="Minecraft">
<configuration> <configuration>

View File

@ -7,10 +7,13 @@ import com.samjakob.spigui.item.ItemBuilder;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import os.arcadiadevs.playerservers.hubcore.PsHubCore; import os.arcadiadevs.playerservers.hubcore.PsHubCore;
import os.arcadiadevs.playerservers.hubcore.enums.ServerStatus; import os.arcadiadevs.playerservers.hubcore.enums.ServerStatus;
import os.arcadiadevs.playerservers.hubcore.models.Server;
import os.arcadiadevs.playerservers.hubcore.utils.BungeeUtil; import os.arcadiadevs.playerservers.hubcore.utils.BungeeUtil;
import os.arcadiadevs.playerservers.hubcore.utils.ChatUtil; import os.arcadiadevs.playerservers.hubcore.utils.ChatUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/** /**
* Handles the selector GUI. * Handles the selector GUI.
@ -45,7 +48,15 @@ public class SelectorGui {
.getServers(); .getServers();
servers.forEach(server -> { List<Server> filteredServers = new ArrayList<>(servers);
filteredServers.sort(Comparator.comparing(s -> s.getInfo().players() != null ? -s.getInfo().players() : 0));
final var serversPage = instance.getConfig().getBoolean("gui.selector.menu.sort-by-players")
? filteredServers
: servers;
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();
@ -53,12 +64,17 @@ public class SelectorGui {
XMaterial.matchXMaterial( XMaterial.matchXMaterial(
instance.getConfig().getString("gui.selector.menu.offline.block")) instance.getConfig().getString("gui.selector.menu.offline.block"))
.orElse(XMaterial.RED_TERRACOTTA).parseItem(); .orElse(XMaterial.RED_TERRACOTTA).parseItem();
final var serverInfo = server.getInfo(); final var serverInfo = server.getInfo();
final var online = serverInfo.status() == ServerStatus.ONLINE;
final boolean showOffline = instance.getConfig().getBoolean("gui.selector.menu.show-offline");
if (!showOffline && !online) {
return;
}
final var itemBuilder = new ItemBuilder( final var itemBuilder = new ItemBuilder(
serverInfo.status() == ServerStatus.ONLINE ? onlinexMaterial : offlinexMaterial); serverInfo.status() == ServerStatus.ONLINE ? onlinexMaterial : offlinexMaterial);
final var online = serverInfo.status() == ServerStatus.ONLINE;
var lore = instance.getConfig().getStringList( var lore = instance.getConfig().getStringList(
serverInfo.status() == ServerStatus.ONLINE ? "gui.selector.menu.online.lore" : serverInfo.status() == ServerStatus.ONLINE ? "gui.selector.menu.online.lore" :
@ -67,9 +83,9 @@ public class SelectorGui {
lore = lore.stream() lore = lore.stream()
.map(s -> s.replaceAll("%server%", server.getId())) .map(s -> s.replaceAll("%server%", server.getId()))
.map(s -> s.replaceAll("%status%", online ? "&aOnline" : "&cOffline")) .map(s -> s.replaceAll("%status%", online ? "&aOnline" : "&cOffline"))
.map(s -> s.replaceAll("%players%", online ? serverInfo.players() + "" : "0")) .map(s -> s.replaceAll("%players%", online ? String.valueOf(serverInfo.players()) : "0"))
.map(s -> s.replaceAll("%maxplayers%", online ? serverInfo.maxPlayers() + "" : "0")) .map(s -> s.replaceAll("%maxplayers%", online ? String.valueOf(serverInfo.maxPlayers()) : "0"))
.map(s -> s.replaceAll("%port%", server.getDefaultAllocation().getPort() + "")) .map(s -> s.replaceAll("%port%", String.valueOf(server.getDefaultAllocation().getPort())))
.map(s -> s.replaceAll("%motd%", online ? serverInfo.motd() : "&cOffline")) .map(s -> s.replaceAll("%motd%", online ? serverInfo.motd() : "&cOffline"))
.map(s -> s.replaceAll("%node%", server.getNode().getName())) .map(s -> s.replaceAll("%node%", server.getNode().getName()))
.map(s -> s.replaceAll("%owner%", server.getOfflinePlayer().getName())) .map(s -> s.replaceAll("%owner%", server.getOfflinePlayer().getName()))
@ -81,9 +97,9 @@ public class SelectorGui {
online ? "gui.selector.menu.online.name" : "gui.selector.menu.offline.name") online ? "gui.selector.menu.online.name" : "gui.selector.menu.offline.name")
.replaceAll("%server%", server.getId()) .replaceAll("%server%", server.getId())
.replaceAll("%status%", online ? "&aOnline" : "&cOffline") .replaceAll("%status%", online ? "&aOnline" : "&cOffline")
.replaceAll("%players%", online ? serverInfo.players() + "" : "0") .replaceAll("%players%", online ? String.valueOf(serverInfo.players()) : "0")
.replaceAll("%maxplayers%", online ? serverInfo.maxPlayers() + "" : "0") .replaceAll("%maxplayers%", online ? String.valueOf(serverInfo.maxPlayers()) : "0")
.replaceAll("%port%", server.getDefaultAllocation().getPort() + "") .replaceAll("%port%", String.valueOf(server.getDefaultAllocation().getPort()))
.replaceAll("%motd%", online ? serverInfo.motd() : "&cOffline") .replaceAll("%motd%", online ? serverInfo.motd() : "&cOffline")
.replaceAll("%node%", server.getNode().getName()) .replaceAll("%node%", server.getNode().getName())
.replaceAll("%owner%", server.getOfflinePlayer().getName()) .replaceAll("%owner%", server.getOfflinePlayer().getName())

View File

@ -40,6 +40,9 @@ gui:
- "&7selector or to create your own one" - "&7selector or to create your own one"
menu: menu:
show-offline: true
sort-by-players: true
name: "&aServer Selector" name: "&aServer Selector"
online: online:
block: "PLAYER_HEAD" block: "PLAYER_HEAD"