| 1 | package net.minecraft.network.chat;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.mojang.datafixers.util.Either;
|
| 5 | import com.mojang.datafixers.util.Pair;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.DataResult;
|
| 8 | import com.mojang.serialization.DynamicOps;
|
| 9 | import com.mojang.serialization.JsonOps;
|
| 10 | import com.mojang.serialization.MapCodec;
|
| 11 | import com.mojang.serialization.MapDecoder;
|
| 12 | import com.mojang.serialization.MapEncoder;
|
| 13 | import com.mojang.serialization.MapLike;
|
| 14 | import com.mojang.serialization.RecordBuilder;
|
| 15 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 16 | import io.netty.buffer.ByteBuf;
|
| 17 | import java.util.Collection;
|
| 18 | import java.util.List;
|
| 19 | import java.util.Optional;
|
| 20 | import java.util.function.Function;
|
| 21 | import java.util.stream.Stream;
|
| 22 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 23 | import net.minecraft.network.chat.contents.KeybindContents;
|
| 24 | import net.minecraft.network.chat.contents.NbtContents;
|
| 25 | import net.minecraft.network.chat.contents.ObjectContents;
|
| 26 | import net.minecraft.network.chat.contents.PlainTextContents;
|
| 27 | import net.minecraft.network.chat.contents.ScoreContents;
|
| 28 | import net.minecraft.network.chat.contents.SelectorContents;
|
| 29 | import net.minecraft.network.chat.contents.TranslatableContents;
|
| 30 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 31 | import net.minecraft.network.codec.StreamCodec;
|
| 32 | import net.minecraft.resources.RegistryOps;
|
| 33 | import net.minecraft.util.ExtraCodecs;
|
| 34 | import net.minecraft.util.GsonHelper;
|
| 35 |
|
| 36 | public class ComponentSerialization {
|
| 37 | public static final Codec<Component> CODEC = Codec.recursive("Component", ComponentSerialization::createCodec);
|
| 38 | public static final StreamCodec<RegistryFriendlyByteBuf, Component> STREAM_CODEC = ByteBufCodecs.fromCodecWithRegistries(CODEC);
|
| 39 | public static final StreamCodec<RegistryFriendlyByteBuf, Optional<Component>> OPTIONAL_STREAM_CODEC = STREAM_CODEC.apply(ByteBufCodecs::optional);
|
| 40 | public static final StreamCodec<RegistryFriendlyByteBuf, Component> TRUSTED_STREAM_CODEC = ByteBufCodecs.fromCodecWithRegistriesTrusted(CODEC);
|
| 41 | public static final StreamCodec<RegistryFriendlyByteBuf, Optional<Component>> TRUSTED_OPTIONAL_STREAM_CODEC = TRUSTED_STREAM_CODEC.apply(
|
| 42 | ByteBufCodecs::optional
|
| 43 | );
|
| 44 | public static final StreamCodec<ByteBuf, Component> TRUSTED_CONTEXT_FREE_STREAM_CODEC = ByteBufCodecs.fromCodecTrusted(CODEC);
|
| 45 |
|
| 46 | public static Codec<Component> flatRestrictedCodec(final int maxFlatSize) {
|
| 47 | return new Codec<Component>() {
|
| 48 | @Override
|
| 49 | public <T> DataResult<Pair<Component, T>> decode(final DynamicOps<T> ops, final T input) {
|
| 50 | return ComponentSerialization.CODEC
|
| 51 | .decode(ops, input)
|
| 52 | .flatMap(
|
| 53 | pair -> this.isTooLarge(ops, pair.getFirst())
|
| 54 | ? DataResult.error(() -> "Component was too large: greater than max size " + maxFlatSize)
|
| 55 | : DataResult.success((Pair<Component, T>)pair)
|
| 56 | );
|
| 57 | }
|
| 58 |
|
| 59 | public <T> DataResult<T> encode(final Component input, final DynamicOps<T> ops, final T prefix) {
|
| 60 | return ComponentSerialization.CODEC.encodeStart(ops, input);
|
| 61 | }
|
| 62 |
|
| 63 | private <T> boolean isTooLarge(final DynamicOps<T> ops, final Component input) {
|
| 64 | DataResult<JsonElement> json = ComponentSerialization.CODEC.encodeStart(asJsonOps(ops), input);
|
| 65 | return json.isSuccess() && GsonHelper.encodesLongerThan(json.getOrThrow(), maxFlatSize);
|
| 66 | }
|
| 67 |
|
| 68 | private static <T> DynamicOps<JsonElement> asJsonOps(final DynamicOps<T> ops) {
|
| 69 | return ops instanceof RegistryOps<T> registryOps ? registryOps.withParent(JsonOps.INSTANCE) : JsonOps.INSTANCE;
|
| 70 | }
|
| 71 | };
|
| 72 | }
|
| 73 |
|
| 74 | private static MutableComponent createFromList(final List<Component> list) {
|
| 75 | MutableComponent result = list.get(0).copy();
|
| 76 |
|
| 77 | for (int i = 1; i < list.size(); i++) {
|
| 78 | result.append(list.get(i));
|
| 79 | }
|
| 80 |
|
| 81 | return result;
|
| 82 | }
|
| 83 |
|
| 84 | public static <T> MapCodec<T> createLegacyComponentMatcher(
|
| 85 | final ExtraCodecs.LateBoundIdMapper<String, MapCodec<? extends T>> types, final Function<T, MapCodec<? extends T>> codecGetter, final String typeFieldName
|
| 86 | ) {
|
| 87 | MapCodec<T> compactCodec = new ComponentSerialization.FuzzyCodec<>(types.values(), codecGetter);
|
| 88 | MapCodec<T> discriminatorCodec = types.codec(Codec.STRING).dispatchMap(typeFieldName, codecGetter, c -> c);
|
| 89 | MapCodec<T> contentsCodec = new ComponentSerialization.StrictEither<>(typeFieldName, discriminatorCodec, compactCodec);
|
| 90 | return ExtraCodecs.orCompressed(contentsCodec, discriminatorCodec);
|
| 91 | }
|
| 92 |
|
| 93 | private static Codec<Component> createCodec(final Codec<Component> topSerializer) {
|
| 94 | ExtraCodecs.LateBoundIdMapper<String, MapCodec<? extends ComponentContents>> contentTypes = new ExtraCodecs.LateBoundIdMapper<>();
|
| 95 | bootstrap(contentTypes);
|
| 96 | MapCodec<ComponentContents> compressedContentsCodec = createLegacyComponentMatcher(contentTypes, ComponentContents::codec, "type");
|
| 97 | Codec<Component> fullCodec = RecordCodecBuilder.create(
|
| 98 | i -> i.group(
|
| 99 | compressedContentsCodec.forGetter(Component::getContents),
|
| 100 | ExtraCodecs.nonEmptyList(topSerializer.listOf()).optionalFieldOf("extra", List.of()).forGetter(Component::getSiblings),
|
| 101 | Style.Serializer.MAP_CODEC.forGetter(Component::getStyle)
|
| 102 | )
|
| 103 | .apply(i, MutableComponent::new)
|
| 104 | );
|
| 105 | return Codec.either(Codec.either(Codec.STRING, ExtraCodecs.nonEmptyList(topSerializer.listOf())), fullCodec)
|
| 106 | .xmap(
|
| 107 | specialOrComponent -> specialOrComponent.map(special -> special.map(Component::literal, ComponentSerialization::createFromList), c -> (Component)c),
|
| 108 | component -> {
|
| 109 | String text = component.tryCollapseToString();
|
| 110 | return text != null ? Either.left(Either.left(text)) : Either.right(component);
|
| 111 | }
|
| 112 | );
|
| 113 | }
|
| 114 |
|
| 115 | private static void bootstrap(final ExtraCodecs.LateBoundIdMapper<String, MapCodec<? extends ComponentContents>> contentTypes) {
|
| 116 | contentTypes.put("text", PlainTextContents.MAP_CODEC);
|
| 117 | contentTypes.put("translatable", TranslatableContents.MAP_CODEC);
|
| 118 | contentTypes.put("keybind", KeybindContents.MAP_CODEC);
|
| 119 | contentTypes.put("score", ScoreContents.MAP_CODEC);
|
| 120 | contentTypes.put("selector", SelectorContents.MAP_CODEC);
|
| 121 | contentTypes.put("nbt", NbtContents.MAP_CODEC);
|
| 122 | contentTypes.put("object", ObjectContents.MAP_CODEC);
|
| 123 | }
|
| 124 |
|
| 125 | private static class FuzzyCodec<T> extends MapCodec<T> {
|
| 126 | private final Collection<MapCodec<? extends T>> codecs;
|
| 127 | private final Function<T, ? extends MapEncoder<? extends T>> encoderGetter;
|
| 128 |
|
| 129 | public FuzzyCodec(final Collection<MapCodec<? extends T>> codecs, final Function<T, ? extends MapEncoder<? extends T>> encoderGetter) {
|
| 130 | this.codecs = codecs;
|
| 131 | this.encoderGetter = encoderGetter;
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public <S> DataResult<T> decode(final DynamicOps<S> ops, final MapLike<S> input) {
|
| 136 | for (MapDecoder<? extends T> codec : this.codecs) {
|
| 137 | DataResult<? extends T> result = codec.decode(ops, input);
|
| 138 | if (result.result().isPresent()) {
|
| 139 | return (DataResult<T>)result;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | return DataResult.error(() -> "No matching codec found");
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public <S> RecordBuilder<S> encode(final T input, final DynamicOps<S> ops, final RecordBuilder<S> prefix) {
|
| 148 | MapEncoder<T> encoder = (MapEncoder<T>)this.encoderGetter.apply(input);
|
| 149 | return encoder.encode(input, ops, prefix);
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public <S> Stream<S> keys(final DynamicOps<S> ops) {
|
| 154 | return this.codecs.stream().flatMap(c -> c.keys(ops)).distinct();
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | public String toString() {
|
| 159 | return "FuzzyCodec[" + this.codecs + "]";
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | private static class StrictEither<T> extends MapCodec<T> {
|
| 164 | private final String typeFieldName;
|
| 165 | private final MapCodec<T> typed;
|
| 166 | private final MapCodec<T> fuzzy;
|
| 167 |
|
| 168 | public StrictEither(final String typeFieldName, final MapCodec<T> typed, final MapCodec<T> fuzzy) {
|
| 169 | this.typeFieldName = typeFieldName;
|
| 170 | this.typed = typed;
|
| 171 | this.fuzzy = fuzzy;
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | public <O> DataResult<T> decode(final DynamicOps<O> ops, final MapLike<O> input) {
|
| 176 | return input.get(this.typeFieldName) != null ? this.typed.decode(ops, input) : this.fuzzy.decode(ops, input);
|
| 177 | }
|
| 178 |
|
| 179 | @Override
|
| 180 | public <O> RecordBuilder<O> encode(final T input, final DynamicOps<O> ops, final RecordBuilder<O> prefix) {
|
| 181 | return this.fuzzy.encode(input, ops, prefix);
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | public <T1> Stream<T1> keys(final DynamicOps<T1> ops) {
|
| 186 | return Stream.concat(this.typed.keys(ops), this.fuzzy.keys(ops)).distinct();
|
| 187 | }
|
| 188 | }
|
| 189 | }
|
| 190 |
|