【问题标题】:Minecraft - Bukkit SocketsMinecraft - Bukkit 插座
【发布时间】:2017-07-19 10:53:08
【问题描述】:

我尝试获取远程服务器的 MOTD,但无法获取颜色。当 MOTD 着色时,插件不起作用。 我知道为什么,但我不知道如何解决。

public PingServer(String host, int port) {
    this.host = host;
    this.port = port;

    try {
        socket.connect(new InetSocketAddress(host, port));
        OutputStream out = socket.getOutputStream();
        InputStream in = socket.getInputStream();
        out.write(0xFE);

        int b;
        StringBuffer str = new StringBuffer();
        while ((b = in.read()) != -1) {
            if (b != 0 && b > 16 && b != 255 && b != 23 && b != 24) {
                str.append((char) b);
            }
        }

        data = str.toString().split("§");
        data[0] = data[0].substring(1, data[0].length());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

根据specification,插件会得到这样的响应:MOTD§ONLINEPLAYERS§MAXPLAYERS,应该在§上拆分以获得不同的部分。但是,§ 也用于chat messages,我不确定如何区分两者。我该如何解决这个问题?

【问题讨论】:

  • 请说明几个例子以及它们应该如何解码。
  • 您使用的是旧的 MC 版本吗? ping 从 1.6 开始就不是那样了。
  • 真的没有。我正在使用 1.8.8,我可以说这种方法运行良好。只有颜色... :(你有更好的主意吗?
  • Xan - 好吧,假设 Motd 是 §aLobby(绿色标题 Lobby),在线玩家为 1,最大玩家为 50。结果:§aLobby§1§50 所以拆分现在不起作用,因为MOTD 不是 data[0] 但 data[1] 和 MOTD 没有着色但看起来像这样:aLobby
  • 哦,您正在执行旧服务器列表 ping。您不应该这样做,因为它是为遗留客户设计的,并且缺少很多信息(可能包括颜色)。见the "Current" section of this article。您不再需要在§ 上拆分,因此不再存在嵌入颜色代码的问题(它将正确存储在 JSON blob 中)。

标签: java sockets ping minecraft bukkit


【解决方案1】:

您目前使用的是 legacy server list ping,专为 beta 1.8 到 1.3 设计。通过仅向服务器发送FE 来触发那个。虽然当前的服务器仍然支持这种 ping,但它已经很老了并且有几个缺陷(包括你发现的那个)。

您应该改为执行current ping。虽然这稍微复杂一些,但您不需要实现很多协议来实际执行它。

您只需要了解协议的一个复杂部分:VarInts。这些有点复杂,因为它们根据值占用不同数量的字节。因此,您的数据包长度可能有点难以计算。

/** See http://wiki.vg/Protocol_version_numbers.  47 = 1.8.x */
private static final int PROTOCOL_VERSION_NUMBER = 47;
private static final int STATUS_PROTOCOL = 1;
private static final JsonParser PARSER = new JsonParser();

/** Pings a server, returning the MOTD */
public static String pingServer(String host, int port) {
    this.host = host;
    this.port = port;

    try {
        socket.connect(new InetSocketAddress(host, port));
        OutputStream out = socket.getOutputStream();
        InputStream in = socket.getInputStream();

        byte[] hostBytes = host.getBytes("UTF-8");
        int handshakeLength =
                varIntLength(0) + // Packet ID
                varIntLength(PROTOCOL_VERSION_NUMBER) + // Protocol version number
                varIntLength(hostBytes.length) + hostBytes.length + // Host
                2 + // Port
                varIntLength(STATUS_PROTOCOL);  // Next state

        writeVarInt(handshakeLength, out);
        writeVarInt(0, out);  // Handshake packet
        writeVarInt(PROTOCOL_VERSION_NUMBER, out);
        writeVarInt(hostBytes.length, out);
        out.write(hostBytes);
        out.write((port & 0xFF00) >> 8);
        out.write(port & 0xFF);
        writeVarInt(STATUS_PROTOCOL, out);

        writeVarInt(varIntLength(0));
        writeVarInt(0);  // Request packet (has no payload)

        int packetLength = readVarInt(in);
        int payloadLength = readVarInt(in);
        byte[] payloadBytes = new int[payloadLength];
        int readLength = in.read(payloadBytes);
        if (readLength < payloadLength) {
            throw new RuntimeException("Unexpected end of stream");
        }
        String payload = new String(payloadBytes, "UTF-8");

        // Now you need to parse the JSON; this is using GSON
        // See https://github.com/google/gson
        // and http://www.javadoc.io/doc/com.google.code.gson/gson/2.8.0
        JsonObject element = (JsonObject) PARSER.parse(payload);
        JsonElement description = element.get("description");
        // This is a naive implementation; it assumes a specific format for the description
        // rather than parsing the entire chat format.  But it works for the way the
        // notchian server impmlements the ping.
        String motd = ((JsonObject) description).get("text").getAsString();

        return motd;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public static int varIntLength(int value) {
    int length = 0;
    do {
        // Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone
        value >>>= 7;
        length++;
    } while (value != 0);
}

public static void writeVarInt(int value, OutputStream out) {
    do {
        byte temp = (byte)(value & 0b01111111);
        // Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone
        value >>>= 7;
        if (value != 0) {
            temp |= 0b10000000;
        }
        out.write(temp);
    } while (value != 0);
}
public static int readVarInt(InputStream in) {
    int numRead = 0;
    int result = 0;
    int read;
    do {
        read = in.read();
        if (read < 0) {
            throw new RuntimeException("Unexpected end of stream");
        }
        int value = (read & 0b01111111);
        result |= (value << (7 * numRead));

        numRead++;
        if (numRead > 5) {
            throw new RuntimeException("VarInt is too big");
        }
    } while ((read & 0b10000000) != 0);

    return result;
}

当前 ping 确实使用 JSON,这意味着您需要使用 GSON。此外,这个实现对chat 格式做了一些假设;此实现可能会在更完整地实现聊天的自定义服务器上中断,但它适用于将 § 嵌入到 motd 中的服务器,而不是使用更完整的聊天系统(这包括 Notchian 服务器实现)。


如果您需要使用旧 ping,您可以假设倒数第二个 § 标志着 MOTD 的结束(而不是第一个 §)。像这样的:

String legacyPingResult = str.toString();
String[] data = new String[3];
int splitPoint2 = legacyPingResult.lastIndexOf('§');
int splitPoint1 = legacyPingResult.lastIndexOf('§', splitPoint2 - 1);

data[0] = legacyPingResult.substring(0, splitPoint1);
data[1] = legacyPingResult.substring(splitPoint1 + 1, splitPoint2);
data[2] = legacyPingResult.substring(splitPoint2 + 1);

但是,我仍然不建议使用旧 ping。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多