diff --git a/pom.xml b/pom.xml index 0bbf5cf..35934c0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.rubeta LegacyPing - 1.2 + 1.4 jar LegacyPing diff --git a/src/main/java/net/rubeta/legacyping/LegacyPing.java b/src/main/java/net/rubeta/legacyping/LegacyPing.java index 1b3b756..2d93a48 100644 --- a/src/main/java/net/rubeta/legacyping/LegacyPing.java +++ b/src/main/java/net/rubeta/legacyping/LegacyPing.java @@ -17,9 +17,11 @@ public final class LegacyPing extends JavaPlugin { final FileConfiguration config = this.getConfig(); final String host = config.getString("host", "0.0.0.0"); final List ports = config.getIntegerList("port"); + final String version = config.getString("displayedVersion", "b1.7.3"); + final int protocol = config.getInt("displayedProtocol", 17); for (int port : ports) { try { - this.threads.add(new ServerThread(this.getServer(), host, port)); + this.threads.add(new ServerThread(this.getServer(), host, port, version, protocol)); } catch (IOException e) { this.getLogger().log(Level.WARNING, "Failed to create ServerSocket", e); } diff --git a/src/main/java/net/rubeta/legacyping/ServerThread.java b/src/main/java/net/rubeta/legacyping/ServerThread.java index 0afe05b..180e4bf 100644 --- a/src/main/java/net/rubeta/legacyping/ServerThread.java +++ b/src/main/java/net/rubeta/legacyping/ServerThread.java @@ -11,17 +11,21 @@ import java.util.concurrent.atomic.AtomicBoolean; public class ServerThread extends Thread { private static final String MODERN_RESPONSE = - "{\"version\": {\"name\": \"b1.7.3\",\"protocol\": -17}," + + "{\"version\": {\"name\": \"%version%\",\"protocol\": -%protocol%}," + "\"players\": {\"max\": %max%,\"online\": %now%}," + "\"description\": {\"text\": \"%motd%\"}," + "\"enforcesSecureChat\": false,\"previewsChat\": true}"; private final ServerSocket serverSocket; private final AtomicBoolean working = new AtomicBoolean(false); private final Server minecraftServer; + private final String version; + private final int protocol; - public ServerThread(final Server server, final String host, int port) throws IOException { + public ServerThread(final Server server, final String host, int port, String version, int protocol) throws IOException { this.minecraftServer = server; this.setName("LegacyPing thread listening " + host + ":" + port); + this.version = version; + this.protocol = Math.abs(protocol); final InetAddress address = InetAddress.getByName(host); this.serverSocket = new ServerSocket(port, 10, address); this.serverSocket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L)); @@ -31,6 +35,8 @@ public class ServerThread extends Thread { public ServerThread(final String host, int port) throws IOException { this.minecraftServer = null; this.setName("LegacyPing thread listening " + host + ":" + port); + this.version = "dummy"; + this.protocol = 0; final InetAddress address = InetAddress.getByName(host); this.serverSocket = new ServerSocket(port, 10, address); this.serverSocket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L)); @@ -52,15 +58,10 @@ public class ServerThread extends Thread { final InputStream inputStream = socket.getInputStream(); final OutputStream outputStream = socket.getOutputStream(); final int firstByte = inputStream.read(); - switch (firstByte) { - case 0: - this.responseForRuBetaModdedClient(outputStream); - break; - case 254: - this.responseForLegacyPing(outputStream); - break; - default: - this.responseForModernPing(outputStream); + if (firstByte == 254) { + this.responseForLegacyPing(outputStream); + } else { + this.responseForModernPing(outputStream); } outputStream.flush(); //socket.close(); @@ -79,21 +80,6 @@ public class ServerThread extends Thread { } } - private void responseForRuBetaModdedClient(final OutputStream outputStream) throws IOException { - final ByteArrayOutputStream result = new ByteArrayOutputStream(); - final DataOutputStream dos = new DataOutputStream(result); - dos.write(255); // Packet 0xFF (PacketDisconnect) - final String response = this.getMotd() + '\u0000' + this.getOnlinePlayers() + '\u0000' + this.getMaxPlayers(); - dos.writeShort(response.length()); - for (char c : response.toCharArray()) { - dos.writeChar(c); - } - // Packet data end - final DataOutputStream clientOutput = new DataOutputStream(outputStream); - clientOutput.writeInt(result.size()); - clientOutput.write(result.toByteArray()); - } - @SuppressWarnings("UnnecessaryUnicodeEscape") private void responseForLegacyPing(final OutputStream outputStream) throws IOException { final DataOutputStream clientOutput = new DataOutputStream(outputStream); @@ -107,7 +93,10 @@ public class ServerThread extends Thread { } private void responseForModernPing(final OutputStream outputStream) throws IOException { - final String responseJson = MODERN_RESPONSE.replace("%max%", String.valueOf(this.getMaxPlayers())) + final String responseJson = MODERN_RESPONSE + .replace("%version%", String.valueOf(this.version)) + .replace("%protocol%", String.valueOf(this.protocol)) + .replace("%max%", String.valueOf(this.getMaxPlayers())) .replace("%now%", String.valueOf(this.getOnlinePlayers())) .replace("%motd%", this.getMotd()); byte[] bytes = responseJson.getBytes(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 803f05f..f06ef01 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -2,3 +2,5 @@ host: '0.0.0.0' port: - 25566 - 25567 +displayedVersion: 'b1.7.3' +displayedProtocol: 17