NBT Attribute Modifiers Legacy - ShaneBeee/SkBee GitHub Wiki

[!WARNING] This wiki currently relates to Minecraft 1.20.4 and below

Attribute modifiers allow you to add instructions to an item, which will modify the attributes of an entity.
You can read more about attributes and modifiers on McWiki

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

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""}"

(see examples)

Simple Method

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

set {_i} to iron chestplate
add nbt compound of "{AttributeModifiers:[{AttributeName:""generic.max_health"",Amount:10,Operation:0,Slot:chest,UUID:[I;-1201124,10148,125632,-20296],Name:""generic.max_health""}]}" to nbt compound of {_i}
give player 1 of {_i} 

Let's break it down.

First we create a new item:

set {_i} to iron chestplate

I hope for your sake you know what this means.

Now we add the attribute modifiers to our NBT:

add nbt compound of "{AttributeModifiers:[{AttributeName:""generic.max_health"",Amount:10,Operation:0,Slot:chest,UUID:[I;-1201124,10148,125632,-20296],Name:""generic.max_health""}]}" to nbt compound 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 nbt compound of {_i}
	set {_a::*} to compound list tag "AttributeModifiers" of {_n}
	add nbt compound of "{AttributeName:""generic.max_health"",Amount:10,Operation:0,Slot:chest,UUID:[I;-1201124,10148,125632,-20296],Name:""generic.max_health""}" to {_a::*}
	set compound list tag "AttributeModifiers" 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:

set {_n} to nbt compound of {_i}

With me so far? Good!

Let's get our attribute modifiers

set {_a::*} to compound list tag "AttributeModifiers" 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 compound of "{AttributeName:""generic.max_health"",Amount:10,Operation:0,Slot:chest,UUID:[I;-1201124,10148,125632,-20296],Name:""generic.max_health""}" 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 "AttributeModifiers" 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.