-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
Petrus Pradella edited this page Jun 27, 2026
·
6 revisions
This walks from an empty file to a typed, documented, reloadable config.
import br.com.finalcraft.everyconfig.config.Config;
import br.com.finalcraft.everyconfig.codec.jackson.YamlCodec;
import java.nio.file.Paths;
Config cfg = Config.open(Paths.get("server.yml"), new YamlCodec());Config.open never throws on a bad file: an absent file starts empty, and a malformed one is backed up to
.bak and starts empty (see Lifecycle, Reload & Watching).
cfg.setValue("server.host", "localhost"); // auto-vivifies the "server" object
cfg.setValue("server.port", 25565);
String host = cfg.getString("server.host");
int port = cfg.getInt("server.port", 25565); // 2nd arg is the default if absentgetOrSetDefaultValue writes the default (and a comment) only if the path is absent, so it is safe to call
on every startup:
int maxPlayers = cfg.getOrSetDefaultValue("server.max-players", 20, "hard player cap");
boolean pvp = cfg.getOrSetDefaultValue("server.pvp", true, "allow player-vs-player");cfg.save(); // atomic write; comments and key order preservedThe resulting server.yml:
server:
host: localhost
port: 25565
# hard player cap
max-players: 20
# allow player-vs-player
pvp: trueclass ServerConfig {
public String host = "localhost";
public int port = 25565;
@Key(transformCase = KeyTransformCase.KEBAB_CASE)
public int maxPlayers = 20; // <-> key "max-players"
@PostInject
void validate() {
if (port < 1 || port > 65535) throw new IllegalStateException("bad port");
}
}
ServerConfig sc = cfg.loadAs(ServerConfig.class, new YamlCodec()); // bind + run @PostInject
sc.maxPlayers = 50;
cfg.mergeFrom(sc, new YamlCodec()); // merge back into the tree (unknown keys survive)
cfg.save();Everything above stays identical; only the codec (and extension) change:
Config cfg = Config.open(Paths.get("server.toml"), new TomlCodec());
// or new JsonCodec() / new JsoncCodec()→ Next: Architecture Overview · The Dynamic API
EveryConfig · br.com.finalcraft:EveryConfig · One config API, every format, comments included · made by Petrus Pradella
Getting Started
Core Concepts
Typed Binding
Operations
Reference
Contributing