Search in sources :

Example 1 with QueryPongS2CPacket

use of net.minecraft.network.packet.s2c.query.QueryPongS2CPacket in project Client by MatHax.

the class ServerListPinger method add.

public void add(final MServerInfo entry, final Runnable runnable) throws UnknownHostException {
    Timer timeoutTimer = new Timer();
    ServerAddress serverAddress = ServerAddress.parse(entry.address);
    timeoutTimer.schedule(new TimerTask() {

        @Override
        public void run() {
            notifyDisconnectListeners();
        }
    }, 20000);
    final ClientConnection clientConnection = ClientConnection.connect(new InetSocketAddress(InetAddress.getByName(serverAddress.getAddress()), serverAddress.getPort()), false);
    failedToConnect = false;
    this.clientConnections.add(clientConnection);
    entry.label = "multiplayer.status.pinging";
    entry.ping = -1L;
    entry.playerListSummary = null;
    clientConnection.setPacketListener(new ClientQueryPacketListener() {

        private boolean sentQuery;

        private boolean received;

        private long startTime;

        public void onResponse(QueryResponseS2CPacket packet) {
            if (this.received)
                clientConnection.disconnect(new TranslatableText("multiplayer.status.unrequested"));
            else {
                this.received = true;
                ServerMetadata serverMetadata = packet.getServerMetadata();
                if (serverMetadata.getDescription() != null)
                    entry.label = serverMetadata.getDescription().asString();
                else
                    entry.label = "";
                if (serverMetadata.getVersion() != null) {
                    entry.version = serverMetadata.getVersion().getGameVersion();
                    entry.protocolVersion = serverMetadata.getVersion().getProtocolVersion();
                } else {
                    entry.version = "multiplayer.status.old";
                    entry.protocolVersion = 0;
                }
                if (serverMetadata.getPlayers() != null) {
                    entry.playerCountLabel = ServerListPinger.getPlayerCountLabel(serverMetadata.getPlayers().getOnlinePlayerCount(), serverMetadata.getPlayers().getPlayerLimit());
                    entry.playerCount = serverMetadata.getPlayers().getOnlinePlayerCount();
                    entry.playercountMax = serverMetadata.getPlayers().getPlayerLimit();
                    List<Text> list = Lists.newArrayList();
                    if (ArrayUtils.isNotEmpty(serverMetadata.getPlayers().getSample())) {
                        GameProfile[] gameProfiles = serverMetadata.getPlayers().getSample();
                        for (GameProfile gameProfile : gameProfiles) {
                            list.add(new LiteralText(gameProfile.getName()));
                        }
                        if (serverMetadata.getPlayers().getSample().length < serverMetadata.getPlayers().getOnlinePlayerCount())
                            list.add(new TranslatableText("multiplayer.status.and_more", serverMetadata.getPlayers().getOnlinePlayerCount() - serverMetadata.getPlayers().getSample().length));
                        entry.playerListSummary = list;
                    }
                } else
                    entry.playerCountLabel = "multiplayer.status.unknown";
                String string = null;
                if (serverMetadata.getFavicon() != null) {
                    String string2 = serverMetadata.getFavicon();
                    if (string2.startsWith("data:image/png;base64,"))
                        string = string2.substring("data:image/png;base64,".length());
                    else
                        MatHax.LOG.error("Invalid server icon (unknown format)");
                }
                if (!Objects.equals(string, entry.getIcon())) {
                    entry.setIcon(string);
                    runnable.run();
                }
                this.startTime = Util.getMeasuringTimeMs();
                clientConnection.send(new QueryPingC2SPacket(this.startTime));
                this.sentQuery = true;
                notifyDisconnectListeners();
            }
        }

        public void onPong(QueryPongS2CPacket packet) {
            long l = this.startTime;
            long m = Util.getMeasuringTimeMs();
            entry.ping = m - l;
            clientConnection.disconnect(new TranslatableText("multiplayer.status.finished"));
        }

        public void onDisconnected(Text reason) {
            if (!this.sentQuery) {
                MatHax.LOG.error("Can't ping {}: {}", entry.address, reason.getString());
                entry.label = "multiplayer.status.cannot_connect";
                entry.playerCountLabel = "";
                entry.playerCount = 0;
                entry.playercountMax = 0;
                ServerListPinger.this.ping(entry);
            }
            notifyDisconnectListeners();
        }

        public ClientConnection getConnection() {
            return clientConnection;
        }
    });
    try {
        clientConnection.send(new HandshakeC2SPacket(serverAddress.getAddress(), serverAddress.getPort(), NetworkState.STATUS));
        clientConnection.send(new QueryRequestC2SPacket());
    } catch (Throwable throwable) {
        MatHax.LOG.error("[MatHax] Couldn't send handshake", throwable);
    }
}
Also used : TranslatableText(net.minecraft.text.TranslatableText) QueryPingC2SPacket(net.minecraft.network.packet.c2s.query.QueryPingC2SPacket) HandshakeC2SPacket(net.minecraft.network.packet.c2s.handshake.HandshakeC2SPacket) InetSocketAddress(java.net.InetSocketAddress) QueryResponseS2CPacket(net.minecraft.network.packet.s2c.query.QueryResponseS2CPacket) ServerAddress(net.minecraft.client.network.ServerAddress) ServerMetadata(net.minecraft.server.ServerMetadata) LiteralText(net.minecraft.text.LiteralText) TranslatableText(net.minecraft.text.TranslatableText) Text(net.minecraft.text.Text) QueryPongS2CPacket(net.minecraft.network.packet.s2c.query.QueryPongS2CPacket) ClientQueryPacketListener(net.minecraft.network.listener.ClientQueryPacketListener) GameProfile(com.mojang.authlib.GameProfile) QueryRequestC2SPacket(net.minecraft.network.packet.c2s.query.QueryRequestC2SPacket) ClientConnection(net.minecraft.network.ClientConnection) LiteralText(net.minecraft.text.LiteralText)

Aggregations

GameProfile (com.mojang.authlib.GameProfile)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerAddress (net.minecraft.client.network.ServerAddress)1 ClientConnection (net.minecraft.network.ClientConnection)1 ClientQueryPacketListener (net.minecraft.network.listener.ClientQueryPacketListener)1 HandshakeC2SPacket (net.minecraft.network.packet.c2s.handshake.HandshakeC2SPacket)1 QueryPingC2SPacket (net.minecraft.network.packet.c2s.query.QueryPingC2SPacket)1 QueryRequestC2SPacket (net.minecraft.network.packet.c2s.query.QueryRequestC2SPacket)1 QueryPongS2CPacket (net.minecraft.network.packet.s2c.query.QueryPongS2CPacket)1 QueryResponseS2CPacket (net.minecraft.network.packet.s2c.query.QueryResponseS2CPacket)1 ServerMetadata (net.minecraft.server.ServerMetadata)1 LiteralText (net.minecraft.text.LiteralText)1 Text (net.minecraft.text.Text)1 TranslatableText (net.minecraft.text.TranslatableText)1