Fix shutdown detection

This commit is contained in:
ME1312 2021-04-23 00:36:54 -04:00
parent 6258256081
commit c6a354fe84
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
8 changed files with 57 additions and 58 deletions

View File

@ -286,7 +286,7 @@ public class ExternalHost extends Host implements ClientHandler {
@Override
public boolean destroy() {
if (Util.getDespiteException(() -> Util.reflect(BungeeCord.class.getDeclaredField("isRunning"), plugin), true)) {
if (Util.getDespiteException(() -> Util.reflect(SubProxy.class.getDeclaredField("running"), plugin), true)) {
return super.destroy();
}
return true;

View File

@ -101,6 +101,7 @@ public final class SubProxy extends BungeeCommon implements Listener {
public long resetDate = 0;
private boolean pluginDeployed = false;
private boolean running = false;
private boolean ready = false;
private boolean reloading = false;
private boolean posted = false;
private LinkedList<String> autorun = null;
@ -340,7 +341,7 @@ public final class SubProxy extends BungeeCommon implements Listener {
List<String> ukeys = new ArrayList<String>();
long begin = Calendar.getInstance().getTime().getTime();
boolean status;
if (!(status = running)) resetDate = begin;
if (!(status = ready)) resetDate = begin;
reloading = true;
ConfigUpdater.updateConfig(new UniversalFile(dir, "SubServers:config.yml"));
@ -549,7 +550,7 @@ public final class SubProxy extends BungeeCommon implements Listener {
ukeys.clear();
if (!posted) Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown, "SubServers.Bungee::System_Shutdown"));
running = true;
running = ready = true;
legServers.clear();
// Initialize SubData
@ -639,7 +640,7 @@ public final class SubProxy extends BungeeCommon implements Listener {
@Override
public void startListeners() {
try {
if (posted || !running) reload();
if (posted || !ready) reload();
if (UPnP.isUPnPAvailable()) {
if (config.get().getMap("Settings").getMap("UPnP", new ObjectMap<String>()).getBoolean("Forward-Proxy", true)) for (ListenerInfo listener : getConfig().getListeners()) {
@ -659,24 +660,24 @@ public final class SubProxy extends BungeeCommon implements Listener {
for (String name : autorun) if (host.getSubServer(name) != null) ar.add(name);
if (ar.size() > 0) new Thread(() -> {
try {
while (running && begin == resetDate && !host.isAvailable()) {
while (ready && begin == resetDate && !host.isAvailable()) {
Thread.sleep(250);
}
long init = Calendar.getInstance().getTime().getTime();
while (running && begin == resetDate && ar.size() > 0) {
while (ready && begin == resetDate && ar.size() > 0) {
SubServer server = host.getSubServer(ar.get(0));
ar.remove(0);
if (server != null && !server.isRunning()) {
server.start();
if (ar.size() > 0 && scd > 0) {
long sleep = Calendar.getInstance().getTime().getTime();
while (running && begin == resetDate && server.getSubData()[0] == null && Calendar.getInstance().getTime().getTime() - sleep < scd) {
while (ready && begin == resetDate && server.getSubData()[0] == null && Calendar.getInstance().getTime().getTime() - sleep < scd) {
Thread.sleep(250);
}
}
}
}
if (running && begin == resetDate && Calendar.getInstance().getTime().getTime() - init >= 5000)
if (ready && begin == resetDate && Calendar.getInstance().getTime().getTime() - init >= 5000)
Logger.get("SubServers").info("The auto-start queue for " + host.getName() + " has been finished");
} catch (Exception e) {
e.printStackTrace();
@ -774,7 +775,7 @@ public final class SubProxy extends BungeeCommon implements Listener {
@Override
public void stopListeners() {
if (running) {
if (ready) {
if (pluginDeployed) {
shutdown = !super.isRunning;
super.isRunning = true;
@ -793,13 +794,14 @@ public final class SubProxy extends BungeeCommon implements Listener {
private boolean shutdown = false;
protected void shutdown() {
if (running) {
if (ready) {
legServers.clear();
legServers.putAll(getServersCopy());
running = false;
ready = false;
Logger.get("SubServers").info("Stopping hosted servers");
String[] hosts = this.hosts.keySet().toArray(new String[0]);
if (shutdown || !super.isRunning) running = false;
for (String host : hosts) {
api.forceRemoveHost(host);
}
@ -879,7 +881,7 @@ public final class SubProxy extends BungeeCommon implements Listener {
@Override
public Map<String, ServerInfo> getServersCopy() {
Map<String, ServerInfo> servers = new CaseInsensitiveMap<ServerInfo>();
if (!running) {
if (!ready) {
servers.putAll(super.getServers());
servers.putAll(legServers);
} else {

View File

@ -113,8 +113,7 @@ public class DefaultUIHandler implements UIHandler, Listener {
((UIRenderer.CreatorOptions) gui.lastVisitedObjects[0]).undo();
gui.hostCreator((UIRenderer.CreatorOptions) gui.lastVisitedObjects[0]);
} else if (item.equals(plugin.api.getLang("SubServers", "Interface.Host-Creator.Submit"))) {
String host = ((UIRenderer.CreatorOptions) gui.lastVisitedObjects[0]).getHost().toLowerCase();
if (player.hasPermission("subservers.host.*.*") || player.hasPermission("subservers.host.*.create") || player.hasPermission("subservers.host." + host + ".*") || player.hasPermission("subservers.host." + host + ".create")) {
if (permits(((UIRenderer.CreatorOptions) gui.lastVisitedObjects[0]).getHost(), player, "subservers.host.%.*", "subservers.host.%.create")) {
player.closeInventory();
gui.setDownloading(plugin.api.getLang("SubServers", "Interface.Generic.Downloading.Response"));
UIRenderer.CreatorOptions options = ((UIRenderer.CreatorOptions) gui.lastVisitedObjects[0]);

View File

@ -258,7 +258,7 @@ public class DefaultUIRenderer extends UIRenderer {
}
Player player = Bukkit.getPlayer(this.player);
if (!(player.hasPermission("subservers.host.*.*") || player.hasPermission("subservers.host.*.create") || player.hasPermission("subservers.host." + name.toLowerCase() + ".*") || player.hasPermission("subservers.host." + name.toLowerCase() + ".create"))) {
if (!permits(name, player, "subservers.host.%.*", "subservers.host.%.create")) {
block = color(7);
blockMeta = block.getItemMeta();
blockMeta.setDisplayName(ChatColor.GRAY+ChatColor.stripColor(plugin.api.getLang("SubServers", "Interface.Host-Admin.Creator")));

View File

@ -48,11 +48,11 @@ public class ObjectPermission {
* @param permissions Permissions to check (use <b>%</b> as a placeholder for the object name)
* @return Permission Check Result
*/
private static boolean permits(String string, Permissible object, String... permissions) {
public static boolean permits(String string, Permissible object, String... permissions) {
if (Util.isNull(object)) throw new NullPointerException();
boolean permitted = false;
for (int p = 0; !permitted && p < permissions.length; p++) {
for (int p = 0; !permitted && p < permissions.length; ++p) {
String perm = permissions[p];
if (perm != null) {
// Check all proxies & individual proxies permission
@ -76,7 +76,7 @@ public class ObjectPermission {
if (Util.isNull(object)) throw new NullPointerException();
boolean permitted = false;
for (int p = 0; !permitted && p < permissions.length; p++) {
for (int p = 0; !permitted && p < permissions.length; ++p) {
String perm = permissions[p];
if (perm != null) {
// Check all servers & individual servers permission
@ -93,7 +93,7 @@ public class ObjectPermission {
List<String> groups = server.getGroups();
if (groups.size() > 0) {
permitted = permitted || object.hasPermission(perm.replace("%", ":*"));
for (int g = 0; !permitted && g < groups.size(); g++) {
for (int g = 0; !permitted && g < groups.size(); ++g) {
permitted = object.hasPermission(perm.replace("%", ":" + groups.get(g).toLowerCase()));
}
}

View File

@ -340,7 +340,7 @@ public final class SubCommand extends BukkitCommand {
}
} else if (args[0].equalsIgnoreCase("start")) {
if (args.length > 1) {
selectServers(sender, args, 1, true, Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.start"), select -> {
selectServers(sender, args, 1, true, new String[]{"subservers.subserver.%.*", "subservers.subserver.%.start"}, select -> {
if (select.subservers.length > 0) {
Container<Integer> success = new Container<Integer>(0);
Container<Integer> running = new Container<Integer>(0);
@ -388,7 +388,7 @@ public final class SubCommand extends BukkitCommand {
}
} else if (args[0].equalsIgnoreCase("restart")) {
if (args.length > 1) {
selectServers(sender, args, 1, true, Arrays.asList(Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.start"), Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.stop")), select -> {
selectServers(sender, args, 1, true, new String[][]{{"subservers.subserver.%.*", "subservers.subserver.%.start"}, {"subservers.subserver.%.*", "subservers.subserver.%.stop"}}, select -> {
if (select.subservers.length > 0) {
// Step 5: Start the stopped Servers once more
final UUID player = (sender instanceof Player)?((Player) sender).getUniqueId():null;
@ -491,7 +491,7 @@ public final class SubCommand extends BukkitCommand {
}
} else if (args[0].equalsIgnoreCase("stop")) {
if (args.length > 1) {
selectServers(sender, args, 1, true, Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.stop"), select -> {
selectServers(sender, args, 1, true, new String[]{"subservers.subserver.%.*", "subservers.subserver.%.stop"}, select -> {
if (select.subservers.length > 0) {
Container<Integer> success = new Container<Integer>(0);
Container<Integer> running = new Container<Integer>(0);
@ -538,7 +538,7 @@ public final class SubCommand extends BukkitCommand {
}
} else if (args[0].equalsIgnoreCase("kill") || args[0].equalsIgnoreCase("terminate")) {
if (args.length > 1) {
selectServers(sender, args, 1, true, Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.terminate"), select -> {
selectServers(sender, args, 1, true, new String[]{"subservers.subserver.%.*", "subservers.subserver.%.terminate"}, select -> {
if (select.subservers.length > 0) {
Container<Integer> success = new Container<Integer>(0);
Container<Integer> running = new Container<Integer>(0);
@ -585,7 +585,7 @@ public final class SubCommand extends BukkitCommand {
}
} else if (args[0].equalsIgnoreCase("cmd") || args[0].equalsIgnoreCase("command")) {
if (args.length > 1) {
selectServers(sender, args, 1, true, Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.command"), select -> {
selectServers(sender, args, 1, true, new String[]{"subservers.subserver.%.*", "subservers.subserver.%.command"}, select -> {
if (select.subservers.length > 0) {
if (select.args.length > 2) {
StringBuilder builder = new StringBuilder(select.args[2]);
@ -628,7 +628,7 @@ public final class SubCommand extends BukkitCommand {
}
} else if (args[0].equalsIgnoreCase("create")) {
if (args.length > 3) {
if (sender.hasPermission("subservers.host.*.*") || sender.hasPermission("subservers.host.*.create") || sender.hasPermission("subservers.host." + args[2].toLowerCase() + ".*") || sender.hasPermission("subservers.host." + args[2].toLowerCase() + ".create")) {
if (permits(args[2], sender, "subservers.host.%.*", "subservers.host.%.create")) {
if (args.length > 5 && Util.isException(() -> Integer.parseInt(args[5]))) {
sender.sendMessage(plugin.api.getLang("SubServers", "Command.Creator.Invalid-Port"));
} else {
@ -673,7 +673,7 @@ public final class SubCommand extends BukkitCommand {
}
} else if (args[0].equalsIgnoreCase("update") || args[0].equalsIgnoreCase("upgrade")) {
if (args.length > 1) {
selectServers(sender, args, 1, true, Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.update"), select -> {
selectServers(sender, args, 1, true, new String[]{"subservers.subserver.%.*", "subservers.subserver.%.update"}, select -> {
if (select.subservers.length > 0) {
String template = (select.args.length > 3)?select.args[2].toLowerCase():null;
Version version = (select.args.length > 2)?new Version(select.args[(template == null)?2:3]):null;
@ -746,7 +746,7 @@ public final class SubCommand extends BukkitCommand {
plugin.gui.getRenderer((Player) sender).hostAdmin(args[2]);
break;
case "host/creator":
if (sender.hasPermission("subservers.host.*.*") || sender.hasPermission("subservers.host.*.create") || sender.hasPermission("subservers.host." + args[2].toLowerCase() + ".*") || sender.hasPermission("subservers.host." + args[2].toLowerCase() + ".create"))
if (permits(args[2], sender, "subservers.host.%.*", "subservers.host.%.create"))
plugin.gui.getRenderer((Player) sender).hostCreator(new UIRenderer.CreatorOptions(args[2]));
else throw new IllegalStateException("Player does not meet the requirements to render this page");
break;
@ -838,13 +838,13 @@ public final class SubCommand extends BukkitCommand {
}
}
private void selectServers(CommandSender sender, String[] rargs, int index, boolean mode, String permissions, Callback<ServerSelection> callback) {
selectServers(sender, rargs, index, mode, Arrays.asList(permissions), callback);
selectServers(sender, rargs, index, mode, new String[]{ permissions }, callback);
}
private void selectServers(CommandSender sender, String[] rargs, int index, boolean mode, List<String> permissions, Callback<ServerSelection> callback) {
selectServers(sender, rargs, index, mode, Arrays.asList(permissions), callback);
private void selectServers(CommandSender sender, String[] rargs, int index, boolean mode, String[] permissions, Callback<ServerSelection> callback) {
selectServers(sender, rargs, index, mode, new String[][]{ permissions }, callback);
}
@SuppressWarnings("unchecked")
private void selectServers(CommandSender sender, String[] rargs, int index, boolean mode, Collection<List<String>> permissions, Callback<ServerSelection> callback) {
private void selectServers(CommandSender sender, String[] rargs, int index, boolean mode, String[][] permissions, Callback<ServerSelection> callback) {
StackTraceElement[] origin = new Exception().getStackTrace();
LinkedList<String> msgs = new LinkedList<String>();
LinkedList<String> args = new LinkedList<String>();
@ -878,13 +878,12 @@ public final class SubCommand extends BukkitCommand {
if (!history.contains(server)) {
history.add(server);
boolean permitted = sender == null || permissions == null || permissions.size() <= 0;
boolean permitted = sender == null || permissions == null || permissions.length == 0;
if (!permitted) {
permitted = true;
List<String>[] checks = permissions.toArray(new List[0]);
for (int p = 0; permitted && p < permissions.size(); p++) {
if (checks[p] == null || checks[p].size() <= 0) continue;
else permitted = permits(server, sender, checks[p].toArray(new String[0]));
for (int p = 0; permitted && p < permissions.length; ++p) {
if (permissions[p] == null || permissions[p].length == 0) continue;
else permitted = permits(server, sender, permissions[p]);
}
}

View File

@ -48,14 +48,14 @@ public class ObjectPermission {
* @param permissions Permissions to check (use <b>%</b> as a placeholder for the object name)
* @return Permission Check Result
*/
private static boolean permits(String string, Subject object, String... permissions) {
public static boolean permits(String string, Subject object, String... permissions) {
if (Util.isNull(object)) throw new NullPointerException();
boolean permitted = false;
for (int p = 0; !permitted && p < permissions.length; p++) {
for (int p = 0; !permitted && p < permissions.length; ++p) {
String perm = permissions[p];
if (perm != null) {
// Check all proxies & individual proxies permission
// Check all objects & individual objects permission
permitted = object.hasPermission(perm.replace("%", "*"))
|| object.hasPermission(perm.replace("%", string.toLowerCase()));
}
@ -76,7 +76,7 @@ public class ObjectPermission {
if (Util.isNull(object)) throw new NullPointerException();
boolean permitted = false;
for (int p = 0; !permitted && p < permissions.length; p++) {
for (int p = 0; !permitted && p < permissions.length; ++p) {
String perm = permissions[p];
if (perm != null) {
// Check all servers & individual servers permission
@ -93,7 +93,7 @@ public class ObjectPermission {
List<String> groups = server.getGroups();
if (groups.size() > 0) {
permitted = permitted || object.hasPermission(perm.replace("%", ":*"));
for (int g = 0; !permitted && g < groups.size(); g++) {
for (int g = 0; !permitted && g < groups.size(); ++g) {
permitted = object.hasPermission(perm.replace("%", ":" + groups.get(g).toLowerCase()));
}
}

View File

@ -650,7 +650,7 @@ public final class SubCommand implements CommandExecutor {
if (canRun(sender)) {
Optional<String[]> s = args.getOne(Text.of("Subservers"));
if (s.isPresent()) {
selectServers(sender, s.get(), true, Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.start"), select -> {
selectServers(sender, s.get(), true, new String[]{"subservers.subserver.%.*", "subservers.subserver.%.start"}, select -> {
if (select.subservers.length > 0) {
Container<Integer> success = new Container<Integer>(0);
Container<Integer> running = new Container<Integer>(0);
@ -710,7 +710,7 @@ public final class SubCommand implements CommandExecutor {
if (canRun(sender)) {
Optional<String[]> s = args.getOne(Text.of("Subservers"));
if (s.isPresent()) {
selectServers(sender, s.get(), true, Arrays.asList(Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.start"), Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.stop")), select -> {
selectServers(sender, s.get(), true, new String[][]{{"subservers.subserver.%.*", "subservers.subserver.%.start"}, {"subservers.subserver.%.*", "subservers.subserver.%.stop"}}, select -> {
if (select.subservers.length > 0) {
// Step 5: Start the stopped Servers once more
final UUID player = (sender instanceof Player)?((Player) sender).getUniqueId():null;
@ -825,7 +825,7 @@ public final class SubCommand implements CommandExecutor {
if (canRun(sender)) {
Optional<String[]> s = args.getOne(Text.of("Subservers"));;
if (s.isPresent()) {
selectServers(sender, s.get(), true, Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.stop"), select -> {
selectServers(sender, s.get(), true, new String[]{"subservers.subserver.%.*", "subservers.subserver.%.stop"}, select -> {
if (select.subservers.length > 0) {
Container<Integer> success = new Container<Integer>(0);
Container<Integer> running = new Container<Integer>(0);
@ -884,7 +884,7 @@ public final class SubCommand implements CommandExecutor {
if (canRun(sender)) {
Optional<String[]> s = args.getOne(Text.of("Subservers"));
if (s.isPresent()) {
selectServers(sender, s.get(), true, Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.terminate"), select -> {
selectServers(sender, s.get(), true, new String[]{"subservers.subserver.%.*", "subservers.subserver.%.terminate"}, select -> {
if (select.subservers.length > 0) {
Container<Integer> success = new Container<Integer>(0);
Container<Integer> running = new Container<Integer>(0);
@ -944,7 +944,7 @@ public final class SubCommand implements CommandExecutor {
Optional<String[]> s = args.getOne(Text.of("Subservers"));
Optional<String> command = args.getOne(Text.of("Command"));
if (s.isPresent()) {
selectServers(sender, s.get(), true, Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.command"), select -> {
selectServers(sender, s.get(), true, new String[]{"subservers.subserver.%.*", "subservers.subserver.%.command"}, select -> {
if (select.subservers.length > 0) {
if (command.isPresent()) {
Container<Integer> success = new Container<Integer>(0);
@ -997,7 +997,7 @@ public final class SubCommand implements CommandExecutor {
Optional<String> version = args.getOne(Text.of("Version"));
Optional<String> port = args.getOne(Text.of("Port"));
if (name.isPresent() && host.isPresent() && template.isPresent()) {
if (sender.hasPermission("subservers.host.*.*") || sender.hasPermission("subservers.host.*.create") || sender.hasPermission("subservers.host." + host.get().toLowerCase() + ".*") || sender.hasPermission("subservers.host." + host.get().toLowerCase() + ".create")) {
if (permits(host.get(), sender, "subservers.host.%.*", "subservers.host.%.create")) {
if (port.isPresent() && Util.isException(() -> Integer.parseInt(port.get()))) {
sender.sendMessage(ChatColor.convertColor(plugin.api.getLang("SubServers","Command.Creator.Invalid-Port")));
return CommandResult.builder().successCount(0).build();
@ -1073,7 +1073,7 @@ public final class SubCommand implements CommandExecutor {
}
if (s.isPresent()) {
selectServers(sender, s.get(), true, Arrays.asList("subservers.subserver.%.*", "subservers.subserver.%.update"), select -> {
selectServers(sender, s.get(), true, new String[]{"subservers.subserver.%.*", "subservers.subserver.%.update"}, select -> {
if (select.subservers.length > 0) {
boolean ts = ft == null;
@ -1224,7 +1224,7 @@ public final class SubCommand implements CommandExecutor {
plugin.gui.getRenderer((Player) sender).hostAdmin(menuopts[0]);
break;
case "host/creator":
if (sender.hasPermission("subservers.host.*.*") || sender.hasPermission("subservers.host.*.create") || sender.hasPermission("subservers.host." + menuopts[0].toLowerCase() + ".*") || sender.hasPermission("subservers.host." + menuopts[0].toLowerCase() + ".create"))
if (permits(menuopts[0], sender, "subservers.host.%.*", "subservers.host.%.create"))
plugin.gui.getRenderer((Player) sender).hostCreator(new UIRenderer.CreatorOptions(menuopts[0]));
else throw new IllegalStateException("Player does not meet the requirements to render this page");
break;
@ -1271,13 +1271,13 @@ public final class SubCommand implements CommandExecutor {
}
}
private void selectServers(CommandSource sender, String[] selection, boolean mode, String permissions, Callback<ServerSelection> callback) {
selectServers(sender, selection, mode, Arrays.asList(permissions), callback);
selectServers(sender, selection, mode, new String[]{ permissions }, callback);
}
private void selectServers(CommandSource sender, String[] selection, boolean mode, List<String> permissions, Callback<ServerSelection> callback) {
selectServers(sender, selection, mode, Arrays.asList(permissions), callback);
private void selectServers(CommandSource sender, String[] selection, boolean mode, String[] permissions, Callback<ServerSelection> callback) {
selectServers(sender, selection, mode, new String[][]{ permissions }, callback);
}
@SuppressWarnings("unchecked")
private void selectServers(CommandSource sender, String[] selection, boolean mode, Collection<List<String>> permissions, Callback<ServerSelection> callback) {
private void selectServers(CommandSource sender, String[] selection, boolean mode, String[][] permissions, Callback<ServerSelection> callback) {
StackTraceElement[] origin = new Exception().getStackTrace();
LinkedList<Text> msgs = new LinkedList<Text>();
LinkedList<Server> select = new LinkedList<Server>();
@ -1291,13 +1291,12 @@ public final class SubCommand implements CommandExecutor {
if (!history.contains(server)) {
history.add(server);
boolean permitted = sender == null || permissions == null || permissions.size() <= 0;
boolean permitted = sender == null || permissions == null || permissions.length == 0;
if (!permitted) {
permitted = true;
List<String>[] checks = permissions.toArray(new List[0]);
for (int p = 0; permitted && p < permissions.size(); p++) {
if (checks[p] == null || checks[p].size() <= 0) continue;
else permitted = permits(server, sender, checks[p].toArray(new String[0]));
for (int p = 0; permitted && p < permissions.length; ++p) {
if (permissions[p] == null || permissions[p].length == 0) continue;
else permitted = permits(server, sender, permissions[p]);
}
}