Проблема с протоколом. MinecraftPing класс - PullRequest
0 голосов
/ 28 мая 2020

Я использую этот метод, но получаю сообщение об ошибке, необходимое для версии (PaperSpigot 1.15.2):

public MinecraftPingReply getPing(final MinecraftPingOptions options) throws IOException {
        MinecraftPingUtil.validate(options.getHostname(), "Hostname cannot be null.");
        MinecraftPingUtil.validate(options.getPort(), "Port cannot be null.");

        final Socket socket = new Socket();
        socket.connect(new InetSocketAddress(options.getHostname(), options.getPort()), options.getTimeout());

        final DataInputStream in = new DataInputStream(socket.getInputStream());
        final DataOutputStream out = new DataOutputStream(socket.getOutputStream());

        //> Handshake

        ByteArrayOutputStream handshake_bytes = new ByteArrayOutputStream();
        DataOutputStream handshake = new DataOutputStream(handshake_bytes);

        handshake.writeByte(MinecraftPingUtil.PACKET_HANDSHAKE);
        MinecraftPingUtil.writeVarInt(handshake, MinecraftPingUtil.PROTOCOL_VERSION);
        MinecraftPingUtil.writeVarInt(handshake, options.getHostname().length());
        handshake.writeBytes(options.getHostname());
        handshake.writeShort(options.getPort());
        MinecraftPingUtil.writeVarInt(handshake, MinecraftPingUtil.STATUS_HANDSHAKE);

        MinecraftPingUtil.writeVarInt(out, handshake_bytes.size());
        out.write(handshake_bytes.toByteArray());

        //> Status request

        out.writeByte(0x01); // Size of packet
        out.writeByte(MinecraftPingUtil.PACKET_STATUSREQUEST);

        //< Status response

        MinecraftPingUtil.readVarInt(in); // Size
        int id = MinecraftPingUtil.readVarInt(in);

        MinecraftPingUtil.io(id == -1, "Server prematurely ended stream.");
        MinecraftPingUtil.io(id != MinecraftPingUtil.PACKET_STATUSREQUEST, "Server returned invalid packet.");

        int length = MinecraftPingUtil.readVarInt(in);
        MinecraftPingUtil.io(length == -1, "Server prematurely ended stream.");
        MinecraftPingUtil.io(length == 0, "Server returned unexpected value.");

        byte[] data = new byte[length];
        in.readFully(data);
        String json = new String(data, options.getCharset());

        //> Ping

        out.writeByte(0x09); // Size of packet
        out.writeByte(MinecraftPingUtil.PACKET_PING);
        out.writeLong(System.currentTimeMillis());

        //< Ping

        MinecraftPingUtil.readVarInt(in); // Size
        id = MinecraftPingUtil.readVarInt(in);
        MinecraftPingUtil.io(id == -1, "Server prematurely ended stream.");
        MinecraftPingUtil.io(id != MinecraftPingUtil.PACKET_PING, "Server returned invalid packet.");

        // Close

        handshake.close();
        handshake_bytes.close();
        out.close();
        in.close();
        socket.close();

        return new Gson().fromJson(json, MinecraftPingReply.class);
    }




public class MinecraftPingUtil {

    public static byte PACKET_HANDSHAKE = 0x00, PACKET_STATUSREQUEST = 0x00,
            PACKET_PING = 0x01;
    public static int PROTOCOL_VERSION = 4;
    public static int STATUS_HANDSHAKE = 1;

    public static void validate(final Object o, final String m) {
        if (o == null) {
            throw new RuntimeException(m);
        }
    }

    public static void io(final boolean b, final String m) throws IOException {
        if (b) {
            throw new IOException(m);
        }
    }

    /**
     * @author thinkofdeath
     * See: https://gist.github.com/thinkofdeath/e975ddee04e9c87faf22
     */
    public static int readVarInt(DataInputStream in) throws IOException {
        int i = 0;
        int j = 0;
        while (true) {
            int k = in.readByte();

            i |= (k & 0x7F) << j++ * 7;

            if (j > 5)
                throw new RuntimeException("VarInt too big");

            if ((k & 0x80) != 128)
                break;
        }

        return i;
    }

    /**
     * @author thinkofdeath
     * See: https://gist.github.com/thinkofdeath/e975ddee04e9c87faf22
     * @throws IOException 
     */
    public static void writeVarInt(DataOutputStream out, int paramInt) throws IOException {
        while (true) {
            if ((paramInt & 0xFFFFFF80) == 0) {
                out.writeByte(paramInt);
                return;
            }

            out.writeByte(paramInt & 0x7F | 0x80);
            paramInt >>>= 7;
        }
    }

}

Подробнее: https://github.com/oliverdunk/MinecraftServerPing/tree/master/src/ch/jamiete/mcping

(для плагина Minecraft) Эта версия работает с 1.8, но не в той версии, которая мне нужна: 1.15.2 Как я могу ее решить?

Ошибка:

Причина: java. lang.IllegalStateException: ожидалась строка, но она была BEGIN_OBJECT в строке 1 столбца 17 путь $ .description в com.google.gson.stream.JsonReader.nextString (JsonReader. java: 825) ~ [patched_1.15.2.jar: git -Paper-318] в com.google.gson.internal.bind.TypeAdapters $ 16.read (TypeAdapters. java: 418) ~ [patched_1.15.2.jar: git -Paper-318] в com.google .gson.internal.bind.TypeAdapters $ 16.read (TypeAdapters. java: 406) ~ [patched_1.15.2.jar: git -Paper-318] в com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1. прочтите (ReflectiveTypeAdapterFactory. java: 129) ~ [patched_1.15.2.jar: git -Paper-318] в com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read (Reflect iveTypeAdapterFactory. java: 220) ~ [patched_1.15.2.jar: git -Paper-318] в com.google.gson.Gson.from Json (Gson. java: 887) ~ [patched_1. 15.2.jar: git -Paper-318] на com.google.gson.Gson.from Json (Gson. java: 852) ~ [patched_1.15.2.jar: git -Paper-318] на com.google.gson.Gson.from Json (Gson. java: 801) ~ [patched_1.15.2.jar: git -Paper-318] на com.google.gson.Gson.from Json (Gson. java: 773) ~ [patched_1.15.2.jar: git -Paper-318] на it.justdevs.bw.lobby.utils.ping.MinecraftPing.getPing (MinecraftPing. java: 121 ) ~ [?:?] в it.justdevs.bw.lobby.utils.StatusItemStack.getItemStack (StatusItemStack. java: 51) ~ [?:?] в it.justdevs.bw.lobby.guis.Inventory.init (Инвентарь. java: 38) ~ [?:?] В it.justdevs. gui .utils.SmartInventory.open (SmartInventory. java: 54) ~ [?:?] В it.justdevs. gui .utils.SmartInventory.open (SmartInventory. java: 37) ~ [?:?] В it.justdevs.bw.lobby.commands.MainCommand.onCommand (MainCommand. java: 26) ~ [?: ?] на org.bukkit.command.PluginCommand.execute (PluginCommand. * 10 45 *: 45) ~ [patched_1.15.2.jar: git -Paper-318]

...