Make the compatibility plugin loadable

Some BungeeCord API calls require an actual plugin object. This is a problem.

Fortunately, the 2020 builds of BungeeCord provide a protected constructor that is actually usable by SubServers, so we implemented it.
This commit is contained in:
ME1312 2020-11-11 02:35:02 -05:00
parent 489b25f48d
commit c62363bdef
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
7 changed files with 104 additions and 28 deletions

View File

@ -1,9 +1,39 @@
package net.ME1312.SubServers.Bungee.Library.Compatibility;
public class Plugin extends net.md_5.bungee.api.plugin.Plugin {
import net.ME1312.Galaxi.Library.Util;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.PluginDescription;
import java.io.File;
public final class Plugin extends net.md_5.bungee.api.plugin.Plugin {
private static final PluginDescription description = new PluginDescription();
private final boolean invalid;
@Deprecated
public Plugin() {
this.invalid = true;
}
private static PluginDescription describe() {
description.setName("SubServers-Bungee");
description.setMain(Plugin.class.getCanonicalName());
description.setFile(Util.getDespiteException(() -> new File(Plugin.class.getProtectionDomain().getCodeSource().getLocation().toURI()), null));
description.setVersion(net.ME1312.SubServers.Bungee.SubProxy.version.toString());
description.setAuthor("ME1312");
return description;
}
public Plugin(ProxyServer proxy) {
super(proxy, describe());
this.invalid = false;
// 2020 BungeeCord builds don't run init(), but future builds may uncomment that line. We wouldn't want to repeat ourselves.
if (getDescription() == null) Util.isException(() -> Util.reflect(net.md_5.bungee.api.plugin.Plugin.class.getDeclaredMethod("init", ProxyServer.class, PluginDescription.class), this, proxy, description));
}
@Override
public void onEnable() {
throw new IllegalStateException("SubServers.Bungee does not run as a plugin, but a wrapper. For more information on how to install, visit this page: https://github.com/ME1312/SubServers-2/wiki/Install");
if (invalid) 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");
}
}

View File

@ -140,7 +140,7 @@ public class SubProtocol extends SubDataProtocol {
instance = new SubProtocol();
log = net.ME1312.SubServers.Bungee.Library.Compatibility.Logger.get("SubData");
SubProxy plugin = SubAPI.getInstance().getInternals();
plugin.getPluginManager().registerListener(null, new PacketOutExRunEvent(plugin));
plugin.getPluginManager().registerListener(plugin.plugin, new PacketOutExRunEvent(plugin));
}
return instance;

View File

@ -20,6 +20,7 @@ import net.ME1312.Galaxi.Library.Config.YAMLSection;
import net.ME1312.SubServers.Bungee.Library.Compatibility.Galaxi.GalaxiCommand;
import net.ME1312.SubServers.Bungee.Library.Compatibility.LegacyServerMap;
import net.ME1312.SubServers.Bungee.Library.Compatibility.Logger;
import net.ME1312.SubServers.Bungee.Library.Compatibility.Plugin;
import net.ME1312.SubServers.Bungee.Library.Fallback.SmartFallback;
import net.ME1312.SubServers.Bungee.Library.ConfigUpdater;
import net.ME1312.SubServers.Bungee.Library.Exception.InvalidHostException;
@ -78,6 +79,7 @@ public final class SubProxy extends BungeeCord implements Listener {
public YAMLConfig servers;
private YAMLConfig bungee;
public YAMLConfig lang;
public final Plugin plugin;
public final SubAPI api = new SubAPI(this);
public SubProtocol subprotocol;
public SubDataServer subdata = null;
@ -234,7 +236,10 @@ public final class SubProxy extends BungeeCord implements Listener {
api.addHostDriver(net.ME1312.SubServers.Bungee.Host.Internal.InternalHost.class, "virtual");
api.addHostDriver(net.ME1312.SubServers.Bungee.Host.External.ExternalHost.class, "network");
getPluginManager().registerListener(null, this);
plugin = Util.getDespiteException(() -> new Plugin(this), null);
if (plugin == null) Logger.get("SubServers").warning("Could not initialize plugin object emulation");
getPluginManager().registerListener(plugin, this);
Logger.get("SubServers").info("Pre-Parsing Config...");
for (String name : servers.get().getMap("Servers").getKeys()) {
@ -273,7 +278,7 @@ public final class SubProxy extends BungeeCord implements Listener {
UPnP.openPortTCP(listener.getHost().getPort());
}
} else {
getLogger().warning("UPnP is currently unavailable; Ports may not be automatically forwarded on this device");
getLogger().warning("UPnP is currently unavailable. Ports may not be automatically forwarded on this device.");
}
super.startListeners();
@ -633,13 +638,13 @@ public final class SubProxy extends BungeeCord implements Listener {
private void post() {
if (!config.get().getMap("Settings").getRawStringList("Disabled-Overrides", Collections.emptyList()).contains("/server"))
getPluginManager().registerCommand(null, SubCommand.BungeeServer.newInstance(this, "server").get());
getPluginManager().registerCommand(plugin, SubCommand.BungeeServer.newInstance(this, "server").get());
if (!config.get().getMap("Settings").getRawStringList("Disabled-Overrides", Collections.emptyList()).contains("/glist"))
getPluginManager().registerCommand(null, new SubCommand.BungeeList(this, "glist"));
getPluginManager().registerCommand(plugin, new SubCommand.BungeeList(this, "glist"));
getPluginManager().registerCommand(null, SubCommand.newInstance(this, "subservers").get());
getPluginManager().registerCommand(null, SubCommand.newInstance(this, "subserver").get());
getPluginManager().registerCommand(null, SubCommand.newInstance(this, "sub").get());
getPluginManager().registerCommand(plugin, SubCommand.newInstance(this, "subservers").get());
getPluginManager().registerCommand(plugin, SubCommand.newInstance(this, "subserver").get());
getPluginManager().registerCommand(plugin, SubCommand.newInstance(this, "sub").get());
GalaxiCommand.group(SubCommand.class);
if (getReconnectHandler() != null && getReconnectHandler().getClass().equals(SmartFallback.class))
@ -851,13 +856,16 @@ public final class SubProxy extends BungeeCord implements Listener {
e.setResponse(new ServerPing(e.getResponse().getVersion(), e.getResponse().getPlayers(), new TextComponent(override.getMotd()), null));
} else {
PrimitiveContainer<Boolean> lock = new PrimitiveContainer<>(true);
boolean mode = plugin != null;
if (mode) e.registerIntent(plugin);
((BungeeServerInfo) override).ping((ping, error) -> {
if (error != null) {
e.setResponse(new ServerPing(e.getResponse().getVersion(), e.getResponse().getPlayers(), new TextComponent(getTranslation("ping_cannot_connect")), null));
} else e.setResponse(ping);
lock.value = false;
if (mode) e.completeIntent(plugin);
}, ((InitialHandler) e.getConnection()).getHandshake().getProtocolVersion());
while (lock.value) Util.isException(() -> Thread.sleep(4));
if (!mode) while (lock.value) Util.isException(() -> Thread.sleep(4));
}
}
} else if (dynamic) {
@ -1001,7 +1009,7 @@ public final class SubProxy extends BungeeCord implements Listener {
}
@EventHandler(priority = Byte.MAX_VALUE)
public void resetPlayer(PlayerDisconnectEvent e) {
public void disconnected(PlayerDisconnectEvent e) {
UUID id = e.getPlayer().getUniqueId();
fallbackLimbo.remove(id);
SubCommand.players.remove(id);

View File

@ -204,7 +204,7 @@ public final class ExHost {
engine.start(this::stop);
if (!UPnP.isUPnPAvailable()) {
log.warn.println("UPnP is currently unavailable; Ports may not be automatically forwarded on this device");
log.warn.println("UPnP is currently unavailable. Ports may not be automatically forwarded on this device.");
}
} catch (Exception e) {
if (engine == null) {

View File

@ -1,4 +1,4 @@
name: SubServers-Bungee
name: SubServers-Sync
main: net.ME1312.SubServers.Sync.Library.Compatibility.Plugin
version: x.x.xx
version: 2.XX.Xx
author: ME1312

View File

@ -14,6 +14,7 @@ import net.ME1312.Galaxi.Library.Config.YAMLConfig;
import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.SubServers.Sync.Library.Compatibility.Galaxi.GalaxiCommand;
import net.ME1312.SubServers.Sync.Library.Compatibility.Logger;
import net.ME1312.SubServers.Sync.Library.Compatibility.Plugin;
import net.ME1312.SubServers.Sync.Library.Fallback.SmartFallback;
import net.ME1312.SubServers.Sync.Library.Metrics;
import net.ME1312.Galaxi.Library.Container.NamedContainer;
@ -44,7 +45,6 @@ import net.md_5.bungee.connection.InitialHandler;
import net.md_5.bungee.event.EventHandler;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.URL;
import java.nio.charset.Charset;
@ -66,6 +66,7 @@ public final class ExProxy extends BungeeCord implements Listener {
public final PrintStream out;
public final UniversalFile dir = new UniversalFile(new File(System.getProperty("user.dir")));
public YAMLConfig config;
public final Plugin plugin;
public final SubAPI api = new SubAPI(this);
public SubProtocol subprotocol;
public static final Version version = Version.fromString("2.16.4a");
@ -109,7 +110,11 @@ public final class ExProxy extends BungeeCord implements Listener {
subprotocol.registerCipher("DHE-128", DHE.get(128));
subprotocol.registerCipher("DHE-192", DHE.get(192));
subprotocol.registerCipher("DHE-256", DHE.get(256));
getPluginManager().registerListener(null, this);
plugin = Util.getDespiteException(() -> new Plugin(this), null);
if (plugin == null) Logger.get("SubServers").warning("Could not initialize plugin object emulation");
getPluginManager().registerListener(plugin, this);
Logger.get("SubServers").info("Loading BungeeCord Libraries...");
}
@ -162,7 +167,7 @@ public final class ExProxy extends BungeeCord implements Listener {
UPnP.openPortTCP(listener.getHost().getPort());
}
} else {
getLogger().warning("UPnP is currently unavailable; Ports may not be automatically forwarded on this device");
getLogger().warning("UPnP is currently unavailable. Ports may not be automatically forwarded on this device.");
}
if (!posted) {
@ -193,7 +198,7 @@ public final class ExProxy extends BungeeCord implements Listener {
}
timer.cancel();
} catch (IOException e) {
net.ME1312.SubServers.Sync.Library.Compatibility.Logger.get("SubData").info("Connection was unsuccessful, retrying in " + reconnect + " seconds");
Logger.get("SubData").info("Connection was unsuccessful, retrying in " + reconnect + " seconds");
}
}
}, (disconnect == null)?0:TimeUnit.SECONDS.toMillis(reconnect), TimeUnit.SECONDS.toMillis(reconnect));
@ -202,13 +207,13 @@ public final class ExProxy extends BungeeCord implements Listener {
private void post() {
if (!config.get().getMap("Settings").getRawStringList("Disabled-Overrides", Collections.emptyList()).contains("/server"))
getPluginManager().registerCommand(null, SubCommand.BungeeServer.newInstance(this, "server").get());
getPluginManager().registerCommand(plugin, SubCommand.BungeeServer.newInstance(this, "server").get());
if (!config.get().getMap("Settings").getRawStringList("Disabled-Overrides", Collections.emptyList()).contains("/glist"))
getPluginManager().registerCommand(null, new SubCommand.BungeeList(this, "glist"));
getPluginManager().registerCommand(plugin, new SubCommand.BungeeList(this, "glist"));
getPluginManager().registerCommand(null, SubCommand.newInstance(this, "subservers").get());
getPluginManager().registerCommand(null, SubCommand.newInstance(this, "subserver").get());
getPluginManager().registerCommand(null, SubCommand.newInstance(this, "sub").get());
getPluginManager().registerCommand(plugin, SubCommand.newInstance(this, "subservers").get());
getPluginManager().registerCommand(plugin, SubCommand.newInstance(this, "subserver").get());
getPluginManager().registerCommand(plugin, SubCommand.newInstance(this, "sub").get());
GalaxiCommand.group(SubCommand.class);
if (getReconnectHandler() != null && getReconnectHandler().getClass().equals(SmartFallback.class))
@ -387,13 +392,16 @@ public final class ExProxy extends BungeeCord implements Listener {
e.setResponse(new ServerPing(e.getResponse().getVersion(), e.getResponse().getPlayers(), new TextComponent(override.getMotd()), null));
} else {
PrimitiveContainer<Boolean> lock = new PrimitiveContainer<>(true);
boolean mode = plugin != null;
if (mode) e.registerIntent(plugin);
((BungeeServerInfo) override).ping((ping, error) -> {
if (error != null) {
e.setResponse(new ServerPing(e.getResponse().getVersion(), e.getResponse().getPlayers(), new TextComponent(getTranslation("ping_cannot_connect")), null));
} else e.setResponse(ping);
lock.value = false;
if (mode) e.completeIntent(plugin);
}, ((InitialHandler) e.getConnection()).getHandshake().getProtocolVersion());
while (lock.value) Util.isException(() -> Thread.sleep(4));
if (!mode) while (lock.value) Util.isException(() -> Thread.sleep(4));
}
}
} else if (dynamic) {
@ -538,7 +546,7 @@ public final class ExProxy extends BungeeCord implements Listener {
}
@EventHandler(priority = Byte.MAX_VALUE)
public void resetPlayer(PlayerDisconnectEvent e) {
public void disconnected(PlayerDisconnectEvent e) {
UUID id = e.getPlayer().getUniqueId();
fallbackLimbo.remove(id);
SubCommand.permitted.remove(id);

View File

@ -1,9 +1,39 @@
package net.ME1312.SubServers.Sync.Library.Compatibility;
public class Plugin extends net.md_5.bungee.api.plugin.Plugin {
import net.ME1312.Galaxi.Library.Util;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.PluginDescription;
import java.io.File;
public final class Plugin extends net.md_5.bungee.api.plugin.Plugin {
private static final PluginDescription description = new PluginDescription();
private final boolean invalid;
@Deprecated
public Plugin() {
this.invalid = true;
}
private static PluginDescription describe() {
description.setName("SubServers-Sync");
description.setMain(Plugin.class.getCanonicalName());
description.setFile(Util.getDespiteException(() -> new File(Plugin.class.getProtectionDomain().getCodeSource().getLocation().toURI()), null));
description.setVersion(net.ME1312.SubServers.Sync.ExProxy.version.toString());
description.setAuthor("ME1312");
return description;
}
public Plugin(ProxyServer proxy) {
super(proxy, describe());
this.invalid = false;
// 2020 BungeeCord builds don't run init(), but future builds may uncomment that line. We wouldn't want to repeat ourselves.
if (getDescription() == null) Util.isException(() -> Util.reflect(net.md_5.bungee.api.plugin.Plugin.class.getDeclaredMethod("init", ProxyServer.class, PluginDescription.class), this, proxy, description));
}
@Override
public void onEnable() {
throw new IllegalStateException("SubServers.Sync does not run as a plugin, but a wrapper. For more information on how to install, visit this page: https://github.com/ME1312/SubServers-2/wiki/Install");
if (invalid) throw new IllegalStateException("SubServers.Sync 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");
}
}