Search in sources :

Example 1 with ServerAddress

use of net.minecraft.client.network.ServerAddress in project mineclub-expanded by Blobanium.

the class TitleScreenMixin method init.

@Inject(at = @At("HEAD"), method = "init")
private void init(CallbackInfo ci) {
    if (!hasStarted && ConfigReader.expressConnect) {
        System.out.println("Connecting");
        ConnectScreen.connect(MinecraftClient.getInstance().currentScreen, MinecraftClient.getInstance(), new ServerAddress("play.mineclub.com", 25565), MineclubExpanded.mineclub);
    }
    hasStarted = true;
}
Also used : ServerAddress(net.minecraft.client.network.ServerAddress) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with ServerAddress

use of net.minecraft.client.network.ServerAddress in project Client by MatHax.

the class ServerListPinger method ping.

private void ping(final MServerInfo serverInfo) {
    final ServerAddress serverAddress = ServerAddress.parse(serverInfo.address);
    (new Bootstrap()).group(ClientConnection.CLIENT_IO_GROUP.get()).handler(new ChannelInitializer<>() {

        protected void initChannel(Channel channel) {
            try {
                channel.config().setOption(ChannelOption.TCP_NODELAY, true);
            } catch (ChannelException ignore) {
            }
            channel.pipeline().addLast(new SimpleChannelInboundHandler<ByteBuf>() {

                public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {
                    super.channelActive(channelHandlerContext);
                    ByteBuf byteBuf = Unpooled.buffer();
                    try {
                        byteBuf.writeByte(254);
                        byteBuf.writeByte(1);
                        byteBuf.writeByte(250);
                        char[] cs = "MC|PingHost".toCharArray();
                        byteBuf.writeShort(cs.length);
                        char[] chars = cs;
                        int csLength = cs.length;
                        int n;
                        char d;
                        for (n = 0; n < csLength; ++n) {
                            d = chars[n];
                            byteBuf.writeChar(d);
                        }
                        byteBuf.writeShort(7 + 2 * serverAddress.getAddress().length());
                        byteBuf.writeByte(127);
                        cs = serverAddress.getAddress().toCharArray();
                        byteBuf.writeShort(cs.length);
                        chars = cs;
                        csLength = cs.length;
                        for (n = 0; n < csLength; ++n) {
                            d = chars[n];
                            byteBuf.writeChar(d);
                        }
                        byteBuf.writeInt(serverAddress.getPort());
                        channelHandlerContext.channel().writeAndFlush(byteBuf).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
                    } finally {
                        byteBuf.release();
                    }
                }

                protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) {
                    short s = byteBuf.readUnsignedByte();
                    if (s == 255) {
                        String string = new String(byteBuf.readBytes(byteBuf.readShort() * 2).array(), StandardCharsets.UTF_16BE);
                        String[] strings = Iterables.toArray(ServerListPinger.ZERO_SPLITTER.split(string), String.class);
                        if ("ยง1".equals(strings[0])) {
                            String string2 = strings[2];
                            String string3 = strings[3];
                            int j = MathHelper.parseInt(strings[4], -1);
                            int k = MathHelper.parseInt(strings[5], -1);
                            serverInfo.protocolVersion = -1;
                            serverInfo.version = string2;
                            serverInfo.label = string3;
                            serverInfo.playerCountLabel = ServerListPinger.getPlayerCountLabel(j, k);
                        }
                    }
                    channelHandlerContext.close();
                }

                public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable throwable) {
                    channelHandlerContext.close();
                }
            });
        }
    }).channel(NioSocketChannel.class).connect(serverAddress.getAddress(), serverAddress.getPort());
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ServerAddress(net.minecraft.client.network.ServerAddress) ByteBuf(io.netty.buffer.ByteBuf) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 3 with ServerAddress

use of net.minecraft.client.network.ServerAddress 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

ServerAddress (net.minecraft.client.network.ServerAddress)3 GameProfile (com.mojang.authlib.GameProfile)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ByteBuf (io.netty.buffer.ByteBuf)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 InetSocketAddress (java.net.InetSocketAddress)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 Inject (org.spongepowered.asm.mixin.injection.Inject)1