Add the delete command to SubServers.Client

This commit is contained in:
ME1312 2021-06-28 23:45:49 -04:00
parent ee82ec32a3
commit 394c4b238b
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
11 changed files with 135 additions and 36 deletions

View File

@ -18,10 +18,7 @@ import net.md_5.bungee.api.ChatColor;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.*;
/**
* External SubServer Class
@ -212,7 +209,7 @@ public class ExternalSubServer extends SubServerImpl {
if (running) {
SubSendCommandEvent event = new SubSendCommandEvent(player, this, command);
host.plugin.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
if (!event.isCancelled() && (player == null || !DISALLOWED_COMMANDS.matcher(command).find())) {
history.add(new LoggedCommand(player, event.getCommand()));
if (event.getCommand().equalsIgnoreCase(stopcmd)) {
host.queue(new PacketExEditServer(this, PacketExEditServer.UpdateType.STOP));

View File

@ -22,10 +22,7 @@ import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
@ -278,7 +275,7 @@ public class InternalSubServer extends SubServerImpl {
if (thread != null && thread.isAlive()) {
SubSendCommandEvent event = new SubSendCommandEvent(player, this, command);
host.plugin.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
if (!event.isCancelled() && (player == null || !DISALLOWED_COMMANDS.matcher(command).find())) {
try {
if (event.getCommand().equalsIgnoreCase(stopcmd)) allowrestart = false;
history.add(new LoggedCommand(player, event.getCommand()));

View File

@ -12,11 +12,13 @@ import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
/**
* SubServer Layout Class
*/
public abstract class SubServerImpl extends ServerImpl implements SubServer {
protected static final Pattern DISALLOWED_COMMANDS = Pattern.compile("^/?(?:[^\\s]+:)?sub(?:servers?)?(?:\\s|$)", Pattern.CASE_INSENSITIVE & Pattern.UNICODE_CASE);
private List<Pair<String, String>> incompatibilities = new ArrayList<Pair<String, String>>();
private SubCreator.ServerTemplate templateV = null;
private String templateS = null;

View File

@ -318,7 +318,7 @@ public class ConfigUpdater {
existing = updated.clone();
i++;
} if (was.compareTo(new Version("21w24h")) <= 0) {
} if (was.compareTo(new Version("21w27a")) <= 0) {
//existing = updated.clone();
i++;
@ -446,6 +446,9 @@ public class ConfigUpdater {
def.put("Command.Update.Template-Disabled", "&cSubServers &4&l\\u00BB&c The template that created &4$str$&c is not enabled");
def.put("Command.Update.Template-Invalid", "&cSubServers &4&l\\u00BB&c The template that created &4$str$&c does not support subserver updating");
def.put("Command.Update.Version-Required", "&cSubServers &4&l\\u00BB&c The template that created &4$str$&c requires a Minecraft version to be specified");
def.put("Command.Delete.Disappeared", "&cSubServers &4&l\\u00BB&c Subserver &4$str$&c has disappeared");
def.put("Command.Delete.Running", "&cSubServers &4&l\\u00BB&c Cannot delete &4$str$&c while it is still running");
def.put("Command.Delete", "&aSubServers &2&l\\u00BB&a Deleting &2$int$&a subserver(s)");
def.put("Command.Teleport", "&aSubServers &2&l\\u00BB&a Teleporting &2$str$&a to server");
def.put("Command.Teleport.Not-Running", "&cSubServers &4&l\\u00BB&c Subserver &4$str$&c is not running");
def.put("Interface.Generic.Back", "&cBack");

View File

@ -638,7 +638,7 @@ public final class SubCommand extends Command implements TabExecutor {
int success = 0;
for (SubServer server : select.subservers) try {
if (server.isRunning()) {
sender.sendMessage("SubServers > Subserver " + server.getName() + " is still running");
sender.sendMessage("SubServers > Cannot delete " + server.getName() + " while it is still running");
} else if (server.getHost().recycleSubServer(server.getName())) {
success++;
}
@ -866,8 +866,8 @@ public final class SubCommand extends Command implements TabExecutor {
return Collections.emptyList();
} else if (args.length <= 1) {
List<String> cmds = new ArrayList<>();
cmds.addAll(Arrays.asList("help", "list", "info", "status", "version", "start", "restart", "stop", "kill", "terminate", "cmd", "command", "create", "update", "upgrade"));
if (!(sender instanceof ProxiedPlayer)) cmds.addAll(Arrays.asList("reload", "sudo", "screen", "remove", "delete", "restore"));
cmds.addAll(Arrays.asList("help", "list", "info", "status", "version", "start", "restart", "stop", "kill", "terminate", "cmd", "command", "create", "update", "upgrade", "restore"));
if (!(sender instanceof ProxiedPlayer)) cmds.addAll(Arrays.asList("reload", "sudo", "screen", "remove", "delete"));
List<String> list = new ArrayList<String>();
for (String cmd : cmds) {
if (cmd.startsWith(last)) list.add(Last + cmd.substring(last.length()));
@ -992,10 +992,10 @@ public final class SubCommand extends Command implements TabExecutor {
args[0].equals("kill") || args[0].equals("terminate") ||
args[0].equals("cmd") || args[0].equals("command") ||
args[0].equals("update") || args[0].equals("upgrade") ||
args[0].equals("remove") || args[0].equals("del") || args[0].equals("delete") ||
(!(sender instanceof ProxiedPlayer) && (
args[0].equals("sudo") || args[0].equals("screen") ||
args[0].equals("remove") || args[0].equals("del") || args[0].equals("delete")
))) {
args[0].equals("sudo") || args[0].equals("screen")
))) {
List<String> list = new ArrayList<String>();
ServerSelection select = selectServers(null, args, 1, true);
if (select.last != null) {
@ -1078,6 +1078,12 @@ public final class SubCommand extends Command implements TabExecutor {
} else {
return Collections.emptyList();
}
} else if (args[0].equals("restore")) {
if (args.length == 2) {
return Collections.singletonList("<Subserver>");
} else {
return Collections.emptyList();
}
} else if (sender instanceof ProxiedPlayer && (args[0].equals("tp") || args[0].equals("teleport"))) {
if (args.length == 2 || args.length == 3) {
List<String> list = new ArrayList<String>();

View File

@ -56,6 +56,7 @@ public class ConfigUpdater {
YAMLSection settings = new YAMLSection();
settings.set("Version", ((now.compareTo(was) <= 0)?was:now).toString());
settings.set("API-Only-Mode", updated.getMap("Settings", new YAMLSection()).getBoolean("API-Only-Mode", false));
settings.set("Allow-Deletion", updated.getMap("Settings", new YAMLSection()).getBoolean("Allow-Deletion", false));
settings.set("Show-Addresses", updated.getMap("Settings", new YAMLSection()).getBoolean("Show-Addresses", false));
settings.set("Use-Title-Messages", updated.getMap("Settings", new YAMLSection()).getBoolean("Use-Title-Messages", true));
settings.set("PlaceholderAPI-Ready", updated.getMap("Settings", new YAMLSection()).getBoolean("PlaceholderAPI-Ready", false));

View File

@ -51,7 +51,6 @@ public final class SubCommand extends Command {
@Override
public boolean execute(CommandSender sender, String label, String[] args) {
label = "/" + label;
if (plugin.api.getSubDataNetwork()[0] == null || plugin.api.getSubDataNetwork()[0].isClosed()) {
new IllegalStateException("SubData is not connected").printStackTrace();
if (!(sender instanceof ConsoleCommandSender)) sender.sendMessage(ChatColor.RED + "An exception has occurred while running this command");
@ -59,6 +58,7 @@ public final class SubCommand extends Command {
new IllegalStateException("There are no lang options available at this time").printStackTrace();
if (!(sender instanceof ConsoleCommandSender)) sender.sendMessage(ChatColor.RED + "An exception has occurred while running this command");
} else {
label = "/" + label;
if (sender.hasPermission("subservers.command")) {
if (args.length > 0) {
if (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("?")) {
@ -730,6 +730,46 @@ public final class SubCommand extends Command {
} else {
sender.sendMessage(plugin.api.getLang("SubServers", "Command.Generic.Usage").replace("$str$", label.toLowerCase() + " " + args[0].toLowerCase() + " <Subservers> [[Template] <Version>]"));
}
} else if ((args[0].equalsIgnoreCase("remove") || args[0].equalsIgnoreCase("del") || args[0].equalsIgnoreCase("delete")) && plugin.config.get().getMap("Settings").getBoolean("Allow-Deletion", false)) {
if (args.length > 1) {
selectServers(sender, args, 1, true, "subservers.subserver.%.delete", select -> {
if (select.subservers.length > 0) {
Container<Integer> success = new Container<Integer>(0);
AsyncConsolidator merge = new AsyncConsolidator(() -> {
if (success.value > 0) sender.sendMessage(plugin.api.getLang("SubServers", "Command.Delete").replace("$int$", success.value.toString()));
});
for (SubServer server : select.subservers) {
if (server.isRunning()) {
sender.sendMessage(plugin.api.getLang("SubServers", "Command.Delete.Running").replace("$str$", server.getName()));
} else {
server.getHost(host -> {
if (host == null) {
sender.sendMessage(plugin.api.getLang("SubServers", "Command.Delete.Disappeared").replace("$str$", server.getName()));
} else {
merge.reserve();
host.recycleSubServer(server.getName(), response -> {
switch (response) {
case 3:
case 4:
sender.sendMessage(plugin.api.getLang("SubServers", "Command.Delete.Disappeared").replace("$str$", server.getName()));
break;
case 0:
success.value++;
break;
}
merge.release();
});
}
});
}
}
}
});
} else {
sender.sendMessage("Usage: " + label + " " + args[0].toLowerCase() + " <Subservers>");
}
} else if (args[0].equalsIgnoreCase("restore")) {
// TODO
} else if (args[0].equalsIgnoreCase("tp") || args[0].equalsIgnoreCase("teleport")) {
executeTeleport(sender, label, args);
} else if ((args[0].equalsIgnoreCase("view") || args[0].equalsIgnoreCase("open")) && sender instanceof Player) {

View File

@ -56,6 +56,7 @@ public class ConfigUpdater {
YAMLSection settings = new YAMLSection();
settings.set("Version", ((now.compareTo(was) <= 0)?was:now).toString());
settings.set("API-Only-Mode", updated.getMap("Settings", new YAMLSection()).getBoolean("API-Only-Mode", false));
settings.set("Allow-Deletion", updated.getMap("Settings", new YAMLSection()).getBoolean("Allow-Deletion", false));
settings.set("Show-Addresses", updated.getMap("Settings", new YAMLSection()).getBoolean("Show-Addresses", false));
settings.set("Use-Title-Messages", updated.getMap("Settings", new YAMLSection()).getBoolean("Use-Title-Messages", true));

View File

@ -121,6 +121,11 @@ public final class SubCommand implements CommandExecutor {
.executor(new UPDATE())
.arguments(GenericArguments.optional(new ListArgument(Text.of("Subservers"))), GenericArguments.optional(GenericArguments.string(Text.of("Template"))), GenericArguments.optional(GenericArguments.string(Text.of("Version"))), GenericArguments.optional(GenericArguments.remainingJoinedStrings(Text.of("..."))))
.build(), "update", "upgrade")
.child(CommandSpec.builder()
.description(Text.of("The SubServers Command - Delete"))
.executor(new DELETE())
.arguments(GenericArguments.optional(new ListArgument(Text.of("Subservers"))), GenericArguments.optional(GenericArguments.remainingJoinedStrings(Text.of("..."))))
.build(), "remove", "del", "delete")
.child(CommandSpec.builder()
.description(Text.of("The SubServers Command - Teleport"))
.executor(new TELEPORT())
@ -1137,6 +1142,60 @@ public final class SubCommand implements CommandExecutor {
}
}
public final class DELETE implements CommandExecutor {
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
if (plugin.config.get().getMap("Settings").getBoolean("Allow-Deletion", false)) {
if (canRun(sender)) {
Optional<String[]> s = args.getOne(Text.of("Subservers"));
if (s.isPresent()) {
selectServers(sender, s.get(), true, "subservers.subserver.%.delete", select -> {
if (select.subservers.length > 0) {
Container<Integer> success = new Container<Integer>(0);
AsyncConsolidator merge = new AsyncConsolidator(() -> {
if (success.value > 0) sender.sendMessage(ChatColor.convertColor(plugin.api.getLang("SubServers", "Command.Delete").replace("$int$", success.value.toString())));
});
for (SubServer server : select.subservers) {
if (server.isRunning()) {
sender.sendMessage(ChatColor.convertColor(plugin.api.getLang("SubServers", "Command.Delete.Running").replace("$str$", server.getName())));
} else {
server.getHost(host -> {
if (host == null) {
sender.sendMessage(ChatColor.convertColor(plugin.api.getLang("SubServers", "Command.Delete.Disappeared").replace("$str$", server.getName())));
} else {
merge.reserve();
host.recycleSubServer(server.getName(), response -> {
switch (response) {
case 3:
case 4:
sender.sendMessage(ChatColor.convertColor(plugin.api.getLang("SubServers", "Command.Delete.Disappeared").replace("$str$", server.getName())));
break;
case 0:
success.value++;
break;
}
merge.release();
});
}
});
}
}
}
});
return CommandResult.builder().successCount(1).build();
} else {
sender.sendMessage(ChatColor.convertColor(plugin.api.getLang("SubServers","Command.Generic.Usage").replace("$str$", "/sub delete <Subservers>")));
return CommandResult.builder().successCount(0).build();
}
} else {
sender.sendMessage(ChatColor.convertColor(plugin.api.getLang("SubServers","Command.Generic.Invalid-Permission").replace("$str$", "subservers.command")));
return CommandResult.builder().successCount(0).build();
}
} else {
return CommandResult.builder().successCount(0).build();
}
}
}
public final class TELEPORT implements CommandExecutor {
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
if (canRun(sender, true)) {

View File

@ -927,13 +927,12 @@ public class SubCommand {
selectServers(sender, args, 0, true, select -> {
if (select.subservers.length > 0) {
Container<Integer> success = new Container<Integer>(0);
Container<Integer> running = new Container<Integer>(0);
AsyncConsolidator merge = new AsyncConsolidator(() -> {
if (success.value > 0) sender.sendMessage("Removing " + success.value + " subserver"+((success.value == 1)?"":"s"));
});
for (SubServer server : select.subservers) {
if (server.isRunning()) {
sender.sendMessage("Subserver " + server.getName() + " is still running");
sender.sendMessage("Cannot delete " + server.getName() + " while it is still running");
} else {
server.getHost(host -> {
if (host == null) {

View File

@ -682,13 +682,12 @@ public final class SubCommand extends Command implements TabExecutor {
selectServers(sender, args, 1, true, select -> {
if (select.subservers.length > 0) {
Container<Integer> success = new Container<Integer>(0);
Container<Integer> running = new Container<Integer>(0);
AsyncConsolidator merge = new AsyncConsolidator(() -> {
if (success.value > 0) sender.sendMessage("SubServers > Removing " + success.value + " subserver"+((success.value == 1)?"":"s"));
});
for (SubServer server : select.subservers) {
if (server.isRunning()) {
sender.sendMessage("SubServers > Subserver " + server.getName() + " is still running");
sender.sendMessage("SubServers > Cannot delete " + server.getName() + " while it is still running");
} else {
server.getHost(host -> {
if (host == null) {
@ -968,8 +967,8 @@ public final class SubCommand extends Command implements TabExecutor {
return Collections.emptyList();
} else if (args.length <= 1) {
List<String> cmds = new ArrayList<>();
cmds.addAll(Arrays.asList("help", "list", "info", "status", "version", "start", "restart", "stop", "kill", "terminate", "cmd", "command", "create", "update", "upgrade"));
if (!(sender instanceof ProxiedPlayer)) cmds.addAll(Arrays.asList("reload", "sudo", "screen", "remove", "delete", "restore"));
cmds.addAll(Arrays.asList("help", "list", "info", "status", "version", "start", "restart", "stop", "kill", "terminate", "cmd", "command", "create", "update", "upgrade", "restore"));
if (!(sender instanceof ProxiedPlayer)) cmds.addAll(Arrays.asList("remove", "delete"));
updateCache();
@ -1074,24 +1073,13 @@ public final class SubCommand extends Command implements TabExecutor {
} else {
return Collections.emptyList();
}
} else if (!(sender instanceof ProxiedPlayer) && (args[0].equals("restore"))) {
/* if (args[0].equals("restore")) */ {
if (args.length == 2) {
return Collections.singletonList("<Subserver>");
} else {
return Collections.emptyList();
}
}
} else if (args[0].equals("start") ||
args[0].equals("restart") ||
args[0].equals("stop") ||
args[0].equals("kill") || args[0].equals("terminate") ||
args[0].equals("cmd") || args[0].equals("command") ||
args[0].equals("update") || args[0].equals("upgrade") ||
(!(sender instanceof ProxiedPlayer) && (
args[0].equals("sudo") || args[0].equals("screen") ||
args[0].equals("remove") || args[0].equals("del") || args[0].equals("delete")
))) {
args[0].equals("remove") || args[0].equals("del") || args[0].equals("delete")) {
List<String> list = new ArrayList<String>();
RawServerSelection select = selectRawServers(null, args, 1, true);
if (select.last != null) {
@ -1175,6 +1163,12 @@ public final class SubCommand extends Command implements TabExecutor {
} else {
return Collections.emptyList();
}
} else if (args[0].equals("restore")) {
if (args.length == 2) {
return Collections.singletonList("<Subserver>");
} else {
return Collections.emptyList();
}
} else if (sender instanceof ProxiedPlayer && (args[0].equals("tp") || args[0].equals("teleport"))) {
if (args.length == 2 || args.length == 3) {
List<String> list = new ArrayList<String>();