| 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.ArrayList;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.concurrent.CompletableFuture;
|
| 11 | import java.util.concurrent.Executor;
|
| 12 | import net.minecraft.core.Holder;
|
| 13 | import net.minecraft.core.RegistrationInfo;
|
| 14 | import net.minecraft.core.Registry;
|
| 15 | import net.minecraft.core.RegistrySynchronization;
|
| 16 | import net.minecraft.nbt.NbtOps;
|
| 17 | import net.minecraft.nbt.Tag;
|
| 18 | import net.minecraft.server.packs.resources.ResourceProvider;
|
| 19 | import net.minecraft.tags.TagKey;
|
| 20 | import net.minecraft.tags.TagLoader;
|
| 21 | import net.minecraft.util.Util;
|
| 22 |
|
| 23 | public class NetworkRegistryLoadTask<T> extends RegistryLoadTask<T> {
|
| 24 | private static final RegistrationInfo NETWORK_REGISTRATION_INFO = new RegistrationInfo(Optional.empty(), Lifecycle.experimental());
|
| 25 | private final Map<ResourceKey<? extends Registry<?>>, RegistryDataLoader.NetworkedRegistryData> entries;
|
| 26 | private final ResourceProvider knownDataSource;
|
| 27 |
|
| 28 | public NetworkRegistryLoadTask(
|
| 29 | final RegistryDataLoader.RegistryData<T> data,
|
| 30 | final Lifecycle lifecycle,
|
| 31 | final Map<ResourceKey<?>, Exception> loadingErrors,
|
| 32 | final Map<ResourceKey<? extends Registry<?>>, RegistryDataLoader.NetworkedRegistryData> entries,
|
| 33 | final ResourceProvider knownDataSource
|
| 34 | ) {
|
| 35 | super(data, lifecycle, loadingErrors);
|
| 36 | this.entries = entries;
|
| 37 | this.knownDataSource = knownDataSource;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public CompletableFuture<?> load(final RegistryOps.RegistryInfoLookup context, final Executor executor) {
|
| 42 | RegistryDataLoader.NetworkedRegistryData registryEntries = this.entries.get(this.registryKey());
|
| 43 | if (registryEntries == null) {
|
| 44 | return CompletableFuture.completedFuture(null);
|
| 45 | }
|
| 46 |
|
| 47 | RegistryOps<Tag> nbtOps = RegistryOps.create(NbtOps.INSTANCE, context);
|
| 48 | RegistryOps<JsonElement> jsonOps = RegistryOps.create(JsonOps.INSTANCE, context);
|
| 49 | FileToIdConverter knownDataPathConverter = FileToIdConverter.registry(this.registryKey());
|
| 50 | List<CompletableFuture<RegistryLoadTask.PendingRegistration<T>>> elements = new ArrayList<>(registryEntries.elements().size());
|
| 51 |
|
| 52 | for (RegistrySynchronization.PackedRegistryEntry entry : registryEntries.elements()) {
|
| 53 | ResourceKey<T> elementKey = ResourceKey.create(this.registryKey(), entry.id());
|
| 54 | Optional<Tag> networkContents = entry.data();
|
| 55 | if (networkContents.isPresent()) {
|
| 56 | elements.add(
|
| 57 | CompletableFuture.supplyAsync(
|
| 58 | () -> new RegistryLoadTask.PendingRegistration<>(
|
| 59 | elementKey,
|
| 60 | RegistryLoadTask.PendingRegistration.loadFromNetwork(this.data.elementCodec(), nbtOps, elementKey, networkContents.get()),
|
| 61 | NETWORK_REGISTRATION_INFO
|
| 62 | ),
|
| 63 | executor
|
| 64 | )
|
| 65 | );
|
| 66 | } else {
|
| 67 | elements.add(
|
| 68 | CompletableFuture.supplyAsync(
|
| 69 | () -> new RegistryLoadTask.PendingRegistration<>(
|
| 70 | elementKey,
|
| 71 | RegistryLoadTask.PendingRegistration.findAndLoadFromResource(this.data.elementCodec(), jsonOps, elementKey, knownDataPathConverter, this.knownDataSource),
|
| 72 | NETWORK_REGISTRATION_INFO
|
| 73 | ),
|
| 74 | executor
|
| 75 | )
|
| 76 | );
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | return Util.sequence(elements).thenAcceptAsync(pendingRegistrations -> {
|
| 81 | this.registerElements(pendingRegistrations.stream());
|
| 82 | Map<TagKey<T>, List<Holder<T>>> pendingTags = TagLoader.loadTagsFromNetwork(registryEntries.tags(), this.readOnlyRegistry());
|
| 83 | this.registerTags(pendingTags);
|
| 84 | }, executor);
|
| 85 | }
|
| 86 | }
|
| 87 |
|