1.4: Теперь можно указывать отдаваемую версию игры и id протокола

This commit is contained in:
Dereku 2024-03-02 21:06:54 +07:00
parent ffd0758857
commit 5932933f77
4 changed files with 22 additions and 29 deletions

View file

@ -6,7 +6,7 @@
<groupId>net.rubeta</groupId>
<artifactId>LegacyPing</artifactId>
<version>1.2</version>
<version>1.4</version>
<packaging>jar</packaging>
<name>LegacyPing</name>

View file

@ -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<Integer> 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);
}

View file

@ -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();

View file

@ -2,3 +2,5 @@ host: '0.0.0.0'
port:
- 25566
- 25567
displayedVersion: 'b1.7.3'
displayedProtocol: 17