Reset servers to unregistered after removal

This commit is contained in:
ME1312 2021-06-06 02:59:50 -04:00
parent ee59429e1d
commit ba6af4d66d
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
7 changed files with 31 additions and 19 deletions

View File

@ -20,7 +20,6 @@ import net.ME1312.SubServers.Bungee.Network.Packet.PacketOutExReset;
import net.ME1312.SubServers.Bungee.SubProxy;
import com.google.common.collect.Range;
import net.md_5.bungee.BungeeCord;
import java.net.InetAddress;
import java.util.*;
@ -180,7 +179,7 @@ public class ExternalHost extends Host implements ClientHandler {
queue(new PacketExAddServer(((ExternalSubServer) server), (server.isRunning())?((ExternalSubLogger) server.getLogger()).getExternalAddress():null, data -> {
if (data.contains(0x0002)) ((ExternalSubServer) server).started(data.getUUID(0x0002));
}));
((ExternalSubServer) server).register();
((ExternalSubServer) server).registered(true);
servers.put(server.getName().toLowerCase(), server);
return true;
} else {
@ -191,18 +190,21 @@ public class ExternalHost extends Host implements ClientHandler {
@Override
protected boolean removeSubServer(UUID player, String name, boolean forced) throws InterruptedException {
if (Util.isNull(name)) throw new NullPointerException();
SubServer server = servers.get(name.toLowerCase());
ExternalSubServer server = (ExternalSubServer) servers.get(name.toLowerCase());
SubRemoveServerEvent event = new SubRemoveServerEvent(player, this, server);
plugin.getPluginManager().callEvent(event);
if (forced || !event.isCancelled()) {
server.registered(false);
if (server.isRunning()) {
server.stop();
server.waitFor();
}
servers.remove(name.toLowerCase());
queue(new PacketExRemoveServer(name.toLowerCase(), data -> {
if (data.getInt(0x0001) != 0 && data.getInt(0x0001) != 1) {
server.registered(true);
servers.put(name.toLowerCase(), server);
}
}));
@ -213,12 +215,13 @@ public class ExternalHost extends Host implements ClientHandler {
@Override
protected boolean recycleSubServer(UUID player, String name, boolean forced) throws InterruptedException {
if (Util.isNull(name)) throw new NullPointerException();
SubServer s = servers.get(name.toLowerCase());
ExternalSubServer s = (ExternalSubServer) servers.get(name.toLowerCase());
String server = s.getName();
SubRemoveServerEvent event = new SubRemoveServerEvent(player, this, s);
plugin.getPluginManager().callEvent(event);
if (forced || !event.isCancelled()) {
s.registered(false);
if (s.isRunning()) {
s.stop();
s.waitFor();
@ -243,6 +246,7 @@ public class ExternalHost extends Host implements ClientHandler {
servers.remove(server.toLowerCase());
Logger.get("SubServers").info("Deleted SubServer: " + server);
} else {
s.registered(true);
Logger.get("SubServers").info("Couldn't remove " + server + " from memory. See " + getName() + " console for more details");
}
}));
@ -253,12 +257,13 @@ public class ExternalHost extends Host implements ClientHandler {
@Override
protected boolean deleteSubServer(UUID player, String name, boolean forced) throws InterruptedException {
if (Util.isNull(name)) throw new NullPointerException();
SubServer s = servers.get(name.toLowerCase());
ExternalSubServer s = (ExternalSubServer) servers.get(name.toLowerCase());
String server = s.getName();
SubRemoveServerEvent event = new SubRemoveServerEvent(player, this, getSubServer(server));
plugin.getPluginManager().callEvent(event);
if (forced || !event.isCancelled()) {
s.registered(false);
if (s.isRunning()) {
s.stop();
s.waitFor();
@ -283,6 +288,7 @@ public class ExternalHost extends Host implements ClientHandler {
servers.remove(server.toLowerCase());
Logger.get("SubServers").info("Deleted SubServer: " + server);
} else {
s.registered(true);
Logger.get("SubServers").info("Couldn't remove " + server + " from memory. See " + getName() + " console for more details");
}
}));

View File

@ -143,7 +143,7 @@ public class ExternalSubCreator extends SubCreator {
String name = server.getName();
String prefix = name + File.separator + "Updater";
Util.isException(() -> Util.reflect(SubServerImpl.class.getDeclaredField("updating"), server, true));
((ExternalSubServer) server).updating(true);
ExternalSubLogger logger = new ExternalSubLogger(this, prefix, log, null);
thread.put(name.toLowerCase(), new ContainedPair<>(server.getAddress().getPort(), logger));
@ -153,7 +153,7 @@ public class ExternalSubCreator extends SubCreator {
logger.start();
host.queue(new PacketExCreateServer(player, server, ft, version, logger.getExternalAddress(), data -> {
finish(player, server, server.getName(), ft, version, server.getAddress().getPort(), prefix, origin, data, s -> {
Util.isException(() -> Util.reflect(SubServerImpl.class.getDeclaredField("updating"), server, false));
((ExternalSubServer) server).updating(false);
if (callback != null) callback.run(s != null);
});
logger.stop();

View File

@ -97,8 +97,12 @@ public class ExternalSubServer extends SubServerImpl {
this.lock = false;
}
void register() {
registered = true;
void registered(boolean value) {
registered = value;
}
void updating(boolean value) {
updating = value;
}
@Override

View File

@ -110,7 +110,7 @@ public class InternalHost extends Host {
SubAddServerEvent event = new SubAddServerEvent(player, this, server);
plugin.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
((InternalSubServer) server).register();
((InternalSubServer) server).registered(true);
servers.put(server.getName().toLowerCase(), server);
if (UPnP.isUPnPAvailable() && plugin.config.get().getMap("Settings").getMap("UPnP", new ObjectMap<String>()).getBoolean("Forward-Servers", false)) UPnP.openPortTCP(server.getAddress().getPort());
return true;
@ -122,10 +122,11 @@ public class InternalHost extends Host {
@Override
protected boolean removeSubServer(UUID player, String name, boolean forced) throws InterruptedException {
if (Util.isNull(name)) throw new NullPointerException();
SubServer server = servers.get(name.toLowerCase());
InternalSubServer server = (InternalSubServer) servers.get(name.toLowerCase());
SubRemoveServerEvent event = new SubRemoveServerEvent(player, this, server);
plugin.getPluginManager().callEvent(event);
if (forced || !event.isCancelled()) {
server.registered(false);
if (server.isRunning()) {
server.stop();
server.waitFor();

View File

@ -430,10 +430,9 @@ public class InternalSubCreator extends SubCreator {
if (host.isAvailable() && host.isEnabled() && host == server.getHost() && server.isAvailable() && !server.isRunning() && ft != null && ft.isEnabled() && ft.canUpdate() && (version != null || !ft.requiresVersion())) {
StackTraceElement[] origin = new Exception().getStackTrace();
Util.isException(() -> Util.reflect(SubServerImpl.class.getDeclaredField("updating"), server, true));
((InternalSubServer) server).updating(true);
CreatorTask task = new CreatorTask(player, server, ft, version, x -> {
Util.isException(() -> Util.reflect(SubServerImpl.class.getDeclaredField("updating"), server, false));
((InternalSubServer) server).updating(false);
if (callback != null) try {
callback.run(x != null);
} catch (Throwable e) {

View File

@ -136,8 +136,12 @@ public class InternalSubServer extends SubServerImpl {
this.lock = false;
}
void register() {
registered = true;
void registered(boolean value) {
registered = value;
}
void updating(boolean value) {
updating = value;
}
private void run() {

View File

@ -20,9 +20,7 @@ public abstract class SubServerImpl extends ServerImpl implements SubServer {
private List<Pair<String, String>> incompatibilities = new ArrayList<Pair<String, String>>();
private SubCreator.ServerTemplate templateV = null;
private String templateS = null;
protected boolean registered;
protected boolean started;
private boolean updating;
protected boolean registered, started, updating;
/**
* Creates a SubServer