diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/PSHubCore.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/PSHubCore.java index 4abd98c..7f7be3f 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/PSHubCore.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/PSHubCore.java @@ -5,6 +5,7 @@ import com.samjakob.spigui.SpiGUI; import lombok.Getter; import lombok.SneakyThrows; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import os.arcadiadevs.playerservers.hubcore.commands.CommandManager; import os.arcadiadevs.playerservers.hubcore.database.DataSource; @@ -48,7 +49,7 @@ public class PSHubCore extends JavaPlugin { new PlayerCount(this).register(); } - getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); + Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); Bukkit.getPluginManager().registerEvents(new ClickEvent(), this); Bukkit.getPluginManager().registerEvents(new JoinEvent(), this); Bukkit.getPluginManager().registerEvents(new HubEvents(), this); @@ -91,6 +92,6 @@ public class PSHubCore extends JavaPlugin { @Override public void onDisable() { super.onDisable(); + getServer().getMessenger().unregisterIncomingPluginChannel(this, "BungeeCord"); } - } diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/commands/CommandManager.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/commands/CommandManager.java index 5533abe..90f862a 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/commands/CommandManager.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/commands/CommandManager.java @@ -21,8 +21,7 @@ public class CommandManager implements CommandExecutor { if (PSHubCore.getInstance().getConfig().getBoolean("gui.enabled")) { if (command.getName().equalsIgnoreCase("servers") || command.getName().equalsIgnoreCase("menu") || command.getName().equalsIgnoreCase("opengui")) { Player player = (Player) commandSender; - GUIUtils gu = new GUIUtils(); - gu.openSelector(player); + GUIUtils.openSelector(player); } } } diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/database/DataBase.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/database/DataBase.java index 1d7fa7c..d90d674 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/database/DataBase.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/database/DataBase.java @@ -11,10 +11,10 @@ public class DataBase { public boolean containsPort(String port) { try (Connection connection = DataSource.getConnection()) { - PreparedStatement stmt = connection.prepareStatement("SELECT * FROM PLAYERSERVERS"); + PreparedStatement stmt = connection.prepareStatement("SELECT * FROM PlayerServers"); ResultSet rs = stmt.executeQuery(); while(rs.next()) { - if (rs.getString("PORT").equals(port)) + if (rs.getString("Port").equals(port)) return true; } return false; @@ -30,16 +30,15 @@ public class DataBase { ArrayList output = new ArrayList<>(); try (Connection connection = DataSource.getConnection()) { - PreparedStatement stmt = connection.prepareStatement("SELECT * FROM PLAYERSERVERS"); + PreparedStatement stmt = connection.prepareStatement("SELECT * FROM PlayerServers"); ResultSet rs = stmt.executeQuery(); while(rs.next()) { output.add(new DBInfoStructure( rs.getString("UUID"), - rs.getString("SERVERID").split("-")[0], - rs.getInt("PORT"), - rs.getString("NAME"), - rs.getString("PLAYERNAME"), - rs.getString("NODE")) + rs.getString("ServerID").split("-")[0], + rs.getInt("Port"), + rs.getString("PlayerName"), + rs.getString("Node")) ); } return output; @@ -53,9 +52,9 @@ public class DataBase { public String getPortByUUID(String UUID) { try (Connection connection = DataSource.getConnection()) { - PreparedStatement stmt = connection.prepareStatement("SELECT * FROM PLAYERSERVERS WHERE UUID='" + UUID + "'"); + PreparedStatement stmt = connection.prepareStatement("SELECT * FROM PlayerServers WHERE UUID='" + UUID + "'"); ResultSet rs = stmt.executeQuery(); rs.next(); - return rs.getString("PORT"); + return rs.getString("Port"); } catch (Exception e) { e.printStackTrace(); @@ -65,13 +64,29 @@ public class DataBase { public String getServerByUUID(String UUID) { try (Connection connection = DataSource.getConnection()) { - PreparedStatement stmt = connection.prepareStatement("SELECT * FROM PLAYERSERVERS WHERE UUID='" + UUID + "'"); + PreparedStatement stmt = connection.prepareStatement("SELECT * FROM PlayerServers WHERE UUID='" + UUID + "'"); ResultSet rs = stmt.executeQuery(); rs.next(); - return rs.getString("SERVERID"); + return rs.getString("ServerID"); } catch (Exception e) { e.printStackTrace(); return null; } } -} + + public boolean containsServer(String serverID) { + try (Connection connection = DataSource.getConnection()) { + PreparedStatement stmt = connection.prepareStatement("SELECT * FROM PlayerServers"); + ResultSet rs = stmt.executeQuery(); + while(rs.next()) { + if (rs.getString("ServerID").equals(serverID)) + return true; + } + return false; + } + catch (Exception e) { + e.printStackTrace(); + return false; + } + } +} \ No newline at end of file diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/database/DataSource.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/database/DataSource.java index b82de61..081ea02 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/database/DataSource.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/database/DataSource.java @@ -41,4 +41,4 @@ public class DataSource return hikari.getConnection(); } -} +} \ No newline at end of file diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/database/structures/DBInfoStructure.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/database/structures/DBInfoStructure.java index cad56bf..858c968 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/database/structures/DBInfoStructure.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/database/structures/DBInfoStructure.java @@ -7,19 +7,17 @@ public class DBInfoStructure { private final String UUID; private final String serverId; - private final String serverName; private final String playerName; private final String node; private final int port; - public DBInfoStructure(String UUID, String ServerID, int Port, String ServerName, String PlayerName, String node) { + public DBInfoStructure(String UUID, String ServerID, int Port, String PlayerName, String node) { this.UUID = UUID; this.serverId = ServerID; this.port = Port; - this.serverName = ServerName; this.playerName = PlayerName; this.node = node; } -} +} \ No newline at end of file diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/database/structures/PingInfoStructure.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/database/structures/PingInfoStructure.java index 0b48dd8..a502f44 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/database/structures/PingInfoStructure.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/database/structures/PingInfoStructure.java @@ -23,4 +23,4 @@ public class PingInfoStructure { public String getMOTD() { return MOTD; } -} +} \ No newline at end of file diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/events/ClickEvent.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/events/ClickEvent.java index 6e6ec9a..6a2b939 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/events/ClickEvent.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/events/ClickEvent.java @@ -26,6 +26,10 @@ public class ClickEvent implements Listener { GUIUtils.openSelector(player); event.setCancelled(true); } + if (player.getInventory().getHeldItemSlot() == PSHubCore.getInstance().getConfig().getInt("gui.item.location1")) { + GUIUtils.openMenu(player); + event.setCancelled(true); + } } @EventHandler @@ -37,8 +41,10 @@ public class ClickEvent implements Listener { final var player = (Player) event.getWhoClicked(); final var inventoryType = event.getInventory().getType(); final var slot = PSHubCore.getInstance().getConfig().getInt("gui.item.location"); + final var slot1 = PSHubCore.getInstance().getConfig().getInt("gui.item.location1"); - if (event.getSlot() == slot && (inventoryType == InventoryType.PLAYER || inventoryType == InventoryType.CRAFTING)) { + if (event.getSlot() == slot && (inventoryType == InventoryType.PLAYER || inventoryType == InventoryType.CRAFTING) + || event.getSlot() == slot1 && (inventoryType == InventoryType.PLAYER || inventoryType == InventoryType.CRAFTING)) { if (player.getGameMode() == GameMode.CREATIVE) { player.closeInventory(); } @@ -51,7 +57,8 @@ public class ClickEvent implements Listener { public void drop(PlayerDropItemEvent event) { final var player = event.getPlayer(); - if (player.getInventory().getHeldItemSlot() == PSHubCore.getInstance().getConfig().getInt("gui.item.location")) { + if (player.getInventory().getHeldItemSlot() == PSHubCore.getInstance().getConfig().getInt("gui.item.location") || + player.getInventory().getHeldItemSlot() == PSHubCore.getInstance().getConfig().getInt("gui.item.location1")) { event.setCancelled(true); } } diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/events/JoinEvent.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/events/JoinEvent.java index a912d18..28a355c 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/events/JoinEvent.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/events/JoinEvent.java @@ -19,13 +19,15 @@ public class JoinEvent implements Listener { @EventHandler public void onJoin(PlayerJoinEvent e) { - final var itemName = PSH.getConfig().getString("gui.item.material"); - final var guiMaterial = XMaterial.matchXMaterial(itemName).orElse(XMaterial.COMPASS).parseMaterial(); + final var itemMaterial = PSH.getConfig().getString("gui.item.material"); + final var itemMaterial1 = PSH.getConfig().getString("gui.item.material1"); + final var selectorMaterial = XMaterial.matchXMaterial(itemMaterial).orElse(XMaterial.COMPASS).parseMaterial(); + final var selectorMaterial1 = XMaterial.matchXMaterial(itemMaterial1).orElse(XMaterial.EMERALD).parseMaterial(); final var player = e.getPlayer(); if (!PSH.getConfig().getBoolean("gui.enabled") || !PSH.getConfig().getBoolean("gui.item.enabled")) { player.getInventory().forEach(item -> { - if (item.getType() == guiMaterial) { + if (item.getType() == selectorMaterial || item.getType() == selectorMaterial1) { player.getInventory().remove(item); } }); @@ -33,20 +35,32 @@ public class JoinEvent implements Listener { } new Thread(() -> { - final var itemStack = new ItemStack(guiMaterial); + final var itemStack = new ItemStack(selectorMaterial); + final var itemStack1 = new ItemStack(selectorMaterial1); final var itemMeta = itemStack.getItemMeta(); + final var itemMeta1 = itemStack1.getItemMeta(); itemMeta.setDisplayName(ChatUtil.translate(PSH.getConfig().getString("gui.item.name"))); + itemMeta1.setDisplayName(ChatUtil.translate(PSH.getConfig().getString("gui.item.name1"))); final List lore = PSH.getConfig().getStringList("gui.item.description") .stream() .map(ChatUtil::translate) .collect(ArrayList::new, ArrayList::add, ArrayList::addAll); + final List lore1 = PSH.getConfig().getStringList("gui.item.description1") + .stream() + .map(ChatUtil::translate) + .collect(ArrayList::new, ArrayList::add, ArrayList::addAll); + itemMeta.setLore(lore); + itemMeta1.setLore(lore1); itemStack.setItemMeta(itemMeta); + itemStack1.setItemMeta(itemMeta1); Bukkit.getScheduler().runTask(PSH, () -> player.getInventory().setItem(PSH.getConfig().getInt("gui.item.location"), itemStack)); + Bukkit.getScheduler().runTask(PSH, () -> player.getInventory().setItem(PSH.getConfig().getInt("gui.item.location1"), itemStack1)); }).start(); + } } diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/objects/ServerCache.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/objects/ServerCache.java index d25d9cf..0cd438a 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/objects/ServerCache.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/objects/ServerCache.java @@ -7,6 +7,8 @@ import os.arcadiadevs.playerservers.hubcore.database.DataBase; import os.arcadiadevs.playerservers.hubcore.utils.PingUtil; import java.util.ArrayList; +import java.util.Map; +import java.util.Objects; import java.util.concurrent.TimeUnit; public class ServerCache { @@ -22,12 +24,17 @@ public class ServerCache { new Thread(() -> { while(PSHubCore.getInstance().isEnabled()) { final var database = new DataBase(); - final var map = PSHubCore.getInstance().multinode.getTable("servers").toMap(); + Map map = null; final var rawServers = database.getServersInfo(); final var servers = new ArrayList(); + if (PSHubCore.getInstance().getConfig().getBoolean("multi-node")) { + map = PSHubCore.getInstance().multinode.getTable("servers").toMap(); + } + + Map finalMap = map; rawServers.forEach(server -> { - var pingUtil = new PingUtil(map.get(server.getNode()).toString().split(" ")[0].replaceAll(":8080", ""), server.getPort()); + var pingUtil = new PingUtil(PSHubCore.getInstance().getConfig().getBoolean("multi-node") ? finalMap.get(server.getNode()).toString().split(" ")[0].replaceAll(":8080", "") : "localhost", server.getPort()); var _server = new Server(); 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 648b781..dfe8498 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/BungeeUtil.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/BungeeUtil.java @@ -16,11 +16,57 @@ public class BungeeUtil { } ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF("Connect"); - out.writeUTF(server); + + try { + out.writeUTF("Connect"); + out.writeUTF(server); + } catch (Exception e) { + e.printStackTrace(); + } player.sendPluginMessage(PSHubCore.getInstance(), "BungeeCord", out.toByteArray()); event.getWhoClicked().closeInventory(); } -} + public static void stopServer(InventoryClickEvent event, Player player, String server) { + + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + + try { + out.writeUTF("stop"); + out.writeUTF(server); + } catch (Exception e) { + e.printStackTrace(); + } + player.sendPluginMessage(PSHubCore.getInstance(), "BungeeCord", out.toByteArray()); + event.getWhoClicked().closeInventory(); + } + + public static void deleteServer(InventoryClickEvent event, Player player, String server) { + + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + + try { + out.writeUTF("delete"); + out.writeUTF(server); + } catch (Exception e) { + e.printStackTrace(); + } + player.sendPluginMessage(PSHubCore.getInstance(), "BungeeCord", out.toByteArray()); + event.getWhoClicked().closeInventory(); + } + + public static void createServer(InventoryClickEvent event, Player player, String server) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + + try { + out.writeUTF("create"); + out.writeUTF(server); + } catch (Exception e) { + e.printStackTrace(); + } + player.sendPluginMessage(PSHubCore.getInstance(), "BungeeCord", out.toByteArray()); + event.getWhoClicked().closeInventory(); + } + +} \ No newline at end of file diff --git a/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/GUIUtils.java b/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/GUIUtils.java index 362dcb1..60f01cf 100644 --- a/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/GUIUtils.java +++ b/src/main/java/os/arcadiadevs/playerservers/hubcore/utils/GUIUtils.java @@ -6,6 +6,7 @@ import com.samjakob.spigui.item.ItemBuilder; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import os.arcadiadevs.playerservers.hubcore.PSHubCore; +import os.arcadiadevs.playerservers.hubcore.database.DataBase; import java.util.ArrayList; @@ -14,14 +15,14 @@ public class GUIUtils { public static void openSelector(Player player) { final var PSH = PSHubCore.getInstance(); - final var menu = PSH.spiGUI.create(ChatUtil.translate(PSH.getConfig().getString("gui.menu.name")), 5); + final var menu = PSH.spiGUI.create(ChatUtil.translate(PSH.getConfig().getString("gui.menu.name")), 6); menu.setAutomaticPaginationEnabled(true); menu.setBlockDefaultInteractions(true); final var onlineXMaterialOptional = XMaterial.matchXMaterial(PSH.getConfig().getString("gui.menu.online.block")); final var offlineXMaterialOptional = XMaterial.matchXMaterial(PSH.getConfig().getString("gui.menu.offline.block")); - + final var onlineXMaterial = onlineXMaterialOptional.orElse(XMaterial.EMERALD_BLOCK).parseMaterial(); final var offlineXMaterial = offlineXMaterialOptional.orElse(XMaterial.REDSTONE_BLOCK).parseMaterial(); @@ -66,4 +67,63 @@ public class GUIUtils { Bukkit.getScheduler().runTask(PSHubCore.getInstance(), () -> player.openInventory(menu.getInventory())); }).start(); } + + public static void openMenu(Player player) { + + final var PSH = PSHubCore.getInstance(); + final var menu = PSH.spiGUI.create(ChatUtil.translate(PSH.getConfig().getString("gui.menu.name1")), 3); + final var db = new DataBase(); + + menu.setAutomaticPaginationEnabled(false); + menu.setBlockDefaultInteractions(true); + + new Thread(() -> { + + final var servers = PSHubCore.getInstance().getServerCache().getServers(); + + servers.forEach(server -> { + if (!db.containsServer(server.getUniqueId())) { + var itemCreate = new ItemBuilder(XMaterial.DIAMOND_BLOCK.parseMaterial()) + .name(ChatUtil.translate("&aCreate Server")) + .build(); + + menu.setButton(0, 13, new SGButton(itemCreate).withListener(listener -> BungeeUtil.createServer(listener, player, player.getDisplayName()))); + return; + } + + if (server.isOnline()) { + var itemJoin = new ItemBuilder(XMaterial.EMERALD_BLOCK.parseMaterial()) + .name(ChatUtil.translate("&aJoin Server")) + .build(); + + var itemDelete = new ItemBuilder(XMaterial.BARRIER.parseMaterial()) + .name("&cDelete Server") + .build(); + + var itemPlayer = new ItemBuilder(XMaterial.OAK_SIGN.parseMaterial()) + .name(player.getDisplayName()) + .lore() + .build(); + + menu.setButton(0, 20, new SGButton(itemJoin).withListener(listener -> BungeeUtil.connectPlayer(listener, player, player.getDisplayName()))); + menu.setButton(0, 16, new SGButton(itemDelete).withListener(listener -> BungeeUtil.deleteServer(listener, player, player.getDisplayName()))); + menu.setButton(0, 4, new SGButton(itemPlayer)); + } else { + var itemStop = new ItemBuilder(XMaterial.REDSTONE_BLOCK.parseMaterial()) + .name(ChatUtil.translate("&cStop Server")) + .build(); + + var itemPlayer = new ItemBuilder(XMaterial.OAK_SIGN.parseMaterial()) + .name(player.getDisplayName()) + .lore() + .build(); + + menu.setButton(0, 12, new SGButton(itemStop).withListener(listener -> BungeeUtil.stopServer(listener, player, player.getDisplayName()))); + menu.setButton(0, 4, new SGButton(itemPlayer)); + } + }); + + Bukkit.getScheduler().runTask(PSHubCore.getInstance(), () -> player.openInventory(menu.getInventory())); + }).start(); + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9902a71..4e2ccba 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -9,13 +9,20 @@ gui: item: enabled: true name: "&aQuick Compass" + name1: "&eMy PlayerServers" material: "COMPASS" + material1: "EMERALD" location: 0 + location1: 8 description: - "&7Click here to open the server" - "&7selector or to create your own one" + description1: + - "&7Click here to open the server" + - "&7selector or to create your own one" menu: name: "&aServer Selector" + name1: "&eMy PlayerServers" cache-time: 20 online: block: "EMERALD_BLOCK"