Skip to main content
replaced https://minecraft.fandom.com/wiki/ with https://minecraft.wiki/w/
Source Link

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 pageMinecraft Wiki page.

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.

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.

added 24 characters in body
Source Link
One 2 Many
  • 12.8k
  • 9
  • 52
  • 91

1Solution for versions 1.17+ Update17 and newer

1.17+ Update

Solution for versions 1.17 and newer

Large update; added a lot more explanations and added mention to item modifiers.
Source Link
One 2 Many
  • 12.8k
  • 9
  • 52
  • 91

As of 1.17, a loot table is no longer required, butthere are two ways to modify items while they are inside of the shulker box still isplayer's inventory.

Preparation

Item modifiers

You'll need to first place down a yellow shulker box, and place a temporary item1.17 introduced (like a piece of dirt)item modifiers, data pack files that can be invoked upon items in slot 0 (upper-left corner).

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

The standard location for this shulker box This is (-30000000, 0the easiest way to modify item properties, 1602)whether they are within the player's inventory or not.

Commands

With 1.17 out, you can use /item to copy theHere is a sample item tomodifier for the shulker box.example in the question post:

item{
 replace block"function": -30000000"minecraft:set_attributes",
 0 1602"modifiers": container.0[
 from entity @s weapon.mainhand{

Then, you can modify your item as much as you want inside the shulker box using any commands you like. For example:

data modify block (x) (y) (z) Items[0]"attribute": merge"minecraft:generic.attack_damage",
 value {tag:{Enchantments:[{id    "name":"minecraft "",
      "amount":sharpness" 5, 
 lvl     "operation":1s}]}} "addition",
execute store result block (x) (y) (z)"id": Items[0].tag.Enchantments[0].lvl"fffe27a9-0000-04d5-0002-50b6fffff656",
 short 1.0 run scoreboard players get"slot": FakePlayer"mainhand"
 myObjective   }
  ]
}

Finally, replaceItem modifiers are placed at the folder /data/<namespace>/item_modifiers/<modifier_name>.json. It can then be invoked upon any item withslot in any container block or entity's inventory, using /item like so:

item replacemodify entity @s weapon.mainhand from<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 -30000000<block> 0<slot> 1602from container.0entity <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.

  1. item replace block -30000000 0 1602 container.0 from entity @s weapon.mainhand
    
  2. data modify block (x) (y) (z) Items[0] merge value {tag:{Enchantments:[{id:"minecraft:sharpness", lvl:1s}]}}
    
  3. execute store result block (x) (y) (z) Items[0].tag.Enchantments[0].lvl short 1.0 run scoreboard players get @s sharpnesslvl
    
  4. item replace entity @s weapon.mainhand from block -30000000 0 1602 container.0
    

Explanation

  1. Copies the item from the player's main hand to the shulker box.
  2. 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.
  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.
  4. This is the reverse of command #1, to copy the modified item and overwrite the old item in the mainhand.

As of 1.17, a loot table is no longer required, but the shulker box still is.

Preparation

You'll need to first place down a yellow 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].

The standard location for this shulker box is (-30000000, 0, 1602).

Commands

With 1.17 out, you can use /item to copy the item to the shulker box.

item replace block -30000000 0 1602 container.0 from entity @s weapon.mainhand

Then, you can modify your item as much as you want inside the shulker box using any commands you like. For example:

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

Finally, replace the item with /item:

item replace entity @s weapon.mainhand from block -30000000 0 1602 container.0

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.

  1. item replace block -30000000 0 1602 container.0 from entity @s weapon.mainhand
    
  2. data modify block (x) (y) (z) Items[0] merge value {tag:{Enchantments:[{id:"minecraft:sharpness", lvl:1s}]}}
    
  3. execute store result block (x) (y) (z) Items[0].tag.Enchantments[0].lvl short 1.0 run scoreboard players get @s sharpnesslvl
    
  4. item replace entity @s weapon.mainhand from block -30000000 0 1602 container.0
    

Explanation

  1. Copies the item from the player's main hand to the shulker box.
  2. 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.
  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.
  4. This is the reverse of command #1, to copy the modified item and overwrite the old item in the mainhand.
Source Link
One 2 Many
  • 12.8k
  • 9
  • 52
  • 91
Loading