RegistryFileCodec .java

net.minecraft.core.registries.codec.RegistryFileCodec (class): members: allowInline, elementCodec, encode, decode, registryKey, create. imports: Lifecycle.

net.minecraft.core.registries.codecclass80 linescommon3.2 KB

Implements

API Surface (4)

<E> Codec<Holder<E>> create(final ResourceKey<? extends Registry<E>> registryKey, final Codec<E> elementCodec, final boolean allowInline)
<T> DataResult<T> encode(final Holder<E> input, final DynamicOps<T> ops, final T prefix)
<T> DataResult<Pair<Holder<E>, T>> decode(final DynamicOps<T> ops, final T input)
String toString()

Fields & Constants (3)

registryKey
elementCodec
allowInline

String literals (7)

"Element "
" is not valid in current registry set"
"Inline definitions not allowed here"
"Registry does not exist: "
"Failed to get element "
" from registry "
"RegistryFileCodec["

Code Graph: Dependencies (7)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

RegistryFileCodec.java80 lines · Minecraft 26.3 · verbatim
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 RegistryFileCodec<E> implements Codec<Holder<E>> {
18
	private final ResourceKey<? extends Registry<E>> registryKey;
19
	private final Codec<E> elementCodec;
20
	private final boolean allowInline;
21
22
	public static <E> Codec<Holder<E>> create(final ResourceKey<? extends Registry<E>> registryKey, final Codec<E> elementCodec, final boolean allowInline) {
23
		return new RegistryFileCodec<>(registryKey, elementCodec, allowInline);
24
	}
25
26
	private RegistryFileCodec(final ResourceKey<? extends Registry<E>> registryKey, final Codec<E> elementCodec, final boolean allowInline) {
27
		this.registryKey = registryKey;
28
		this.elementCodec = elementCodec;
29
		this.allowInline = allowInline;
30
	}
31
32
	public <T> DataResult<T> encode(final Holder<E> input, final DynamicOps<T> ops, final T prefix) {
33
		if (ops instanceof RegistryOps<?> registryOps) {
34
			Optional<? extends HolderOwner<E>> maybeOwner = registryOps.getter(this.registryKey);
35
			if (maybeOwner.isPresent()) {
36
				if (!input.canSerializeIn((HolderOwner<E>)maybeOwner.get())) {
37
					return DataResult.error(() -> "Element " + input + " is not valid in current registry set");
38
				}
39
40
				return input.unwrap().map(id -> Identifier.CODEC.encode(id.identifier(), ops, prefix), value -> this.elementCodec.encode((E)value, ops, prefix));
41
			}
42
		}
43
44
		return this.elementCodec.encode(input.value(), ops, prefix);
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
			DataResult<Pair<Identifier, T>> decoded = Identifier.CODEC.decode(ops, input);
51
			if (decoded.result().isEmpty()) {
52
				return !this.allowInline
53
					? DataResult.error(() -> "Inline definitions not allowed here")
54
					: this.elementCodec.decode(ops, input).map(p -> p.mapFirst(Holder::direct));
55
			}
56
57
			Optional<HolderGetter<E>> maybeLookup = registryOps.getter(this.registryKey);
58
			if (maybeLookup.isEmpty()) {
59
				return DataResult.error(() -> "Registry does not exist: " + this.registryKey.identifier());
60
			}
61
62
			HolderGetter<E> lookup = maybeLookup.get();
63
			Pair<Identifier, T> pair = decoded.result().get();
64
			ResourceKey<E> elementKey = ResourceKey.create(this.registryKey, pair.getFirst());
65
			return lookup.get(elementKey)
66
				.map(DataResult::success)
67
				.orElseGet(() -> DataResult.error(() -> "Failed to get element " + elementKey.identifier() + " from registry " + elementKey.registry()))
68
				.<Pair<Holder<E>, T>>map(h -> Pair.of(h, pair.getSecond()))
69
				.setLifecycle(Lifecycle.stable());
70
		} else {
71
			return this.elementCodec.decode(ops, input).map(p -> p.mapFirst(Holder::direct));
72
		}
73
	}
74
75
	@Override
76
	public String toString() {
77
		return "RegistryFileCodec[" + this.registryKey + " " + this.elementCodec + "]";
78
	}
79
}
80

Decompiled Minecraft 26.3 source of RegistryFileCodec (net/minecraft/core/registries/codec/RegistryFileCodec.java) with Mojang official mappings, stored verbatim on the site.