- Баллы
- 108
- Имя в Minecraft
- CraftCoderr
Как получить состояние другого сервера в minecraft 1.8? В 1.5.2 получилось, а в 1.8 видимо протокол поменяли, а инфы найти никакой не могу.
Решение:
JSONObject будет вида:
@Shevchik @fromgate
Решение:
Java:
private static JSONObject fetchData(InetSocketAddress host) throws IOException {
Socket socket = null;
DataOutputStream dataOut = null;
DataInputStream dataIn = null;
try {
socket = new Socket(host.getAddress(), host.getPort());
socket.setSoTimeout(timeout);
dataOut = new DataOutputStream(socket.getOutputStream());
dataIn = new DataInputStream(socket.getInputStream());
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
final DataOutputStream handshake = new DataOutputStream(byteOut);
handshake.write(0);
PacketUtils.writeVarInt(handshake, 4);
PacketUtils.writeString(handshake, host.getAddress().getHostAddress(), PacketUtils.UTF8);
handshake.writeShort(host.getPort());
PacketUtils.writeVarInt(handshake, 1);
byte[] bytes = byteOut.toByteArray();
PacketUtils.writeVarInt(dataOut, bytes.length);
dataOut.write(bytes);
bytes = new byte[] { 0 };
PacketUtils.writeVarInt(dataOut, bytes.length);
dataOut.write(bytes);
PacketUtils.readVarInt(dataIn);
PacketUtils.readVarInt(dataIn);
final byte[] responseData = new byte[PacketUtils.readVarInt(dataIn)];
dataIn.readFully(responseData);
final String jsonString = new String(responseData, PacketUtils.UTF8);
Object jsonObject = JSONValue.parse(jsonString);
JSONObject json = (JSONObject) jsonObject;
return json;
}
finally {
PacketUtils.closeQuietly(dataOut);
PacketUtils.closeQuietly(dataIn);
PacketUtils.closeQuietly(socket);
}
}
XML:
{
"version": {
"name": "1.7.9",
"protocol": 5
},
"players": {
"max": 100,
"online": 5,
"sample": [
{
"name": "thinkofdeath",
"id": "4566e69f-c907-48ee-8d71-d7ba5aa00d20"
}
]
},
"description": {
"text": "Hello world"
},
"favicon": "data:image/png;base64,<data>"
}
@Shevchik @fromgate
Последнее редактирование: