advanced-scripting/Basic Syntax
Basic Syntax
Welcome to Advanced Scripting 101! You're here to learn about Commander Script (or some other snappier name)! At this point, we're going to assume you know how to form basic replacement lines in your replacement files. If not, check out the Replacement Syntax page!
Basic Script
First thing's first: this is a script:
{ HelloFunction say Hello world! say How are you today? }
Note the following syntax:
- The script starts with an open curly brace, and ends with a close curly brace on its own line.
- After the open curly brace is an Script Alias (or, in common programmer parlance, a method name). This name identifies your script, and you can refer to it by that name. Scripts that are defined as part of a replacement are not required to have an alias, but stand alone scripts do.
- Between curly braces are one or more script lines. Here they are commands, specifically say commands, that are run by the minecraft server.
That's a basic script! Commander has supported these since 1.2! but lets move on to something a little more complex:
Advanced Script
{ ReportSeaLevel @blockY = $(me.position.y); @sl = $(world.sealevel); #in case the sea level is different between worlds [if @blockY > @sl] { echo You are above sea level. } [else if @blockY < @sl] echo You are below sea level. [else] echo You are at sea level! }
Ok, so this is a considerably more advanced script we have here! We'll go more in depth in other pages, but for now let's go over the code:
{ ReportSeaLevel
This line, as above, starts a script with the alias ReportSeaLevel.
@blockY = $(me.position.y);
This line defines a variable "blockY" if it has not been defined before, and assigns the environment variable "me.position.y" to it. It is not a command and is not sent as one to the minecraft server. This variable is only good inside this script.
@sl = $(world.sealevel); #in case the sea level is different between worlds
This line also assigns a variable from an environment variable, but it also has a comment at the end. Comments start with a hash symbol (#) and go until the end of the line. Anything after the hash is ignored by the parser.
[if @blockY > @sl] { echo You are above sea level. }
This is an if statement. It is a kind of construct (sometimes called a control structure by programmers). A construct is not a command, and is not sent as one to the minecraft server.
Some constructs require a line or block of code after them. This one has a block, defined by an open curly brace and a matching close curly brace on its own line. The open brace can go at the end of a line, or on its own line. There can't be anything meaningful on the same line after an open brace (a comment is not meaningful to the parser).
In the block here is our first real command! The echo command is provided by Commander and repeats a string of text to the player who issued the command, or in this case, the player who is running the script!
[else if @blockY < @sl] echo You are below sea level.
This is an else if construct (which, for you programmers, is actually an if attached to an else). This line will run if the previous if statement is false, and only execute its line or block if it is true. Notice here that there is only a single line after the if construct. If a construct is not followed by a block, it will only apply to the next line only.
[else] echo You are at sea level! }
Finally, the else construct only runs its line or block if the previous if statement was false, and doesn't test anything more to see if it runs. And a close curly brace ends the script!
For more syntax, as well as all the various lines the compiler recognizes, see the other pages in the Advanced Scripting section!
Comments