# State dependent modules

### custom module config

Each custom module can configure these 3 optional configuration parameters:

| option           | description                                                                                          |
| ---------------- | ---------------------------------------------------------------------------------------------------- |
| enabled-on-state | A string value which defines that this module should only get enabled when the given state is active |
| enable-state     | Tell staff++ to enable this state when the module is executed.                                       |
| disable-state    | Tell staff++ to disable this state when the module is executed.                                      |

### Modes config

In the modes config we can specify which states should be active when entering the mode:

```yaml
modes:
  default:
    ...
    initial-item-states:
    - nightvision-off
```

Using this system we can create modules which are enabled only on certain states.\
Because state changes can be done by staff modules themselves we can create some flexible configurations.

## Examples

### Nightvision modules

We defined 2 modules. 1 for toggling night mode on. And one for turning it off.\
The first module switches the state to `nightvision-on` This state change makes it so that the second module will be enabled and shown inside the player's inventory.\
Vice versa the second module triggers the state to `nightvision-off`

{% code title="custom-modules.yml" %}

```yaml
custom-modules:
  - name: TurnOnNightVisionModule
    enabled: true
    enabled-on-state: nightvision-off
    enable-state: nightvision-on
    disable-state: nightvision-off
    type: COMMAND_STATIC
    item: 
      type: OBSIDIAN
      name: '&bEnables night vision'
      lore: '&7Click to enable night vision'
    commands: 
       - "nightvision on"
  - name: TurnOffNightVisionModule
    enabled: true
    enabled-on-state: nightvision-on
    enable-state: nightvision-off
    disable-state: nightvision-on
    type: COMMAND_STATIC
    item: 
      type: DIAMOND
      name: '&bDisables night vision'
      lore: '&7Click to disable night vision'
    commands: 
       - "nightvision off"
```

{% endcode %}

### Modes config

To configure the mode I did the following:

* disable the default night vision of staff mode
* Disable night vision when exiting staff mode
* Set initial state to night vision off. So when you enter staff mode the obsidian block will be in your inventory
* We defined the module on the same inventory location. Due to the state only one will be shown at a time.

```yaml
modes:
  default:
    # other config here
    ...
    night-vision: false
    disable-commands:
    - command: nightvision off %staff%
    initial-item-states:
    - nightvision-off
    gui:
    - TurnOnNightVisionModule:1
    - TurnOffNightVisionModule:1
```

<figure><img src="https://3621425798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MPQohtcLxhXAvpSXOMt-3566562695%2Fuploads%2FejW7Oy6TcNFXwhDXRDdB%2Fnight_vision_states.gif?alt=media&#x26;token=64118160-c4a4-43f8-9c56-d3cdf00e9af9" alt=""><figcaption></figcaption></figure>
