| 1 | package net.minecraft.client.multiplayer;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.Comparator;
|
| 6 | import java.util.HashMap;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Locale;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.function.BiConsumer;
|
| 11 | import java.util.stream.Collectors;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.CrashReport;
|
| 15 | import net.minecraft.CrashReportCategory;
|
| 16 | import net.minecraft.ReportedException;
|
| 17 | import net.minecraft.core.LayeredRegistryAccess;
|
| 18 | import net.minecraft.core.Registry;
|
| 19 | import net.minecraft.core.RegistryAccess;
|
| 20 | import net.minecraft.core.RegistrySynchronization;
|
| 21 | import net.minecraft.core.HolderLookup.RegistryLookup;
|
| 22 | import net.minecraft.core.Registry.PendingTags;
|
| 23 | import net.minecraft.core.RegistryAccess.Frozen;
|
| 24 | import net.minecraft.core.RegistrySynchronization.PackedRegistryEntry;
|
| 25 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 26 | import net.minecraft.resources.RegistryDataLoader;
|
| 27 | import net.minecraft.resources.ResourceKey;
|
| 28 | import net.minecraft.resources.RegistryDataLoader.NetworkedRegistryData;
|
| 29 | import net.minecraft.server.packs.resources.ResourceProvider;
|
| 30 | import net.minecraft.tags.TagLoader;
|
| 31 | import net.minecraft.tags.TagNetworkSerialization.NetworkPayload;
|
| 32 | import net.minecraft.util.Util;
|
| 33 | import org.jspecify.annotations.Nullable;
|
| 34 | import org.slf4j.Logger;
|
| 35 |
|
| 36 | @Environment(EnvType.CLIENT)
|
| 37 | public class RegistryDataCollector {
|
| 38 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 39 | private RegistryDataCollector.@Nullable ContentsCollector contentsCollector;
|
| 40 | private RegistryDataCollector.@Nullable TagCollector tagCollector;
|
| 41 |
|
| 42 | public void appendContents(final ResourceKey<? extends Registry<?>> registry, final List<PackedRegistryEntry> elementData) {
|
| 43 | if (this.contentsCollector == null) {
|
| 44 | this.contentsCollector = new RegistryDataCollector.ContentsCollector();
|
| 45 | }
|
| 46 |
|
| 47 | this.contentsCollector.append(registry, elementData);
|
| 48 | }
|
| 49 |
|
| 50 | public void appendTags(final Map<ResourceKey<? extends Registry<?>>, NetworkPayload> data) {
|
| 51 | if (this.tagCollector == null) {
|
| 52 | this.tagCollector = new RegistryDataCollector.TagCollector();
|
| 53 | }
|
| 54 |
|
| 55 | data.forEach(this.tagCollector::append);
|
| 56 | }
|
| 57 |
|
| 58 | private static <T> PendingTags<T> resolveRegistryTags(
|
| 59 | final Frozen context, final ResourceKey<? extends Registry<? extends T>> registryKey, final NetworkPayload tags
|
| 60 | ) {
|
| 61 | Registry<T> staticRegistry = context.lookupOrThrow(registryKey);
|
| 62 | return staticRegistry.prepareTagReload(tags.resolve(staticRegistry));
|
| 63 | }
|
| 64 |
|
| 65 | private RegistryAccess loadNewElementsAndTags(
|
| 66 | final ResourceProvider knownDataSource, final RegistryDataCollector.ContentsCollector contentsCollector, final boolean tagsForSynchronizedRegistriesOnly
|
| 67 | ) {
|
| 68 | LayeredRegistryAccess<ClientRegistryLayer> base = ClientRegistryLayer.createRegistryAccess();
|
| 69 | Frozen loadingContext = base.getAccessForLoading(ClientRegistryLayer.REMOTE);
|
| 70 | Map<ResourceKey<? extends Registry<?>>, NetworkedRegistryData> entriesToLoad = new HashMap<>();
|
| 71 | contentsCollector.elements
|
| 72 | .forEach(
|
| 73 | (registryKey, elements) -> entriesToLoad.put(
|
| 74 | (ResourceKey<? extends Registry<?>>)registryKey, new NetworkedRegistryData((List<PackedRegistryEntry>)elements, NetworkPayload.EMPTY)
|
| 75 | )
|
| 76 | );
|
| 77 | List<PendingTags<?>> pendingStaticTags = new ArrayList<>();
|
| 78 | if (this.tagCollector != null) {
|
| 79 | this.tagCollector.forEach((registryKey, tags) -> {
|
| 80 | if (!tags.isEmpty()) {
|
| 81 | if (RegistrySynchronization.isNetworkable((ResourceKey<? extends Registry<?>>)registryKey)) {
|
| 82 | entriesToLoad.compute((ResourceKey<? extends Registry<?>>)registryKey, (key, previousData) -> {
|
| 83 | List<PackedRegistryEntry> elements = previousData != null ? previousData.elements() : List.of();
|
| 84 | return new NetworkedRegistryData(elements, tags);
|
| 85 | });
|
| 86 | } else if (!tagsForSynchronizedRegistriesOnly) {
|
| 87 | pendingStaticTags.add(resolveRegistryTags(loadingContext, (ResourceKey<? extends Registry<?>>)registryKey, tags));
|
| 88 | }
|
| 89 | }
|
| 90 | });
|
| 91 | }
|
| 92 |
|
| 93 | List<RegistryLookup<?>> contextRegistriesWithTags = TagLoader.buildUpdatedLookups(loadingContext, pendingStaticTags);
|
| 94 |
|
| 95 | Frozen receivedRegistries;
|
| 96 | try {
|
| 97 | long start = Util.getMillis();
|
| 98 | receivedRegistries = RegistryDataLoader.load(
|
| 99 | entriesToLoad, knownDataSource, contextRegistriesWithTags, RegistryDataLoader.SYNCHRONIZED_REGISTRIES, Util.backgroundExecutor()
|
| 100 | )
|
| 101 | .join();
|
| 102 | long end = Util.getMillis();
|
| 103 | LOGGER.debug("Loading network data took {} ms", end - start);
|
| 104 | } catch (Exception e) {
|
| 105 | CrashReport report = CrashReport.forThrowable(e, "Network Registry Load");
|
| 106 | addCrashDetails(report, entriesToLoad, pendingStaticTags);
|
| 107 | throw new ReportedException(report);
|
| 108 | }
|
| 109 |
|
| 110 | RegistryAccess registries = base.replaceFrom(ClientRegistryLayer.REMOTE, receivedRegistries).compositeAccess();
|
| 111 | pendingStaticTags.forEach(PendingTags::apply);
|
| 112 | return registries;
|
| 113 | }
|
| 114 |
|
| 115 | private static void addCrashDetails(
|
| 116 | final CrashReport report, final Map<ResourceKey<? extends Registry<?>>, NetworkedRegistryData> dynamicRegistries, final List<PendingTags<?>> staticRegistries
|
| 117 | ) {
|
| 118 | CrashReportCategory details = report.addCategory("Received Elements and Tags");
|
| 119 | details.setDetail(
|
| 120 | "Dynamic Registries",
|
| 121 | () -> dynamicRegistries.entrySet()
|
| 122 | .stream()
|
| 123 | .sorted(Comparator.comparing(entry -> entry.getKey().identifier()))
|
| 124 | .map(
|
| 125 | entry -> String.format(
|
| 126 | Locale.ROOT, "\n\t\t%s: elements=%d tags=%d", entry.getKey().identifier(), entry.getValue().elements().size(), entry.getValue().tags().size()
|
| 127 | )
|
| 128 | )
|
| 129 | .collect(Collectors.joining())
|
| 130 | );
|
| 131 | details.setDetail(
|
| 132 | "Static Registries",
|
| 133 | () -> staticRegistries.stream()
|
| 134 | .sorted(Comparator.comparing(entry -> entry.key().identifier()))
|
| 135 | .map(entry -> String.format(Locale.ROOT, "\n\t\t%s: tags=%d", entry.key().identifier(), entry.size()))
|
| 136 | .collect(Collectors.joining())
|
| 137 | );
|
| 138 | }
|
| 139 |
|
| 140 | private static void loadOnlyTags(final RegistryDataCollector.TagCollector tagCollector, final Frozen originalRegistries, final boolean includeSharedRegistries) {
|
| 141 | tagCollector.forEach((registryKey, tags) -> {
|
| 142 | if (includeSharedRegistries || RegistrySynchronization.isNetworkable((ResourceKey<? extends Registry<?>>)registryKey)) {
|
| 143 | resolveRegistryTags(originalRegistries, (ResourceKey<? extends Registry<?>>)registryKey, tags).apply();
|
| 144 | }
|
| 145 | });
|
| 146 | }
|
| 147 |
|
| 148 | private static void updateComponents(final Frozen frozenRegistries, final boolean includeSharedRegistries) {
|
| 149 | BuiltInRegistries.DATA_COMPONENT_INITIALIZERS.build(frozenRegistries).forEach(pendingComponents -> {
|
| 150 | if (includeSharedRegistries || RegistrySynchronization.isNetworkable(pendingComponents.key())) {
|
| 151 | pendingComponents.apply();
|
| 152 | }
|
| 153 | });
|
| 154 | }
|
| 155 |
|
| 156 | public Frozen collectGameRegistries(
|
| 157 | final ResourceProvider knownDataSource, final Frozen originalRegistries, final boolean tagsAndComponentsForSynchronizedRegistriesOnly
|
| 158 | ) {
|
| 159 | RegistryAccess registries;
|
| 160 | if (this.contentsCollector != null) {
|
| 161 | registries = this.loadNewElementsAndTags(knownDataSource, this.contentsCollector, tagsAndComponentsForSynchronizedRegistriesOnly);
|
| 162 | } else {
|
| 163 | if (this.tagCollector != null) {
|
| 164 | loadOnlyTags(this.tagCollector, originalRegistries, !tagsAndComponentsForSynchronizedRegistriesOnly);
|
| 165 | }
|
| 166 |
|
| 167 | registries = originalRegistries;
|
| 168 | }
|
| 169 |
|
| 170 | Frozen frozenRegistries = registries.freeze();
|
| 171 | updateComponents(frozenRegistries, !tagsAndComponentsForSynchronizedRegistriesOnly);
|
| 172 | return frozenRegistries;
|
| 173 | }
|
| 174 |
|
| 175 | @Environment(EnvType.CLIENT)
|
| 176 | private static class ContentsCollector {
|
| 177 | private final Map<ResourceKey<? extends Registry<?>>, List<PackedRegistryEntry>> elements = new HashMap<>();
|
| 178 |
|
| 179 | public void append(final ResourceKey<? extends Registry<?>> registry, final List<PackedRegistryEntry> elementData) {
|
| 180 | this.elements.computeIfAbsent(registry, ignore -> new ArrayList<>()).addAll(elementData);
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | @Environment(EnvType.CLIENT)
|
| 185 | private static class TagCollector {
|
| 186 | private final Map<ResourceKey<? extends Registry<?>>, NetworkPayload> tags = new HashMap<>();
|
| 187 |
|
| 188 | public void append(final ResourceKey<? extends Registry<?>> registry, final NetworkPayload tagData) {
|
| 189 | this.tags.put(registry, tagData);
|
| 190 | }
|
| 191 |
|
| 192 | public void forEach(final BiConsumer<? super ResourceKey<? extends Registry<?>>, ? super NetworkPayload> action) {
|
| 193 | this.tags.forEach(action);
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 |
|