How to give a certain mob levitation if it is in a 6-block radius?
First, create a new scoreboard objective:
/scoreboard objectives add holdingItem dummy
The following commands should be placed in Repeat Always Active comand blocks:
-
To track players holding your preferred item (I'll assume it's a diamond) you need to give all players holding a diamond a
holdingItem
score of one:/scoreboard players set @a holdingItem 1 {SelectedItem:{id:"minecraft:diamond"}}
-
You'll need to reset the score every tick, so the Creeper doesn't go flying every time you get close:
/scoreboard players set @a holdingItem 0
-
Now, all you have to do is execute the command for all creepers around any player with a
holdingItem
score of 1:/execute @a[score_holdingItem_min=1] ~ ~ ~ /effect @e[type=Creeper,r=6] minecraft:levitation 1
TL;DR Edit: One-command version
Copy-paste this simplified command into a regular impulse command block and run:
/summon minecraft:falling_block ~ ~1 ~ {Block:command_block,Time:1,TileEntityData:{Command:/scoreboard objectives add holdingItem dummy},Passengers:[{id:minecraft:falling_block,Block:repeating_command_block,Time:1,TileEntityData:{Command:/scoreboard players set @a holdingItem 1 {SelectedItem:{id:"minecraft:diamond"}}},Passengers:[{id:minecraft:falling_block,Block:repeating_command_block,Time:1,TileEntityData:{Command:/execute @a[score_holdingItem_min=1] ~ ~ ~ /effect @e[type=Creeper,r=6] minecraft:levitation 1},Passengers:[{id:minecraft:falling_block,Block:repeating_command_block,Time:1,TileEntityData:{Command:/scoreboard players set @a holdingItem 0},Passengers:[{id:minecraft:falling_block,Block:command_block,Time:1,TileEntityData:{Command:/fill ~ ~-5 ~-1 ~ ~1 ~-1 redstone_block},Passengers:[{id:minecraft:falling_block,Block:redstone_block,Time:1}]}]}]}]}]}
It will throw a few command blocks above it (one impulse, then 3 repeat, then 1 impulse). To stop it, destroy all the command blocks.