TransWikia.com

How can I edit NBT data of items in a player's inventory?

Arqade Asked on February 25, 2021

As the title says, I’m wondering about whether it is possible to change part of an NBT or add NBT to an item that a player is holding. My goal is to make custom enchants. For example when a player uses an item to enchant a sword they get the score "EnchantMight" 5, which would cause the next weapon they hold to get "Might V". This, in reality, would just be +5 additive damage in the NBT. I know /replaceitem would be an option, but that only works if the new item is predetermined.

I hoped

/data modify entity @p {SelectedItem:[{AttributeModifiers:[{AttributeName:"generic.attack_damage",Amount:5,Operation:0,UUID:[I;-120919,1237,151734,-2474],Name:"generic.attack_damage"}]}]} insert/append/merge

would work, but obviously it didn’t.

If anyone even just has an idea I can try out, it’d be of help.
If you give a datapack related answer, please explain clearly as I don’t know a lot about it.

One Answer

An interesting workaround to change NBT of a custom item is to copy it to a chest, modify it in the chest (where it can be modified) and then use /loot in replace mode to replace the item slot.

Note: Once 1.17 fully comes out, you can replace this method with the /item command.

Outside preparation

You'll need a special loot table to make this happen. Click this link to access a data pack with the loot table that you'll need.

This loot table will cause a shulker box to drop its contents instead of itself when broken with a tool with the custom tag {dropContents:1b}.

Minecraft preparation

You'll need to first place down a shulker box, and place a temporary item (like a piece of dirt) in slot 0 (upper-left corner).

Important: Ensure the shulker box is otherwise empty, and always write to it with the NBT path Items[0].

Commands

The first step is to copy the item to the shulker box:

data modify block (x) (y) (z) Items[0] merge from entity (selector) SelectedItem

(Note, if your copy source includes the Slot tag, you'll need to use three separate commands to copy ID, Count, and tag separately.)

The second step is to modify that item, use whatever commands you want here to reference the shulker box. Here are some sample commands. Try these ones out for your first try. These commands will set the Sharpness enchantment level to the scoreboard value you specify.

data modify block (x) (y) (z) Items[0] merge value {tag:{Enchantments:[{id:"minecraft:sharpness", lvl:1s}]}}
execute store result block (x) (y) (z) Items[0].tag.Enchantments[0].lvl short 1.0 run scoreboard players get FakePlayer myObjective

Once the modification is complete, here's the magic: use /loot to replace the item in the specified slot.

/loot replace entity (selector) weapon.mainhand 1 mine block (x) (y) (z) stick{dropContents:1b}

Answered by randomuser922 on February 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP