Can I turn score into absorption hearts?
This can be done using "AttributeModifiers
" that are changed dynamically using /execute store
. Previously you were able to abuse MC-123307 for this to do it directly in the player's hand, now you need to remove the item and summon a new one, which instantly gets picked up.
Firstly, the commands, which need to be executed in the same tick (for example using a command block line or a function):
kill @e[type=item,nbt={Item:{tag:{a:1}}}]
clear @p stone{a:1}
execute at @p run summon item ~ ~ ~ {Item:{id:"stone",Count:1,tag:{a:1,AttributeModifiers:[{AttributeName:"generic.maxHealth",Amount:0,UUIDLeast:1,UUIDMost:1}]}}}
execute as @p at @s store result entity @e[type=item,nbt={Item:{tag:{a:1}}},sort=nearest,limit=1] Item.tag.AttributeModifiers[0].Amount byte 2 run scoreboard players get @s hearts
I think it's best to explain the third and fourth command first:
- The third command summons an item entity at the player's position. This entity holds a stone item (stone is just arbitrarily chosen, it can be anything). This stone is tagged with my custom and creatively named tag "a", which is set to 1. This is also pretty arbitrary, you just need something that can later be checked for. It also has
AttributeModifiers
, which in theory changes the player's maximum health, but in practice a value of 0 gets ignored here. This value will be changed dynamically in the next command: - The fourth command modifies this item entity by copying the value of the player's "hearts" score into the potency of the item's
maxHealth
AttributeModifier, scaled by a factor of 2 (because health points are half a heart). So if your score was for example 63 (the maximum that works as expected), then it wil be changed toAmount:126
and you will have 126 health points=63 hearts when you pick up and hold the item.
Now the commands are over for this tick, so the player instantly picks up the item (the item entity had no PickupDelay
). When you trigger the commands the next time, the first two commands do the following:
- The first command clears all item entities on the ground that have the custom
a:1
tag, this is just to prevent issues if there are multiple on the ground at once. - The second command clears all items from the player's inventory that are tagged
a:1
, this is to remove the old item to make space for the new one and also to remove possible duplicates, in case there can be some.
Then the third and fourth command are executed again, creating the new item.
Some problems with this system:
- It's currently not perfectly usable for multiplayer, especially if you use it for multiple players in the same tick using command blocks, it will likely mess up. But it should be pretty easy to adjust for multiplayer.
- It's not tamper-proof: If the player for example puts the item into a chest, it will not be detected there. Making this tamper-proof would require probably about 4000 commands, not 4. And it would create a lot of lag.
An alternative system that doesn't have these downsides: Write a single /replaceitem
command for every possible amount of hearts. It only works with up to 63½ hearts anyway, so that's just a bit of effort.