| 1 | package net.minecraft.core.registries.codec;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import com.mojang.serialization.DynamicOps;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Optional;
|
| 10 | import net.minecraft.core.Holder;
|
| 11 | import net.minecraft.core.HolderGetter;
|
| 12 | import net.minecraft.core.HolderOwner;
|
| 13 | import net.minecraft.core.HolderSet;
|
| 14 | import net.minecraft.core.Registry;
|
| 15 | import net.minecraft.resources.RegistryOps;
|
| 16 | import net.minecraft.resources.ResourceKey;
|
| 17 | import net.minecraft.tags.TagKey;
|
| 18 | import net.minecraft.util.ExtraCodecs;
|
| 19 |
|
| 20 | public class HolderSetCodec<E> implements Codec<HolderSet<E>> {
|
| 21 | private final ResourceKey<? extends Registry<E>> registryKey;
|
| 22 | private final Codec<Either<TagKey<E>, List<Holder<E>>>> tagKeyOrValuesCodec;
|
| 23 |
|
| 24 | private static <E> Codec<List<Holder<E>>> directCodec(final Codec<Holder<E>> elementCodec, final boolean alwaysUseList) {
|
| 25 | Codec<List<Holder<E>>> listCodec = elementCodec.listOf();
|
| 26 | return alwaysUseList ? listCodec : ExtraCodecs.compactListCodec(elementCodec, listCodec);
|
| 27 | }
|
| 28 |
|
| 29 | public static <E> Codec<HolderSet<E>> create(
|
| 30 | final ResourceKey<? extends Registry<E>> registryKey, final Codec<Holder<E>> elementCodec, final boolean alwaysUseList
|
| 31 | ) {
|
| 32 | return new HolderSetCodec<>(registryKey, elementCodec, alwaysUseList);
|
| 33 | }
|
| 34 |
|
| 35 | private HolderSetCodec(final ResourceKey<? extends Registry<E>> registryKey, final Codec<Holder<E>> elementCodec, final boolean alwaysUseList) {
|
| 36 | this.registryKey = registryKey;
|
| 37 | this.tagKeyOrValuesCodec = Codec.either(TagKey.hashedCodec(registryKey), directCodec(elementCodec, alwaysUseList));
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public <T> DataResult<Pair<HolderSet<E>, T>> decode(final DynamicOps<T> ops, final T input) {
|
| 42 | return this.tagKeyOrValuesCodec
|
| 43 | .decode(ops, input)
|
| 44 | .flatMap(
|
| 45 | tagKeyOrValues -> {
|
| 46 | DataResult<HolderSet<E>> result = tagKeyOrValues.getFirst()
|
| 47 | .map(
|
| 48 | tagKey -> {
|
| 49 | if (ops instanceof RegistryOps<T> registryOps) {
|
| 50 | Optional<HolderGetter<E>> maybeRegistry = registryOps.getter(this.registryKey);
|
| 51 | return maybeRegistry.isPresent()
|
| 52 | ? lookupTag(maybeRegistry.get(), (TagKey<E>)tagKey)
|
| 53 | : DataResult.error(() -> "Registry " + this.registryKey.identifier() + " is not available in this context");
|
| 54 | } else {
|
| 55 | return DataResult.error(() -> "Registries are not available in this context");
|
| 56 | }
|
| 57 | },
|
| 58 | values -> DataResult.success(HolderSet.direct((List<? extends Holder<E>>)values))
|
| 59 | );
|
| 60 | return result.map(holders -> Pair.of((HolderSet<E>)holders, (T)tagKeyOrValues.getSecond()));
|
| 61 | }
|
| 62 | );
|
| 63 | }
|
| 64 |
|
| 65 | private static <E> DataResult<HolderSet<E>> lookupTag(final HolderGetter<E> registry, final TagKey<E> key) {
|
| 66 | return registry.get(key)
|
| 67 | .map(DataResult::success)
|
| 68 | .orElseGet(() -> DataResult.error(() -> "Missing tag: '" + key.location() + "' in '" + key.registry().identifier() + "'"));
|
| 69 | }
|
| 70 |
|
| 71 | public <T> DataResult<T> encode(final HolderSet<E> input, final DynamicOps<T> ops, final T prefix) {
|
| 72 | if (input instanceof HolderSet.Named<E> named && ops instanceof RegistryOps<T> registryOps) {
|
| 73 | Optional<? extends HolderOwner<E>> maybeOwner = registryOps.getter(this.registryKey);
|
| 74 | if (!maybeOwner.isPresent()) {
|
| 75 | return DataResult.error(() -> "Registry " + this.registryKey.identifier() + " is not available in this context");
|
| 76 | } else {
|
| 77 | return !named.canSerializeIn((HolderOwner<T>)maybeOwner.get())
|
| 78 | ? DataResult.error(() -> "HolderSet " + named + " is not valid in current registry set")
|
| 79 | : this.tagKeyOrValuesCodec.encode(Either.left(named.key()), ops, prefix);
|
| 80 | }
|
| 81 | } else {
|
| 82 | return this.tagKeyOrValuesCodec.encode(Either.right(input.stream().toList()), ops, prefix);
|
| 83 | }
|
| 84 | }
|
| 85 | }
|
| 86 |
|