LegacyServerPinger.java

net.minecraft.client.multiplayer.LegacyServerPinger

信息

  • 全限定名:net.minecraft.client.multiplayer.LegacyServerPinger
  • 类型:public class
  • 包:net.minecraft.client.multiplayer
  • 源码路径:src/main/java/net/minecraft/client/multiplayer/LegacyServerPinger.java
  • 起始行号:L16
  • 继承:SimpleChannelInboundHandler
  • 职责:

    TODO

字段/常量

  • SPLITTER

    • 类型: Splitter
    • 修饰符: private static final
    • 源码定位: L17
    • 说明:

      TODO

  • address

    • 类型: ServerAddress
    • 修饰符: private final
    • 源码定位: L18
    • 说明:

      TODO

  • output

    • 类型: LegacyServerPinger.Output
    • 修饰符: private final
    • 源码定位: L19
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.client.multiplayer.LegacyServerPinger.Output
    • 类型: interface
    • 修饰符: public
    • 源码定位: L76
    • 说明:

      TODO

构造器

public LegacyServerPinger(ServerAddress address, LegacyServerPinger.Output output) @ L21

  • 构造器名:LegacyServerPinger
  • 源码定位:L21
  • 修饰符:public

参数:

  • address: ServerAddress
  • output: LegacyServerPinger.Output

说明:

TODO

方法

下面的方法块按源码顺序生成。

public void channelActive(ChannelHandlerContext ctx) @ L26

  • 方法名:channelActive
  • 源码定位:L26
  • 返回类型:void
  • 修饰符:public

参数:

  • ctx: ChannelHandlerContext

说明:

TODO

protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) @ L51

  • 方法名:channelRead0
  • 源码定位:L51
  • 返回类型:void
  • 修饰符:protected

参数:

  • ctx: ChannelHandlerContext
  • msg: ByteBuf

说明:

TODO

public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) @ L69

  • 方法名:exceptionCaught
  • 源码定位:L69
  • 返回类型:void
  • 修饰符:public

参数:

  • ctx: ChannelHandlerContext
  • cause: Throwable

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class LegacyServerPinger extends SimpleChannelInboundHandler<ByteBuf> {
    private static final Splitter SPLITTER = Splitter.on('\u0000').limit(6);
    private final ServerAddress address;
    private final LegacyServerPinger.Output output;
 
    public LegacyServerPinger(ServerAddress address, LegacyServerPinger.Output output) {
        this.address = address;
        this.output = output;
    }
 
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        ByteBuf toSend = ctx.alloc().buffer();
 
        try {
            toSend.writeByte(254);
            toSend.writeByte(1);
            toSend.writeByte(250);
            LegacyProtocolUtils.writeLegacyString(toSend, "MC|PingHost");
            int sizeIndex = toSend.writerIndex();
            toSend.writeShort(0);
            int payloadStart = toSend.writerIndex();
            toSend.writeByte(127);
            LegacyProtocolUtils.writeLegacyString(toSend, this.address.getHost());
            toSend.writeInt(this.address.getPort());
            int payloadSize = toSend.writerIndex() - payloadStart;
            toSend.setShort(sizeIndex, payloadSize);
            ctx.channel().writeAndFlush(toSend).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
        } catch (Exception var6) {
            toSend.release();
            throw var6;
        }
    }
 
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
        short firstByte = msg.readUnsignedByte();
        if (firstByte == 255) {
            String str = LegacyProtocolUtils.readLegacyString(msg);
            List<String> split = SPLITTER.splitToList(str);
            if ("\u00a71".equals(split.get(0))) {
                int protocolVersion = Mth.getInt(split.get(1), 0);
                String version = split.get(2);
                String motd = split.get(3);
                int curPlayers = Mth.getInt(split.get(4), -1);
                int maxPlayers = Mth.getInt(split.get(5), -1);
                this.output.handleResponse(protocolVersion, version, motd, curPlayers, maxPlayers);
            }
        }
 
        ctx.close();
    }
 
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        ctx.close();
    }
 
    @FunctionalInterface
    @OnlyIn(Dist.CLIENT)
    public interface Output {
        void handleResponse(int protocolVersion, String gameVersion, String motd, int players, int maxPlayers);
    }
}

引用的其他类

  • ServerAddress

    • 引用位置: 参数/字段
  • LegacyProtocolUtils

    • 引用位置: 方法调用
    • 关联成员: LegacyProtocolUtils.readLegacyString(), LegacyProtocolUtils.writeLegacyString()
  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.getInt()