Minecraft – How to make @e look only in an object or mob name

minecraft-commandsminecraft-java-edition

I am trying to do a command on when you throw a snowball named "Lightning", it spawns lightning bolt at it every second. I ran into a slight problem which I cannot solve! Here is the command:

/execute @e[type=Snowball,name=Lightning] ~ ~ ~ /summon LightningBolt

Best Answer

I assume by "a snowball named "Lightning"" you mean that the item is named "Lightning" (like if you renamed it with an anvil).

Unfortunately, this information does not pass over to the thrown snowball entity (the item name doesn't become the entity name, for example), so what you want is a bit more complicated to achieve.

You should create a new objective to keep track of when a player throws a snowball:

/scoreboard objectives add ThrownSnowball stat.useItem.minecraft.snowball

Then, on a clock, run the following commands in this order:

/execute @a[tag=HoldingLightning,score_ThrownSnowball_min=1] ~ ~ ~ /scoreboard players tag @e[type=Snowball,c=1] add LightningBall
/scoreboard players set @a[score_ThrownSnowball_min=1] ThrownSnowball 0
/execute @e[type=Snowball,tag=LightningBall] ~ ~ ~ /summon LightningBolt
/scoreboard players tag @a[tag=HoldingLightning] remove HoldingLightning
/scoreboard players tag @a add HoldingLightning {SelectedItem:{id:"minecraft:snowball",tag:{display:{Name:"Lightning"}}}}

This should tag all players holding the snowball renamed to "Lightning" with HoldingLightning. If that player then has a ThrownSnowball score of at least one (meaning they've thrown a snowball in this tick, as this score resets at the end of the commands), they give the closest snowball the LightningBall tag. You can then use @e[type=Snowball,tag=LightningBall] to run the summon command as often as you want (you can put the summon command on a separate, slower clock).