# Conditions

## Basic functionalities

This option will add some conditions for this event. If the conditions are accomplished, then the default actions will be executed.

Every line of the **conditions list** refers to a condition that MUST be true. You can use all kind of variables, even PlaceholderAPI or ConditionalEvents variables. Use the variables previously show in the [**Event Types**](/conditionalevents/event-types.md) page or the ones in the [**Variables**](/conditionalevents/global-variables.md) page.

{% hint style="info" %}
For example, in this case a Player Attack event has 3 conditions. The victim must be another player AND the item used must be a Diamond Sword AND the name of this item should be "Super Sword":
{% endhint %}

```yaml
conditions:
- '%victim% == PLAYER'
- '%item% == DIAMOND_SWORD'
- '%item_name% == Super Sword'
```

{% hint style="info" %}
You can also use 'and' separator in the same line, to accomplish the same as above.
{% endhint %}

```yaml
conditions:
- '%victim% == PLAYER and %item% == DIAMOND_SWORD and %item_name% == Super Sword'
```

{% hint style="info" %}
You can also check for 'or' condition separator, meaning that a condition line will be approved if one of many conditions in the same line is accomplished. For example, in this Player Command event there is just one condition. The command should start with //calc OR start with //solve OR start with //eval:
{% endhint %}

```yaml
conditions:
- '%command% startsWith //calc or %command% startsWith //solve or %command% startsWith //eval'
```

{% hint style="warning" %}
You can only use a condition separator of one type in the same line. Meaning you can't mix ORs and ANDs in one condition line.
{% endhint %}

## Advanced functionalities

{% hint style="info" %}
You can also compare two variables in one condition line.
{% endhint %}

```yaml
conditions:
- '%item_name% !contains %player_name%'
```

{% hint style="info" %}
Finally, you can even compare variables with math formulas. If you want to do so, remember to set the following option on the event:\
`allow_math_formulas_in_conditions: true`
{% endhint %}

```yaml
conditions:
- '%command% equals /test-kills'
- '%statistic_player_kills% >= %statistic_deaths%*2'

conditions:
- '%player_x% == (%player_z%*3)-1000'
```

## Type of Conditionals

### For Text

* equals, ==
* !equals, !=
* equalsIgnoreCase
* !equalsIgnoreCase
* startsWith
* !startsWith
* contains
* !contains
* endsWith
* !endsWith
* matches (For regex)
* !matches (For regex)

### For Numbers

* equals, ==
* !equals, !=
* \>=
* <=
* \>
* <

## Execute Option

Sometimes you will want to execute different actions depending on which conditions the player accomplishes or not. For example:

```yaml
example:
    type: player_join
    conditions:
    - '%vault_rank% equals admin execute actions1'
    - '%vault_rank% equals vip execute actions2'
    actions:
      default:
      - 'to_all: message: &a%player% &ejoined the game.'
      actions1:
      - 'to_all: message: &4&lADMIN &c%player% &ejoined the game.'
      actions2:
      - 'to_all: message: &b&lVIP &a%player% &ejoined the game.'
```

This event will check when a player joins the server. If the rank of the player is admin, then the actions from "actions1" will execute. The same will happen if the rank of the player is vip, this case "actions2" will execute. Finally, if no "execute" conditions are met, the default actions will be executed.

{% hint style="warning" %}
If an "execute" condition is not accomplished it will continue to the next condition. If no more conditions are defined below an "execute" condition, the default actions will be executed. You can create as many sets of actions you want.
{% endhint %}

### AND Conditions on Execute Option

The 'and' condition is very useful in some cases when using the execute option. For example, let's think you want to do something similar as above, sending a message to the whole server when a player with certain rank joins the server. But this time, the player must also have a certain level:

```yaml
example:
    type: player_join
    conditions:
    - '%vault_rank% equals admin execute actions1'
    - '%vault_rank% equals vip and %player_level% > 20 execute actions2'
    actions:
      default: []
      actions1:
      - 'to_all: message: &4&lADMIN &c%player% &ejoined the game.'
      actions2:
      - 'to_all: message: &b&lVIP &a%player% &ejoined the game.'
```

### Parameters

On execute conditions you can add optional parameters that allows you to reduce the amount of action groups in some cases and reuse the same action group with different parameters.

For example, imagine we have the following event:

<pre class="language-yaml"><code class="lang-yaml"><strong># This event will check when a player uses the /vip &#x3C;player> command 
</strong><strong># and set the specified player to the vip rank. 
</strong><strong># An error will show when the player doesn't have permissions. Another
</strong><strong># error will show when the player doesn't use arguments.
</strong><strong>vip_rank:
</strong>    type: player_command
    conditions:
    - "%main_command% == /vip"
    - "%player_has_permission_conditionalevents.admin% != yes execute error_action1"
    - "%args_length% &#x3C; 1 execute error_action2"
    actions:
      default:
      - "cancel_event: true"
      - "player_command: lp user %arg_1% parent set vip"
      error_action1:
      - "cancel_event: true"
      - "centered_message: &#x26;c&#x26;m                    &#x26;r &#x26;c&#x26;lERROR! &#x26;c&#x26;m                    "
      - "centered_message:  "
      - "centered_message: &#x26;cYou don't have permissions"
      - "centered_message:  "
      - "centered_message: &#x26;c&#x26;m                                                    "
      - "playsound: BLOCK_NOTE_BLOCK_PLING;10;0.1"
      error_action2:
      - "cancel_event: true"
      - "centered_message: &#x26;c&#x26;m                    &#x26;r &#x26;c&#x26;lERROR! &#x26;c&#x26;m                    "
      - "centered_message:  "
      - "centered_message: &#x26;cYou must use &#x26;7/vip &#x3C;player>"
      - "centered_message:  "
      - "centered_message: &#x26;c&#x26;m                                                    "
      - "playsound: BLOCK_NOTE_BLOCK_PLING;10;0.1"
</code></pre>

As you can see, `error_action1` and `error_action2` action groups are very similar, the only difference is the error message. We can simplify this event by using just one `error_action` action group by using parameters.

Parameters are specified after the action group name on the execute option, using brackets `{ }`.&#x20;

{% hint style="info" %}
execute error\_action{%variable1%=Variable 1 value;%variable2%=Variable 2 value}
{% endhint %}

The idea is to create parameters as temporal variables that can be used in the action group. You can create multiple parameters  by using `;`. If we use parameters in the previous example, the result is this:

```yaml
vip_rank:
    type: player_command
    conditions:
    - "%main_command% == /vip"
    - "%player_has_permission_conditionalevents.admin% != yes execute error_action{%error_message%=&cYou don't have permissions}"
    - "%args_length% < 1 execute error_action{%error_message%=&cYou must use &7/vip <player>}"
    actions:
      default:
      - "cancel_event: true"
      - "player_command: lp user %arg_1% parent set vip"
      error_action:
      - "cancel_event: true"
      - "centered_message: &c&m                    &r &c&lERROR! &c&m                    "
      - "centered_message:  "
      - "centered_message: %error_message%"
      - "centered_message:  "
      - "centered_message: &c&m                                                    "
      - "playsound: BLOCK_NOTE_BLOCK_PLING;10;0.1"
```

We use the `%error_message%` variable and assign a value in both conditions. Then, in the `error_action` we can use this variable wherever we want.

## Examples

{% tabs %}
{% tab title="Example 1" %}
Here is a full config example of a player attack event:

```yaml
example1:
    type: player_attack
    conditions:
    - '%victim% == PLAYER'
    - '%item% == DIAMOND_SWORD'
    - '%item_name% == Super Sword'
    - '%random_1_10% >= 8'
    actions:
      default:
      - 'message: &aYour diamond sword poison effect was activated!'
      - 'to_target: give_potion_effect: POISON;120;1'
```

In this case, the plugin will check when a player attacks another player with a diamond sword called "Super Sword". A random number will be generated. If this number equals 8 or is greater than 8, then a message will be sended to the player and the target player will be affected by a poison effect.
{% endtab %}

{% tab title="Example 2" %}
Here is another example of the "execute" option:

```yaml
example2:
    type: block_interact
    conditions:
    - '%block_x% == 40'
    - '%block_y% == 60'
    - '%block_z% == 40'
    - '%block_world% equals lobby'
    - '%block% equals STONE_BUTTON'
    - '%action_type% equals RIGHT_CLICK'
    - '%statistic_jump% < 1000 execute actions2'
    actions:
      default:
      - "message: &aYou''ve received $5000!"
      - 'console_command: eco give %player% 5000'
      actions2:
      - 'message: &cYou need at least 1000 jumps to use this button.'
```

In this case, the player must press a button in certain coordinates. But the last condition says that if he doesn't has more than 1000 jumps then the actions from "actions2" will be executed instead of the default actions.
{% endtab %}

{% tab title="Example 3" %}
Here is an example of a conditional comparing two variables:

```yaml
example3:
    type: item_interact
    conditions:
    - '%item_name% startsWith Axe of'
    - '%item_name% !contains %player_name%'
    actions:
      default:
      - "message: &cThis is not your axe so you can''t use it."
      - 'cancel_event: true'
```

Imagine there is an item in your server called "Axe of \<player>". Every player has an axe with their name on it. In this case, the plugin will check if the playing is interacting with an item that starts with the name "Axe of". Then, if the item name DOESN'T contains the player name, it will cancel the event and tell the player that the item is not theirs.
{% endtab %}

{% tab title="Example 4" %}
Here is an example of a condition including a math formula:

```yaml
example4:
    type: player_command
    allow_math_formulas_in_conditions: true
    conditions:
    - '%command% equals /test-kills'
    - '%statistic_player_kills% >= %statistic_deaths%*2 execute actions1'
    actions:
      actions1:
      - 'message: &aYou have a lot of kills, congrats!'
      - 'cancel_event: true'
      default:
      - 'message: &cYou need to have AT LEAST 2 times more kills than deaths'
      - 'message: &cto use this command. Currently these are your stats:'
      - 'message: &7Kills: &6%statistic_player_kills%'
      - 'message: &7Deaths: &6%statistic_deaths%'
      - 'cancel_event: true'
```

In this example we are creating the following command: /test-kills. When the player executes the command, the plugin will check if the player has at least 2 more kills than deaths. A player with 40 kills and 20 deaths would be able to use the command, since `40 >= 20*2`
{% endtab %}

{% tab title="Example 5" %}
Here is an example of the "matches" conditional, checking if the player wrote a word similar to "Hello".

```yaml
example5:
    type: player_chat
    conditions:
    - "%message% matches (.*)h+e+l+l+o+(.*)"
    actions:
      default:
      - "message: &8[&7Server&8] &eHello %player%!"
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ajneb97.gitbook.io/conditionalevents/conditions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
