Added AdminGui

This commit is contained in:
Cuftica 2022-12-11 23:14:20 +01:00
parent 2f7e8e40be
commit f60ddc3097
15 changed files with 440 additions and 163 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea
out
target
PSHubCore.iml

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="minecraft" name="Minecraft">
<configuration>
<autoDetectTypes>
<platformType>SPIGOT</platformType>
</autoDetectTypes>
</configuration>
</facet>
</component>
</module>

View File

@ -29,6 +29,50 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>target</id>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>PSHubCore</finalName>
<outputDirectory>${dir}</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>PSHubCore</finalName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<repositories>
<repository>
<id>jitpack.io</id>

48
pom.xml
View File

@ -32,6 +32,54 @@
</plugins>
</build>
<profiles>
<profile>
<id>target</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>PSHubCore</finalName>
<outputDirectory>${dir}</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>default</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>PSHubCore</finalName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<repositories>
<repository>
<id>jitpack.io</id>

View File

@ -74,6 +74,8 @@ public class PsHubCore extends JavaPlugin {
Bukkit.getPluginManager().registerEvents(new HubEvents(getConfig()), this);
Objects.requireNonNull(getCommand("servers")).setExecutor(new CommandManager());
Objects.requireNonNull(getCommand("playermenu")).setExecutor(new CommandManager());
Objects.requireNonNull(getCommand("adminmenu")).setExecutor(new CommandManager());
// Initialize SpiGUI
spiGui = new SpiGUI(this);
@ -90,10 +92,9 @@ public class PsHubCore extends JavaPlugin {
* Extracts a file from the jar to the data folder.
*
* @param name The name of the file.
* @return The file
* @throws IOException If the file cannot be extracted.
*/
private File extractFile(String name) throws IOException {
private void extractFile(String name) throws IOException {
var configFile = new File(this.getDataFolder(), name);
//noinspection ResultOfMethodCallIgnored
@ -114,7 +115,6 @@ public class PsHubCore extends JavaPlugin {
}
}
return configFile;
}
@Override

View File

@ -5,10 +5,16 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import os.arcadiadevs.playerservers.hubcore.PsHubCore;
import os.arcadiadevs.playerservers.hubcore.guis.AdminGui;
import os.arcadiadevs.playerservers.hubcore.guis.SelectorGui;
import os.arcadiadevs.playerservers.hubcore.utils.ChatUtil;
import os.arcadiadevs.playerservers.hubcore.guis.PlayerMenuGui;
/**
* A class that handles all the commands.
*
* @author ArcadiaDevs
*/
public class CommandManager implements CommandExecutor {
@Override
@ -26,11 +32,21 @@ public class CommandManager implements CommandExecutor {
}
if (command.getName().equalsIgnoreCase("servers") ||
command.getName().equalsIgnoreCase("menu") ||
command.getName().equalsIgnoreCase("opengui")) {
SelectorGui.openGui(player);
}
if (command.getName().equalsIgnoreCase("playermenu") ||
command.getName().equalsIgnoreCase("pmenu") ||
command.getName().equalsIgnoreCase("playergui")) {
PlayerMenuGui.openGui(player);
}
if (command.getName().equalsIgnoreCase("adminmenu") ||
command.getName().equalsIgnoreCase("admingui")) {
AdminGui.openGui(player);
}
return true;
}
}

View File

@ -0,0 +1,12 @@
package os.arcadiadevs.playerservers.hubcore.enums;
public enum MessageAction {
CONNECT,
DELETE,
CREATE,
ADELETE,
ASTOP,
ASTART,
START,
STOP
}

View File

@ -2,5 +2,5 @@ package os.arcadiadevs.playerservers.hubcore.enums;
public enum PowerAction {
START,
STOP,
STOP
}

View File

@ -0,0 +1,154 @@
package os.arcadiadevs.playerservers.hubcore.guis;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
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.enums.PowerAction;
import os.arcadiadevs.playerservers.hubcore.enums.ServerStatus;
import os.arcadiadevs.playerservers.hubcore.models.Server;
import os.arcadiadevs.playerservers.hubcore.utils.ChatUtil;
import os.arcadiadevs.playerservers.hubcore.utils.GuiUtils;
import java.util.ArrayList;
/**
* Admin gui for admins to manage all servers.
*
* @author ArcadiaDevs
*/
public class AdminGui {
public static void openGui(Player player) {
final var instance = PsHubCore.getInstance();
final var menu = instance.getSpiGui()
.create(ChatUtil.translate(instance.getConfig().getString("gui.admin-menu.menu.name")), 5);
menu.setAutomaticPaginationEnabled(true);
menu.setBlockDefaultInteractions(true);
final var onlinexMaterial =
XMaterial.matchXMaterial(instance.getConfig().getString("gui.admin-menu.menu.online.block"))
.orElse(XMaterial.PLAYER_HEAD).parseMaterial();
final var offlinexMaterial =
XMaterial.matchXMaterial(instance.getConfig().getString("gui.admin-menu.menu.offline.block"))
.orElse(XMaterial.RED_TERRACOTTA).parseMaterial();
final var servers = PsHubCore.getInstance()
.getServersController()
.getServers();
servers.forEach(server -> {
final var itemBuilder = new ItemBuilder(
server.getStatus() == ServerStatus.ONLINE ? onlinexMaterial : offlinexMaterial);
final var online = server.getInfo() != null && server.getInfo().motd() != null && server.getStatus() == ServerStatus.ONLINE;
var lore = instance.getConfig().getStringList(online
? "gui.player-menu.menu.info.online.lore"
: "gui.player-menu.menu.info.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 ? server.getInfo().players() + "" : "0"))
.map(s -> s.replaceAll("%maxplayers%", online ? server.getInfo().maxPlayers() + "" : "0"))
.map(s -> s.replaceAll("%port%", server.getDefaultAllocation().getPort() + ""))
.map(s -> s.replaceAll("%motd%", online ? server.getInfo().motd() : "&cOffline"))
.map(s -> s.replaceAll("%node%", server.getNode().getName()))
.map(s -> s.replaceAll("%owner%", 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.admin-menu.menu.online.name" : "gui.admin-menu.menu.offline.name")
.replaceAll("%server%", server.getId())
.replaceAll("%status%", online ? "&aOnline" : "&cOffline")
.replaceAll("%players%", online ? server.getInfo().players() + "" : "0")
.replaceAll("%maxplayers%", online ? server.getInfo().maxPlayers() + "" : "0")
.replaceAll("%port%", server.getDefaultAllocation().getPort() + "")
.replaceAll("%motd%", online ? server.getInfo().motd() : "&cOffline")
.replaceAll("%node%", server.getNode().getName())
.replaceAll("%owner%", server.getOfflinePlayer().getName())
.replaceAll("%ip%", server.getNode().getIp())
))
.lore(lore)
.build();
menu.setButton(0, menu.getInventory().firstEmpty(), new SGButton(item).withListener(
listener -> openServerMenu(player, server)));
});
XSound.BLOCK_NOTE_BLOCK_BASS.play(player);
player.openInventory(menu.getInventory());
}
public static void openServerMenu(Player player, Server server) {
XSound.BLOCK_NOTE_BLOCK_BASS.play(player);
final var instance = PsHubCore.getInstance();
final var rows = 5;
final var menu = instance.getSpiGui()
.create(ChatUtil.translate(server.getOfflinePlayer().getName()), rows);
menu.setAutomaticPaginationEnabled(false);
menu.setBlockDefaultInteractions(true);
GuiUtils.addBorder(menu, rows);
if (server == null) {
ChatUtil.sendMessage(player, "&9Error> &cCould not find your server!");
return;
}
final var itemStop = new SGButton(new ItemBuilder(XMaterial.RED_TERRACOTTA.parseItem())
.name(ChatUtil.translate(
instance.getConfig().getString("gui.player-menu.menu.stop.name")))
.lore(instance.getConfig().getStringList("gui.player-menu.menu.stop.lore"))
.build())
.withListener(listener -> {
server.adminStop(player);
player.closeInventory();
});
final var itemStart = new SGButton(new ItemBuilder(XMaterial.GREEN_TERRACOTTA.parseItem())
.name(ChatUtil.translate(
instance.getConfig().getString("gui.player-menu.menu.start.name")))
.lore(instance.getConfig().getStringList("gui.player-menu.menu.start.lore"))
.build())
.withListener(listener -> {
server.adminStart(player);
player.closeInventory();
});
final var itemDelete = new SGButton(new ItemBuilder(XMaterial.BARRIER.parseItem())
.name(ChatUtil.translate(
instance.getConfig().getString("gui.player-menu.menu.delete.name")))
.lore(instance.getConfig().getStringList("gui.player-menu.menu.delete.lore"))
.build())
.withListener(listener -> {
server.adminDelete(player);
player.closeInventory();
});
final var online = server.getInfo() != null && server.getInfo().motd() != null && server.getStatus() == ServerStatus.ONLINE;
if (online) {
menu.setButton(9 * 2 + 2, itemStop);
}
if (!online) {
menu.setButton(9 * 2 + 2, itemStart);
}
menu.setButton(9*2+6 , itemDelete);
player.openInventory(menu.getInventory());
}
}

View File

@ -7,18 +7,24 @@ import com.samjakob.spigui.item.ItemBuilder;
import java.util.ArrayList;
import org.bukkit.entity.Player;
import os.arcadiadevs.playerservers.hubcore.PsHubCore;
import os.arcadiadevs.playerservers.hubcore.enums.MessageAction;
import os.arcadiadevs.playerservers.hubcore.enums.PowerAction;
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.GuiUtils;
/**
* Player gui to manage their own server.
*
* @author ArcadiaDevs
*/
public class PlayerMenuGui {
public static void openGui(Player player) {
final var instance = PsHubCore.getInstance();
final var serversController = instance.getServersController();
final var server = serversController.getServer(player);
if (!serversController.hasServer(player)) {
final var menu = instance.getSpiGui().create(
@ -41,7 +47,7 @@ public class PlayerMenuGui {
menu.setButton(0, 13, new SGButton(itemCreate).withListener(
listener -> {
BungeeUtil.createServer(player);
server.create();
XSound.ENTITY_EXPERIENCE_ORB_PICKUP.play(player);
}));
@ -58,10 +64,8 @@ public class PlayerMenuGui {
GuiUtils.addBorder(menu, rows);
final var server = serversController.getServer(player);
if (server == null) {
player.sendMessage(ChatUtil.translate("&9Error> &cCould not find your server!"));
ChatUtil.sendMessage(player, "&9Error> &cCould not find your server!");
return;
}
@ -127,7 +131,7 @@ public class PlayerMenuGui {
instance.getConfig().getString("gui.player-menu.menu.start.name")))
.lore(instance.getConfig().getStringList("gui.player-menu.menu.start.lore"))
.build())
.withListener(listener -> server.executePowerAction(PowerAction.START));
.withListener(listener -> server.start());
menu.setButton(20, itemStart);
}
@ -200,7 +204,7 @@ public class PlayerMenuGui {
.build())
.withListener(listener2 -> {
XSound.ENTITY_EXPERIENCE_ORB_PICKUP.play(player);
server.executePowerAction(PowerAction.STOP);
server.stop();
player.closeInventory();
});

View File

@ -8,9 +8,13 @@ import java.util.ArrayList;
import org.bukkit.entity.Player;
import os.arcadiadevs.playerservers.hubcore.PsHubCore;
import os.arcadiadevs.playerservers.hubcore.enums.ServerStatus;
import os.arcadiadevs.playerservers.hubcore.utils.BungeeUtil;
import os.arcadiadevs.playerservers.hubcore.utils.ChatUtil;
/**
* Server selector gui
*
* @author ArcadiaDevs
*/
public class SelectorGui {
public static void openGui(Player player) {
@ -36,7 +40,8 @@ public class SelectorGui {
servers.forEach(server -> {
final var itemBuilder = new ItemBuilder(
server.getStatus() == ServerStatus.ONLINE ? onlinexMaterial : offlinexMaterial);
final var online = server.getStatus() == ServerStatus.ONLINE;
final var online = server.getInfo() != null && server.getInfo().motd() != null && server.getStatus() == ServerStatus.ONLINE;
var lore = instance.getConfig().getStringList(
server.getStatus() == ServerStatus.ONLINE ? "gui.selector.menu.online.lore" :
@ -71,7 +76,7 @@ public class SelectorGui {
.build();
menu.setButton(0, menu.getInventory().firstEmpty(), new SGButton(item).withListener(
listener -> BungeeUtil.connectPlayer(player)));
listener -> server.connect()));
});
XSound.BLOCK_NOTE_BLOCK_BASS.play(player);

View File

@ -1,29 +1,25 @@
package os.arcadiadevs.playerservers.hubcore.models;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import os.arcadiadevs.playerservers.hubcore.PsHubCore;
import os.arcadiadevs.playerservers.hubcore.enums.MessageAction;
import os.arcadiadevs.playerservers.hubcore.enums.PowerAction;
import os.arcadiadevs.playerservers.hubcore.enums.ServerStatus;
import os.arcadiadevs.playerservers.hubcore.utils.ServerPinger;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import os.arcadiadevs.playerservers.hubcore.enums.PowerAction;
import os.arcadiadevs.playerservers.hubcore.enums.ServerStatus;
import os.arcadiadevs.playerservers.hubcore.utils.BungeeUtil;
import os.arcadiadevs.playerservers.hubcore.utils.ServerPinger;
/**
* The model of a server.
@ -79,8 +75,11 @@ public class Server {
return Bukkit.getPlayer(owner);
}
/**
* Connect player to the server.
*/
public void connect() {
BungeeUtil.connectPlayer(getPlayer());
serverAction(MessageAction.CONNECT, getPlayer());
}
/**
@ -126,23 +125,69 @@ public class Server {
}
/**
* Execute a power action on the server.
* Sends bungee message.
*
* @param action The action to execute
* @param action message to bungee
*/
public void executePowerAction(PowerAction action) {
switch (action) {
case START -> BungeeUtil.startServer(getPlayer());
case STOP -> BungeeUtil.stopServer(getPlayer());
default -> throw new IllegalStateException("Unexpected value: " + action);
private void serverAction(MessageAction action, Player player, String id) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
try {
out.writeUTF(action.toString().toLowerCase());
if (id != null) {
out.writeUTF(id);
}
} catch (Exception e) {
e.printStackTrace();
}
player.sendPluginMessage(PsHubCore.getInstance(), "BungeeCord", out.toByteArray());
player.closeInventory();
}
private void serverAction(MessageAction action, Player player) {
serverAction(action, player, null);
}
public void start() {
serverAction(MessageAction.START, getPlayer());
}
public void stop() {
serverAction(MessageAction.STOP, getPlayer());
}
public void create() {
serverAction(MessageAction.CREATE, getPlayer());
}
/**
* Deletes the server.
*/
public void delete() {
BungeeUtil.deleteServer(getPlayer());
serverAction(MessageAction.DELETE, getPlayer());
}
/**
* Deletes specified server.
*/
public void adminDelete(Player player) {
serverAction(MessageAction.ADELETE, player, getId());
}
/**
* Stops specified server.
*/
public void adminStop(Player player) {
serverAction(MessageAction.ASTOP, player, getId());
}
/**
* Starts specified server.
*/
public void adminStart(Player player) {
serverAction(MessageAction.ASTART, player, getId());
}
}

View File

@ -1,74 +0,0 @@
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;
@SuppressWarnings("UnstableApiUsage")
public class BungeeUtil {
public static void connectPlayer(Player player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
try {
out.writeUTF("Connect");
} catch (Exception e) {
e.printStackTrace();
}
player.sendPluginMessage(PsHubCore.getInstance(), "BungeeCord", out.toByteArray());
player.closeInventory();
}
public static void stopServer(Player player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
try {
out.writeUTF("stop");
} catch (Exception e) {
e.printStackTrace();
}
player.sendPluginMessage(PsHubCore.getInstance(), "BungeeCord", out.toByteArray());
player.closeInventory();
}
public static void deleteServer(Player player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
try {
out.writeUTF("delete");
} catch (Exception e) {
e.printStackTrace();
}
player.sendPluginMessage(PsHubCore.getInstance(), "BungeeCord", out.toByteArray());
player.closeInventory();
}
public static void createServer(Player player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
try {
out.writeUTF("create");
} catch (Exception e) {
e.printStackTrace();
}
player.sendPluginMessage(PsHubCore.getInstance(), "BungeeCord", out.toByteArray());
player.closeInventory();
}
public static void startServer(Player player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
try {
out.writeUTF("start");
} catch (Exception e) {
e.printStackTrace();
}
player.sendPluginMessage(PsHubCore.getInstance(), "BungeeCord", out.toByteArray());
player.closeInventory();
}
}

View File

@ -124,6 +124,30 @@ gui:
name: "&aStart the server"
lore:
- "&7Click here to start the server"
admin-menu:
enabled: true
menu:
name: "&aServers"
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%'
# Should we enable multinode support?
multi-node: false

View File

@ -10,5 +10,15 @@ commands:
usage: /servers
description: Opens up server selector GUI
aliases:
- menu
- opengui
playermenu:
usage: /playermenu
description: Opens up player-menu GUI
aliases:
- pmenu
- playergui
adminmenu:
usage: /adminmenu
description: Opens up admin-menu GUI
aliases:
- adminmenu