/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.mojang.serialization.DynamicOps * io.netty.buffer.ByteBuf */ package net.minecraft.core; import com.mojang.serialization.DynamicOps; import io.netty.buffer.ByteBuf; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.Set; import java.util.function.BiConsumer; import java.util.stream.Collectors; import java.util.stream.Stream; import net.minecraft.core.LayeredRegistryAccess; import net.minecraft.core.RegistrationInfo; import net.minecraft.core.Registry; import net.minecraft.core.RegistryAccess; import net.minecraft.nbt.Tag; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.resources.Identifier; import net.minecraft.resources.RegistryDataLoader; import net.minecraft.resources.ResourceKey; import net.minecraft.server.RegistryLayer; import net.minecraft.server.packs.repository.KnownPack; public class RegistrySynchronization { private static final Set>> NETWORKABLE_REGISTRIES = RegistryDataLoader.SYNCHRONIZED_REGISTRIES.stream().map(RegistryDataLoader.RegistryData::key).collect(Collectors.toUnmodifiableSet()); public static void packRegistries(DynamicOps ops, RegistryAccess registries, Set clientKnownPacks, BiConsumer>, List> output) { RegistryDataLoader.SYNCHRONIZED_REGISTRIES.forEach(registryEntry -> RegistrySynchronization.packRegistry(ops, registryEntry, registries, clientKnownPacks, output)); } private static void packRegistry(DynamicOps ops, RegistryDataLoader.RegistryData registryData, RegistryAccess registries, Set clientKnownPacks, BiConsumer>, List> output) { registries.lookup(registryData.key()).ifPresent(registry -> { ArrayList packedElements = new ArrayList(registry.size()); registry.listElements().forEach(element -> { Optional contents; boolean canSkipContents = registry.registrationInfo(element.key()).flatMap(RegistrationInfo::knownPackInfo).filter(clientKnownPacks::contains).isPresent(); if (canSkipContents) { contents = Optional.empty(); } else { Tag encodedElement = (Tag)registryData.elementCodec().encodeStart(ops, element.value()).getOrThrow(s -> new IllegalArgumentException("Failed to serialize " + String.valueOf(element.key()) + ": " + s)); contents = Optional.of(encodedElement); } packedElements.add(new PackedRegistryEntry(element.key().identifier(), contents)); }); output.accept(registry.key(), packedElements); }); } private static Stream> ownedNetworkableRegistries(RegistryAccess access) { return access.registries().filter(e -> RegistrySynchronization.isNetworkable(e.key())); } public static Stream> networkedRegistries(LayeredRegistryAccess registries) { return RegistrySynchronization.ownedNetworkableRegistries(registries.getAccessFrom(RegistryLayer.WORLDGEN)); } public static Stream> networkSafeRegistries(LayeredRegistryAccess registries) { Stream> staticRegistries = registries.getLayer(RegistryLayer.STATIC).registries(); Stream> networkedRegistries = RegistrySynchronization.networkedRegistries(registries); return Stream.concat(networkedRegistries, staticRegistries); } public static boolean isNetworkable(ResourceKey> key) { return NETWORKABLE_REGISTRIES.contains(key); } public record PackedRegistryEntry(Identifier id, Optional data) { public static final StreamCodec STREAM_CODEC = StreamCodec.composite(Identifier.STREAM_CODEC, PackedRegistryEntry::id, ByteBufCodecs.TAG.apply(ByteBufCodecs::optional), PackedRegistryEntry::data, PackedRegistryEntry::new); } }