RegistryLoadTask.java

net.minecraft.resources.RegistryLoadTask

信息

  • 全限定名:net.minecraft.resources.RegistryLoadTask
  • 类型:public abstract class
  • 包:net.minecraft.resources
  • 源码路径:src/main/java/net/minecraft/resources/RegistryLoadTask.java
  • 起始行号:L29
  • 职责:

    TODO

字段/常量

  • registryWriteLock

    • 类型: Object
    • 修饰符: private final
    • 源码定位: L30
    • 说明:

      TODO

  • data

    • 类型: RegistryDataLoader.RegistryData<T>
    • 修饰符: protected final
    • 源码定位: L31
    • 说明:

      TODO

  • registry

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

      TODO

  • concurrentRegistrationGetter

    • 类型: ConcurrentHolderGetter<T>
    • 修饰符: protected final
    • 源码定位: L33
    • 说明:

      TODO

  • loadingErrors

    • 类型: Map<ResourceKey<?>,Exception>
    • 修饰符: protected final
    • 源码定位: L34
    • 说明:

      TODO

  • elementsRegistered

    • 类型: boolean
    • 修饰符: private volatile
    • 源码定位: L35
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.resources.RegistryLoadTask.PendingRegistration
    • 类型: record
    • 修饰符: protected
    • 源码定位: L100
    • 说明:

      TODO

构造器

protected RegistryLoadTask(RegistryDataLoader.RegistryData<T> data, Lifecycle lifecycle, Map<ResourceKey<?>,Exception> loadingErrors) @ L37

  • 构造器名:RegistryLoadTask
  • 源码定位:L37
  • 修饰符:protected

参数:

  • data: RegistryDataLoader.RegistryData
  • lifecycle: Lifecycle
  • loadingErrors: Map<ResourceKey<?>,Exception>

说明:

TODO

方法

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

protected ResourceKey<?extends Registry<T>> registryKey() @ L44

  • 方法名:registryKey
  • 源码定位:L44
  • 返回类型:ResourceKey<?extends Registry>
  • 修饰符:protected

参数:

说明:

TODO

protected Registry<T> readOnlyRegistry() @ L48

  • 方法名:readOnlyRegistry
  • 源码定位:L48
  • 返回类型:Registry
  • 修饰符:protected

参数:

说明:

TODO

public abstract CompletableFuture<?> load(RegistryOps.RegistryInfoLookup context, Executor executor) @ L56

  • 方法名:load
  • 源码定位:L56
  • 返回类型:CompletableFuture<?>
  • 修饰符:public abstract

参数:

  • context: RegistryOps.RegistryInfoLookup
  • executor: Executor

说明:

TODO

public RegistryOps.RegistryInfo<?> createRegistryInfo() @ L58

  • 方法名:createRegistryInfo
  • 源码定位:L58
  • 返回类型:RegistryOps.RegistryInfo<?>
  • 修饰符:public

参数:

说明:

TODO

protected void registerElements(Stream<RegistryLoadTask.PendingRegistration<T>> elements) @ L62

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

参数:

  • elements: Stream<RegistryLoadTask.PendingRegistration>

说明:

TODO

protected void registerTags(Map<TagKey<T>,List<Holder<T>>> pendingTags) @ L73

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

参数:

  • pendingTags: Map<TagKey,List<Holder>>

说明:

TODO

public boolean freezeRegistry(Map<ResourceKey<?>,Exception> loadingErrors) @ L79

  • 方法名:freezeRegistry
  • 源码定位:L79
  • 返回类型:boolean
  • 修饰符:public

参数:

  • loadingErrors: Map<ResourceKey<?>,Exception>

说明:

TODO

public Optional<Registry<T>> validateRegistry(Map<ResourceKey<?>,Exception> loadingErrors) @ L89

  • 方法名:validateRegistry
  • 源码定位:L89
  • 返回类型:Optional<Registry>
  • 修饰符:public

参数:

  • loadingErrors: Map<ResourceKey<?>,Exception>

说明:

TODO

代码

public abstract class RegistryLoadTask<T> {
    private final Object registryWriteLock = new Object();
    protected final RegistryDataLoader.RegistryData<T> data;
    private final WritableRegistry<T> registry;
    protected final ConcurrentHolderGetter<T> concurrentRegistrationGetter;
    protected final Map<ResourceKey<?>, Exception> loadingErrors;
    private volatile boolean elementsRegistered;
 
    protected RegistryLoadTask(RegistryDataLoader.RegistryData<T> data, Lifecycle lifecycle, Map<ResourceKey<?>, Exception> loadingErrors) {
        this.data = data;
        this.registry = new MappedRegistry<>(data.key(), lifecycle);
        this.loadingErrors = loadingErrors;
        this.concurrentRegistrationGetter = new ConcurrentHolderGetter<>(this.registryWriteLock, this.registry.createRegistrationLookup());
    }
 
    protected ResourceKey<? extends Registry<T>> registryKey() {
        return this.registry.key();
    }
 
    protected Registry<T> readOnlyRegistry() {
        if (!this.elementsRegistered) {
            throw new IllegalStateException("Elements not registered");
        } else {
            return this.registry;
        }
    }
 
    public abstract CompletableFuture<?> load(RegistryOps.RegistryInfoLookup context, Executor executor);
 
    public RegistryOps.RegistryInfo<?> createRegistryInfo() {
        return new RegistryOps.RegistryInfo<>(this.registry, this.concurrentRegistrationGetter, this.registry.registryLifecycle());
    }
 
    protected void registerElements(Stream<RegistryLoadTask.PendingRegistration<T>> elements) {
        synchronized (this.registryWriteLock) {
            elements.forEach(
                element -> element.value
                    .ifLeft(value -> this.registry.register(element.key, (T)value, element.registrationInfo))
                    .ifRight(error -> this.loadingErrors.put(element.key, error))
            );
            this.elementsRegistered = true;
        }
    }
 
    protected void registerTags(Map<TagKey<T>, List<Holder<T>>> pendingTags) {
        synchronized (this.registryWriteLock) {
            this.registry.bindTags(pendingTags);
        }
    }
 
    public boolean freezeRegistry(Map<ResourceKey<?>, Exception> loadingErrors) {
        try {
            this.registry.freeze();
            return true;
        } catch (Exception var3) {
            loadingErrors.put(this.registry.key(), var3);
            return false;
        }
    }
 
    public Optional<Registry<T>> validateRegistry(Map<ResourceKey<?>, Exception> loadingErrors) {
        Map<ResourceKey<?>, Exception> registryErrors = new HashMap<>();
        this.data.validator().validate(this.registry, registryErrors);
        if (registryErrors.isEmpty()) {
            return Optional.of(this.registry);
        } else {
            loadingErrors.putAll(registryErrors);
            return Optional.empty();
        }
    }
 
    protected record PendingRegistration<T>(ResourceKey<T> key, Either<T, Exception> value, RegistrationInfo registrationInfo) {
        public static <T> Either<T, Exception> loadFromResource(
            Decoder<T> elementDecoder, RegistryOps<JsonElement> ops, ResourceKey<T> elementKey, Resource thunk
        ) {
            try {
                Either var6;
                try (Reader reader = thunk.openAsReader()) {
                    JsonElement json = StrictJsonParser.parse(reader);
                    var6 = Either.left(elementDecoder.parse(ops, json).getOrThrow());
                }
 
                return var6;
            } catch (Exception var9) {
                return Either.right(
                    new IllegalStateException(
                        String.format(Locale.ROOT, "Failed to parse %s from pack %s", elementKey.identifier(), thunk.sourcePackId()), var9
                    )
                );
            }
        }
 
        public static <T> Either<T, Exception> findAndLoadFromResource(
            Decoder<T> elementDecoder, RegistryOps<JsonElement> ops, ResourceKey<T> elementKey, FileToIdConverter converter, ResourceProvider resourceProvider
        ) {
            Identifier resourceId = converter.idToFile(elementKey.identifier());
            return resourceProvider.getResource(resourceId)
                .map(resource -> loadFromResource(elementDecoder, ops, elementKey, resource))
                .orElseGet(
                    () -> Either.right(
                        new IllegalStateException(String.format(Locale.ROOT, "Failed to find resource %s for element %s", resourceId, elementKey.identifier()))
                    )
                );
        }
 
        public static <T> Either<T, Exception> loadFromNetwork(Decoder<T> elementDecoder, RegistryOps<Tag> ops, ResourceKey<T> elementKey, Tag contents) {
            try {
                DataResult<T> parseResult = elementDecoder.parse(ops, contents);
                return Either.left(parseResult.getOrThrow());
            } catch (Exception var5) {
                return Either.right(
                    new IllegalStateException(
                        String.format(Locale.ROOT, "Failed to parse value %s for key %s from server", contents, elementKey.identifier()), var5
                    )
                );
            }
        }
    }
}

引用的其他类