| 1 | package net.minecraft.resources;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.mojang.serialization.JsonOps;
|
| 5 | import com.mojang.serialization.Lifecycle;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.Map.Entry;
|
| 10 | import java.util.concurrent.CompletableFuture;
|
| 11 | import java.util.concurrent.Executor;
|
| 12 | import java.util.function.Function;
|
| 13 | import net.minecraft.core.Holder;
|
| 14 | import net.minecraft.core.RegistrationInfo;
|
| 15 | import net.minecraft.server.packs.repository.KnownPack;
|
| 16 | import net.minecraft.server.packs.resources.Resource;
|
| 17 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 18 | import net.minecraft.tags.TagKey;
|
| 19 | import net.minecraft.tags.TagLoader;
|
| 20 | import net.minecraft.util.Util;
|
| 21 | import net.minecraft.util.thread.ParallelMapTransform;
|
| 22 |
|
| 23 | public class ResourceManagerRegistryLoadTask<T> extends RegistryLoadTask<T> {
|
| 24 | private static final Function<Optional<KnownPack>, RegistrationInfo> REGISTRATION_INFO_CACHE = Util.memoize(knownPack -> {
|
| 25 | Lifecycle lifecycle = knownPack.map(KnownPack::isVanilla).map(info -> Lifecycle.stable()).orElse(Lifecycle.experimental());
|
| 26 | return new RegistrationInfo(knownPack, lifecycle);
|
| 27 | });
|
| 28 | private final ResourceManager resourceManager;
|
| 29 |
|
| 30 | public ResourceManagerRegistryLoadTask(
|
| 31 | final RegistryDataLoader.RegistryData<T> data,
|
| 32 | final Lifecycle lifecycle,
|
| 33 | final Map<ResourceKey<?>, Exception> loadingErrors,
|
| 34 | final ResourceManager resourceManager
|
| 35 | ) {
|
| 36 | super(data, lifecycle, loadingErrors);
|
| 37 | this.resourceManager = resourceManager;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public CompletableFuture<?> load(final RegistryOps.RegistryInfoLookup context, final Executor executor) {
|
| 42 | FileToIdConverter lister = FileToIdConverter.registry(this.registryKey());
|
| 43 | return CompletableFuture.<Map<Identifier, Resource>>supplyAsync(() -> lister.listMatchingResources(this.resourceManager), executor)
|
| 44 | .thenCompose(
|
| 45 | registryResources -> {
|
| 46 | RegistryOps<JsonElement> ops = RegistryOps.create(JsonOps.INSTANCE, context);
|
| 47 | return ParallelMapTransform.schedule(
|
| 48 | (Map<Identifier, Resource>)registryResources,
|
| 49 | (resourceId, thunk) -> {
|
| 50 | ResourceKey<T> elementKey = ResourceKey.create(this.registryKey(), lister.fileToId(resourceId));
|
| 51 | RegistrationInfo registrationInfo = REGISTRATION_INFO_CACHE.apply(thunk.knownPackInfo());
|
| 52 | return new RegistryLoadTask.PendingRegistration<>(
|
| 53 | elementKey, RegistryLoadTask.PendingRegistration.loadFromResource(this.data.elementCodec(), ops, elementKey, thunk), registrationInfo
|
| 54 | );
|
| 55 | },
|
| 56 | executor
|
| 57 | );
|
| 58 | }
|
| 59 | )
|
| 60 | .thenAcceptAsync(
|
| 61 | loadedEntries -> {
|
| 62 | this.registerElements(loadedEntries.entrySet().stream().sorted(Entry.comparingByKey()).map(Entry::getValue));
|
| 63 | TagLoader.ElementLookup<Holder<T>> tagElementLookup = TagLoader.ElementLookup.fromGetters(
|
| 64 | this.registryKey(), this.concurrentRegistrationGetter, this.readOnlyRegistry()
|
| 65 | );
|
| 66 | Map<TagKey<T>, List<Holder<T>>> pendingTags = TagLoader.loadTagsForRegistry(this.resourceManager, this.registryKey(), tagElementLookup);
|
| 67 | this.registerTags(pendingTags);
|
| 68 | },
|
| 69 | executor
|
| 70 | );
|
| 71 | }
|
| 72 | }
|
| 73 |
|