Language reference
Variable code is Java's expression syntax, with Java's precedence, associativity, number promotion and overflow.
Where code goes
| Field | Contains | Can change things? |
|---|---|---|
fields of variables | field declarations | only its own initial values |
code of execute | statements | yes |
code of for_each | statements | yes, including value |
expression of expression | one boolean expression | no |
value of modify_variable | one expression | no |
${…} in with_variables and /opp with | one expression | no |
Names
| Name | Means |
|---|---|
mana | this entity's variable (the actor, in bi-entity code) |
self.mana | the same, written out |
other.mana | the target's variable. Only in bi-entity code. |
Scoreboard, Item, Death, Stat, Global | built-in names, reserved |
value, index, size | the current element of the innermost loop (page 11) |
history.size(), … | a list's methods (page 11) |
Literals
42, 0xFF, 1_000_000, 10L, 1.5, 1.5f, 2e3, 1d, true, false, and "text" with the escapes \n \t \" \\ \uXXXX.
Comments: // and /* */.
Operators
Highest precedence first:
| Operators | Notes |
|---|---|
x++ x-- | variables only |
++x --x +x -x !x ~x (type) x | |
* / % | whole-number division rounds toward zero. Dividing an int or long by zero is an error |
+ - | + joins text when either side is a String |
<< >> >>> | int/long |
< <= > >= | numbers |
== != | numbers, booleans, Strings |
& ^ | | bitwise on int/long, logical on booleans |
&& || | short-circuit |
?: | |
= += -= *= /= %= &= |= ^= <<= >>= >>>= | actions only |
Types and conversions
As in Java:
- Mixed arithmetic promotes: int, then long, then float, then double.
5 / 2is2, and5 / 2.0is2.5. - int and long overflow wraps around.
=only widens.int x = someDouble;is an error. Writex = (int) someDouble;.- Compound assignment casts, so
x += 1.5on anintis allowed. - Casts between number types behave like Java's. To convert between numbers and booleans or Strings, use
String.valueOf(x),"" + xorInteger.parseInt(s).
Built-ins
| Group | Members |
|---|---|
Math. | abs max min clamp round floor ceil rint signum sqrt cbrt pow exp log log10 sin cos tan asin acos atan atan2 hypot toRadians toDegrees floorDiv floorMod random, PI, E |
Integer., Long., Float., Double. | MAX_VALUE, MIN_VALUE, parseInt(s) / parseLong / parseFloat / parseDouble |
Double. | POSITIVE_INFINITY, NEGATIVE_INFINITY, NaN, isNaN(x), isInfinite(x) |
Boolean. / String. | Boolean.parseBoolean(s), String.valueOf(x) |
Scoreboard. | get(objective), get(holder, objective), has(objective), has(holder, objective): see page 9 |
Item. | id(slot), id(power, slot), count(slot), count(power, slot): what's in a vanilla slot or a slot of an origins:inventory power. cooldown(item or slot): vanilla cooldown ticks left. Each takes an optional first argument self or other. See page 14 |
Death. | has(), x(), y(), z(), dimension(), distance(): where the player last died. See Death below |
Stat. | get(stat), get(self or other, stat): a vanilla statistic. See Stat below |
Global. | has(key), get(key), set(key, value), delete(key), bind(key, self or other), lock(key, owner), unlock(key), list(key), list(key, type): the global store. See page 12 |
| String methods | length() isEmpty() isBlank() equals(x) equalsIgnoreCase(s) contains(s) startsWith(s) endsWith(s) indexOf(s) lastIndexOf(s) compareTo(s) concat(s) toUpperCase() toLowerCase() trim() strip() substring(i) substring(i, j) replace(a, b) repeat(n) |
| List methods | size() isEmpty() get(i) contains(v) indexOf(v) set(i, v) add(v) add(i, v) remove(x) clear(): see page 11 |
Overloads resolve as in javac:
Math.abs(int)staysintMath.max(int, long)islongMath.round(double)islongMath.round(float)isint
Death
The player's last death location, the one vanilla keeps for the recovery compass. Each method takes an optional self or other (bi-entity code only), like Death.distance(other).
| Method | Type | Value |
|---|---|---|
Death.has() | boolean | whether there's a death location. Always false for non-players. |
Death.x(), Death.y(), Death.z() | int | the block where they died. An error without a death location, so check Death.has() first. |
Death.dimension() | String | the dimension they died in, like minecraft:the_nether. Same error rule. |
Death.distance() | double | blocks from the entity to the centre of that block. -1 when there's no death location or it's in another dimension. Never an error. |
- Vanilla saves the location, so it survives relogging and restarts. Only a death changes it.
- The same distance is the
last_death_distanceattribute.
Example: execute code that stores a hint in a String variable: grave_hint = Death.has() ? "grave at " + Death.x() + " " + Death.z() : "no grave";
Stat
Stat.get(stat) is the player's value for a vanilla statistic, an int. Stat.get(other, stat) reads the other entity in bi-entity code.
The name is written as scoreboard criteria write it: stat type and id with their : swapped for ., joined by a :. For example:
"minecraft.custom:minecraft.jump""minecraft.mined:minecraft.stone""minecraft.killed:minecraft.zombie"- An unknown statistic is an error when the code runs.
- Non-players and the client read
0. Stats only exist on the server. - Units are vanilla's: centimetres for distances, ticks for time. See the
statcondition for the list of types.
Example: blocks walked per hour played, as a double:
Stat.get("minecraft.custom:minecraft.walk_one_cm") / 100.0 / (Stat.get("minecraft.custom:minecraft.play_time") / 72000.0)Differences from Java
==on Strings compares the text, like.equals().- No
null. An empty String is"". - No
char,byteorshort. - No control flow (
if,for,while,return), local variables,new, or methods of your own. Use?:for choices and the loop actions on page 11 for lists. - Types are checked when code runs. Syntax errors, unknown methods, assigning in a condition, and field initializers fail when the pack loads. A type error like
mana = "full";is reported when that code runs. - The two branches of
?:keep their own types, soc ? 1 : 2.0can be anint. toUpperCase()andtoLowerCase()ignore the system language.- No octal numbers.
010is an error.
Errors
- Load errors fail the power when the pack loads, with the line, column and nearby text:
expected ';' but found 'casting' (at column 16, near "casting = true"). - Errors while running are logged once per distinct message, prefixed
[variables], with the code that caused them. An action stops at the failing statement, and earlier changes stay. A condition counts asfalse.