I need to test if the player has 3 or more gold nuggets. I am using the command testfor @a {Inventory:[{id:minecraft:gold_nugget,Count:3b}]}. It activates the comparator I have next to it, but only if they have exactly three. Is there a way to test for 3 or more, without using command blocks for every number? Thanks if you can help! If you can't thanks for trying! If you didn't, thanks for reading...
In order to do this, you must use CommandStats. CommandStats will set a target's score based on the success of the command the initiator has run. CommandStats are stored in either a relevant block (command block, sign), or on an entity such as the player. In order to cause CommandStats to modify a score through an entity, you'll need to use /execute.
Prerequisites:
Objective to hold the CommandStats resulting value.
/scoreboard objectives add ITEMS dummy
CommandStats:
The following will either need to be on a clock or activated a single time. Whichever one fits your system better. The order does not matter.
1. There are a couple different ways you can go about using CommandStats. In this case, I'll show the use of storing individual player scores that will be equal to the number of items that player has. The following CommandStat will set the closest player's (intending to be the initiator of commands, IE the player) ITEMS score equal to the number of 'affected items'.
/stats entity @a set AffectedItems @p ITEMS
2. In order for CommandStats to modify a score, the target must already be tracked on the scoreboard. Easiest way is to 'add 0' to all scores, which does not modify previously tracked scores but will force players who aren't tracked to be tracked with a default of 0.
/scoreboard players add @a ITEMS 0
Inventory Detection
Each time you need to detect the number of items in a player's inventory, you will need to run the following in numerical order.
1. First, all players will remove 0 gold nuggets from their inventory. What this does is preserve their inventory, while their "AffectedItems" CommandStat will receive the number of items that could have been cleared. This will then automatically set the player's score equal to that number.
2. You can then detect the number of items on the player based on their score. The following command will output true if there is a player that has an "ITEMS" score of at least 3, which translates to a player who has at least 3 gold nuggets in their inventory.
/testfor @a[score_ITEMS_min=3]
Keep in mind this also gets around the issue of items spread around their inventory. By only checking for raw NBT data, you're looking for a specific stack of items. If they have 2 items in one slot, and 1 item in another slot, the original /testfor won't return true because it did not find a slot with 3 items. CommandStats gets around that.
Inactive account. See Bio.
In order to do this, you must use CommandStats. CommandStats will set a target's score based on the success of the command the initiator has run. CommandStats are stored in either a relevant block (command block, sign), or on an entity such as the player. In order to cause CommandStats to modify a score through an entity, you'll need to use /execute.
Prerequisites:
Objective to hold the CommandStats resulting value.
CommandStats:
The following will either need to be on a clock or activated a single time. Whichever one fits your system better. The order does not matter.
1. There are a couple different ways you can go about using CommandStats. In this case, I'll show the use of storing individual player scores that will be equal to the number of items that player has. The following CommandStat will set the closest player's (intending to be the initiator of commands, IE the player) ITEMS score equal to the number of 'affected items'.
2. In order for CommandStats to modify a score, the target must already be tracked on the scoreboard. Easiest way is to 'add 0' to all scores, which does not modify previously tracked scores but will force players who aren't tracked to be tracked with a default of 0.
Inventory Detection
Each time you need to detect the number of items in a player's inventory, you will need to run the following in numerical order.
1. First, all players will remove 0 gold nuggets from their inventory. What this does is preserve their inventory, while their "AffectedItems" CommandStat will receive the number of items that could have been cleared. This will then automatically set the player's score equal to that number.
2. You can then detect the number of items on the player based on their score. The following command will output true if there is a player that has an "ITEMS" score of at least 3, which translates to a player who has at least 3 gold nuggets in their inventory.
Keep in mind this also gets around the issue of items spread around their inventory. By only checking for raw NBT data, you're looking for a specific stack of items. If they have 2 items in one slot, and 1 item in another slot, the original /testfor won't return true because it did not find a slot with 3 items. CommandStats gets around that.
Minecraft-things: http://skylinerw.com
More Minecraft-things: https://sourceblock.net
Guides for command-related features (eventually moving to Source Block): https://github.com/skylinerw/guides
I primarily hang out in the /r/MinecraftCommands discord, where there's a lot of people that help with commands: https://discord.gg/QAFXFtZ
Their corresponding subreddit: https://www.reddit.com/r/MinecraftCommands/
Thank you! This helps a ton!
Edit: Is the items subcommand a specific stat, or were you just using that as the name for the objective?
Inactive account. See Bio.
"ITEMS" is just the name of the objective, which you can change if needed. "AffectedItems" is the specific CommandStat and will need to stay as that.
Minecraft-things: http://skylinerw.com
More Minecraft-things: https://sourceblock.net
Guides for command-related features (eventually moving to Source Block): https://github.com/skylinerw/guides
I primarily hang out in the /r/MinecraftCommands discord, where there's a lot of people that help with commands: https://discord.gg/QAFXFtZ
Their corresponding subreddit: https://www.reddit.com/r/MinecraftCommands/
Is there a way of using the same command but for players instead?