CustomData .java

net.minecraft.world.item.component.CustomData (class): members: COMPOUND_TAG_CODEC, matchedBy, copyTag, tag, set, update, hashCode, equals, isEmpty, EMPTY.

net.minecraft.world.item.componentclass84 linescommon2.4 KB

API Surface (10)

CustomData of(final CompoundTag tag)
boolean matchedBy(final CompoundTag expectedTag)
void update(final DataComponentType<CustomData> component, final ItemStack itemStack, final Consumer<CompoundTag> consumer)
void set(final DataComponentType<CustomData> component, final ItemStack itemStack, final CompoundTag tag)
CustomData update(final Consumer<CompoundTag> consumer)
boolean isEmpty()
CompoundTag copyTag()
boolean equals(final Object obj)
int hashCode()
String toString()

Fields & Constants (5)

EMPTY
COMPOUND_TAG_CODEC
CODEC
STREAM_CODEC
tag

Code Graph: Dependencies (7)

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

Full Source

CustomData.java84 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.component;
2
3
import com.mojang.serialization.Codec;
4
import io.netty.buffer.ByteBuf;
5
import java.util.function.Consumer;
6
import net.minecraft.core.component.DataComponentType;
7
import net.minecraft.nbt.CompoundTag;
8
import net.minecraft.nbt.NbtUtils;
9
import net.minecraft.nbt.TagParser;
10
import net.minecraft.network.codec.ByteBufCodecs;
11
import net.minecraft.network.codec.StreamCodec;
12
import net.minecraft.world.item.ItemStack;
13
14
public final class CustomData {
15
	public static final CustomData EMPTY = new CustomData(new CompoundTag());
16
	public static final Codec<CompoundTag> COMPOUND_TAG_CODEC = Codec.withAlternative(CompoundTag.CODEC, TagParser.FLATTENED_CODEC);
17
	public static final Codec<CustomData> CODEC = COMPOUND_TAG_CODEC.xmap(CustomData::new, data -> data.tag);
18
	@Deprecated
19
	public static final StreamCodec<ByteBuf, CustomData> STREAM_CODEC = ByteBufCodecs.COMPOUND_TAG.map(CustomData::new, data -> data.tag);
20
	private final CompoundTag tag;
21
22
	private CustomData(final CompoundTag tag) {
23
		this.tag = tag;
24
	}
25
26
	public static CustomData of(final CompoundTag tag) {
27
		return new CustomData(tag.copy());
28
	}
29
30
	public boolean matchedBy(final CompoundTag expectedTag) {
31
		return NbtUtils.compareNbt(expectedTag, this.tag, true);
32
	}
33
34
	public static void update(final DataComponentType<CustomData> component, final ItemStack itemStack, final Consumer<CompoundTag> consumer) {
35
		CustomData newData = itemStack.getOrDefault(component, EMPTY).update(consumer);
36
		if (newData.tag.isEmpty()) {
37
			itemStack.remove(component);
38
		} else {
39
			itemStack.set(component, newData);
40
		}
41
	}
42
43
	public static void set(final DataComponentType<CustomData> component, final ItemStack itemStack, final CompoundTag tag) {
44
		if (!tag.isEmpty()) {
45
			itemStack.set(component, of(tag));
46
		} else {
47
			itemStack.remove(component);
48
		}
49
	}
50
51
	public CustomData update(final Consumer<CompoundTag> consumer) {
52
		CompoundTag newTag = this.tag.copy();
53
		consumer.accept(newTag);
54
		return new CustomData(newTag);
55
	}
56
57
	public boolean isEmpty() {
58
		return this.tag.isEmpty();
59
	}
60
61
	public CompoundTag copyTag() {
62
		return this.tag.copy();
63
	}
64
65
	@Override
66
	public boolean equals(final Object obj) {
67
		if (obj == this) {
68
			return true;
69
		} else {
70
			return obj instanceof CustomData customData ? this.tag.equals(customData.tag) : false;
71
		}
72
	}
73
74
	@Override
75
	public int hashCode() {
76
		return this.tag.hashCode();
77
	}
78
79
	@Override
80
	public String toString() {
81
		return this.tag.toString();
82
	}
83
}
84

Decompiled Minecraft 26.3 source of CustomData (net/minecraft/world/item/component/CustomData.java) with Mojang official mappings, stored verbatim on the site.