TagKey .java

net.minecraft.tags.TagKey (record, 55 lines): members: registry, location, hashedCodec, isFor, cast, VALUES, streamCodec, create, codec. imports: Interner.

net.minecraft.tagsrecord55 linescommonextends Registry<T>>2.0 KB

Implements

API Surface (7)

<T> Codec<TagKey<T>> codec(final ResourceKey<? extends Registry<T>> registryName)
<T> Codec<TagKey<T>> hashedCodec(final ResourceKey<? extends Registry<T>> registryName)
<T> StreamCodec<ByteBuf, TagKey<T>> streamCodec(final ResourceKey<? extends Registry<T>> registryName)
<T> TagKey<T> create(final ResourceKey<? extends Registry<T>> registry, final Identifier location)
boolean isFor(final ResourceKey<? extends Registry<?>> registry)
<E> Optional<TagKey<E>> cast(final ResourceKey<? extends Registry<E>> registry)
String toString()

Fields & Constants (1)

VALUES

String literals (2)

"Not a tag id"
"TagKey["

Code Graph: Dependencies (4)

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

Full Source

TagKey.java55 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.tags;
2
3
import com.google.common.collect.Interner;
4
import com.google.common.collect.Interners;
5
import com.mojang.serialization.Codec;
6
import com.mojang.serialization.DataResult;
7
import io.netty.buffer.ByteBuf;
8
import java.util.Optional;
9
import net.fabricmc.fabric.api.tag.FabricTagKey;
10
import net.minecraft.core.Registry;
11
import net.minecraft.network.codec.StreamCodec;
12
import net.minecraft.resources.Identifier;
13
import net.minecraft.resources.ResourceKey;
14
15
public record TagKey<T>(ResourceKey<? extends Registry<T>> registry, Identifier location) implements FabricTagKey {
16
	private static final Interner<TagKey<?>> VALUES = Interners.newWeakInterner();
17
18
	@Deprecated
19
	public TagKey {
20
	}
21
22
	public static <T> Codec<TagKey<T>> codec(final ResourceKey<? extends Registry<T>> registryName) {
23
		return Identifier.CODEC.xmap(name -> create(registryName, name), TagKey::location);
24
	}
25
26
	public static <T> Codec<TagKey<T>> hashedCodec(final ResourceKey<? extends Registry<T>> registryName) {
27
		return Codec.STRING
28
			.comapFlatMap(
29
				name -> name.startsWith("#") ? Identifier.read(name.substring(1)).map(id -> create(registryName, id)) : DataResult.error(() -> "Not a tag id"),
30
				e -> "#" + e.location
31
			);
32
	}
33
34
	public static <T> StreamCodec<ByteBuf, TagKey<T>> streamCodec(final ResourceKey<? extends Registry<T>> registryName) {
35
		return Identifier.STREAM_CODEC.map(location -> create(registryName, location), TagKey::location);
36
	}
37
38
	public static <T> TagKey<T> create(final ResourceKey<? extends Registry<T>> registry, final Identifier location) {
39
		return (TagKey<T>)VALUES.intern(new TagKey<>(registry, location));
40
	}
41
42
	public boolean isFor(final ResourceKey<? extends Registry<?>> registry) {
43
		return this.registry == registry;
44
	}
45
46
	public <E> Optional<TagKey<E>> cast(final ResourceKey<? extends Registry<E>> registry) {
47
		return this.isFor(registry) ? Optional.of((TagKey<E>)this) : Optional.empty();
48
	}
49
50
	@Override
51
	public String toString() {
52
		return "TagKey[" + this.registry.identifier() + " / " + this.location + "]";
53
	}
54
}
55

Decompiled Minecraft 26.3 source of TagKey (net/minecraft/tags/TagKey.java) with Mojang official mappings, stored verbatim on the site.