# Configuration

## File-related methods

```java
Config.init();
Config.reload();
Config.save();
```

* `init()` - Creates the configuration file if it doesn't exist and then saves the configuration file.
* `reload()` - Reloads (reads) the configuration from the file.
* `save()` - Saves (writes) the current configuration to the file.

## Retrieving values

{% hint style="info" %}
The Jetbrains IntelliJ IDEA Lombok plugin will cause issues with the method name of `val`. You can disable this in your IDE settings.
{% endhint %}

**Types**

* boolean
* double
* float
* int
* ItemBuilder
* ItemStack
* List<...> (ConfigList)
* Location
* Material
* Text
* UUID
* Object
* T (if you provide Class\<T>)

Anyways let's retrieve a join message.

```java
Component component = Config.key("join.join_message") // ConfigKey
        .val() // ConfigVal
        .asText() // Text
        .component(); // Component
```

## Modifying values

### Updating/Setting

There are 3 methods all named `set()`. The one without any parameters will lead you to a builder, and the rest are just shorter methods to do the same thing as the builder.

**Types**

* Object
* Text
* Location
* ItemStack
* ItemBuilder
* List\<?>
* int
* double
* long
* float
* boolean

Default value will make it so what you set is going to be the default for the config if the value ever disappears, instead of returning null.

We'll use the builder for this.

```java
Config.set()
        .key(Config.key("join.join_message"))
        .defaultVal(true)
        .val(Text.text("<green>A mysterious player has joined..."))
        .build();
```

### Deletion

`Config` has 2 methods.

1. `delete(ConfigKey)`
2. `delete(ConfigSection)`

I don't think I need to document those self explanatory methods.

## Sections

A `ConfigSection` is a key which holds keys inside of it.

In our case, in `join.join_message` the `join` key is a configuration section.

So we can retrieve a configuration section by doing:

```java
ConfigSection /* ... */ = Config.section("join");
```

### Retrieving Values

If we want to retrieve the `join_message` key from the `join` configuration section:

```java
Object object = Config.section("join").get("join_message");
String miniMessage = (String) object;
Component component = TextStyle.style(miniMessage);
```

### Modifying Values

#### Updating/Setting

Use `section.set()`

Same as setting a normal key to a value. It will return a ConfigSectionSetter.

#### Deletion

Use `section.delete()`

**This will delete both the section and also the keys inside of it.**

{% hint style="info" %}
If you want to delete just a key, use `section.set()` and make the value `null`.
{% endhint %}

### Selection

This accepts a `ConfigSection`. It lets you iterate over it easily (using a `forEach()` method)

```java
Config.selection(Config.section("join")).forEach(keyVal -> {
    ConfigKey key = keyVal.key();
    ConfigVal val = keyVal.val();

    // prints out the name of the key.
    System.out.println(key.path);
});
```


---

# 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://manered.gitbook.io/utils/docs/configuration.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.
