RegistryFixedCodec.java

net.minecraft.resources.RegistryFixedCodec

信息

  • 全限定名:net.minecraft.resources.RegistryFixedCodec
  • 类型:public final class
  • 包:net.minecraft.resources
  • 源码路径:src/main/java/net/minecraft/resources/RegistryFixedCodec.java
  • 起始行号:L14
  • 实现:Codec<Holder>
  • 职责:

    TODO

字段/常量

  • registryKey
    • 类型: ResourceKey<?extends Registry<E>>
    • 修饰符: private final
    • 源码定位: L15
    • 说明:

      TODO

内部类/嵌套类型

构造器

private RegistryFixedCodec(ResourceKey<?extends Registry<E>> registryKey) @ L21

  • 构造器名:RegistryFixedCodec
  • 源码定位:L21
  • 修饰符:private

参数:

  • registryKey: ResourceKey<?extends Registry>

说明:

TODO

方法

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

public static <E> RegistryFixedCodec<E> create(ResourceKey<?extends Registry<E>> registryKey) @ L17

  • 方法名:create
  • 源码定位:L17
  • 返回类型: RegistryFixedCodec
  • 修饰符:public static

参数:

  • registryKey: ResourceKey<?extends Registry>

说明:

TODO

public <T> DataResult<T> encode(Holder<E> input, DynamicOps<T> ops, T prefix) @ L25

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

参数:

  • input: Holder
  • ops: DynamicOps
  • prefix: T

说明:

TODO

public <T> DataResult<Pair<Holder<E>,T>> decode(DynamicOps<T> ops, T input) @ L44

  • 方法名:decode
  • 源码定位:L44
  • 返回类型: DataResult<Pair<Holder,T>>
  • 修饰符:public

参数:

  • ops: DynamicOps
  • input: T

说明:

TODO

public String toString() @ L68

  • 方法名:toString
  • 源码定位:L68
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

代码

public final class RegistryFixedCodec<E> implements Codec<Holder<E>> {
    private final ResourceKey<? extends Registry<E>> registryKey;
 
    public static <E> RegistryFixedCodec<E> create(ResourceKey<? extends Registry<E>> registryKey) {
        return new RegistryFixedCodec<>(registryKey);
    }
 
    private RegistryFixedCodec(ResourceKey<? extends Registry<E>> registryKey) {
        this.registryKey = registryKey;
    }
 
    public <T> DataResult<T> encode(Holder<E> input, DynamicOps<T> ops, T prefix) {
        if (ops instanceof RegistryOps<?> registryOps) {
            Optional<HolderOwner<E>> maybeOwner = registryOps.owner(this.registryKey);
            if (maybeOwner.isPresent()) {
                if (!input.canSerializeIn(maybeOwner.get())) {
                    return DataResult.error(() -> "Element " + input + " is not valid in current registry set");
                }
 
                return input.unwrap()
                    .map(
                        id -> Identifier.CODEC.encode(id.identifier(), ops, prefix),
                        value -> DataResult.error(() -> "Elements from registry " + this.registryKey + " can't be serialized to a value")
                    );
            }
        }
 
        return DataResult.error(() -> "Can't access registry " + this.registryKey);
    }
 
    @Override
    public <T> DataResult<Pair<Holder<E>, T>> decode(DynamicOps<T> ops, T input) {
        if (ops instanceof RegistryOps<?> registryOps) {
            Optional<HolderGetter<E>> lookup = registryOps.getter(this.registryKey);
            if (lookup.isPresent()) {
                return Identifier.CODEC
                    .decode(ops, input)
                    .flatMap(
                        pair -> {
                            Identifier id = pair.getFirst();
                            return lookup.get()
                                .get(ResourceKey.create(this.registryKey, id))
                                .map(DataResult::success)
                                .orElseGet(() -> DataResult.error(() -> "Failed to get element " + id))
                                .<Pair<Holder<E>, T>>map(h -> Pair.of(h, (T)pair.getSecond()))
                                .setLifecycle(Lifecycle.stable());
                        }
                    );
            }
        }
 
        return DataResult.error(() -> "Can't access registry " + this.registryKey);
    }
 
    @Override
    public String toString() {
        return "RegistryFixedCodec[" + this.registryKey + "]";
    }
}

引用的其他类

  • Holder

    • 引用位置: 参数/实现/返回值
  • Registry

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

    • 引用位置: 参数/字段/方法调用
    • 关联成员: ResourceKey.create()