mojira.dev
MC-179181

Match element nodes in NBT paths throw an unexpected UnsupportedOperationException against non-compound lists

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

  1. /data modify storage mc-179181: list set value [0b]
  2. /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));
    }
}

Comments 1

Fixed in 25w09a. With support for heterogeneous list tags, this error no longer occurs.

intsuc

(Unassigned)

Confirmed

Commands

nbt

1.15.2, 20w16a, 20w17a, 20w18a, 20w20b, ..., 23w46a, 1.21.1, 24w33a, 1.21.4, 25w04a

Retrieved