| 1 | package net.minecraft.core;
|
| 2 |
|
| 3 | import com.mojang.serialization.DynamicOps;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.function.BiConsumer;
|
| 10 | import java.util.stream.Collectors;
|
| 11 | import java.util.stream.Stream;
|
| 12 | import net.minecraft.nbt.Tag;
|
| 13 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 14 | import net.minecraft.network.codec.StreamCodec;
|
| 15 | import net.minecraft.resources.Identifier;
|
| 16 | import net.minecraft.resources.RegistryDataLoader;
|
| 17 | import net.minecraft.resources.ResourceKey;
|
| 18 | import net.minecraft.server.RegistryLayer;
|
| 19 | import net.minecraft.server.packs.repository.KnownPack;
|
| 20 |
|
| 21 | public class RegistrySynchronization {
|
| 22 | private static final Set<ResourceKey<? extends Registry<?>>> NETWORKABLE_REGISTRIES = RegistryDataLoader.SYNCHRONIZED_REGISTRIES
|
| 23 | .stream()
|
| 24 | .map(RegistryDataLoader.RegistryData::key)
|
| 25 | .collect(Collectors.toUnmodifiableSet());
|
| 26 |
|
| 27 | public static void packRegistries(
|
| 28 | final DynamicOps<Tag> ops,
|
| 29 | final RegistryAccess registries,
|
| 30 | final Set<KnownPack> clientKnownPacks,
|
| 31 | final BiConsumer<ResourceKey<? extends Registry<?>>, List<RegistrySynchronization.PackedRegistryEntry>> output
|
| 32 | ) {
|
| 33 | RegistryDataLoader.SYNCHRONIZED_REGISTRIES
|
| 34 | .forEach(registryEntry -> packRegistry(ops, (RegistryDataLoader.RegistryData<?>)registryEntry, registries, clientKnownPacks, output));
|
| 35 | }
|
| 36 |
|
| 37 | private static <T> void packRegistry(
|
| 38 | final DynamicOps<Tag> ops,
|
| 39 | final RegistryDataLoader.RegistryData<T> registryData,
|
| 40 | final RegistryAccess registries,
|
| 41 | final Set<KnownPack> clientKnownPacks,
|
| 42 | final BiConsumer<ResourceKey<? extends Registry<?>>, List<RegistrySynchronization.PackedRegistryEntry>> output
|
| 43 | ) {
|
| 44 | registries.lookup(registryData.key())
|
| 45 | .ifPresent(
|
| 46 | registry -> {
|
| 47 | List<RegistrySynchronization.PackedRegistryEntry> packedElements = new ArrayList<>(registry.size());
|
| 48 | registry.listElements()
|
| 49 | .forEach(
|
| 50 | element -> {
|
| 51 | boolean canSkipContents = registry.registrationInfo(element.key())
|
| 52 | .flatMap(RegistrationInfo::knownPackInfo)
|
| 53 | .filter(clientKnownPacks::contains)
|
| 54 | .isPresent();
|
| 55 | Optional<Tag> contents;
|
| 56 | if (canSkipContents) {
|
| 57 | contents = Optional.empty();
|
| 58 | } else {
|
| 59 | Tag encodedElement = registryData.elementCodec()
|
| 60 | .encodeStart(ops, element.value())
|
| 61 | .getOrThrow(s -> new IllegalArgumentException("Failed to serialize " + element.key() + ": " + s));
|
| 62 | contents = Optional.of(encodedElement);
|
| 63 | }
|
| 64 |
|
| 65 | packedElements.add(new RegistrySynchronization.PackedRegistryEntry(element.key().identifier(), contents));
|
| 66 | }
|
| 67 | );
|
| 68 | output.accept(registry.key(), packedElements);
|
| 69 | }
|
| 70 | );
|
| 71 | }
|
| 72 |
|
| 73 | private static Stream<RegistryAccess.RegistryEntry<?>> ownedNetworkableRegistries(final RegistryAccess access) {
|
| 74 | return access.registries().filter(e -> isNetworkable(e.key()));
|
| 75 | }
|
| 76 |
|
| 77 | public static Stream<RegistryAccess.RegistryEntry<?>> networkedRegistries(final LayeredRegistryAccess<RegistryLayer> registries) {
|
| 78 | return ownedNetworkableRegistries(registries.getAccessFrom(RegistryLayer.WORLD));
|
| 79 | }
|
| 80 |
|
| 81 | public static Stream<RegistryAccess.RegistryEntry<?>> networkSafeRegistries(final LayeredRegistryAccess<RegistryLayer> registries) {
|
| 82 | Stream<RegistryAccess.RegistryEntry<?>> staticRegistries = registries.getLayer(RegistryLayer.STATIC).registries();
|
| 83 | Stream<RegistryAccess.RegistryEntry<?>> networkedRegistries = networkedRegistries(registries);
|
| 84 | return Stream.concat(networkedRegistries, staticRegistries);
|
| 85 | }
|
| 86 |
|
| 87 | public static boolean isNetworkable(final ResourceKey<? extends Registry<?>> key) {
|
| 88 | return NETWORKABLE_REGISTRIES.contains(key);
|
| 89 | }
|
| 90 |
|
| 91 | public record PackedRegistryEntry(Identifier id, Optional<Tag> data) {
|
| 92 | public static final StreamCodec<ByteBuf, RegistrySynchronization.PackedRegistryEntry> STREAM_CODEC = StreamCodec.composite(
|
| 93 | Identifier.STREAM_CODEC,
|
| 94 | RegistrySynchronization.PackedRegistryEntry::id,
|
| 95 | ByteBufCodecs.TAG.apply(ByteBufCodecs::optional),
|
| 96 | RegistrySynchronization.PackedRegistryEntry::data,
|
| 97 | RegistrySynchronization.PackedRegistryEntry::new
|
| 98 | );
|
| 99 | }
|
| 100 | }
|
| 101 |
|