Summon object and events while throwing a snowball
Solution 1:
Using the execute command is the correct way to do this, but you can not use nbt data in the execute command.
From the command wiki the execute command syntax:
execute <entity> <x> <y> <z> <command …>
There is no argument for data tag information with the execute command. This is why you got the error that stated {display:{Name:example}}
is not a number. Look at testfor to see a command with the data tag argument. You will have to use a different method of targeting the entity within the execute command.
This solution uses four command blocks. These command blocks need to be in loaded chunks in order to function properly. If placed in the spawn chunks, they will always remain loaded.
I also recommend you silence command block output to prevent chat spam. Use command:
/gamerule commandBlockOutput false
The first command block adds the tag HoldingMoo
to the player holding a snowball named Moo
. It is Repeat Unconditional Always Active and its command is:
scoreboard players tag @a add HoldingMoo {SelectedItem:{id:"minecraft:snowball",tag:{display:{Name:"Moo"}}}}
The second command block adds the tag Moo
to any snowball within a radius of 3 from a player tagged HoldingMoo
. It is Chain Conditional Always Active and its command is:
execute @a[tag=HoldingMoo] ~ ~ ~ scoreboard players tag @e[type=snowball,r=3] add Moo
The third command block removes the tag HoldingMoo
from all players. This is for when a player is no longer holding the named snowball. It is Chain Unconditional Always Active and its command is:
scoreboard players tag @a remove HoldingMoo
The final command block is not apart of the chain. It is where you run the summon command. It is Repeat Unconditional Always Active and its command is:
execute @e[tag=Moo] ~ ~ ~ summon cow ~ ~10 ~
You could build a second copy of the same setup with a different set of tags for a different named snowball to do different things. I built a second set up using the tags HoldingBolt
and Bolt
to summon lightning. Here is the command I ran in the last command block:
execute @e[tag=Bolt] ~ ~ ~ summon lightning_bolt ~ ~ ~
Here is a picture of the cow carnage:
Here is a picture of the lightning snowball:
This solution is simple and will work for most needs but is not perfect. If a player were holding a snowball named Moo
and another player threw a snowball at the first player, it might summon a few cows.
If you would like to be more specific, you can create an objective using useitem
which will add one to the score every time a player throws a snowball. This will only tag a snowball within the radius of a player who is both holding a custom named snowball and has thrown a snowball.
Create the objective manually by typing this command into chat:
/scoreboard objectives add Thrown stat.useItem.minecraft.snowball
Then be more specific with the target selector by changing the first command block command. This will now only add the tag HoldingMoo
to a player who is holding a snowball named Moo
and has thrown a snowball. The modified command:
scoreboard players tag @a[score_Thrown_min=1] add HoldingMoo {SelectedItem:{id:"minecraft:snowball",tag:{display:{Name:"Moo"}}}}
This method will require one more command block on the chain, to make it four long. This extra command resets the score tracking how many snowballs each player has thrown. This command block is Chain Unconditional Always Active and its command is:
scoreboard players reset @a Thrown