General improvements and fixes

This commit is contained in:
Marko Milovanovic 2022-06-05 04:47:59 +02:00
parent 68842155a0
commit 6c673a19bc
5 changed files with 82 additions and 56 deletions

View File

@ -11,12 +11,12 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import os.arcadiadevs.playerservers.hubcore.PSHubCore;
import os.arcadiadevs.playerservers.hubcore.utils.ColorUtils;
import os.arcadiadevs.playerservers.hubcore.utils.ChatUtil;
import os.arcadiadevs.playerservers.hubcore.utils.GUIUtils;
import java.util.Objects;
import static os.arcadiadevs.playerservers.hubcore.utils.ColorUtils.translate;
import static os.arcadiadevs.playerservers.hubcore.utils.ChatUtil.translate;
@SuppressWarnings("UnstableApiUsage")
public class ClickEvent implements Listener {
@ -35,7 +35,6 @@ public class ClickEvent implements Listener {
e.setCancelled(true);
}
}
@EventHandler
@ -46,22 +45,6 @@ public class ClickEvent implements Listener {
if (e.getCurrentItem() != null && e.getCurrentItem().getItemMeta() != null
&& e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(translate(PSHubCore.getInstance().getConfig().getString("compass-name"))))
e.setCancelled(true);
else if (e.getView().getTitle().equalsIgnoreCase(translate("&aServer Selector"))) {
if (e.getCurrentItem() != null && e.getCurrentItem().getItemMeta() != null && e.getCurrentItem().getItemMeta().getLore() != null) {
if (e.getCurrentItem().getType() == XMaterial.EMERALD_BLOCK.parseMaterial()) {
String UUID = e.getCurrentItem().getItemMeta().getLore().get(1).split(" ")[1].replaceAll("§7", "");
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Connect");
out.writeUTF(UUID);
p.sendPluginMessage(PSHubCore.getInstance(), "BungeeCord", out.toByteArray());
e.getWhoClicked().closeInventory();
} else if (e.getCurrentItem().getType() == XMaterial.REDSTONE_BLOCK.parseMaterial())
p.sendMessage(ColorUtils.translate("&9PlayerServers> &7Oops, the server you tried to connect to is offline."));
e.setCancelled(true);
}
}
}
@EventHandler

View File

@ -13,7 +13,7 @@ import os.arcadiadevs.playerservers.hubcore.database.DataBase;
import java.util.ArrayList;
import static os.arcadiadevs.playerservers.hubcore.utils.ColorUtils.translate;
import static os.arcadiadevs.playerservers.hubcore.utils.ChatUtil.translate;
public class JoinEvent implements Listener {

View File

@ -0,0 +1,26 @@
package os.arcadiadevs.playerservers.hubcore.utils;
import com.cryptomorin.xseries.XMaterial;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import os.arcadiadevs.playerservers.hubcore.PSHubCore;
public class BungeeUtil {
public static void connectPlayer(InventoryClickEvent event, Player player, String server) {
if (event.getCurrentItem().getType() == XMaterial.REDSTONE_BLOCK.parseMaterial()) {
player.sendMessage(ChatUtil.translate("&9PlayerServers> &7Oops, the server you tried to connect to is offline."));
return;
}
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Connect");
out.writeUTF(server);
player.sendPluginMessage(PSHubCore.getInstance(), "BungeeCord", out.toByteArray());
event.getWhoClicked().closeInventory();
}
}

View File

@ -2,7 +2,7 @@ package os.arcadiadevs.playerservers.hubcore.utils;
import org.bukkit.ChatColor;
public class ColorUtils {
public class ChatUtil {
public static String translate(String text) {
return ChatColor.translateAlternateColorCodes('&', text);

View File

@ -5,7 +5,6 @@ import com.samjakob.spigui.SGMenu;
import com.samjakob.spigui.buttons.SGButton;
import com.samjakob.spigui.item.ItemBuilder;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import os.arcadiadevs.playerservers.hubcore.PSHubCore;
import os.arcadiadevs.playerservers.hubcore.database.DataBase;
@ -26,47 +25,65 @@ public class GUIUtils {
final PingUtil pu = new PingUtil();
final PSHubCore PSH = PSHubCore.getInstance();
final SGMenu menu = PSH.spiGUI.create(ChatColor.GREEN + "Server Selector " + ChatColor.GRAY + "(Page {currentPage}/{maxPage})", 5);
final Map<String, Object> map = PSH.multinode.getTable("servers").toMap();
final SGMenu menu = PSH.spiGUI.create(ChatUtil.translate("&aServer Selector"), 5);
final Map<String, Object> map = PSHubCore.getInstance().multinode.getTable("servers").toMap();
menu.setAutomaticPaginationEnabled(true);
menu.setBlockDefaultInteractions(true);
Bukkit.getScheduler().runTaskAsynchronously(PSH, () -> {
for (DBInfoStructure is : db.getServersInfo()) {
if (pu.isOnline(PSH.getConfig().getBoolean("multi-node") ? map.get(is.getNode()).toString() : "127.0.0.1", is.getPort())) {
new Thread(() -> {
final var servers = db.getServersInfo();
final PingInfoStructure pus = pu.getData(Integer.parseInt(is.getPort()));
// Sort servers by pu.isOnline(server)
servers.sort((s1, s2) -> {
final var pingUtil1 = new PingUtil(map.get(s1.getNode()).toString().split(" ")[0].replaceAll(":8080", ""), s1.getPort());
final var pingUtil2 = new PingUtil(map.get(s2.getNode()).toString().split(" ")[0].replaceAll(":8080", ""), s2.getPort());
menu.setButton(0, menu.getInventory().firstEmpty(), new SGButton(
new ItemBuilder(Objects.requireNonNull(XMaterial.EMERALD_BLOCK.parseMaterial()))
.name(translate("&a" + is.getPlayerName() + "'s server"))
.lore(
translate("&7UUID: &a" + is.getServerID().split("-")[0]),
translate("&7Port: &a" + is.getPort()),
translate(String.format("&cOnline: &7%d/%d", pus.getOnline(), pus.getMax())),
translate("&7MOTD: &a" + pus.getMOTD())
)
.build()
));
}
final var p1 = pingUtil1.isOnline();
final var p2 = pingUtil2.isOnline();
if (p1 && !p2) {
return -1;
} else if (!p1 && p2) {
return 1;
} else {
return 0;
}
});
servers.forEach(server -> {
final var pingUtil = new PingUtil(map.get(server.getNode()).toString().split(" ")[0].replaceAll(":8080", ""), server.getPort());
final var itemBuilder = new ItemBuilder(Objects.requireNonNull(
pingUtil.isOnline() ? XMaterial.EMERALD_BLOCK.parseMaterial() : XMaterial.REDSTONE_BLOCK.parseMaterial()
));
final ItemStack item;
if (pingUtil.isOnline()) {
item = itemBuilder
.name(ChatUtil.translate("&a" + server.getPlayerName() + "'s server"))
.lore(
ChatUtil.translate("&7UUID: &a" + server.getServerId().split("-")[0]),
ChatUtil.translate("&7Port: &a" + server.getPort()),
ChatUtil.translate(String.format("&7Online: &a%d/%d", pingUtil.getData().getOnline(), pingUtil.getData().getMax())),
ChatUtil.translate("&7MOTD: &a" + pingUtil.getData().getMOTD())
)
.build();
} else {
item = itemBuilder
.name(ChatUtil.translate("&c" + server.getPlayerName() + "'s server"))
.lore(
ChatUtil.translate("&7UUID: &c" + server.getServerId().split("-")[0]),
ChatUtil.translate("&7Port: &c" + server.getPort())
)
.build();
}
for (DBInfoStructure is : db.getServersInfo()) {
if (!pu.isOnline(PSH.getConfig().getBoolean("multi-node") ? map.get(is.getNode()).toString() : "127.0.0.1", is.getPort())) {
menu.setButton(0, menu.getInventory().firstEmpty(), new SGButton(item).withListener(listener -> BungeeUtil.connectPlayer(listener, player, server.getServerId())));
});
menu.setButton(0, menu.getInventory().firstEmpty(), new SGButton(
new ItemBuilder(Objects.requireNonNull(XMaterial.REDSTONE_BLOCK.parseMaterial()))
.name(translate("&c" + is.getPlayerName() + "'s server"))
.lore(
translate("&7Status: &cOffline"),
translate("&7UUID: &c" + is.getServerID().split("-")[0]),
translate("&7Port: &c" + is.getPort())
)
.build()
));
}
}
Bukkit.getScheduler().runTask(PSH, () -> player.openInventory(menu.getInventory()));
});
Bukkit.getScheduler().runTask(PSHubCore.getInstance(), () -> player.openInventory(menu.getInventory()));
}).start();
}
}