How to use a dummy's scoreboard value inside a command

1.12−

The thing is, although there should be a simple answer for this, there isn't. I haven't been able to find a way to do this, except for doing one check per level, for example, running the following...

/give @p[score_dummy=4,score_dummy_min=4] diamond_sword 1 0 {ench:[{id:16,lvl:1}]}

...rinse and repeat for each level!

This can get very repetitive, especially in older versions, which aren't that good at converting between NBT and scoreboard values.

1.13+

I have a simple solution using clever modification of the /execute command!

Run the following two commands in order:

/give @p diamond_sword{Enchantments:[{id:"minecraft:sharpness",lvl:1s}]}
/execute store result entity @p Inventory[{id:"minecraft:diamond_sword"}].tag.Enchantments[{id:"minecraft:sharpness"}].lvl short 1.0 run scoreboard players get @p dummy

This command will give the player a diamond sword, and then modify it to the correct level of Sharpness.


A way you can do this is having command blocks for each scoreboard level, such as

/give @a[score_dummy_min=1,score_dummy=1] minecraft:diamond_sword 1 0 {ench:[{id:16,lvl:1}]}

/give @a[score_dummy_min=1,score_dummy=2] minecraft:diamond_sword 1 0 {ench:[{id:16,lvl:2}]}

and so on, as there is no way to use scoreboard values as variables in vanilla