How can I specify a count that's greater than or equal to a given number in a command?



  • I was looking to know if a player (in Minecraft v. 1.16.5) had enough items to execute an action so I wrote the following command:

    /execute at @a[nbt={Inventory:[{id: "minecraft:gold_ingot",Count:3b}]}] run say hello
    

    It works but only when the player has 3 gold and not more so to remedy it I tried :

    /execute at @a[nbt={Inventory:[{id: "minecraft:gold_ingot",Count:3..10b}]}] run say hello" 10 for example
    

    But it just doesn't work. How can I accomplish this?



  • The NBT format only can test for “matching” or “not matching.” There is no concept of numerical comparisons, so you cannot do greater than/less than comparisons with NBT alone.

    If you want to test for a range of numbers, that number will first have to be transferred from the NBT system to the scoreboard system, which does have numerical comparisons:

    execute if score   matches  run …
    

    Depending on your use case, you may be able to further simplify this command by placing the score conditional in a selector:

    execute as @a[scores={=}]
    

    Generically, turning NBT into scores would be done with /execute store result score … run data get …, however, NBT operations are expensive on performance and should be avoided if possible.

    For your case specifically, you can use /clear with a count of 0 to get the number of items, and store that to a score, like so:

    execute store result score   run clear @s … 0
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2