From 58819ea9ceba53dd9157e2c0bb853a4df826bb17 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Sun, 21 Jul 2019 18:52:30 -0500 Subject: [PATCH] Finish ping-passthrough option --- .../geysermc/connector/GeyserConnector.java | 9 +++++ .../network/ConnectorServerEventHandler.java | 21 ++++++++-- .../thread/PingPassthroughThread.java | 40 +++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/thread/PingPassthroughThread.java diff --git a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java index 6d2735d0..50ec067b 100644 --- a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java +++ b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java @@ -46,12 +46,14 @@ import org.geysermc.connector.network.remote.RemoteJavaServer; import org.geysermc.connector.network.translators.TranslatorsInit; import org.geysermc.connector.plugin.GeyserPluginLoader; import org.geysermc.connector.plugin.GeyserPluginManager; +import org.geysermc.connector.thread.PingPassthroughThread; import org.geysermc.connector.utils.Toolbox; import java.io.*; import java.net.InetSocketAddress; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; public class GeyserConnector implements Connector { @@ -83,6 +85,9 @@ public class GeyserConnector implements Connector { @Getter private final ScheduledExecutorService generalThreadPool; + @Getter + private PingPassthroughThread passthroughThread; + public static void main(String[] args) { instance = new GeyserConnector(); } @@ -138,6 +143,10 @@ public class GeyserConnector implements Connector { pluginManager = new GeyserPluginManager(new GeyserPluginLoader(this)); pluginManager.getLoader().loadPlugins(); + passthroughThread = new PingPassthroughThread(this); + if (config.isPingPassthrough()) + generalThreadPool.scheduleAtFixedRate(passthroughThread, 1, 1, TimeUnit.SECONDS); + BedrockServer bedrockServer = new BedrockServer(new InetSocketAddress(config.getBedrock().getAddress(), config.getBedrock().getPort())); bedrockServer.setHandler(new ConnectorServerEventHandler(this)); bedrockServer.bind().whenComplete((avoid, throwable) -> { diff --git a/connector/src/main/java/org/geysermc/connector/network/ConnectorServerEventHandler.java b/connector/src/main/java/org/geysermc/connector/network/ConnectorServerEventHandler.java index f80a6f4a..b43025ac 100644 --- a/connector/src/main/java/org/geysermc/connector/network/ConnectorServerEventHandler.java +++ b/connector/src/main/java/org/geysermc/connector/network/ConnectorServerEventHandler.java @@ -25,6 +25,7 @@ package org.geysermc.connector.network; +import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo; import com.nukkitx.protocol.bedrock.BedrockPong; import com.nukkitx.protocol.bedrock.BedrockServerEventHandler; import com.nukkitx.protocol.bedrock.BedrockServerSession; @@ -33,6 +34,7 @@ import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.configuration.GeyserConfiguration; import org.geysermc.connector.console.GeyserLogger; import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.utils.MessageUtils; import java.net.InetSocketAddress; @@ -56,16 +58,27 @@ public class ConnectorServerEventHandler implements BedrockServerEventHandler { GeyserConfiguration config = connector.getConfig(); BedrockPong pong = new BedrockPong(); pong.setEdition("MCPE"); - pong.setMotd(config.getBedrock().getMotd1()); - pong.setSubMotd(config.getBedrock().getMotd2()); - pong.setPlayerCount(2); - pong.setMaximumPlayerCount(config.getMaxPlayers()); pong.setGameType("Default"); pong.setNintendoLimited(false); pong.setProtocolVersion(GeyserConnector.BEDROCK_PACKET_CODEC.getProtocolVersion()); pong.setVersion("1.12.0"); pong.setIpv4Port(19132); + if (connector.getConfig().isPingPassthrough()) { + ServerStatusInfo serverInfo = connector.getPassthroughThread().getInfo(); + + if (serverInfo != null) { + pong.setMotd(MessageUtils.getBedrockMessage(serverInfo.getDescription())); + pong.setSubMotd(config.getBedrock().getMotd2()); + pong.setPlayerCount(serverInfo.getPlayerInfo().getOnlinePlayers()); + pong.setMaximumPlayerCount(serverInfo.getPlayerInfo().getMaxPlayers()); + } + } else { + pong.setPlayerCount(1); + pong.setMaximumPlayerCount(config.getMaxPlayers()); + pong.setMotd(config.getBedrock().getMotd1()); + pong.setSubMotd(config.getBedrock().getMotd2()); + } return pong; } diff --git a/connector/src/main/java/org/geysermc/connector/thread/PingPassthroughThread.java b/connector/src/main/java/org/geysermc/connector/thread/PingPassthroughThread.java new file mode 100644 index 00000000..ce7e54a0 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/thread/PingPassthroughThread.java @@ -0,0 +1,40 @@ +package org.geysermc.connector.thread; + +import com.github.steveice10.mc.protocol.MinecraftConstants; +import com.github.steveice10.mc.protocol.MinecraftProtocol; +import com.github.steveice10.mc.protocol.data.SubProtocol; +import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo; +import com.github.steveice10.mc.protocol.data.status.handler.ServerInfoHandler; +import com.github.steveice10.packetlib.Client; +import com.github.steveice10.packetlib.tcp.TcpSessionFactory; +import lombok.Getter; +import org.geysermc.connector.GeyserConnector; + +public class PingPassthroughThread implements Runnable { + + private GeyserConnector connector; + + public PingPassthroughThread(GeyserConnector connector) { + this.connector = connector; + } + + @Getter + private ServerStatusInfo info; + + private Client client; + + @Override + public void run() { + try { + this.client = new Client(connector.getConfig().getRemote().getAddress(), connector.getConfig().getRemote().getPort(), new MinecraftProtocol(SubProtocol.STATUS), new TcpSessionFactory()); + this.client.getSession().setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, (ServerInfoHandler) (session, info) -> { + this.info = info; + this.client.getSession().disconnect(null); + }); + + client.getSession().connect(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } +}