| 1 | package net.minecraft.client.multiplayer;
|
| 2 |
|
| 3 | import com.google.common.base.Splitter;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import io.netty.channel.ChannelFutureListener;
|
| 6 | import io.netty.channel.ChannelHandlerContext;
|
| 7 | import io.netty.channel.SimpleChannelInboundHandler;
|
| 8 | import java.util.List;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.client.multiplayer.resolver.ServerAddress;
|
| 12 | import net.minecraft.server.network.LegacyProtocolUtils;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class LegacyServerPinger extends SimpleChannelInboundHandler<ByteBuf> {
|
| 17 | private static final Splitter SPLITTER = Splitter.on('\u0000').limit(6);
|
| 18 | private final ServerAddress address;
|
| 19 | private final LegacyServerPinger.Output output;
|
| 20 |
|
| 21 | public LegacyServerPinger(final ServerAddress address, final LegacyServerPinger.Output output) {
|
| 22 | this.address = address;
|
| 23 | this.output = output;
|
| 24 | }
|
| 25 |
|
| 26 | @Override
|
| 27 | public void channelActive(final ChannelHandlerContext ctx) throws Exception {
|
| 28 | super.channelActive(ctx);
|
| 29 | ByteBuf toSend = ctx.alloc().buffer();
|
| 30 |
|
| 31 | try {
|
| 32 | toSend.writeByte(254);
|
| 33 | toSend.writeByte(1);
|
| 34 | toSend.writeByte(250);
|
| 35 | LegacyProtocolUtils.writeLegacyString(toSend, "MC|PingHost");
|
| 36 | int sizeIndex = toSend.writerIndex();
|
| 37 | toSend.writeShort(0);
|
| 38 | int payloadStart = toSend.writerIndex();
|
| 39 | toSend.writeByte(127);
|
| 40 | LegacyProtocolUtils.writeLegacyString(toSend, this.address.getHost());
|
| 41 | toSend.writeInt(this.address.getPort());
|
| 42 | int payloadSize = toSend.writerIndex() - payloadStart;
|
| 43 | toSend.setShort(sizeIndex, payloadSize);
|
| 44 | ctx.channel().writeAndFlush(toSend).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
|
| 45 | } catch (Exception e) {
|
| 46 | toSend.release();
|
| 47 | throw e;
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | protected void channelRead0(final ChannelHandlerContext ctx, final ByteBuf msg) {
|
| 52 | short firstByte = msg.readUnsignedByte();
|
| 53 | if (firstByte == 255) {
|
| 54 | String str = LegacyProtocolUtils.readLegacyString(msg);
|
| 55 | List<String> split = SPLITTER.splitToList(str);
|
| 56 | if ("§1".equals(split.get(0))) {
|
| 57 | int protocolVersion = Mth.getInt(split.get(1), 0);
|
| 58 | String version = split.get(2);
|
| 59 | String motd = split.get(3);
|
| 60 | int curPlayers = Mth.getInt(split.get(4), -1);
|
| 61 | int maxPlayers = Mth.getInt(split.get(5), -1);
|
| 62 | this.output.handleResponse(protocolVersion, version, motd, curPlayers, maxPlayers);
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | ctx.close();
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
|
| 71 | ctx.close();
|
| 72 | }
|
| 73 |
|
| 74 | @FunctionalInterface
|
| 75 | @Environment(EnvType.CLIENT)
|
| 76 | public interface Output {
|
| 77 | void handleResponse(int protocolVersion, String gameVersion, String motd, int players, int maxPlayers);
|
| 78 | }
|
| 79 | }
|
| 80 |
|