| 1 | package net.minecraft.core.registries.codec;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import com.mojang.serialization.DynamicOps;
|
| 7 | import com.mojang.serialization.Lifecycle;
|
| 8 | import java.util.Optional;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.core.HolderGetter;
|
| 11 | import net.minecraft.core.HolderOwner;
|
| 12 | import net.minecraft.core.Registry;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 | import net.minecraft.resources.RegistryOps;
|
| 15 | import net.minecraft.resources.ResourceKey;
|
| 16 |
|
| 17 | public final class RegistryFixedCodec<E> implements Codec<Holder<E>> {
|
| 18 | private final ResourceKey<? extends Registry<E>> registryKey;
|
| 19 |
|
| 20 | public static <E> Codec<Holder<E>> create(final ResourceKey<? extends Registry<E>> registryKey) {
|
| 21 | return new RegistryFixedCodec<>(registryKey);
|
| 22 | }
|
| 23 |
|
| 24 | private RegistryFixedCodec(final ResourceKey<? extends Registry<E>> registryKey) {
|
| 25 | this.registryKey = registryKey;
|
| 26 | }
|
| 27 |
|
| 28 | public <T> DataResult<T> encode(final Holder<E> input, final DynamicOps<T> ops, final T prefix) {
|
| 29 | if (ops instanceof RegistryOps<?> registryOps) {
|
| 30 | Optional<? extends HolderOwner<E>> maybeOwner = registryOps.getter(this.registryKey);
|
| 31 | if (maybeOwner.isPresent()) {
|
| 32 | if (!input.canSerializeIn((HolderOwner<E>)maybeOwner.get())) {
|
| 33 | return DataResult.error(() -> "Element " + input + " is not valid in current registry set");
|
| 34 | }
|
| 35 |
|
| 36 | return input.unwrap()
|
| 37 | .map(
|
| 38 | id -> Identifier.CODEC.encode(id.identifier(), ops, prefix),
|
| 39 | value -> DataResult.error(() -> "Elements from registry " + this.registryKey + " can't be serialized to a value")
|
| 40 | );
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | return DataResult.error(() -> "Can't access registry " + this.registryKey.identifier());
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public <T> DataResult<Pair<Holder<E>, T>> decode(final DynamicOps<T> ops, final T input) {
|
| 49 | if (ops instanceof RegistryOps<?> registryOps) {
|
| 50 | Optional<HolderGetter<E>> lookup = registryOps.getter(this.registryKey);
|
| 51 | if (lookup.isPresent()) {
|
| 52 | return Identifier.CODEC
|
| 53 | .decode(ops, input)
|
| 54 | .flatMap(
|
| 55 | pair -> {
|
| 56 | ResourceKey<E> elementKey = ResourceKey.create(this.registryKey, pair.getFirst());
|
| 57 | return lookup.get()
|
| 58 | .get(elementKey)
|
| 59 | .map(DataResult::success)
|
| 60 | .orElseGet(() -> DataResult.error(() -> "Failed to get element " + elementKey.identifier() + " from registry " + elementKey.registry()))
|
| 61 | .<Pair<Holder<E>, T>>map(h -> Pair.of(h, (T)pair.getSecond()))
|
| 62 | .setLifecycle(Lifecycle.stable());
|
| 63 | }
|
| 64 | );
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | return DataResult.error(() -> "Can't access registry " + this.registryKey.identifier());
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public String toString() {
|
| 73 | return "RegistryFixedCodec[" + this.registryKey + "]";
|
| 74 | }
|
| 75 | }
|
| 76 |
|