Skip to content

Commit

Permalink
Keep the console reader alive during shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Feb 7, 2021
1 parent 4e66461 commit 76e11f0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
Expand Up @@ -4,6 +4,7 @@
import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.Galaxi.Library.Map.ObjectMapValue;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.SubServers.Bungee.Library.Compatibility.Logger;
import net.ME1312.SubServers.Bungee.Library.Exception.InvalidHostException;
import net.ME1312.SubServers.Bungee.Library.Exception.InvalidServerException;
import net.ME1312.SubServers.Bungee.SubAPI;
Expand Down Expand Up @@ -463,12 +464,14 @@ public boolean forceDeleteSubServer(UUID player, String name) throws Interrupted
*
* @return Success Status
*/
@SuppressWarnings("unchecked")
public boolean destroy() {
try {
String[] subservers = getSubServers().keySet().toArray(new String[0]);
Map.Entry<String, SubServer>[] subservers = getSubServers().entrySet().toArray(new Map.Entry[0]);

for (String server : subservers) {
forceRemoveSubServer(server);
for (Map.Entry<String, SubServer> entry : subservers) {
if (entry.getValue().isRunning()) Logger.get("SubServers").info("Stopping " + entry.getValue().getName());
forceRemoveSubServer(entry.getKey());
}
getCreator().terminate();
getCreator().waitFor();
Expand Down
Expand Up @@ -7,12 +7,12 @@
import net.md_5.bungee.api.plugin.PluginDescription;

import java.io.File;
import java.io.IOException;

public final class Plugin extends net.md_5.bungee.api.plugin.Plugin {
private static final PluginDescription description = new PluginDescription();
private final ExceptionRunnable enable;
private final Runnable disable;
private boolean enabled;

@Deprecated
public Plugin() {
Expand Down Expand Up @@ -43,12 +43,17 @@ public void onEnable() {
if (enable == null) {
throw new IllegalStateException("SubServers.Bungee does not run as a plugin, but a wrapper. For more information on how to install, please visit this page: https://github.com/ME1312/SubServers-2/wiki/Install");
} else try {
enabled = true;
enable.run();
} catch (Throwable e) {
e.printStackTrace();
}
}

public boolean isActive() {
return enabled;
}

@Override
public void onDisable() {
if (disable != null) disable.run();
Expand Down
Expand Up @@ -731,6 +731,11 @@ public void run() {
@Override
public void stopListeners() {
if (running) {
if (plugin != null && plugin.isActive()) {
shutdown = !super.isRunning;
super.isRunning = true;
}

ListenerInfo[] listeners = getConfig().getListeners().toArray(new ListenerInfo[0]);
super.stopListeners();

Expand All @@ -742,6 +747,7 @@ public void stopListeners() {
}
}

private boolean shutdown = false;
protected void shutdown() {
if (running) {
legServers.clear();
Expand Down Expand Up @@ -771,6 +777,8 @@ protected void shutdown() {
subdata.close();
Thread.sleep(500);
} catch (InterruptedException | IOException e) {}

if (shutdown) super.isRunning = false;
}
}

Expand Down
23 changes: 13 additions & 10 deletions SubServers.Host/src/net/ME1312/SubServers/Host/ExHost.java
Expand Up @@ -291,21 +291,24 @@ public void run() {
}
}

@SuppressWarnings("unchecked")
private void stop() {
if (running) {
log.info.println("Stopping hosted servers");
String[] subservers = servers.keySet().toArray(new String[0]);
Map.Entry<String, SubServerImpl>[] subservers = servers.entrySet().toArray(new Map.Entry[0]);

for (String name : subservers) {
SubServerImpl server = servers.get(name);
server.stop();
try {
server.waitFor();
} catch (Exception e) {
log.error.println(e);
for (Map.Entry<String, SubServerImpl> entry : subservers) {
if (entry.getValue().isRunning()) {
log.info.println("Stopping " + entry.getValue().getName());
entry.getValue().stop();
try {
entry.getValue().waitFor();
} catch (Exception e) {
log.error.println(e);
}
}
servers.remove(name);
if (UPnP.isUPnPAvailable() && UPnP.isMappedTCP(server.getPort())) UPnP.closePortTCP(server.getPort());
servers.remove(entry.getKey());
if (UPnP.isUPnPAvailable() && UPnP.isMappedTCP(entry.getValue().getPort())) UPnP.closePortTCP(entry.getValue().getPort());
}
servers.clear();

Expand Down

0 comments on commit 76e11f0

Please sign in to comment.