For I minecraft map I'm making I want to test whether or not a player has a specific potion in their inventory. How would one do this in 1.13+?
1 Answer
Using
/data get entity @s
on oneself while having a potion on you would reveal the Inventory NBT data you need for this command. Combing this with the answer for 1.13- given here:
How to test for specific potions in inventories?
the command would be something along the lines of:
execute if entity @p[nbt={Inventory:[{id:"minecraft:potion",tag:{Potion:"minecraft:mundane"}}]}] run say hi
where 'mundane' can be changed into the name of the potion wanted, for example: awkward, water or regeneration.
By replacing 'potion' into 'splash_potion' or 'lingering_potion' all other types of potions can be reached.
The normal extend potions can be searched for by changing 'mundane' to, for example, 'long_swiftness', level II potions can be searched for by changing 'mundane' to, for example, 'strong_swiftness'.
Custom potions are more difficult to detect, and can be detected using the following command:
\execute if entity @p[nbt={Inventory:[{id:"minecraft:potion",tag:{CustomPotionEffects:[{Id:1,Amplifier:2,Duration:1400}]}}]}]
where the number after Id represents the numerical ID of the potion effect as can be found on the wiki, the amplifier is the level of the potion effect, where level 0 is a normal potion effect, and the duration is the duration in game ticks (= 20 x length in seconds). All three of these identifiers are optional and if not set will default to be true for any potion.
If multiple effects need to be detected for some reason, replace the
CustomPotionEffects:[{...}] with CustomPotionEffects:[{...},{...}]
where every {...} has one or more of the Id,Amplifier,Duration tags, as above.
/testfor
no longer exists.