How to run a command (using /execute) based on a player having 7 or more xp points in Minecraft?
I'm trying to build a datapack that lets you craft xp bottles from glass bottles at the expense of 7 xp per bottle (because they drop 3 to 11 xp so the average is 7). I have almost everything working except for a check for if they have too little xp. If they have 7 or less xp, I would like to remove one bottle of enchanting from their inventory and give them one glass bottle, because the function for removing xp is called after they've crafted one and a scoreboard has counted it. My remove function currently looks like this:
#create dummy scoreboard to multiply bottles crafted by seven to get xp to be removed
scoreboard objectives add 7xp dummy
scoreboard players set @s 7xp -7
scoreboard players operation @s xpCrafted *= @s 7xp
#how do I get the value in xpCrafted into the command to remove xp?
xp add @s -7 points
#cleanup
scoreboard players reset xpCrafted
scoreboard players reset 7xp
scoreboard players set @e xpCrafted 0
scoreboard objectives remove 7xp
And my tick function like this:
execute as @e[scores={xpCrafted=1..}] at @s run function craft_xp:removexp
As the inline comment says, how do I get that value in xpCrafted into my xp removing command, and also how should I check to make sure the player has enough xp for the operation in the tick command (or elsewhere)? Presumably I would run a different command to remove the extra bottle if they didn't have enough xp, but I don't know how because there doesn't seem to be any type of if/else structure in commands. Finally, should a question like this be in two different questions or is one ok on this forum?
There is an xp
scoreboard type:
/scoreboard objectives add scoreboardName xp {"text":"scoreboardDisplayName"}
For XP points that already existed before you set up the scoreboard, you can use the subcommand of the /xp
command called "query
", with it you can get the current level and the points since the last level:
/execute store result score @s <scoreboard> run xp query @s points
Additionally, you'll probably also track people with 1 or more complete XP levels, like this: @a[xp=1..]