How would I make something spawn when and where a snowball hits something? [duplicate]

I know how to make things spawn as they follow an object, but is there any way to make them spawn once lets say a snowball hits its target?

I am working on a way to make snowballs that spawn tnt upon hitting their target.


I have found a solution that works relatively well. The main issue is that when a snowball lands, the entity disappears immediately. To counteract this, we can use an armor stand marker to track the last known location of the snowball. This is not exactly where the snowball hits, but close enough for practical purposes.

First, you need to set up a scoreboard objective to track the snowball's lifetime, and another to detect a lonely armorstand.

/scoreboard objectives add snowballTime dummy
/scoreboard objectives add hasSnowball dummy

Now, create a fill clock (or use 1.9's repeat/chain command blocks), and run the following commands:

/scoreboard players add @e[type=Snowball] snowballTime 1

/execute @e[type=Snowball,score_snowballTime=1] ~ ~ ~ summon ArmorStand ~ ~ ~ {NoGravity:1b,CustomName:"SnowballMarker",Marker:1b,Invisible:1b}
/execute @e[type=Snowball,score_snowballTime_min=2] ~ ~ ~ tp @e[type=ArmorStand,name=SnowballMarker,c=1] @e[type=Snowball,r=1]

/scoreboard players set @e[type=ArmorStand,name=SnowballMarker] hasSnowball 0
/execute @e[type=Snowball] ~ ~ ~ scoreboard players set @e[type=ArmorStand,name=SnowballMarker,r=1] hasSnowball 1

/execute @e[type=ArmorStand,name=SnowballMarker,score_hasSnowball=0] ~ ~ ~ summon PrimedTnt ~ ~ ~ {Fuse:0}
/kill @e[type=ArmorStand,name=SnowballMarker,score_hasSnowball=0]

The first command will assign an ever-increasing score to every snowball entity in the world. The second and third commands will either create a new (invisible, hitboxless) armor stand (called "SnowballMarker") at the location of the snowball, or teleport an existing one to the snowball.

The fourth and fifth commands will set the hasSnowball score on the armor stand to 0 or 1, depending on whether or not there is a snowball within a radius of 1 block.

Command 6 summons TNT at every armor stand that does not have a snowball, and the last command removes these armor stands afterwards. Set the Fuse to whatever you like.

(I you are curious about the way this works, I suggest removing Invisible:1b from the summon command of the armor stand, so you can see them in action)