The bug
While all elements of a list tag are being inserted into all elements of the same list tag, the source list tag is modified.
How to reproduce
data modify storage mc-221421: list set value [[], []]
data modify storage mc-221421: list[] append from storage mc-221421: list[]
data get storage mc-221421: list
Expected behavior
Storage mc-221421: has the following contents: [[[], []], [[], []]]
The tags
[], []
are inserted into each tag.
Actual behavior
Storage mc-221421: has the following contents: [[[], []], [[[], []], [[[], []]]]]
The tags
[], []
are inserted into the first tag.The modified first tag is inserted into the second tag.
The modified second tag is inserted into the second tag.
Code analysis
net.minecraft.server.commands.data.DataCommands.java
private static int insertAtIndex(int index, CompoundTag tag, NbtPath path, List<Tag> sources) throws CommandSyntaxException {
var targets = path.getOrCreate(tag, ListTag::new);
var result = 0;
for (var target : targets) {
if (!(target instanceof CollectionTag)) {
throw ERROR_EXPECTED_LIST.create(target);
}
var success = false;
var collection = (CollectionTag<?>) target;
var cursor = index < 0 ? collection.size() + index + 1 : index;
for (var source : sources) {
try {
if (collection.addTag(cursor, source.copy() /* Copying should be done in batches outside of the sources iteration. */)) {
++cursor;
success = true;
}
} catch (IndexOutOfBoundsException e) {
throw ERROR_INVALID_INDEX.create(cursor);
}
}
result += success ? 1 : 0;
}
return result;
}
Fixed in 1.19.3 Pre-Release 3. The expected behavior is now observed.