Minecraft Blogs / Tutorial

Cmd-Block Tutorial: /execute

  • 9,129 views, 3 today
  • 2
  • 0
  • 5
Rum+'s Avatar Rum+
Level 50 : Grandmaster Architect
29
Advanced Tutorial on the /execute command

So here is a blog on command blocks to demonstrate ways to use and possible uses of the /execute command, one of the most important commands, if not THE most important, used mainly by cmd blocks.

I will make this demonstation through a series of examples and possible combinations with other commands, so if you don't have the very very basic knowledge and understanding of command blocks and their commands, this tutorial is not for you. :/


In my examples I will mostly use the "set and destroy" clock, one of the fastest clocks in minecraft. (if you don't know which one I'm talking about, it's okay. any really fast clock will do) Take as granted that any command I talk about is executed through a command block that is connected on that clock unless I state otherwise.

Target entity is any entity that can be targeted through the selectors given.

A slot activated command is a command that is activated as soon as a specific hotbar slot is selected. It is one of the best ways to manually execute a command.

x -> y When you see this, it means command block y is placed right next to x, and this way it is only powered through command block x. This means it is powered right after cmd block x, but almost at the same time.



/execute @e[selectors for target entity] ~ ~ ~ COMMAND
The command itself. It executes a command at the location of the target entity. It's like the command block teleports for a brief moment and executes the COMMAND at a ~ ~ ~ location from the target.



Chain Targeting / Multi-Path Targeting

.1 /execute @e[selectors for main target entite] ~ ~ ~ execute @e[selectors for final target entity] ~ ~ ~ COMMAND

.2 /execute @e[selectors for first target entity] ~ ~ ~ execute @e[selectors for second target entity] ~ ~ ~ . . . execute @e[selectors for final target entity] ~ ~ ~  COMMAND


Command 1 is pretty simple. Instead of executing a command at the location of the main target entity, it executes a command at the final target entity that is located thanks to the main target entity. For instance, you can execute a command on all pigs within a certain radious from any sheep.

Command 2 executes a command at the location of the final target entity. I think this one's use is better explained with an example:
Let's say you have a "Protector" team and players in that team can protect villagers from zombies by dealing damage to them depending on how many zombies are gathered around the villager:

/execute @a[team=protector,and a selector that finds the player who activated the ability] ~ ~ ~ execute @e[type=Villager,r=10] ~ ~ ~ execute @e[type=Zombie,r=5] ~ ~ ~  execute @e[type=Villager,r=5,c=1] ~ ~ ~ effect @e[type=Zombie,r=5] 7 1 0

This is a slot activated command btw, otherwise it would be really OP. What it does is locate all villagers within 10 blocks from the player who activated the ability, then locate all the zombies in a 5 block radious from each villager, then from each zombie locate the nearest villager in a 5 block radious and then for each different path created this way deal 6 damage to each zombie in a 5 block radious. So, if there is one villager and 3 zombies around him, the command will execute a command at each zombie that will execute the command /effect at the location of the villager for every time it was executed, which is 3 times.




Contagious Targeting

objectives needed:
name: cont, critiria: dummy, starting value: 0

.1 /execute @e[score_cont_min=1] ~ ~ ~ scoreboard players set @e[r=X] cont 1
.2 /execute @e[score_cont_min=1] ~ ~ ~ COMMAND


Pretty simple, too. Let's say there is at least 1 entity in the world that has a score of 1 for cont. All entities within a X block radious from that entity have their cont objective set to 1, too, thanks to command 1. Then, command 2 executes a command at the location of ALL affected entities (entities with a cont score=1). 

The second command can be executed either at every tick, or be manually activated. For instance, at every tick it can give Weakness 5 and Wither 2 for 1 second to the affected entities, which is like a real, fatal desease, right? As soon as it is cured, after a short delay of 1 sec, the effects will go away.



Testfor for Selectors Alternative

.1 /execute @e[selectors] ~ ~ ~ execute @e[selectors for target entity] ~ ~ ~ COMMAND

"execute @e[selectors for target entity]" can be excluded if the entity we want to execute the command at is the same as the entity we are testing, or is a fixed to the world effect, such as "setblock x y z".

Yes, I know it is just the simple version of the chain targeting, but has a more specific use. And it also follows some rules. And it is better used as manually or only-once activated.
1. The selectors for target entity will always find an entity. 
2. The other selectors may or may not be able to locate an entity (e.g. can be a score value, or it must be within a certain radious)
The main idea here is that the COMMAND is executed only when the first group of selectors can locate an entity. Otherwise, nothing happens.
For example, you can spawn particles above your head every time a cow is within 5 blocks. Or, if a player has scored 10 kills, spawn fireworks at a certain location.



Individual Global Operations

objectives needed:
name: doh, critiria: dummy, starting value: any
name: meh, critiria: any, starting value: 0

.1 /execute @a ~ ~ ~ scoreboard players operation @p doh <operation> @p meh
.2 /scoreboard players set @a meh 0
1 -> 2

Command 1 calculates stuff based on the values meh takes over time. E.g. meh is the damage dealt by players and the operation is += which is addition. doh shows the total damage each player has dealt separately.



Various Quick Commands

/execute @e[selectors] ~ ~ ~ testforblock ~ ~-1 ~ <tilename>
Tests if the block beneath the entity is the one given in tilename.

/execute @e[selectors] ~ ~ ~ kill @e[r=X,rm=1]
Kill everything within X blocks from the target entity.

.1 /execute @e[type=Pig,score_expsh_min=1] ~ ~ ~ xp 10 @p[r=X]
.2 /execute @a ~ ~ ~ scoreboard players set @e[type=Pig,score_expsh_min=1,r=X] expsh 0
1 -> 2

Any player that gets within X blocks from a pig gets 10 xp points. A pig can give xp only once.



That's all for now. I will update this list with more stuff as soon as I have more time and ideas.
If you have a question / idea / suggestion leave a comment. C:


Tags

Create an account or sign in to comment.

1
03/18/2016 11:41 am
Level 1 : New Crafter
RekoPlayz
RekoPlayz's Avatar
i want to know how to execute a command when an entity has an effect brought onto them,EX:the glowing effect executing /summon
1
06/13/2015 9:23 am
Level 22 : Expert Pirate
chrisdude011
chrisdude011's Avatar
Could you please explain why this does not work.
/execute @p[score_OpenDoor_min=0,max=0] ~ ~ ~ /say hi
If i have a score of 2 it still says hi and i want it to run only if a player has a score of 0
1
06/13/2015 3:08 pm
Level 50 : Grandmaster Architect
Rum+
Rum+'s Avatar
Replace "max" with "score_OpenDoor"
1
10/26/2014 3:08 pm
Level 8 : Apprentice Engineer
TechEpic
TechEpic's Avatar
can u tell me why this wont work i have it in a command block running a clock. it's supposed to be a kind of a radar. i am running this in 1.8 singleplayer. /execute @p ~ ~ ~ execute @e[r=10,type=!Player]  ~ ~ ~ playsound random.pop @p
1
10/26/2014 8:15 pm
Level 50 : Grandmaster Architect
Rum+
Rum+'s Avatar
Damn, took me 20 minutes to figure out what's wrong with this perfect line of command. It seems you have an extra space after "@e[r=10,type=!Player]". :P
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome