Custom config adapters

You can create your own ConfigAdapter for any type you want, as long as you can write the logic to deserialize it.

A ConfigAdapter needs to implement the getType method, which returns the class of the type that the config adapter is converting to, and the convert method. The convert method takes in an object and tries to convert it to the config adapter's type.

Since we use Bukkit's deserialization logic, the provided object can be anything that ConfigurationSection#get can return.

It's recommended to look at some of Rebar's builtin config adapters to learn how they work if you are confused on how to write a new one.

Example: UUID config adapter

class UUIDConfigAdapter implements ConfigAdapter<UUID> {

    @Override
    public @NotNull Type getType() {
        return UUID.class;
    }

    @Override
    public UUID convert(@NotNull Object value) {
        if (value instanceof String string) {
            return java.util.UUID.fromString(string);
        }

        List<Long> longs = ConfigAdapter.LIST.from(ConfigAdapter.LONG).convert(value);
        Preconditions.checkState(longs.size() == 2, "Expected a list of 2 longs for UUID, got " + longs.size());
        return new UUID(longs.get(0), longs.get(1));
    }
}

Tip

Test your config adapter with invalid inputs to check that the error messages it gives are useful.

Tip

Often it can be simpler to just use ConfigSection's methods than to write a new ConfigAdapter.