Skip to content

NBT Attribute Modifiers 1.20.5

Shane Bee edited this page Apr 26, 2024 · 12 revisions

Warning

This wiki relates to Minecraft 1.20.5+

Attribute modifiers allow you to add instructions to an item, which will modify the attributes of an entity.
You can read more about attributes on McWiki as well as the attribute modifier component format on McWiki

If you need help making attribute modifiers check out GamerGeeks Generator
(click "show advanced options" then "attributes")

Warning

Due to changes in Minecraft's NBT components and the give command... the output from the above link will need a bit of re-working to be used in an NBT compound.
See examples below to compare and contrast.

In this tutorial, we will modify the player's max health for example.
I will provide both a Simple Method and Advanced Method, depending on what you are more comfortable with.

IMPORTANT NOTE:

Don't forget to double up those inner quotes, ex:

- "{someKey:"some value in double quotes"}"
+ "{someKey:""some value in double quotes""}"

(see examples)

Simple Method:

This is a super simple way of adding a modifier (or 2 or 3) to an item

set {_i} to 1 of diamond sword
add nbt of "{""minecraft:attribute_modifiers"":{modifiers:[{type:""generic.attack_damage"",amount:20,slot:mainhand,name:""generic.attack_damage"",uuid:[I;-124325,2370,23656,-4740],operation:add_value}]}}" to components nbt of {_i}
give player 1 of {_i} 

Let's break it down.

First we create a new item:

set {_i} to 1 of diamond sword

I hope for your sake you know what this means.

Now we add the attribute modifiers to our NBT:
(we use the components nbt expression to make sure we're not just grabbing the custom data component)

add nbt of "{""minecraft:attribute_modifiers"":{modifiers:[{type:""generic.attack_damage"",amount:20,slot:mainhand,name:""generic.attack_damage"",uuid:[I;-124325,2370,23656,-4740],operation:add_value}]}}" to components nbt of {_i}

If the item already has some attribute modifiers, this I believe will override, and you will most likely want to see the advanced method below for adding.
This may seem daunting but it's actually easy, you can use a website like This one to create your attribute modifiers.

Now that we've added the modifier to our NBT of the item, let's give the item to the player:

give player 1 of {_i} 

While the player is wearing this chestplate, their max health will go up a bit, and when they remove it, their max health will go back to default.

Advanced Method

This is a simple function which will give us an iron chest plate, which will add 10 to the player's max health:

function healthPlate() :: itemtype:
	set {_i} to 1 of iron chestplate
	set {_n} to components nbt of {_i}
	set {_a::*} to compound list tag "minecraft:attribute_modifiers;modifiers" of {_n}
	add nbt of "{type:""generic.max_health"",amount:15,slot:chest,name:""generic.max_health"",uuid:[I;-124325,16844,232117,-33688],operation:add_value}" to {_a::*}
	set compound list tag "minecraft:attribute_modifiers;modifiers" of {_n} to {_a::*}
	return {_i}

Let's break it down.

This here just sets a variable to a new item. If you are stuck here, god help you!

set {_i} to 1 of iron chestplate

Now we need to get the NBT of that item:
(Here we make sure to use the components nbt expression to ensure we have access to components and not just the "minecraft:custom_data" component)

set {_n} to components nbt of {_i}

With me so far? Good!

Let's get our attribute modifiers

set {_a::*} to compound list tag "minecraft:attribute_modifiers;modifiers" of {_n}

Here we're just creating a list of the attribute modifiers currently on the tool.
Since this is a new tool, it's actually just going to be an empty list, but in case you are adding to an already defined tool, let's just grab it.

Now let's add a modifier:

add nbt of "{type:""generic.max_health"",amount:10,slot:chest,name:""generic.max_health"",uuid:[I;-124325,16844,232117,-33688],operation:add_value}" to {_a::*}

This may seem daunting but it's actually easy, you can use a website like This one to create your attribute modifiers.
Now all we are doing is adding this modifier to our list.

Now we just want to set that list back to our NBT:

set compound list tag "minecraft:attribute_modifiers;modifiers" of {_n} to {_a::*}

This just set the attribute modifiers tag in our NBT to the list we created.

Last but not least:

return {_i}

We're just returning the item with the modified NBT.

Give this item to the player, and while the player is wearing this chest plate, their max health will go up a bit, and when they remove it, their max health will go back to default.