IdDispatchCodec.java

net.minecraft.network.codec.IdDispatchCodec

信息

  • 全限定名:net.minecraft.network.codec.IdDispatchCodec
  • 类型:public class
  • 包:net.minecraft.network.codec
  • 源码路径:src/main/java/net/minecraft/network/codec/IdDispatchCodec.java
  • 起始行号:L13
  • 实现:StreamCodec<B,V>
  • 职责:

    TODO

字段/常量

  • UNKNOWN_TYPE

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

      TODO

  • typeGetter

    • 类型: Function<V,?extends T>
    • 修饰符: private final
    • 源码定位: L15
    • 说明:

      TODO

  • byId

    • 类型: List<IdDispatchCodec.Entry<B,V,T>>
    • 修饰符: private final
    • 源码定位: L16
    • 说明:

      TODO

  • toId

    • 类型: Object2IntMap<T>
    • 修饰符: private final
    • 源码定位: L17
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.network.codec.IdDispatchCodec.Builder

    • 类型: class
    • 修饰符: public static
    • 源码定位: L70
    • 说明:

      TODO

  • net.minecraft.network.codec.IdDispatchCodec.DontDecorateException

    • 类型: interface
    • 修饰符: public
    • 源码定位: L99
    • 说明:

      TODO

  • net.minecraft.network.codec.IdDispatchCodec.Entry

    • 类型: record
    • 修饰符: private
    • 源码定位: L102
    • 说明:

      TODO

构造器

private IdDispatchCodec(Function<V,?extends T> typeGetter, List<IdDispatchCodec.Entry<B,V,T>> byId, Object2IntMap<T> toId) @ L19

  • 构造器名:IdDispatchCodec
  • 源码定位:L19
  • 修饰符:private

参数:

  • typeGetter: Function<V,?extends T>
  • byId: List<IdDispatchCodec.Entry<B,V,T>>
  • toId: Object2IntMap

说明:

TODO

方法

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

public V decode(B input) @ L25

  • 方法名:decode
  • 源码定位:L25
  • 返回类型:V
  • 修饰符:public

参数:

  • input: B

说明:

TODO

public void encode(B output, V value) @ L44

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

参数:

  • output: B
  • value: V

说明:

TODO

public static <B extends ByteBuf,V,T> IdDispatchCodec.Builder<B,V,T> builder(Function<V,?extends T> typeGetter) @ L66

  • 方法名:builder
  • 源码定位:L66
  • 返回类型:<B extends ByteBuf,V,T> IdDispatchCodec.Builder<B,V,T>
  • 修饰符:public static

参数:

  • typeGetter: Function<V,?extends T>

说明:

TODO

代码

public class IdDispatchCodec<B extends ByteBuf, V, T> implements StreamCodec<B, V> {
    private static final int UNKNOWN_TYPE = -1;
    private final Function<V, ? extends T> typeGetter;
    private final List<IdDispatchCodec.Entry<B, V, T>> byId;
    private final Object2IntMap<T> toId;
 
    private IdDispatchCodec(Function<V, ? extends T> typeGetter, List<IdDispatchCodec.Entry<B, V, T>> byId, Object2IntMap<T> toId) {
        this.typeGetter = typeGetter;
        this.byId = byId;
        this.toId = toId;
    }
 
    public V decode(B input) {
        int id = VarInt.read(input);
        if (id >= 0 && id < this.byId.size()) {
            IdDispatchCodec.Entry<B, V, T> entry = this.byId.get(id);
 
            try {
                return (V)entry.serializer.decode(input);
            } catch (Exception var5) {
                if (var5 instanceof IdDispatchCodec.DontDecorateException) {
                    throw var5;
                } else {
                    throw new DecoderException("Failed to decode packet '" + entry.type + "'", var5);
                }
            }
        } else {
            throw new DecoderException("Received unknown packet id " + id);
        }
    }
 
    public void encode(B output, V value) {
        T type = (T)this.typeGetter.apply(value);
        int id = this.toId.getOrDefault(type, -1);
        if (id == -1) {
            throw new EncoderException("Sending unknown packet '" + type + "'");
        } else {
            VarInt.write(output, id);
            IdDispatchCodec.Entry<B, V, T> entry = this.byId.get(id);
 
            try {
                StreamCodec<? super B, V> codec = (StreamCodec<? super B, V>)entry.serializer;
                codec.encode(output, value);
            } catch (Exception var7) {
                if (var7 instanceof IdDispatchCodec.DontDecorateException) {
                    throw var7;
                } else {
                    throw new EncoderException("Failed to encode packet '" + type + "'", var7);
                }
            }
        }
    }
 
    public static <B extends ByteBuf, V, T> IdDispatchCodec.Builder<B, V, T> builder(Function<V, ? extends T> typeGetter) {
        return new IdDispatchCodec.Builder<>(typeGetter);
    }
 
    public static class Builder<B extends ByteBuf, V, T> {
        private final List<IdDispatchCodec.Entry<B, V, T>> entries = new ArrayList<>();
        private final Function<V, ? extends T> typeGetter;
 
        private Builder(Function<V, ? extends T> typeGetter) {
            this.typeGetter = typeGetter;
        }
 
        public IdDispatchCodec.Builder<B, V, T> add(T type, StreamCodec<? super B, ? extends V> serializer) {
            this.entries.add(new IdDispatchCodec.Entry<>(serializer, type));
            return this;
        }
 
        public IdDispatchCodec<B, V, T> build() {
            Object2IntOpenHashMap<T> toId = new Object2IntOpenHashMap<>();
            toId.defaultReturnValue(-2);
 
            for (IdDispatchCodec.Entry<B, V, T> entry : this.entries) {
                int id = toId.size();
                int previous = toId.putIfAbsent(entry.type, id);
                if (previous != -2) {
                    throw new IllegalStateException("Duplicate registration for type " + entry.type);
                }
            }
 
            return new IdDispatchCodec<>(this.typeGetter, List.copyOf(this.entries), toId);
        }
    }
 
    public interface DontDecorateException {
    }
 
    private record Entry<B, V, T>(StreamCodec<? super B, ? extends V> serializer, T type) {
    }
}

引用的其他类

  • VarInt

    • 引用位置: 方法调用
    • 关联成员: VarInt.read(), VarInt.write()
  • StreamCodec

    • 引用位置: 实现