From 4abed8ad5125c6cbb285adb06c21d8721358119e Mon Sep 17 00:00:00 2001 From: cuftica Date: Thu, 20 Jul 2023 01:19:39 +0200 Subject: [PATCH] New system for compass --- pom.xml | 12 +- .../playerservers/hubcore/PsHubCore.java | 18 ++- .../hubcore/dto/ServerRecord.java | 23 +++ .../events/MessageChannelListener.java | 135 ++++++++++++++++++ .../hubcore/guis/SelectorGui.java | 99 +------------ .../hubcore/utils/BungeeUtil.java | 11 ++ .../playerservers/hubcore/utils/ChatUtil.java | 5 + .../hubcore/utils/formatter/Formattable.java | 9 ++ .../hubcore/utils/formatter/Formatter.java | 26 ++++ 9 files changed, 238 insertions(+), 100 deletions(-) create mode 100644 src/main/java/os/arcadiadevs/playerservers/hubcore/dto/ServerRecord.java create mode 100644 src/main/java/os/arcadiadevs/playerservers/hubcore/events/MessageChannelListener.java create mode 100644 src/main/java/os/arcadiadevs/playerservers/hubcore/utils/formatter/Formattable.java create mode 100644 src/main/java/os/arcadiadevs/playerservers/hubcore/utils/formatter/Formatter.java diff --git a/pom.xml b/pom.xml index 82594eb..6eecbea 100644 --- a/pom.xml +++ b/pom.xml @@ -188,8 +188,18 @@ hibernate-core 6.1.5.Final + + org.jetbrains + annotations + 24.0.1 + compile + + + com.google.code.gson + gson + 2.10.1 + - PSHubCore PSHubCore v3-dev diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/PsHubCore.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/PsHubCore.java index 0bb5ed4..be9b931 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/PsHubCore.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/PsHubCore.java @@ -1,9 +1,12 @@ package os.arcadiadevs.playerservers.hubcore; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.samjakob.spigui.SpiGUI; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.lang.module.Configuration; import java.util.Objects; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -11,6 +14,7 @@ import java.util.concurrent.TimeUnit; import lombok.Getter; import lombok.SneakyThrows; import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; import org.hibernate.SessionFactory; import os.arcadiadevs.playerservers.hubcore.cache.ServerCache; @@ -20,6 +24,7 @@ import os.arcadiadevs.playerservers.hubcore.controllers.ServersController; import os.arcadiadevs.playerservers.hubcore.events.ClickEvent; import os.arcadiadevs.playerservers.hubcore.events.HubEvents; import os.arcadiadevs.playerservers.hubcore.events.JoinEvent; +import os.arcadiadevs.playerservers.hubcore.events.MessageChannelListener; import os.arcadiadevs.playerservers.hubcore.models.Allocation; import os.arcadiadevs.playerservers.hubcore.models.Node; import os.arcadiadevs.playerservers.hubcore.models.Server; @@ -52,21 +57,28 @@ public class PsHubCore extends JavaPlugin { @Getter private ServerCache serverCache; + private Gson gson; + @SneakyThrows @Override public void onEnable() { instance = this; + + gson = new GsonBuilder().setPrettyPrinting().create(); + + System.out.println(gson.getClass()); + getConfig().options().copyDefaults(true); saveConfig(); - extractFile("hibernate.cfg.xml"); + FileConfiguration config = getConfig(); var configuration = new org.hibernate.cfg.Configuration() .addAnnotatedClass(Server.class) .addAnnotatedClass(Node.class) .addAnnotatedClass(Allocation.class); - if (getConfig().getBoolean("mysql.get-from-file")) { + if (config.getBoolean("mysql.get-from-file")) { configuration .configure(new File(this.getDataFolder().getAbsolutePath() + "/" + "hibernate.cfg.xml")); } else { @@ -99,6 +111,8 @@ public class PsHubCore extends JavaPlugin { } Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); + Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new MessageChannelListener(gson)); + Bukkit.getPluginManager().registerEvents(new ClickEvent(this), this); Bukkit.getPluginManager().registerEvents(new JoinEvent(this), this); Bukkit.getPluginManager().registerEvents(new HubEvents(getConfig()), this); diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/dto/ServerRecord.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/dto/ServerRecord.java new file mode 100644 index 0000000..9583bea --- /dev/null +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/dto/ServerRecord.java @@ -0,0 +1,23 @@ +package os.arcadiadevs.playerservers.hubcore.dto; + +import os.arcadiadevs.playerservers.hubcore.utils.formatter.Formattable; + +import java.util.HashMap; + +public record ServerRecord(String name, String motd, boolean online, String ip, Integer port, Integer players, Integer maxPlayers) implements Formattable { + + @Override + public HashMap getPlaceHolders() { + return new HashMap<>() {{ + put("%server%", name); + put("%status%", online ? "&aOnline" : "&cOffline"); + put("%players%", players.toString()); + put("%maxplayers%", maxPlayers.toString()); + put("%port%", port.toString()); + put("%motd%", motd); + put("%node%", ip); + put("%owner%", name); + put("%ip%", ip); + }}; + } +} diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/events/MessageChannelListener.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/events/MessageChannelListener.java new file mode 100644 index 0000000..2a97272 --- /dev/null +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/events/MessageChannelListener.java @@ -0,0 +1,135 @@ +package os.arcadiadevs.playerservers.hubcore.events; + +import com.cryptomorin.xseries.XMaterial; +import com.cryptomorin.xseries.XSound; +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteStreams; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.samjakob.spigui.buttons.SGButton; +import com.samjakob.spigui.item.ItemBuilder; +import lombok.AllArgsConstructor; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.messaging.PluginMessageListener; +import org.jetbrains.annotations.NotNull; +import os.arcadiadevs.playerservers.hubcore.PsHubCore; +import os.arcadiadevs.playerservers.hubcore.dto.ServerRecord; +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.ChatUtil; +import os.arcadiadevs.playerservers.hubcore.utils.ServerPinger; +import os.arcadiadevs.playerservers.hubcore.utils.formatter.Formatter; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +@AllArgsConstructor +public class MessageChannelListener implements PluginMessageListener { + + private final Gson gson; + + public void onPluginMessageReceived(String channel, @NotNull Player player, byte[] bytes) { + if (!channel.equalsIgnoreCase("BungeeCord")) { + return; + } + + ByteArrayDataInput in = ByteStreams.newDataInput(bytes); + String subChannel = in.readUTF(); + + if (subChannel.equalsIgnoreCase("servers")) { + String json = in.readUTF(); + + TypeToken> typeToken = new TypeToken<>() { + }; + List serverRecord = new Gson().fromJson(json, typeToken.getType()); + + openGui(player, serverRecord); + System.out.println("Server record: " + serverRecord); + } + + } + + private void openGui(Player player, List serverRecord) { + final var instance = PsHubCore.getInstance(); + final var menu = instance.getSpiGui() + .create(ChatUtil.translate(instance.getConfig().getString("gui.selector.menu.name")), 5); + final var useCache = instance.getConfig().getBoolean("cache.enabled"); + + menu.setAutomaticPaginationEnabled(true); + menu.setBlockDefaultInteractions(true); + + final var servers = useCache + ? + PsHubCore.getInstance() + .getServerCache() + .getServers() + : + PsHubCore.getInstance() + .getServersController() + .getServers(); + + List filteredServers = new ArrayList<>(servers); + + filteredServers.sort(Comparator.comparing(s -> { + final ServerPinger.PingResult info = s.getInfo(); + if (info == null || info.status() != ServerStatus.ONLINE) { + return 1; + } + return 0; + })); + + List filteredServersByPlayers = new ArrayList<>(filteredServers); + + 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; + + final XMaterial onlinexMaterial = + XMaterial.matchXMaterial(instance.getConfig().getString("gui.selector.menu.online.block")) + .orElse(XMaterial.PLAYER_HEAD); + final XMaterial offlinexMaterial = + XMaterial.matchXMaterial(instance.getConfig().getString("gui.selector.menu.offline.block")) + .orElse(XMaterial.RED_TERRACOTTA); + + final boolean showOffline = instance.getConfig().getBoolean("gui.selector.menu.show-offline"); + + serverRecord.stream() + .filter(server -> showOffline || server.online()) + .forEach(server -> { + boolean online = server.online(); + XMaterial material = online ? onlinexMaterial : offlinexMaterial; + List onlineLore = Formatter.format(server, + instance.getConfig().getStringList("gui.selector.menu.online.lore")); + + List offlineLore = Formatter.format(server, + instance.getConfig().getStringList("gui.selector.menu.offline.lore")); + + String onlineName = Formatter.format(server, + instance.getConfig().getString("gui.selector.menu.online.name")); + + String offlineName = Formatter.format(server, + instance.getConfig().getString("gui.selector.menu.offline.name")); + + ItemBuilder itemBuilder = new ItemBuilder(material.parseMaterial()) + .name(online ? onlineName : offlineName); + + ItemStack item = itemBuilder + .lore(online ? onlineLore : offlineLore) + .build(); + + menu.addButton(new SGButton(item).withListener( + listener -> BungeeUtil.connectPlayer(player, server.name()))); + }); + + XSound.BLOCK_NOTE_BLOCK_BASS.play(player); + player.openInventory(menu.getInventory()); + } + + +} \ No newline at end of file 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..28c1057 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/guis/SelectorGui.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/guis/SelectorGui.java @@ -6,6 +6,7 @@ import com.samjakob.spigui.buttons.SGButton; import com.samjakob.spigui.item.ItemBuilder; import org.bukkit.entity.Player; import os.arcadiadevs.playerservers.hubcore.PsHubCore; +import os.arcadiadevs.playerservers.hubcore.dto.ServerRecord; import os.arcadiadevs.playerservers.hubcore.enums.ServerStatus; import os.arcadiadevs.playerservers.hubcore.models.Server; import os.arcadiadevs.playerservers.hubcore.utils.BungeeUtil; @@ -29,103 +30,7 @@ public class SelectorGui { * @param player The player to open the GUI for. */ public static void openGui(Player player) { - - final var instance = PsHubCore.getInstance(); - final var menu = instance.getSpiGui() - .create(ChatUtil.translate(instance.getConfig().getString("gui.selector.menu.name")), 5); - final var useCache = instance.getConfig().getBoolean("cache.enabled"); - - menu.setAutomaticPaginationEnabled(true); - menu.setBlockDefaultInteractions(true); - - final var servers = useCache - ? - PsHubCore.getInstance() - .getServerCache() - .getServers() - : - PsHubCore.getInstance() - .getServersController() - .getServers(); - - List filteredServers = new ArrayList<>(servers); - - filteredServers.sort(Comparator.comparing(s -> { - final ServerPinger.PingResult info = s.getInfo(); - if (info == null || info.status() != ServerStatus.ONLINE) { - return 1; - } - return 0; - })); - - List filteredServersByPlayers = new ArrayList<>(filteredServers); - - 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 -> { - final var onlinexMaterial = - XMaterial.matchXMaterial(instance.getConfig().getString("gui.selector.menu.online.block")) - .orElse(XMaterial.PLAYER_HEAD).parseItem(); - final var offlinexMaterial = - XMaterial.matchXMaterial( - instance.getConfig().getString("gui.selector.menu.offline.block")) - .orElse(XMaterial.RED_TERRACOTTA).parseItem(); - - 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( - serverInfo.status() == ServerStatus.ONLINE ? onlinexMaterial : offlinexMaterial); - - var lore = instance.getConfig().getStringList( - serverInfo.status() == ServerStatus.ONLINE ? "gui.selector.menu.online.lore" : - "gui.selector.menu.offline.lore"); - - lore = lore.stream() - .map(s -> s.replaceAll("%server%", server.getId())) - .map(s -> s.replaceAll("%status%", online ? "&aOnline" : "&cOffline")) - .map(s -> s.replaceAll("%players%", online ? String.valueOf(serverInfo.players()) : "0")) - .map(s -> s.replaceAll("%maxplayers%", online ? String.valueOf(serverInfo.maxPlayers()) : "0")) - .map(s -> s.replaceAll("%port%", String.valueOf(server.getDefaultAllocation().getPort()))) - .map(s -> s.replaceAll("%motd%", online ? serverInfo.motd() : "&cOffline")) - .map(s -> s.replaceAll("%node%", server.getNode().getName())) - .map(s -> s.replaceAll("%owner%", server.getOfflinePlayer().getName() == null ? "Unknown" : server.getOfflinePlayer().getName())) - .map(s -> s.replaceAll("%ip%", server.getNode().getIp())) - .collect(ArrayList::new, ArrayList::add, ArrayList::addAll); - - var item = itemBuilder - .name(ChatUtil.translate(instance.getConfig().getString( - online ? "gui.selector.menu.online.name" : "gui.selector.menu.offline.name") - .replaceAll("%server%", server.getId()) - .replaceAll("%status%", online ? "&aOnline" : "&cOffline") - .replaceAll("%players%", online ? String.valueOf(serverInfo.players()) : "0") - .replaceAll("%maxplayers%", online ? String.valueOf(serverInfo.maxPlayers()) : "0") - .replaceAll("%port%", String.valueOf(server.getDefaultAllocation().getPort())) - .replaceAll("%motd%", online ? serverInfo.motd() : "&cOffline") - .replaceAll("%node%", server.getNode().getName()) - .replaceAll("%owner%", server.getOfflinePlayer().getName() == null ? "Unknown" : server.getOfflinePlayer().getName()) - .replaceAll("%ip%", server.getNode().getIp()) - )) - .skullOwner(server.getOfflinePlayer().getName() == null ? "MHF_Question" : server.getOfflinePlayer().getName()) - .lore(lore) - .build(); - - menu.addButton(new SGButton(item).withListener( - listener -> BungeeUtil.connectPlayer(player, server.getId()))); - }); - - XSound.BLOCK_NOTE_BLOCK_BASS.play(player); - player.openInventory(menu.getInventory()); - + BungeeUtil.checkIfOnline(player); } } diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/BungeeUtil.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/BungeeUtil.java index 8a2d7d8..662ee30 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/BungeeUtil.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/BungeeUtil.java @@ -75,4 +75,15 @@ public class BungeeUtil { player.closeInventory(); } + public static void checkIfOnline(Player player) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + + try { + out.writeUTF("hcp"); + } catch (Exception e) { + e.printStackTrace(); + } + player.sendPluginMessage(PsHubCore.getInstance(), "BungeeCord", out.toByteArray()); + player.closeInventory(); + } } \ No newline at end of file diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/ChatUtil.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/ChatUtil.java index 81bf5f5..2c554c1 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/ChatUtil.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/ChatUtil.java @@ -2,6 +2,11 @@ package os.arcadiadevs.playerservers.hubcore.utils; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import os.arcadiadevs.playerservers.hubcore.dto.ServerRecord; +import os.arcadiadevs.playerservers.hubcore.enums.ServerStatus; + +import java.util.Collections; +import java.util.List; public class ChatUtil { diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/formatter/Formattable.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/formatter/Formattable.java new file mode 100644 index 0000000..c41a7c2 --- /dev/null +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/formatter/Formattable.java @@ -0,0 +1,9 @@ +package os.arcadiadevs.playerservers.hubcore.utils.formatter; + +import java.util.HashMap; + +public interface Formattable { + + HashMap getPlaceHolders(); + +} diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/formatter/Formatter.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/formatter/Formatter.java new file mode 100644 index 0000000..182ddbb --- /dev/null +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/formatter/Formatter.java @@ -0,0 +1,26 @@ +package os.arcadiadevs.playerservers.hubcore.utils.formatter; + +import java.util.ArrayList; +import java.util.List; + +public class Formatter { + + public static String format(T t, String input) { + for (var entry : t.getPlaceHolders().entrySet()) { + input = input.replace(entry.getKey(), entry.getValue()); + } + + return input; + } + + public static List format(T t, List input) { + List output = new ArrayList<>(); + + for (String s : input) { + output.add(format(t, s)); + } + + return output; + } + +}