Java Coding Class 4 notes: Extra

Hi class! Here's part 2 for class 4 notes. This explains everything there is to the execute command, and everything in here we have not gotten into in class 4. In this article, I'm going to assume that you have read my article on selectors, and if you haven't, you can read it here.

Java Coding Class: Selectors (wequil.school)


Now, on to the commands part. There will be two parts, Location Modifiers, and Condition Subcommands. We will start with part 1. We know that 'as' means the location of the entity we are targeting. We want to learn all the other ways of locating where the command is being executed, too.

First of all, we need to clear up exactly how this process works. If I add a way of locating where the command is being executed, Relative Coordinates are used. For example, if I use an execute command with a Location Modifier, say, at 64 32 12, that becomes the origin point.


/execute (Location Modifier, say, equals 64 32 12) run spawnpoint @p ~ ~ ~

Since after the execute part I am entering a Location Modifier with an X Y Z value that equals 64 32 12, the base relative coordinate (~ ~ ~) becomes that, since the execute command is making 64 32 12 the origin point.

I know that's a little confusing, but bear with me here. On to the Location Modifiers.


1. 'Align'

This allows you to set the origin point to whatever your X, Y, or Z coordinates are while flooring the integers - i.e. rounding them downwards.

The letters X Y Z can be mixed in any order, as well.

For example, given my location is 64.2 23 -12.6 and I run the command:


/execute align zxy run spawnpoint @p ~ ~ ~

Since you can mix the letters X Y and Z in any order, I chose zxy for the purpose of making it so you understand it better.

Since it is aligning my Z, X, and Y coordinates to the origin point, (originally myself, or my feet) this will make the spawnpoint at 64, 23, -13, since it is flooring the integers, or rounding down. That's it for the Align Location Modifier, on to the next.


2. anchored

This allows you to anchor the position to either your eyes or your feet (default). This will only work with Local coordinates since Absolute coordinates have nothing to do with your eyes or feet, since the origin point is 0 0 0, and Relative coordinates also have some Absolute traits. For example, if I run the command:


/execute anchored eyes run tp @s ^ ^ ^

This will teleport my feet to where my eyes are. If I run the command:



/execute anchored eyes run tp @s ^5 ^ ^

This is essentially saying "place the executor's feet 5 blocks left of where its eyes are."

That's it for the anchored Location Modifier, on to the next.


3. At

This sets the execution position, rotation, and dimension to match those of an entity, but it doesn't change the executor. For example, if I run the command:


execute as @e[type=sheep] at @s run tp ~ ~1 ~

This will move all sheep in the world up one block. The reason 'as' is used here is because using 'at' first is useless since it does not change the executor. For example, if I ran the command:


execute at @e[type=sheep] run kill @s

This would kill me since even though I am targeting the location of all the sheep, it does not change the executor.

That's all there is to the 'at' Location Modifier, on to the next.


4. in

This allows you to modify locations from the 3 dimensions: overworld, nether, and end.

For example, given that my location in the overworld is 16, 64, 16, and I run the command:


/execute in minecraft:the_nether positioned as @s run summon zombie ~ ~ ~

This will summon a zombie in 16, 64, 16 inside the nether since my position in the overworld is 16 64 16. That's it for the 'in' Location Modifier, on to the next.


5. positioned

This allows you to change the position of the origin point that the command is being executed from. For example, if I run the command:


/execute positioned 30 20 10 run summon zombie

This will summon a zombie at 30, 20, 10 in X Y Z format. That's it for the 'positioned' Location Modifier, and now we are done with part 1! Now, on to part 2, Condition Subcommands.


Condition Subcommands consist of two words: if, and unless. Unless has the exact same syntax as if, and the only difference between the two is that 'unless' is the opposite of 'if'. Because of this, we will be referring to them as if/unless. The purpose of these two conditions is to restrict commands from happening if a certain value is not met or is met. Down below, I'll explain all the ways to do this.


1. (if/unless) block

This allows you to test for a singular block in a certain position. For example, if I run the command:


/execute if block 64 32 12 diamond_block run summon wither

This will summon a zombie if the block at 64 32 12 is a diamond block. If instead of 'if' I write 'unless', it will essentially be saying "if the block at 64, 32, 12 is anything but a diamond block, summon a wither." That's it for the 'block' subcommand, on to the next.


2. (if/unless) entity


This allows you to check if one or more entities exist. For example, say I want to run a command if an iron golem named Beckett does not exist. If it doesn't exist, I want to summon a silverfish. The command would look like this:


/execute unless entity @e[type=iron_golem,name=Beckett] run summon silverfish

That's it for the entity subcommand, on to the next.


3. (if/unless) score

This has to do with the scoreboard command. If you don't know the scoreboard command, you can skip this section.

With this subcommand, you can choose if two scoreboard values are less than, less than or equal to, equal to, more than or equal to, or more than. This is sorted like this:

less than: <

less than or equal to: <=

equal to: =

more than or equal to: >=

more than: >=


For example, say I want to set a stone block at -32 45 12 only if my value on objective A is less than or equal to Jimmy's value on objective B. The command would look like this:


/execute if score Trxh A <= Jimmy B

That's it for the score subcommand, and now you have learned all there is to the execute command!


I hope you learned something, and as always, see you next class!


~ Benny