Solution for versions 1.17 and newer
As of 1.17, there are two ways to modify items while they are inside of the player's inventory.
Item modifiers
1.17 introduced item modifiers, data pack files that can be invoked upon items in the inventory to modify their properties. This is the easiest way to modify item properties, whether they are within the player's inventory or not.
Here is a sample item modifier for the example in the question post:
{
"function": "minecraft:set_attributes",
"modifiers": [
{
"attribute": "minecraft:generic.attack_damage",
"name": "",
"amount": 5,
"operation": "addition",
"id": "fffe27a9-0000-04d5-0002-50b6fffff656",
"slot": "mainhand"
}
]
}
Item modifiers are placed at the folder /data/<namespace>/item_modifiers/<modifier_name>.json
. It can then be invoked upon any item slot in any container block or entity's inventory, using /item
like so:
item modify entity @s weapon.mainhand <modifier_reference>
where <modifier_reference>
is your reference to the data pack file containing your item modifier's data. For example, if your item modifier's location is /data/special_enchants/item_modifiers/add_attributes.json
, then your modifier reference would be special_enchants:add_attributes
.
You can generate item modifier files using a generator such as misode.github.io, and you can read more about these changes on the relevant Minecraft Wiki page.
Commands only
With commands and command blocks only, /item
still allows us to cut down on the number of commands we run. However, we cannot take advantage of the performance benefit of item modifiers, given that /data
(the only other alternative) has a high impact on performance and is considered an expensive operation. This is why data packs are recommended for larger projects.
This method uses a temporary slot to store items while they are being modified. Usually, the standard location for this purpose is a yellow shulker box placed at the coordinates (-30000000, 0, 1602).
To copy an item from the player's main hand to an inventory slot of another block or entity, one of the following commands can be used:
item replace block <block> <slot> from entity <player> <slot>
item replace entity <entity> <slot> from entity <player> <slot>
The left side block or entity and slot refers to the location where the item(s) will be copied to, and the right hand side refers to where the item(s) are coming from. Therefore, the command can be reversed to copy the item(s) back to the player.
Example
The following example adds the Sharpness enchantment to the item in the player's main hand, and sets the level of this enchantment to one of the player's score values.
-
item replace block -30000000 0 1602 container.0 from entity @s weapon.mainhand
-
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 @s sharpnesslvl
-
item replace entity @s weapon.mainhand from block -30000000 0 1602 container.0
Explanation
- Copies the item from the player's main hand to the shulker box.
- Accesses the item's data, and adds a temporary enchantment of Sharpness. Currently it has level I, but this will be changed by command #3.
- This command runs a
/scoreboard players get
to get the player's sharpnesslvl
score. Whatever number is retrieved gets set as the level on the enchantment as per the /execute store
part.
- This is the reverse of command #1, to copy the modified item and overwrite the old item in the mainhand.