mirror of
https://github.com/arcadiadevs/hubcore.git
synced 2024-11-24 06:35:10 +01:00
New system for compass
This commit is contained in:
parent
fa918759cd
commit
4abed8ad51
12
pom.xml
12
pom.xml
@ -188,8 +188,18 @@
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>6.1.5.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>24.0.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.10.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<groupId>PSHubCore</groupId>
|
||||
<artifactId>PSHubCore</artifactId>
|
||||
<version>v3-dev</version>
|
||||
|
@ -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);
|
||||
|
@ -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<String, String> 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);
|
||||
}};
|
||||
}
|
||||
}
|
@ -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<List<ServerRecord>> typeToken = new TypeToken<>() {
|
||||
};
|
||||
List<ServerRecord> serverRecord = new Gson().fromJson(json, typeToken.getType());
|
||||
|
||||
openGui(player, serverRecord);
|
||||
System.out.println("Server record: " + serverRecord);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void openGui(Player player, List<ServerRecord> 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<Server> 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<Server> 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<String> onlineLore = Formatter.format(server,
|
||||
instance.getConfig().getStringList("gui.selector.menu.online.lore"));
|
||||
|
||||
List<String> 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());
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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<Server> 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<Server> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
||||
|
@ -0,0 +1,9 @@
|
||||
package os.arcadiadevs.playerservers.hubcore.utils.formatter;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public interface Formattable {
|
||||
|
||||
HashMap<String, String> getPlaceHolders();
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package os.arcadiadevs.playerservers.hubcore.utils.formatter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Formatter {
|
||||
|
||||
public static <T extends Formattable> String format(T t, String input) {
|
||||
for (var entry : t.getPlaceHolders().entrySet()) {
|
||||
input = input.replace(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
public static <T extends Formattable> List<String> format(T t, List<String> input) {
|
||||
List<String> output = new ArrayList<>();
|
||||
|
||||
for (String s : input) {
|
||||
output.add(format(t, s));
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user