3

I want to create a Minecraft datapack in which I can shoot an arrow and, when it reaches its target, summons a lightning bolt. Currently, I have this:

execute at @e[type=Arrow] unless block ~ ~-1 ~ air run summon minecraft: lightning_bolt ~ ~ ~

…but this infinitely summons lightning. I would like to make it so that the arrow is killed after the lightning is summoned, but I don't know how to run multiple commands in /execute at the same time.

3 Answers 3

1

Commands in datapacks and commandblocks are sequential; That means we can use two different commands with the same if statement to reproduce a sequential behaviour. Also, arrows have a NBT that will tell if they've hit a block, so instead of using ...unless block ~ -1~ ~ air... (which will fail in multiple instances) you can use said NBT. Here are both commands:

execute as @e[type=arrow,nbt={inGround:1b}] at @s run summon minecraft:lightning_bolt ~ ~ ~
execute as @e[type=arrow,nbt={inGround:1b}] run kill @s

Do also note that ANY arrow will behave like that, including of other players and skeletons. You can make it so a certain bow is needed for the arrow to react in such way, but that requires a few more commands (mainly a scoreboard to track used bow stat and one to tag an arrow nearby a player with said score).

2

While it isn't possible to use execute for multiple commands at once, you can create a second command to kill the arrow with a very similar execute command put into a chain command block.

execute at @e[type=arrow] unless block ~ ~-1 ~ air run kill @e[type=arrow]

It also works in a second repeating command block. For some reason though it depends in what order you activate the commands in a repeating command block, that being activating the summon loop then activating the kill loop, in that order, works, same if you power them at once. I don't know if data packs activate commands in order, or all at once, so just watch out for that.

Also do remember that the command you made and this solution here is going to make every arrow strike, kill every arrow entity too. If you want to only make one bow shoot arrows and make them summon lightning you are going to require a different solution.

2
  • 1
    This has an unintended side effect: it will kill all arrows in the entire world if even one arrow is detected that matches the conditions. Are you sure that is what you meant to do with this command?
    – One 2 Many
    Commented Aug 31, 2022 at 23:54
  • @ExpertCoder14 Well, they asked if there's a way to it, I was thinking about mentioning Tags in order to not kill every arrow, but maybe that what's the player wants, as the command they're using does so. Neither have they asked how to make it so only it's killed. The data pack just makes every arrow summon the lightning, and that's what they asked for. Commented Sep 1, 2022 at 5:50
2

The other answers here are good, but, if you're using a lot of commands targeting the same entity, or something like that, you may want to use tags.

For example, let's say that we have the following commands:

execute at @e[type=arrow,nbt={inGround:1b}] run summon tnt
execute as @e[type=arrow,nbt={inGround:1b}] run kill @s

This sounds good, right? It will hit the ground, explode once, then kill itself.But, the thing is, this won't always work. If the block the arrow is in is destroyed, the arrow is no longer inGround:1b, so it won't kill itself. Instead it will be bounced into the sky by the explosion, and continue exploding and bouncing.

Instead, tag your entity. Tags are useful for a number of things, but, here's a working version of the explosive arrow command.

execute as @e[type=arrow,nbt={inGround:1b}] run tag @s ArrowThatExplodes
execute at @e[tag=ArrowThatExplodes] run summon tnt
kill @e[tag=ArrowThatExplodes]

Now what will happen is, the arrow will be detected in the ground. It will summon a tnt, and instead of detecting again for an arrow in the ground, it instead targets the same arrow and kills it.

With your lightning use case, you don't really need this, although it may hep with lessening the amount you have to type. I imagine it will also improve performance, though I haven't tested it.

1
  • All commands are run in a single tick, meaning the state of the arrow can't change inbetween commands. But you are right, having the same if/targeting on each line can be costy. The best way would instead be to use another function and run the function as said entity.
    – BunnyMerz
    Commented Sep 29, 2022 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.