The bug
An item modifier that has multiple item functions to modify an item in your mainhand, cannot have the second function depend on the first. They behave as if their effects are buffered until all are finished.
The only way I could get this to work is by splitting the two functions into separate modifier files, and running both with separate commands.
How to reproduce
Save the following item modifier:
[{
"function": "minecraft:set_nbt",
"tag": "{PageContent: '\"Hello World\"'}"
},
{
"function": "minecraft:copy_nbt",
"source": "this",
"ops": [
{
"source": "SelectedItem.tag.PageContent",
"target": "pages",
"op": "append"
}
]
}]
And run these commands:
item replace entity @p weapon.mainhand with minecraft:written_book{pages:[], PageContent:"default"}
item modify entity @p weapon.mainhand namespace:item_modifier
(substituting namespace:item_modifier
for the appropriate location of the file)
Notice that the resulting book contains the text "default" when you open it.
Expected behaviour:
When the first item function modifies the value of the custom NBT field, I was expecting the second item function to be able to copy the resulting value into the page list of the book.
Actual behaviour:
What happens is that the second item function copies the default text set by the parent command, instead of the value that the first item function changed it to.
Comments 2
A potential fix then would be reworking the "source" to allow it to point to the item being modified in question, rather than the entity holding it. Then all functions in the modifier should have access to the (updated) version of the item.
The scope of "this" has always confused me from the beginning. We're in the context of an item, but it refers to the entity. That is not what "this" intuitively should mean here.
Confirmed but probably intended.
The item is not replaced in its slot until all the modifiers are done, so while subsequent modifiers can reference changes to the item, they won't observe any changes to the world (e.g. via
SelectedItem
)