Minecraft 1.13: conditionally apply effect when player holds specific item [duplicate]

Trying to have a levitation effect applied to any player when and only when that player holds a specific item (in this case, a renamed feather). Pre-1.13 it worked using a string of command blocks that tagged a player using scoreboards and applied the effect to that player, but like many of the command blocks in this world, the 1.13 update broke them. I realised that I should be able to use the execute if command, but I cannot seem to get it to work - it seems to either apply it to the player closest to the command block (even when another player is the only holding the item) or it applies it to all players.

Command currently looks like this:

execute if entity @a[nbt={SelectedItem:{id:"minecraft:feather",tag:{display:{Name:"\"Levitator\""}}}}] run effect give @p minecraft:levitation 1 4

I tried modifying that by nesting it with various combinations of execute as and execute at, but it still seemed to have the same issues (it would always execute relative to the command block not the player).

Any ideas on how to get this to work?


Solution 1:

Currently your command says "if anyone is holding the item, apply levitation to the nearest player.". Instead, you can put your first selector in the effect command, so that it applies levitation to the players holding the item:

effect give @a[nbt={SelectedItem:{id:"minecraft:feather",tag:{display:{Name:"\"Levitator\""}}}}] minecraft:levitation 1 4