MCCompiled Wiki 1.19 Help

Comparison

Comparison is the most important part of writing any logic, and a majority of MCCompiled code will contain comparisons of some sort. Comparison is primarily done with two commands: if and else.

Syntax

An if statement accepts as many comparisons at once as you would like. Each comparison must be separated by the and keyword to indicate to MCCompiled that the last comparison ended.

if <comparison>
if <comparison> and <comparison> and /* ...continue... */

if statements must be followed by either a block or single statement which will only be run if all the comparisons in the command evaluate true.

Else

After an if statement ends (as well as its block/statement), you can choose to place an else command too. The else command follows the same rules about how it needs to be followed by either a block or single statement.

if <comparison> { // code to run if true } else { // code to run if false }

Inverting a Comparison

You can invert a comparison by specifying not before it. In doing so, you check if the comparison evaluates false instead of true.

if not <comparison>

Value Comparison

Values can be compared to one another or compared to literal values. Comparisons automatically handle all the necessary type conversions, ranges, setup, etc. for you so that you can focus more on the logic than extraneous stuff.

When comparing values, the syntax of the comparison is <a> <operator> <b>. The parameter a can be any value or literal, as well as b.

The operator can be any of the following, which decides what case A and B have to match in order for the comparison to be true.

Checks if A and B are equal.


if a == b { // ... }

Checks if A and B are not equal.


if a != b { // ... }

Checks if A is less than B.


if a < b { // ... }

Checks if A is less than or equal to B.


if a <= b { // ... }

Checks if A is more (greater) than B.


if a > b { // ... }

Checks if A is more (greater) than or equal to B.


if a >= b { // ... }

Value Comparison Example

The following example checks if the player is in debt; if their balance is less than 0, then they are in debt.

define int balance if balance < 0 { print "You are in debt!" }

Let's expand on this example by adding an else statement and adding another if statement. If the player has enough money to buy the item (indicated by cost), they'll receive the item and pay the cost. If the player doesn't have enough money, they will be let know and nothing will happen.

define int balance define int cost cost = 40 if balance < 0 print "You are in debt!" else { if balance >= cost { balance -= cost give @s diamond 1 } else print "You don't have enough money! Missing {cost - balance}" }

Comparing Booleans

If you have a boolean value, you can compare it as normal or completely omit the operator and B value. When you do this, you are implicitly checking if the boolean is true. With the not operator, you can check if the boolean is false.

define bool isJumping if isJumping { // the player is jumping }

Selector Comparison

If you want to compare using a selector, specify the selector as-is. This form requires the selector be @s to keep its meaning concise. The following example shows using this comparison to check if the executing player has a tag:

if @s[tag=safe] { // ... }

Entity Comparison

There are two different comparison methods of checking entities, besides selectors (seen above).

Any

Check if any entity matches the given selector.

  • any <selector>

if any @e[r=10,type=cow] { // found a cow somewhere nearby }
Count

Counts the given selector and checks it as if it was a value. Supports all the operators seen above and can be compared to other values or literals.

  • count <selector> <operator> <b>

if count @e[type=cow] > 10 { // cow overpopulation (more than 10) }

Block Comparison

Allows you to check for individual blocks or groups of blocks within the world. Like with all other comparisons, you can invert these with not for use-cases like checking if a block is not air.

Block

Checks for a block at specific coordinates.

  • block <x, y, z> <block>

if block ~ ~-1 ~ air { // player is in the air }
Blocks

Checks if a region of blocks matches another region.

  • blocks <region-start x, y, z> <region-end x, y, z> <dest x, y, z> all

  • blocks <region-start x, y, z> <region-end x, y, z> <dest x, y, z> masked

if blocks 0 0 0 10 10 10 30 0 30 { // region matched }
Last modified: 28 October 2024