GUI and config updates

This commit is contained in:
Marko Milovanovic 2022-07-22 19:45:23 +02:00
parent e7f66472fc
commit 4971314e73
7 changed files with 175 additions and 118 deletions

View File

@ -89,12 +89,12 @@ public class DataBase {
}
}
public static boolean containsServer(String serverID) {
public static boolean containsServer(String UUID) {
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))
if (rs.getString("UUID").equals(UUID))
return true;
}
return false;

View File

@ -22,11 +22,11 @@ public class ClickEvent implements Listener {
return;
}
if (player.getInventory().getHeldItemSlot() == PSHubCore.getInstance().getConfig().getInt("gui.item.location")) {
if (player.getInventory().getHeldItemSlot() == PSHubCore.getInstance().getConfig().getInt("gui.selector.item.location")) {
GUIUtils.openSelector(player);
event.setCancelled(true);
}
if (player.getInventory().getHeldItemSlot() == PSHubCore.getInstance().getConfig().getInt("gui.item.location1")) {
if (player.getInventory().getHeldItemSlot() == PSHubCore.getInstance().getConfig().getInt("gui.player-menu.item.location")) {
GUIUtils.openMenu(player);
event.setCancelled(true);
}
@ -40,8 +40,8 @@ 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");
final var slot = PSHubCore.getInstance().getConfig().getInt("gui.selector.item.location");
final var slot1 = PSHubCore.getInstance().getConfig().getInt("gui.player-menu.item.location");
if (event.getSlot() == slot && (inventoryType == InventoryType.PLAYER || inventoryType == InventoryType.CRAFTING)
|| event.getSlot() == slot1 && (inventoryType == InventoryType.PLAYER || inventoryType == InventoryType.CRAFTING)) {
@ -57,8 +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") ||
player.getInventory().getHeldItemSlot() == PSHubCore.getInstance().getConfig().getInt("gui.item.location1")) {
if (player.getInventory().getHeldItemSlot() == PSHubCore.getInstance().getConfig().getInt("gui.selector.item.location") ||
player.getInventory().getHeldItemSlot() == PSHubCore.getInstance().getConfig().getInt("gui.player-menu.item.location")) {
event.setCancelled(true);
}
}

View File

@ -19,15 +19,24 @@ public class JoinEvent implements Listener {
@EventHandler
public void onJoin(PlayerJoinEvent e) {
final var itemMaterial = PSH.getConfig().getString("gui.item.material");
final var itemMaterial1 = PSH.getConfig().getString("gui.item.material1");
final var itemMaterial = PSH.getConfig().getString("gui.selector.item.material");
final var itemMaterial1 = PSH.getConfig().getString("gui.player-menu.item.material");
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")) {
if (!PSH.getConfig().getBoolean("gui.selector.item.enabled")) {
player.getInventory().forEach(item -> {
if (item.getType() == selectorMaterial || item.getType() == selectorMaterial1) {
if (item.getType() == selectorMaterial) {
player.getInventory().remove(item);
}
});
return;
}
if (!PSH.getConfig().getBoolean("gui.player-menu.item.enabled")) {
player.getInventory().forEach(item -> {
if (item.getType() == selectorMaterial1) {
player.getInventory().remove(item);
}
});
@ -40,15 +49,15 @@ public class JoinEvent implements Listener {
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")));
itemMeta.setDisplayName(ChatUtil.translate(PSH.getConfig().getString("gui.selector.item.name")));
itemMeta1.setDisplayName(ChatUtil.translate(PSH.getConfig().getString("gui.player-menu.item.name")));
final List<String> lore = PSH.getConfig().getStringList("gui.item.description")
final List<String> lore = PSH.getConfig().getStringList("gui.selector.item.description")
.stream()
.map(ChatUtil::translate)
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
final List<String> lore1 = PSH.getConfig().getStringList("gui.item.description1")
final List<String> lore1 = PSH.getConfig().getStringList("gui.player-menu.item.description")
.stream()
.map(ChatUtil::translate)
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
@ -58,8 +67,8 @@ public class JoinEvent implements Listener {
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));
Bukkit.getScheduler().runTask(PSH, () -> player.getInventory().setItem(PSH.getConfig().getInt("gui.selector.item.location"), itemStack));
Bukkit.getScheduler().runTask(PSH, () -> player.getInventory().setItem(PSH.getConfig().getInt("gui.player-menu.item.location"), itemStack1));
}).start();
}

View File

@ -23,7 +23,7 @@ public class ServerCache {
@SneakyThrows
private void startTask() {
new Thread(() -> {
while(PSHubCore.getInstance().isEnabled()) {
while (PSHubCore.getInstance().isEnabled()) {
final List<Server> servers;
try {
@ -33,7 +33,6 @@ public class ServerCache {
return;
}
servers.sort((s1, s2) -> {
final var p1 = s1.getServerStatus() == ServerStatus.ONLINE;
final var p2 = s2.getServerStatus() == ServerStatus.ONLINE;

View File

@ -70,4 +70,17 @@ public class BungeeUtil {
event.getWhoClicked().closeInventory();
}
public static void startServer(InventoryClickEvent event, Player player, String server) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
try {
out.writeUTF("start");
out.writeUTF(server);
} catch (Exception e) {
e.printStackTrace();
}
player.sendPluginMessage(PSHubCore.getInstance(), "BungeeCord", out.toByteArray());
event.getWhoClicked().closeInventory();
}
}

View File

@ -4,10 +4,12 @@ import com.cryptomorin.xseries.XMaterial;
import com.samjakob.spigui.buttons.SGButton;
import com.samjakob.spigui.item.ItemBuilder;
import org.bukkit.Bukkit;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import os.arcadiadevs.playerservers.hubcore.PSHubCore;
import os.arcadiadevs.playerservers.hubcore.database.DataBase;
import os.arcadiadevs.playerservers.hubcore.enums.ServerStatus;
import os.arcadiadevs.playerservers.hubcore.objects.Server;
import java.util.ArrayList;
@ -16,13 +18,13 @@ public class GUIUtils {
public static void openSelector(Player player) {
final var PSH = PSHubCore.getInstance();
final var menu = PSH.getSpiGUI().create(ChatUtil.translate(PSH.getConfig().getString("gui.menu.name")), 6);
final var menu = PSH.getSpiGUI().create(ChatUtil.translate(PSH.getConfig().getString("gui.selector.menu.name")), 5);
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 onlineXMaterialOptional = XMaterial.matchXMaterial(PSH.getConfig().getString("gui.selector.menu.online.block"));
final var offlineXMaterialOptional = XMaterial.matchXMaterial(PSH.getConfig().getString("gui.selector.menu.offline.block"));
final var onlineXMaterial = onlineXMaterialOptional.orElse(XMaterial.EMERALD_BLOCK).parseMaterial();
final var offlineXMaterial = offlineXMaterialOptional.orElse(XMaterial.REDSTONE_BLOCK).parseMaterial();
@ -33,36 +35,36 @@ public class GUIUtils {
servers.forEach(server -> {
final var itemBuilder = new ItemBuilder(server.getServerStatus() == ServerStatus.ONLINE ? onlineXMaterial : offlineXMaterial);
var lore = PSH.getConfig().getStringList(server.getServerStatus() == ServerStatus.ONLINE ? "gui.menu.online.lore" : "gui.menu.offline.lore");
var lore = PSH.getConfig().getStringList(server.getServerStatus() == ServerStatus.ONLINE ? "gui.selector.menu.online.lore" : "gui.selector.menu.offline.lore");
lore = lore.stream()
.map(s -> s.replaceAll("%server%", server.getUniqueId()))
.map(s -> s.replaceAll("%status%", server.isOnline() ? "&aOnline" : "&cOffline"))
.map(s -> s.replaceAll("%players%", server.isOnline() ? server.getPlayers() + "" : "0"))
.map(s -> s.replaceAll("%maxplayers%", server.isOnline() ? server.getMaxPlayers() + "" : "0"))
.map(s -> s.replaceAll("%server%", server.getPlayerName()))
.map(s -> s.replaceAll("%status%", server.getServerStatus() == ServerStatus.ONLINE ? "&aOnline" : "&cOffline"))
.map(s -> s.replaceAll("%players%", server.getServerStatus() == ServerStatus.ONLINE ? server.getData().getOnline() + "" : "0"))
.map(s -> s.replaceAll("%maxplayers%", server.getServerStatus() == ServerStatus.ONLINE ? server.getData().getMax() + "" : "0"))
.map(s -> s.replaceAll("%port%", server.getPort() + ""))
.map(s -> s.replaceAll("%motd%", server.isOnline() ? server.getMotd() : "&cOffline"))
.map(s -> s.replaceAll("%motd%", server.getServerStatus() == ServerStatus.ONLINE ? server.getData().getMOTD() : "&cOffline"))
.map(s -> s.replaceAll("%node%", server.getNode()))
.map(s -> s.replaceAll("%owner%", server.getOwner()))
.map(s -> s.replaceAll("%ip%", server.getAddress()))
.map(s -> s.replaceAll("%owner%", server.getPlayerName()))
.map(s -> s.replaceAll("%ip%", server.getHostname()))
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
var item = itemBuilder
.name(ChatUtil.translate(PSH.getConfig().getString(server.isOnline() ? "gui.menu.online.name" : "gui.menu.offline.name")
.replaceAll("%server%", server.getUniqueId())
.replaceAll("%status%", server.isOnline() ? "&aOnline" : "&cOffline"))
.replaceAll("%players%", server.isOnline() ? server.getPlayers() + "" : "0")
.replaceAll("%maxplayers%", server.isOnline() ? server.getMaxPlayers() + "" : "0")
.replaceAll("%port%", server.getPort() + "")
.replaceAll("%motd%", server.isOnline() ? server.getMotd() : "&cOffline")
.replaceAll("%node%", server.getNode())
.replaceAll("%owner%", server.getOwner())
.replaceAll("%ip%", server.getAddress())
.name(ChatUtil.translate(PSH.getConfig().getString(server.getServerStatus() == ServerStatus.ONLINE ? "gui.selector.menu.online.name" : "gui.selector.menu.offline.name")
.replaceAll("%server%", server.getPlayerName())
.replaceAll("%status%", server.getServerStatus() == ServerStatus.ONLINE ? "&aOnline" : "&cOffline"))
.replaceAll("%players%", server.getServerStatus() == ServerStatus.ONLINE ? server.getData().getOnline() + "" : "0")
.replaceAll("%maxplayers%", server.getServerStatus() == ServerStatus.ONLINE ? server.getData().getMax() + "" : "0")
.replaceAll("%port%", server.getPort() + "")
.replaceAll("%motd%", server.getServerStatus() == ServerStatus.ONLINE ? server.getData().getMOTD() : "&cOffline")
.replaceAll("%node%", server.getNode())
.replaceAll("%owner%", server.getPlayerName())
.replaceAll("%ip%", server.getHostname())
)
.lore(lore)
.build();
menu.setButton(0, menu.getInventory().firstEmpty(), new SGButton(item).withListener(listener -> BungeeUtil.connectPlayer(listener, player, server.getUniqueId())));
menu.setButton(0, menu.getInventory().firstEmpty(), new SGButton(item).withListener(listener -> BungeeUtil.connectPlayer(listener, player, server.getPlayerName())));
});
Bukkit.getScheduler().runTask(PSHubCore.getInstance(), () -> player.openInventory(menu.getInventory()));
@ -72,57 +74,69 @@ public class GUIUtils {
public static void openMenu(Player player) {
final var PSH = PSHubCore.getInstance();
final var menu = PSH.getSpiGUI().create(ChatUtil.translate(PSH.getConfig().getString("gui.menu.name1")), 3);
final var db = new DataBase();
final var menu = PSH.getSpiGUI().create(ChatUtil.translate(PSH.getConfig().getString("gui.player-menu.menu.name")), 4);
menu.setAutomaticPaginationEnabled(false);
menu.setBlockDefaultInteractions(true);
new Thread(() -> {
if (!DataBase.containsServer(player.getUniqueId().toString())) {
var itemCreate = new ItemBuilder(XMaterial.DIAMOND_BLOCK.parseMaterial())
.name(ChatUtil.translate("&aCreate Server"))
.build();
final var servers = PSHubCore.getInstance().getServerCache().getServers();
menu.setButton(0, 13, new SGButton(itemCreate).withListener(listener -> BungeeUtil.createServer(listener, player, player.getDisplayName())));
Bukkit.getScheduler().runTask(PSHubCore.getInstance(), () -> player.openInventory(menu.getInventory()));
return;
}
servers.forEach(server -> {
if (!DataBase.containsServer(server.getUniqueId())) {
var itemCreate = new ItemBuilder(XMaterial.DIAMOND_BLOCK.parseMaterial())
.name(ChatUtil.translate("&aCreate Server"))
.build();
final var server = new Server(player);
menu.setButton(0, 13, new SGButton(itemCreate).withListener(listener -> BungeeUtil.createServer(listener, player, player.getDisplayName())));
return;
}
var lore = PSH.getConfig().getStringList(server.getServerStatus() == ServerStatus.ONLINE ? "gui.player-menu.menu.online.lore" : "gui.player-menu.menu.offline.lore");
if (server.isOnline()) {
var itemJoin = new ItemBuilder(XMaterial.EMERALD_BLOCK.parseMaterial())
.name(ChatUtil.translate("&aJoin Server"))
.build();
lore = lore.stream()
.map(s -> s.replaceAll("%server%", server.getPlayerName()))
.map(s -> s.replaceAll("%status%", server.getServerStatus() == ServerStatus.ONLINE ? "&aOnline" : "&cOffline"))
.map(s -> s.replaceAll("%players%", server.getServerStatus() == ServerStatus.ONLINE ? server.getData().getOnline() + "" : "0"))
.map(s -> s.replaceAll("%maxplayers%", server.getServerStatus() == ServerStatus.ONLINE ? server.getData().getMax() + "" : "0"))
.map(s -> s.replaceAll("%port%", server.getPort() + ""))
.map(s -> s.replaceAll("%motd%", server.getServerStatus() == ServerStatus.ONLINE ? server.getData().getMOTD() : "&cOffline"))
.map(s -> s.replaceAll("%node%", server.getNode()))
.map(s -> s.replaceAll("%owner%", server.getPlayerName()))
.map(s -> s.replaceAll("%ip%", server.getHostname()))
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
var itemDelete = new ItemBuilder(XMaterial.BARRIER.parseMaterial())
.name("&cDelete Server")
.build();
final var itemPlayer = new ItemBuilder(XMaterial.OAK_SIGN.parseMaterial())
.name(ChatUtil.translate(PSH.getConfig().getString(server.getServerStatus() == ServerStatus.ONLINE ? "gui.player-menu.menu.online.name" : "gui.player-menu.menu.offline.name")
.replaceAll("%server%", server.getPlayerName())))
.lore(lore)
.build();
var itemPlayer = new ItemBuilder(XMaterial.OAK_SIGN.parseMaterial())
.name(player.getDisplayName())
.lore()
.build();
var itemDelete = new ItemBuilder(XMaterial.BARRIER.parseMaterial())
.name("&cDelete Server")
.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();
if (server.getServerStatus() == ServerStatus.ONLINE) {
var itemJoin = new ItemBuilder(XMaterial.OAK_DOOR.parseMaterial())
.name(ChatUtil.translate("&aJoin Server"))
.build();
var itemStop = new ItemBuilder(XMaterial.REDSTONE_BLOCK.parseMaterial())
.name(ChatUtil.translate("&cStop Server"))
.build();
menu.setButton(0, 22, new SGButton(itemJoin).withListener(listener -> BungeeUtil.connectPlayer(listener, player, player.getDisplayName())));
menu.setButton(0, 20, new SGButton(itemStop).withListener(listener -> BungeeUtil.stopServer(listener, player, player.getDisplayName())));
}
var itemPlayer = new ItemBuilder(XMaterial.OAK_SIGN.parseMaterial())
.name(player.getDisplayName())
.lore()
.build();
if (server.getServerStatus() == ServerStatus.OFFLINE) {
var itemStart = new ItemBuilder(XMaterial.EMERALD_BLOCK.parseMaterial())
.name(ChatUtil.translate("&aStart Server"))
.build();
menu.setButton(0, 12, new SGButton(itemStop).withListener(listener -> BungeeUtil.stopServer(listener, player, player.getDisplayName())));
menu.setButton(0, 4, new SGButton(itemPlayer));
}
});
menu.setButton(0, 20, new SGButton(itemStart).withListener(listener -> BungeeUtil.startServer(listener, player, player.getDisplayName())));
}
menu.setButton(0, 24, new SGButton(itemDelete).withListener(listener -> BungeeUtil.deleteServer(listener, player, player.getDisplayName())));
menu.setButton(0, 4, new SGButton(itemPlayer));
Bukkit.getScheduler().runTask(PSHubCore.getInstance(), () -> player.openInventory(menu.getInventory()));
}).start();

View File

@ -4,47 +4,69 @@ mysql:
password: webmaster
database: playerservers
useSSL: true
gui:
enabled: true
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"
name: '&a%server%''s server'
lore:
- '&7Status: %status%'
- '&7Players: &2%players%&8/&a%maxplayers%'
- '&7Owner: &a%owner%'
- '&7Motd: &a%motd%'
- '&7Node: &a%node%'
- '&7Ip: &a%ip%&8:&a%port%'
offline:
block: "REDSTONE_BLOCK"
name: '&c%server%''s server'
lore:
- '&7Status: %status%'
- '&7Owner: &c%owner%'
- '&7Node: &c%node%'
- '&7Ip: &c%ip%&8:&c%port%'
selector:
item:
enabled: true
name: "&aQuick Compass"
material: "COMPASS"
location: 0
description:
- "&7Click here to open the server"
- "&7selector or to create your own one"
menu:
name: "&aServer Selector"
cache-time: 20
online:
block: "EMERALD_BLOCK"
name: '&a%server%''s server'
lore:
- '&7Status: %status%'
- '&7Players: &2%players%&8/&a%maxplayers%'
- '&7Owner: &a%owner%'
- '&7Motd: &a%motd%'
- '&7Node: &a%node%'
- '&7Ip: &a%ip%&8:&a%port%'
offline:
block: "REDSTONE_BLOCK"
name: '&c%server%''s server'
lore:
- '&7Status: %status%'
- '&7Owner: &c%owner%'
- '&7Node: &c%node%'
- '&7Ip: &c%ip%&8:&c%port%'
player-menu:
item:
enabled: true
name: "&eMy PlayerServers"
material: "EMERALD"
location: 8
description:
- "&7Click here to open the server"
- "&7selector or to create your own one"
menu:
name: "&eMy PlayerServers"
online:
name: '&a%server%''s server'
lore:
- '&7Status: %status%'
- '&7Players: &2%players%&8/&a%maxplayers%'
- '&7Owner: &a%owner%'
- '&7Motd: &a%motd%'
- '&7Node: &a%node%'
- '&7Ip: &a%ip%&8:&a%port%'
offline:
name: '&c%server%''s server'
lore:
- '&7Status: %status%'
- '&7Owner: &c%owner%'
- '&7Node: &c%node%'
- '&7Ip: &c%ip%&8:&c%port%'
disable-weather: true
disable-damage: true
# EXPERIMENTAL
multi-node: false