The bug
If the target tag is a non-compound list and none of its elements matches the pattern, a match element node in an NBT path throws java.lang.UnsupportedOperationException
when adding a copy of the pattern to the target tag to create a parent tag.
How to reproduce
/data modify storage mc-179181: list set value [0b]
/data modify storage mc-179181: list[{}].child set value 1b
→ ❌
An unexpected error occurred trying to execute that command
(Trying to add tag of type 10 to list of 1
)
Code analysis
// net.minecraft.commands.arguments.NbtPathArgument.MatchElementNode
public void getOrCreateTag(Tag tag, Supplier<Tag> supplier, List<Tag> list) {
MutableBoolean anyMatch = new MutableBoolean();
if (tag instanceof ListTag) {
ListTag listTag = (ListTag) tag;
listTag.stream().filter(this.predicate).forEach(t -> {
list.add(t);
anyMatch.setTrue();
});
if (anyMatch.isFalse()) {
CompoundTag parent = this.pattern.copy();
// If none of the elements of `listTag` matches `this.predicate`, a compound tag `parent` is added to `listTag`.
listTag.add(parent);
list.add(parent);
}
}
}
// net.minecraft.nbt.ListTag
public void add(int index, Tag tag) {
if (!this.addTag(index, tag)) {
// Since `tag` is a compound and this list is not a compound list in this case, `this.addTag(index, tag)` returns false and `UnsupportedOperationException` is thrown.
throw new UnsupportedOperationException(String.format("Trying to add tag of type %d to list of %d", tag.getId(), this.type));
}
}
Fixed in 25w09a. With support for heterogeneous list tags, this error no longer occurs.