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

How to make @e look only in an object or mob name? - Different pairs of socks placed accurately

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).




Pictures about "How to make @e look only in an object or mob name?"

How to make @e look only in an object or mob name? - Pair of cotton socks for male clothes
How to make @e look only in an object or mob name? - Crop faceless informal woman with opened notebook
How to make @e look only in an object or mob name? - From above of tool for production of traditional Vietnamese noodles with white dough near plastic container and wicker basket





Name dependent MOB textures in Minecraft 1.16+ (Rename mobs to change the texture)




Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Ryutaro Tsukata, Ryutaro Tsukata, Skylar Kang, Quang Nguyen Vinh