Hi everyone! In the 4th java coding class today we learned the execute command. We only really went into the basics, but I will also include a part 2 for the execute command if you want to learn a little extra. For now, we will just be learning what we learned in class.
I also made a video explaining what we learned on my youtube channel if you want to watch that instead of reading this:
Now, onto the command: The execute command allows you to run any command off of any entity in the game. We use selectors for this. This allows you to pick what entities you want to affect. Selectors are a little bit complicated, so I made an article explaining in-depth everything about them, but if you don't want to read that I will explain the basics further down.
We have touched upon @a and @p before, but now I will introduce a new one: '@e'.
This means every single entity in the entire game, which counts as anything that has AI, or Artificial Intelligence. So, that counts as players, monsters, and animals, as well as more obscure ones like armor stands and arrows.
When we run the command:
This will kill every entity in the game. But what if, say, I only want to kill zombies? We can add a [ to it, and then a list of words will come up. It should look something like this:
All these words are things that allow you to select certain entities, and we get into all of them in the article above. But for now, we will just look at the type= selector.
After the brackets, put in type=
This will bring up every single entity in the game, and you can type in the one you want to summon. Keep in mind if your entity of choice has spaces in its name, you have to use underscores. (_)
After you end the bracket, your selector should look like something like this:
This will kill all of the entities you selected. Now that we've learned how to select things, we can move on to the actual command.
When you do /execute, a big menu will pop up of things that allow us to select where the command is going to be executed. For now, though, we will just use 'at'. If you want to learn what all of these things mean, you can find out on the Class Notes 4: Extra page, at the bottom.
'as' basically means that if I target a zombie, and then I run a command, it will run the command at the zombie's position, essentially changing the point of execution. After at, we write the entity you want. In my case, I will choose a zombie. So, my command looks like this so far:
/execute as @e[type=zombie]
After that, we can pick 'run' to signify that we want to run the command. After that, we can enter the command we would like to use!
In my case, I will use the setblock command. If I do:
/execute as @e[type=zombie] run setblock ~ ~2 ~ lava
This will put lava on top of all the zombies in the world!
TIP: The execute command is pretty useless if you use it with a command that has a built-in selector feature. For example, if I run the command:
/execute @e[type=zombie] run effect give @e[type=zombie] absorption
This would do the same thing as if I just ran the effect command on its own. But for commands like /fill and /setblock, this command shines.
I encourage you to make a cool project like this and send it to me via Slack, or just show me on the realm next class! For example, one that I can think of right now is a rain of arrows. If I run the command:
/execute as @e[type=snowball] run summon arrow ~ ~1.5 ~
This will summon a rain of arrows on the ground. The reason I made it 1.5 blocks above the snowball is to not kill it. A lot of times when your coding in any language, you want to think practically. Half the time it's not because you made a syntax error, but just because you weren't using common sense.
That's all there is to the basics of the execute command. If you want to learn more that we didn't get to in the 4th class, read the article here: Java Coding Class 4 notes: Extra (wequil.school)
I hoped you learned something while reading this and as always, see you next class!