CopyCustomDataFunction .java

net.minecraft.world.level.storage.loot.functions.CopyCustomDataFunction (class): members: CopyOperation, MergeStrategy, operations, copyData, validate.

net.minecraft.world.level.storage.loot.functionsclass197 linescommonextends LootItemConditionalFunction7.0 KB

API Surface (15)

MapCodec<CopyCustomDataFunction> codec()
void validate(final ValidationContext context)
ItemStack run(final ItemStack itemStack, final LootContext context)
CopyCustomDataFunction.Builder copyData(final NbtProvider source)
CopyCustomDataFunction.Builder copyData(final LootContext.EntityTarget source)
CopyCustomDataFunction.Builder copy(final String sourcePath, final String targetPath, final CopyCustomDataFunction.MergeStrategy mergeStrategy)
CopyCustomDataFunction.Builder copy(final String sourcePath, final String targetPath)
CopyCustomDataFunction.Builder getThis()
LootItemFunction build()
void apply(final Supplier<Tag> target, final Tag source)
void merge(final Tag target, final NbtPathArgument.NbtPath path, final List<Tag> sources)
void merge(final Tag target, final NbtPathArgument.NbtPath path, final List<Tag> sources)
void merge(final Tag target, final NbtPathArgument.NbtPath path, final List<Tag> sources)
void merge(final Tag target, final NbtPathArgument.NbtPath path, List<Tag> sources)
String getSerializedName()

Fields & Constants (3)

MAP_CODEC
source
operations

String literals (5)

"source"
"target"
"replace"
"append"
"merge"

Full Source

CopyCustomDataFunction.java197 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.storage.loot.functions;
2
3
import com.google.common.collect.Iterables;
4
import com.google.common.collect.Lists;
5
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6
import com.mojang.serialization.Codec;
7
import com.mojang.serialization.MapCodec;
8
import com.mojang.serialization.codecs.RecordCodecBuilder;
9
import java.util.List;
10
import java.util.Optional;
11
import java.util.function.Supplier;
12
import net.minecraft.commands.arguments.NbtPathArgument;
13
import net.minecraft.core.Holder;
14
import net.minecraft.core.component.DataComponents;
15
import net.minecraft.nbt.CompoundTag;
16
import net.minecraft.nbt.ListTag;
17
import net.minecraft.nbt.Tag;
18
import net.minecraft.util.StringRepresentable;
19
import net.minecraft.world.item.ItemStack;
20
import net.minecraft.world.item.component.CustomData;
21
import net.minecraft.world.level.storage.loot.LootContext;
22
import net.minecraft.world.level.storage.loot.Validatable;
23
import net.minecraft.world.level.storage.loot.ValidationContext;
24
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
25
import net.minecraft.world.level.storage.loot.providers.nbt.ContextNbtProvider;
26
import net.minecraft.world.level.storage.loot.providers.nbt.NbtProvider;
27
import net.minecraft.world.level.storage.loot.providers.nbt.NbtProviders;
28
import org.apache.commons.lang3.mutable.MutableObject;
29
30
public class CopyCustomDataFunction extends LootItemConditionalFunction {
31
	public static final MapCodec<CopyCustomDataFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
32
		i -> commonFields(i)
33
			.and(
34
				i.group(
35
					NbtProviders.CODEC.fieldOf("source").forGetter(f -> f.source),
36
					CopyCustomDataFunction.CopyOperation.CODEC.listOf().fieldOf("ops").forGetter(f -> f.operations)
37
				)
38
			)
39
			.apply(i, CopyCustomDataFunction::new)
40
	);
41
	private final NbtProvider source;
42
	private final List<CopyCustomDataFunction.CopyOperation> operations;
43
44
	private CopyCustomDataFunction(
45
		final Optional<Holder<LootItemCondition>> condition, final NbtProvider source, final List<CopyCustomDataFunction.CopyOperation> operations
46
	) {
47
		super(condition);
48
		this.source = source;
49
		this.operations = List.copyOf(operations);
50
	}
51
52
	@Override
53
	public MapCodec<CopyCustomDataFunction> codec() {
54
		return MAP_CODEC;
55
	}
56
57
	@Override
58
	public void validate(final ValidationContext context) {
59
		super.validate(context);
60
		Validatable.validate(context, "source", this.source);
61
	}
62
63
	@Override
64
	public ItemStack run(final ItemStack itemStack, final LootContext context) {
65
		Tag sourceTag = this.source.get(context);
66
		if (sourceTag == null) {
67
			return itemStack;
68
		}
69
70
		MutableObject<CompoundTag> result = new MutableObject<>();
71
		Supplier<Tag> lazyTargetCopy = () -> {
72
			if (result.get() == null) {
73
				result.setValue(itemStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag());
74
			}
75
76
			return result.get();
77
		};
78
		this.operations.forEach(op -> op.apply(lazyTargetCopy, sourceTag));
79
		CompoundTag resultTag = result.get();
80
		if (resultTag != null) {
81
			CustomData.set(DataComponents.CUSTOM_DATA, itemStack, resultTag);
82
		}
83
84
		return itemStack;
85
	}
86
87
	@Deprecated
88
	public static CopyCustomDataFunction.Builder copyData(final NbtProvider source) {
89
		return new CopyCustomDataFunction.Builder(source);
90
	}
91
92
	public static CopyCustomDataFunction.Builder copyData(final LootContext.EntityTarget source) {
93
		return new CopyCustomDataFunction.Builder(ContextNbtProvider.forContextEntity(source));
94
	}
95
96
	public static class Builder extends LootItemConditionalFunction.Builder<CopyCustomDataFunction.Builder> {
97
		private final NbtProvider source;
98
		private final List<CopyCustomDataFunction.CopyOperation> ops = Lists.newArrayList();
99
100
		private Builder(final NbtProvider source) {
101
			this.source = source;
102
		}
103
104
		public CopyCustomDataFunction.Builder copy(final String sourcePath, final String targetPath, final CopyCustomDataFunction.MergeStrategy mergeStrategy) {
105
			try {
106
				this.ops.add(new CopyCustomDataFunction.CopyOperation(NbtPathArgument.NbtPath.of(sourcePath), NbtPathArgument.NbtPath.of(targetPath), mergeStrategy));
107
				return this;
108
			} catch (CommandSyntaxException e) {
109
				throw new IllegalArgumentException(e);
110
			}
111
		}
112
113
		public CopyCustomDataFunction.Builder copy(final String sourcePath, final String targetPath) {
114
			return this.copy(sourcePath, targetPath, CopyCustomDataFunction.MergeStrategy.REPLACE);
115
		}
116
117
		protected CopyCustomDataFunction.Builder getThis() {
118
			return this;
119
		}
120
121
		@Override
122
		public LootItemFunction build() {
123
			return new CopyCustomDataFunction(this.getCondition(), this.source, this.ops);
124
		}
125
	}
126
127
	private record CopyOperation(NbtPathArgument.NbtPath sourcePath, NbtPathArgument.NbtPath targetPath, CopyCustomDataFunction.MergeStrategy op) {
128
		public static final Codec<CopyCustomDataFunction.CopyOperation> CODEC = RecordCodecBuilder.create(
129
			i -> i.group(
130
					NbtPathArgument.NbtPath.CODEC.fieldOf("source").forGetter(CopyCustomDataFunction.CopyOperation::sourcePath),
131
					NbtPathArgument.NbtPath.CODEC.fieldOf("target").forGetter(CopyCustomDataFunction.CopyOperation::targetPath),
132
					CopyCustomDataFunction.MergeStrategy.CODEC.fieldOf("op").forGetter(CopyCustomDataFunction.CopyOperation::op)
133
				)
134
				.apply(i, CopyCustomDataFunction.CopyOperation::new)
135
		);
136
137
		public void apply(final Supplier<Tag> target, final Tag source) {
138
			try {
139
				List<Tag> sourceTags = this.sourcePath.get(source);
140
				if (!sourceTags.isEmpty()) {
141
					this.op.merge(target.get(), this.targetPath, sourceTags);
142
				}
143
			} catch (CommandSyntaxException var4) {
144
			}
145
		}
146
	}
147
148
	public enum MergeStrategy implements StringRepresentable {
149
		REPLACE("replace") {
150
			@Override
151
			public void merge(final Tag target, final NbtPathArgument.NbtPath path, final List<Tag> sources) throws CommandSyntaxException {
152
				path.set(target, Iterables.getLast(sources));
153
			}
154
		},
155
		APPEND("append") {
156
			@Override
157
			public void merge(final Tag target, final NbtPathArgument.NbtPath path, final List<Tag> sources) throws CommandSyntaxException {
158
				List<Tag> targets = path.getOrCreate(target, ListTag::new);
159
				targets.forEach(tag -> {
160
					if (tag instanceof ListTag listTag) {
161
						sources.forEach(source -> listTag.add(source.copy()));
162
					}
163
				});
164
			}
165
		},
166
		MERGE("merge") {
167
			@Override
168
			public void merge(final Tag target, final NbtPathArgument.NbtPath path, final List<Tag> sources) throws CommandSyntaxException {
169
				List<Tag> targets = path.getOrCreate(target, CompoundTag::new);
170
				targets.forEach(tag -> {
171
					if (tag instanceof CompoundTag compoundTag) {
172
						sources.forEach(source -> {
173
							if (source instanceof CompoundTag sourceTag) {
174
								compoundTag.merge(sourceTag);
175
							}
176
						});
177
					}
178
				});
179
			}
180
		};
181
182
		public static final Codec<CopyCustomDataFunction.MergeStrategy> CODEC = StringRepresentable.fromEnum(CopyCustomDataFunction.MergeStrategy::values);
183
		private final String name;
184
185
		public abstract void merge(final Tag target, final NbtPathArgument.NbtPath path, List<Tag> sources) throws CommandSyntaxException;
186
187
		MergeStrategy(final String name) {
188
			this.name = name;
189
		}
190
191
		@Override
192
		public String getSerializedName() {
193
			return this.name;
194
		}
195
	}
196
}
197

Decompiled Minecraft 26.3 source of CopyCustomDataFunction (net/minecraft/world/level/storage/loot/functions/CopyCustomDataFunction.java) with Mojang official mappings, stored verbatim on the site.