2019-12-01 22:52:07 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2019-12-01 22:52:07 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
package org.geysermc.geyser.platform.bungeecord;
|
2019-12-01 22:52:07 +00:00
|
|
|
|
2022-09-04 21:11:08 +00:00
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import net.md_5.bungee.BungeeCord;
|
2020-04-27 20:45:14 +00:00
|
|
|
import net.md_5.bungee.api.config.ListenerInfo;
|
2019-12-01 22:52:07 +00:00
|
|
|
import net.md_5.bungee.api.plugin.Plugin;
|
2022-08-01 15:01:24 +00:00
|
|
|
import net.md_5.bungee.protocol.ProtocolConstants;
|
2022-02-27 22:38:55 +00:00
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
2020-11-14 23:49:56 +00:00
|
|
|
import org.geysermc.common.PlatformType;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.GeyserBootstrap;
|
2022-06-08 12:09:14 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
2022-09-04 18:08:17 +00:00
|
|
|
import org.geysermc.geyser.api.command.Command;
|
|
|
|
import org.geysermc.geyser.api.extension.Extension;
|
2022-03-20 02:55:29 +00:00
|
|
|
import org.geysermc.geyser.api.network.AuthType;
|
2022-06-08 12:09:14 +00:00
|
|
|
import org.geysermc.geyser.command.GeyserCommandManager;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.configuration.GeyserConfiguration;
|
|
|
|
import org.geysermc.geyser.dump.BootstrapDumpInfo;
|
|
|
|
import org.geysermc.geyser.ping.GeyserLegacyPingPassthrough;
|
|
|
|
import org.geysermc.geyser.ping.IGeyserPingPassthrough;
|
|
|
|
import org.geysermc.geyser.platform.bungeecord.command.GeyserBungeeCommandExecutor;
|
2022-06-08 12:09:14 +00:00
|
|
|
import org.geysermc.geyser.text.GeyserLocale;
|
|
|
|
import org.geysermc.geyser.util.FileUtils;
|
2023-04-23 01:33:23 +00:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2019-12-01 22:52:07 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2022-09-04 21:11:08 +00:00
|
|
|
import java.lang.reflect.Field;
|
2020-04-27 20:45:14 +00:00
|
|
|
import java.net.InetSocketAddress;
|
2021-07-31 16:52:49 +00:00
|
|
|
import java.net.SocketAddress;
|
2020-06-20 17:54:40 +00:00
|
|
|
import java.nio.file.Path;
|
2021-08-24 19:11:38 +00:00
|
|
|
import java.nio.file.Paths;
|
2022-09-04 21:11:08 +00:00
|
|
|
import java.util.Collection;
|
2022-09-04 18:08:17 +00:00
|
|
|
import java.util.Map;
|
2023-04-23 01:33:23 +00:00
|
|
|
import java.util.Optional;
|
2019-12-01 22:52:07 +00:00
|
|
|
import java.util.UUID;
|
2022-09-04 21:11:08 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2019-12-02 14:45:51 +00:00
|
|
|
import java.util.logging.Level;
|
2019-12-01 22:52:07 +00:00
|
|
|
|
2020-04-29 20:01:53 +00:00
|
|
|
public class GeyserBungeePlugin extends Plugin implements GeyserBootstrap {
|
2019-12-01 22:52:07 +00:00
|
|
|
|
2022-10-18 20:05:55 +00:00
|
|
|
private GeyserCommandManager geyserCommandManager;
|
2019-12-01 22:52:07 +00:00
|
|
|
private GeyserBungeeConfiguration geyserConfig;
|
2021-07-31 16:52:49 +00:00
|
|
|
private GeyserBungeeInjector geyserInjector;
|
2019-12-01 22:52:07 +00:00
|
|
|
private GeyserBungeeLogger geyserLogger;
|
2020-05-23 21:50:04 +00:00
|
|
|
private IGeyserPingPassthrough geyserBungeePingPassthrough;
|
2019-12-01 22:52:07 +00:00
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
private GeyserImpl geyser;
|
2020-03-17 16:43:09 +00:00
|
|
|
|
2019-12-01 22:52:07 +00:00
|
|
|
@Override
|
2022-09-04 21:11:08 +00:00
|
|
|
public void onLoad() {
|
2022-09-20 17:18:41 +00:00
|
|
|
GeyserLocale.init(this);
|
|
|
|
|
2022-09-04 21:19:56 +00:00
|
|
|
// Copied from ViaVersion.
|
|
|
|
// https://github.com/ViaVersion/ViaVersion/blob/b8072aad86695cc8ec6f5e4103e43baf3abf6cc5/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java#L43
|
|
|
|
try {
|
2022-12-07 16:59:20 +00:00
|
|
|
ProtocolConstants.class.getField("MINECRAFT_1_19_3");
|
2022-09-04 21:19:56 +00:00
|
|
|
} catch (NoSuchFieldException e) {
|
|
|
|
getLogger().warning(" / \\");
|
|
|
|
getLogger().warning(" / \\");
|
|
|
|
getLogger().warning(" / | \\");
|
|
|
|
getLogger().warning(" / | \\ " + GeyserLocale.getLocaleStringLog("geyser.bootstrap.unsupported_proxy", getProxy().getName()));
|
|
|
|
getLogger().warning(" / \\ " + GeyserLocale.getLocaleStringLog("geyser.may_not_work_as_intended_all_caps"));
|
|
|
|
getLogger().warning(" / o \\");
|
|
|
|
getLogger().warning("/_____________\\");
|
|
|
|
}
|
|
|
|
|
2019-12-01 22:52:07 +00:00
|
|
|
if (!getDataFolder().exists())
|
|
|
|
getDataFolder().mkdir();
|
|
|
|
|
2019-12-02 16:54:07 +00:00
|
|
|
try {
|
2020-06-10 22:58:29 +00:00
|
|
|
if (!getDataFolder().exists())
|
|
|
|
getDataFolder().mkdir();
|
2021-12-03 16:01:06 +00:00
|
|
|
File configFile = FileUtils.fileOrCopiedFromResource(new File(getDataFolder(), "config.yml"),
|
|
|
|
"config.yml", (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString()), this);
|
2020-06-10 22:58:29 +00:00
|
|
|
this.geyserConfig = FileUtils.loadConfig(configFile, GeyserBungeeConfiguration.class);
|
|
|
|
} catch (IOException ex) {
|
2021-12-03 16:01:06 +00:00
|
|
|
getLogger().log(Level.SEVERE, GeyserLocale.getLocaleStringLog("geyser.config.failed"), ex);
|
2020-06-10 22:58:29 +00:00
|
|
|
ex.printStackTrace();
|
2021-12-03 16:01:06 +00:00
|
|
|
return;
|
2019-12-02 16:54:07 +00:00
|
|
|
}
|
2019-12-01 22:52:07 +00:00
|
|
|
|
2020-03-17 16:43:09 +00:00
|
|
|
this.geyserLogger = new GeyserBungeeLogger(getLogger(), geyserConfig.isDebugMode());
|
2020-05-21 03:43:22 +00:00
|
|
|
GeyserConfiguration.checkGeyserConfiguration(geyserConfig, geyserLogger);
|
2020-05-03 20:30:20 +00:00
|
|
|
|
2022-09-04 21:11:08 +00:00
|
|
|
this.geyser = GeyserImpl.load(PlatformType.BUNGEECORD, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
2021-06-06 23:01:16 +00:00
|
|
|
// Remove this in like a year
|
|
|
|
if (getProxy().getPluginManager().getPlugin("floodgate-bungee") != null) {
|
2021-11-20 23:29:46 +00:00
|
|
|
geyserLogger.severe(GeyserLocale.getLocaleStringLog("geyser.bootstrap.floodgate.outdated", "https://ci.opencollab.dev/job/GeyserMC/job/Floodgate/job/master/"));
|
2021-06-06 23:01:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-03 22:27:42 +00:00
|
|
|
// Force-disable query if enabled, or else Geyser won't enable
|
|
|
|
for (ListenerInfo info : getProxy().getConfig().getListeners()) {
|
|
|
|
if (info.isQueryEnabled() && info.getQueryPort() == geyserConfig.getBedrock().port()) {
|
|
|
|
try {
|
|
|
|
Field queryField = ListenerInfo.class.getDeclaredField("queryEnabled");
|
|
|
|
queryField.setAccessible(true);
|
|
|
|
queryField.setBoolean(info, false);
|
|
|
|
geyserLogger.warning("We force-disabled query on port " + info.getQueryPort() + " in order for Geyser to boot up successfully. " +
|
|
|
|
"To remove this message, disable query in your proxy's config.");
|
|
|
|
} catch (NoSuchFieldException | IllegalAccessException e) {
|
|
|
|
geyserLogger.warning("Could not force-disable query. Geyser may not start correctly!");
|
|
|
|
if (geyserLogger.isDebug()) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 17:18:41 +00:00
|
|
|
if (geyserConfig.getRemote().authType() == AuthType.FLOODGATE && getProxy().getPluginManager().getPlugin("floodgate") == null) {
|
|
|
|
geyserLogger.severe(GeyserLocale.getLocaleStringLog("geyser.bootstrap.floodgate.not_installed") + " " + GeyserLocale.getLocaleStringLog("geyser.bootstrap.floodgate.disabling"));
|
|
|
|
return;
|
|
|
|
} else if (geyserConfig.isAutoconfiguredRemote() && getProxy().getPluginManager().getPlugin("floodgate") != null) {
|
|
|
|
// Floodgate installed means that the user wants Floodgate authentication
|
|
|
|
geyserLogger.debug("Auto-setting to Floodgate authentication.");
|
|
|
|
geyserConfig.getRemote().setAuthType(AuthType.FLOODGATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
geyserConfig.loadFloodgate(this);
|
|
|
|
|
2022-09-04 21:11:08 +00:00
|
|
|
// Big hack - Bungee does not provide us an event to listen to, so schedule a repeating
|
|
|
|
// task that waits for a field to be filled which is set after the plugin enable
|
|
|
|
// process is complete
|
|
|
|
this.awaitStartupCompletion(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private void awaitStartupCompletion(int tries) {
|
|
|
|
// After 20 tries give up waiting. This will happen
|
|
|
|
// just after 3 minutes approximately
|
|
|
|
if (tries >= 20) {
|
|
|
|
this.geyserLogger.warning("BungeeCord plugin startup is taking abnormally long, so Geyser is starting now. " +
|
|
|
|
"If all your plugins are loaded properly, this is a bug! " +
|
|
|
|
"If not, consider cutting down the amount of plugins on your proxy as it is causing abnormally slow starting times.");
|
|
|
|
this.postStartup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Field listenersField = BungeeCord.getInstance().getClass().getDeclaredField("listeners");
|
|
|
|
listenersField.setAccessible(true);
|
|
|
|
|
|
|
|
Collection<Channel> listeners = (Collection<Channel>) listenersField.get(BungeeCord.getInstance());
|
2022-09-04 21:19:56 +00:00
|
|
|
if (listeners.isEmpty()) {
|
2022-09-04 21:11:08 +00:00
|
|
|
this.getProxy().getScheduler().schedule(this, this::postStartup, tries, TimeUnit.SECONDS);
|
|
|
|
} else {
|
|
|
|
this.awaitStartupCompletion(++tries);
|
|
|
|
}
|
|
|
|
} catch (NoSuchFieldException | IllegalAccessException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void postStartup() {
|
|
|
|
GeyserImpl.start();
|
2019-12-01 22:52:07 +00:00
|
|
|
|
2021-11-09 16:44:28 +00:00
|
|
|
this.geyserInjector = new GeyserBungeeInjector(this);
|
2021-07-31 16:52:49 +00:00
|
|
|
this.geyserInjector.initializeLocalChannel(this);
|
|
|
|
|
2022-10-18 20:05:55 +00:00
|
|
|
this.geyserCommandManager = new GeyserCommandManager(geyser);
|
2022-01-16 21:09:53 +00:00
|
|
|
this.geyserCommandManager.init();
|
2020-04-19 23:08:14 +00:00
|
|
|
|
2022-09-04 18:08:17 +00:00
|
|
|
this.getProxy().getPluginManager().registerCommand(this, new GeyserBungeeCommandExecutor("geyser", this.geyser, this.geyserCommandManager.getCommands()));
|
|
|
|
for (Map.Entry<Extension, Map<String, Command>> entry : this.geyserCommandManager.extensionCommands().entrySet()) {
|
|
|
|
Map<String, Command> commands = entry.getValue();
|
|
|
|
if (commands.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getProxy().getPluginManager().registerCommand(this, new GeyserBungeeCommandExecutor(entry.getKey().description().id(), this.geyser, commands));
|
|
|
|
}
|
2022-09-04 21:11:08 +00:00
|
|
|
|
|
|
|
if (geyserConfig.isLegacyPingPassthrough()) {
|
|
|
|
this.geyserBungeePingPassthrough = GeyserLegacyPingPassthrough.init(geyser);
|
|
|
|
} else {
|
|
|
|
this.geyserBungeePingPassthrough = new GeyserBungeePingPassthrough(getProxy());
|
|
|
|
}
|
2019-12-01 22:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisable() {
|
2021-11-20 21:34:30 +00:00
|
|
|
if (geyser != null) {
|
|
|
|
geyser.shutdown();
|
2021-07-31 16:52:49 +00:00
|
|
|
}
|
|
|
|
if (geyserInjector != null) {
|
|
|
|
geyserInjector.shutdown();
|
|
|
|
}
|
2019-12-01 22:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GeyserBungeeConfiguration getGeyserConfig() {
|
|
|
|
return geyserConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GeyserBungeeLogger getGeyserLogger() {
|
|
|
|
return geyserLogger;
|
|
|
|
}
|
2020-04-19 23:08:14 +00:00
|
|
|
|
|
|
|
@Override
|
2022-01-16 21:09:53 +00:00
|
|
|
public GeyserCommandManager getGeyserCommandManager() {
|
2020-04-19 23:08:14 +00:00
|
|
|
return this.geyserCommandManager;
|
|
|
|
}
|
2020-05-23 21:50:04 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public IGeyserPingPassthrough getGeyserPingPassthrough() {
|
|
|
|
return geyserBungeePingPassthrough;
|
|
|
|
}
|
2020-06-20 17:54:40 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Path getConfigFolder() {
|
|
|
|
return getDataFolder().toPath();
|
|
|
|
}
|
2020-06-27 15:36:48 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public BootstrapDumpInfo getDumpInfo() {
|
|
|
|
return new GeyserBungeeDumpInfo(getProxy());
|
|
|
|
}
|
2021-07-31 16:52:49 +00:00
|
|
|
|
2021-08-24 19:11:38 +00:00
|
|
|
@Override
|
|
|
|
public Path getLogsPath() {
|
2021-08-25 10:31:12 +00:00
|
|
|
return Paths.get(getProxy().getName().equals("BungeeCord") ? "proxy.log.0" : "logs/latest.log");
|
2021-08-24 19:11:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 16:52:49 +00:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public SocketAddress getSocketAddress() {
|
|
|
|
return this.geyserInjector.getServerSocketAddress();
|
|
|
|
}
|
2023-04-23 01:33:23 +00:00
|
|
|
|
|
|
|
@NotNull
|
|
|
|
@Override
|
|
|
|
public String getServerBindAddress() {
|
|
|
|
return findCompatibleListener().map(InetSocketAddress::getHostString).orElse("");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getServerPort() {
|
|
|
|
return findCompatibleListener().stream().mapToInt(InetSocketAddress::getPort).findFirst().orElse(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Optional<InetSocketAddress> findCompatibleListener() {
|
|
|
|
return getProxy().getConfig().getListeners().stream()
|
|
|
|
.filter(info -> info.getSocketAddress() instanceof InetSocketAddress)
|
|
|
|
.map(info -> (InetSocketAddress) info.getSocketAddress())
|
|
|
|
.findFirst();
|
|
|
|
}
|
2019-12-01 22:52:07 +00:00
|
|
|
}
|