4

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?

5
  • I dont know about Minecraft programmation, but greater than or equal in normal programmation is >=. If the option is not available, is there something to stop you writing in the quantity -1 and just using greater than?
    – Fredy31
    Aug 29, 2022 at 14:33
  • I could have sworn that we have a question covering this already, but I can't find it. I'll give you the answer, but we can certainly explore redirecting/merging the questions if we find one.
    – One 2 Many
    Aug 29, 2022 at 16:02
  • The title should be changed to greater than 3 items nbt or something
    – Aceplante
    Aug 29, 2022 at 18:57
  • @Fredy31 this is the correct way to do this normally, but it doesn't work within NBT target selectors.
    – MBraedley
    Aug 30, 2022 at 1:20
  • 3

2 Answers 2

5

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 <scoreholder> <objective> matches <range> 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={<objective>=<range>}]

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 <scoreholder> <objective> run clear @s … 0
4
  • "however, NBT operations are expensive on performance and should be avoided if possible" -> any evidence for this?
    – pppery
    Aug 29, 2022 at 18:03
  • @pppery NBT operations with commands are among the most expensive operations to perform. This is because the game needs to save the entity's memory state into NBT, modify it, and merge the modified NBT back into the entity's memory object. This process takes time, just like it takes time to save and load Minecraft worlds, albeit this on a much smaller scale. Nevertheless these operations are best avoided wherever possible, and minimized where not possible.
    – One 2 Many
    Aug 29, 2022 at 18:26
  • 1
    Personally, I prefer using selectors for simple scoreboard matches. I find it much easier to read than an execute command.
    – MBraedley
    Aug 30, 2022 at 1:27
  • @MBraedley You are certainly right about the selectors. The command here can certainly be simplified, and I'll do that now.
    – One 2 Many
    Aug 30, 2022 at 3:39
2

Unfortunately, greater than/equal to etc can only be used to check scores and does not work with NBT. If you want to check all potential stack size values from 3-10 for example, that would likely require 7 commands.

Not the answer you're looking for? Browse other questions tagged .