#Checking inventory of players for items.
1 messages · Page 1 of 1 (latest)
<@&935561184806060073>
@shut fable
can you send us more information? which items, with which nbt, are slots important?
You can easily check if the item is anywhere in the players inventory using
execute store result … run clear @s <item>{<nbt>} 0
Because it is a clear 0 it won't use the item :)
How would I execute a command after running checking the players inventory?
execute store result score <score> <board> run clear @s <item>{<nbt>} 0
execute if score <score> <board> matches 6 run say I have exactly 6 of that exact item
execute if score <score> <board> matches 1.. run tellraw @a {"text":"I have exactly ","extra":[{"score":{"objective":"<board>","target":<score>}},{"text":" of that exact item"}]}
i should say you can also do, for example, “matches ..6” instead of “matches 6” to check for 6 or less, or something like 3..6 to check for 3 to six
and of course 6.. for six or more
like he did 1.. to check if the player has at least one of that item
If you already have the scoreboard stuff, ignore this.
You can also check your inventory with advancements.
btw for items around the player you can just do either
“execute as @a at @s as @e[type=Item,nbt={Item:{<use data get on an item entity for this nbt structure>}},distance=<range around the player, e.g. 0..3 for 0-3 blocks away>] run -” to run a command from the pov of each item within range of a player (not recommended, would be lag intensive since you do an @e from @a effectively multiplying the targets needed)
or
“execute as @e[type=item,nbt={Item:{}}] at @s if entity @p[distance=0..3] run -” (much more recommended, since it doesnt need to multiply the @, however this will only work if you are checking the same items and conditions for every player)
however you still have to be careful either way with the amount of @e target selectors you use, especially when nbt is involved and it’s something youd likely need to put in the tick function, because checking the nbt data of every entity every tick is simply not very optimized. So, i would suggest only doing it periodically if that would work for your needs, else consolidating all of your @e usages into a single function (e.g. “execute as @e run namespace:function” and then shove all your @e things into that function, but replace @e with @s, so it only needs to make a list of entities once rather than constantly)
tbh tho if its your only or one of your only @e usages in the tick function then dont mind my obsessive optimization tips
@a + @e in the same command... 😖
yeah
hence why i warned against that
but if you need specific items to be instantly deleted from every player’s vicinity, different items for different players, that kind of command is probably the only way to effectively do so (though there are slight optimizations you could make like replacing the nbts with tags that are periodically added to the right players/items, etc)
tysm guys