Configs
A config (.jmc file) is Jumper cut down to data: values, variables, expressions and conditions. It is a replacement for YAML or JSON that can compute a value from another one, and it never runs anything dangerous: loops and Java are not part of the language.
Writing a config
The top-level variables are the config:
// server.jmc
int port = 25565;
boolean dev = false;
int maxPlayers = dev ? 5 : 100;
dyn motd = ["Welcome", "to the server"];
dyn db = { url: "jdbc:mysql://localhost/mc", pool: { min: 2, max: 10 } };
if (dev) {
maxPlayers = 1;
}Its value is the table {port: 25565, dev: false, maxPlayers: 100, motd: [...], db: {...}}, in declaration order. Variables that end up null are left out.
A config may instead end with return <value>;, and then that value is the config:
dyn ports = [25565, 25566];
return { main: ports[0], fallback: ports[1] };What a config may contain
| Allowed | Not allowed |
|---|---|
variable declarations (typed or dyn) and assignments | loops |
literals: numbers, strings, true, false, null, tables, arrays | functions and lambdas |
operators, ?:, + on strings | classes |
if / else, switch | import |
return | try / throw |
| comments | Java classes, built-in functions |
Types are checked as in scripts: int a = "s"; is an error. Anything not allowed is a syntax error with a line and a column:
Syntax error: A loop is not allowed in a config (.jmc) (line 1, col 1)Printing a config
The command line runs a .jmc file by printing its value:
java -jar jmp.jar server.jmc
{port: 25565, dev: false, maxPlayers: 100, motd: [Welcome, to the server], db: {url: jdbc:mysql://localhost/mc, pool: {min: 2, max: 10}}}The config of a plugin, in Java
The shortest way from a Java program to a config file and back is a record. Its components are the variables of the file, its values the defaults, and a comment on a component becomes a comment in the file:
import me.padej.jumper.interp.Config;
@Config.Comment("MyPlugin settings. Edit and /reload.")
record ServerConfig(
@Config.Comment("The port players connect to") int port,
boolean dev,
@Config.Comment("Shown in the server list") List<String> motd,
Mode mode,
Db db) {}
record Db(String url, @Config.Comment("Connections kept open") int poolMax) {}
enum Mode { SURVIVAL, CREATIVE }
static final ServerConfig DEFAULTS = new ServerConfig(
25565, false, List.of("Welcome", "to the server"), Mode.SURVIVAL, new Db("jdbc:h2:mem:mc", 10));
ServerConfig cfg = Config.loadOrCreate(Path.of("plugins/MyPlugin/server.jmc"), DEFAULTS);
cfg.port(); // 25565 until someone edits the file
cfg.db().poolMax();The first run finds no file and writes one from the defaults:
// MyPlugin settings. Edit and /reload.
// The port players connect to
int port = 25565;
boolean dev = false;
// Shown in the server list
dyn motd = ["Welcome", "to the server"];
String mode = "SURVIVAL";
dyn db = { url: "jdbc:h2:mem:mc", poolMax: 10 };Later runs read the file. A user may change values, add comments, delete lines they do not care about or write expressions (int port = dev ? 25566 : 25565;): the record gets the values the config computes, and whatever the file does not mention comes from DEFAULTS. When a new version of the plugin adds a component, loadOrCreate appends it to the file, with its comment, and does not touch a character of what was already there. Files that are a return <value>; or that assign a variable again further down are left alone (the defaults still apply in memory).
Config.loadOrCreate(Path, defaults) | read into an object like defaults; write the file from defaults if it is missing; append the variables it lacks |
Config.load(Path, defaults) | the same without writing anything; a missing file gives defaults |
Config.load(Path, Class) | read into a record or class, no defaults: a missing primitive is an error, a missing reference is null |
Config.parse(String, Class) | the same for config text |
Config.bind(value, Class) | a value from Config.load(Path) or Config.parse(String) as an object |
Config.write(Path, object) | write an object as a config, comments included |
Types
| Component | Config value |
|---|---|
int, long, double, float, short, byte, boolean, char and their boxes | a number of that size or smaller (an int fits a long or a double, never the other way round), true/false, a one-character string |
String | a string |
| an enum | a string with the name of a constant |
List<T>, Set<T>, Collection<T>, T[] | an array whose elements are T |
Map<String, V> | a table whose values are V, in the file's order |
| another record, or a class with public fields | a table |
Object, JTable, JArray | the value as Config.load(Path) gives it, unchecked |
A class works like a record when it has a no-arg constructor and public fields; a field the config does not mention keeps what its initializer gave it. Variables in the file that the type does not have are ignored, so a config may hold helper variables (boolean dev = ... used only in expressions).
A value that does not fit is an IllegalArgumentException naming the variable and the types, with the file in front when the config came from a file:
plugins/MyPlugin/server.jmc: db.poolMax: expected an int, got the String "ten"
mode: "PEACEFUL" is not one of SURVIVAL, CREATIVE
motd[1]: expected a String, got the int 2
dev: the config has no value for this booleanReading a config as a table
Without a record, Config.load gives the value of the file: a JTable for a config of variables (or whatever return gives). Numbers come as Integer, Long, Double; strings as String; arrays as a List; tables as JTable.
JTable cfg = (JTable) Config.load(Path.of("server.jmc"));
int port = cfg.getInt("port", 25565); // the default when the key is missing
List<?> motd = cfg.getArray("motd"); // null when missing
int max = cfg.getTable("db").getTable("pool").getInt("max", 10);
boolean dev = cfg.getBoolean("dev", false);JTable | |
|---|---|
getInt, getLong, getDouble, getBoolean, getString (key, default) | the value, or the default when the key is missing; an int reads as a long or a double |
getTable(key), getArray(key) | the table or array, or null |
get(key), has(key), keys(), size(), asMap() | the raw value and the rest of the table |
A value of another type is a JmpError that names the key: 'port' is the String "x", not an int. The plain get gives an Object to cast yourself.
Config.load(Path) | read and evaluate a file |
Config.parse(String) | evaluate config text |
A config that breaks the rules throws ParseError (me.padej.jumper.parser); an error while computing a value throws JmpError. Both carry the line.
A config runs with no access to Java at all, whatever policy the host uses for scripts, so loading a config written by someone else is safe.
Writing a config from Java
Config.write writes a value as a .jmc file. The value may be a record or an object as above, or plain Java: a Map for a table, a List for an array, strings, numbers and booleans.
Config.write(Path.of("server.jmc"), Config.table(
"port", 25565,
"dev", false,
"motd", List.of("Welcome", "to the server"),
"db", Config.table("url", "jdbc:mysql://localhost/mc", "pool", Config.table("min", 2, "max", 10))));writes this file, which reads back to the same values:
int port = 25565;
boolean dev = false;
dyn motd = ["Welcome", "to the server"];
dyn db = {
url: "jdbc:mysql://localhost/mc",
pool: { min: 2, max: 10 }
};Config.write(Path, value) | write the file (UTF-8). Missing folders are created, and an existing file is replaced in one step |
Config.write(Path, value, header) | the same with a comment at the top of the file |
Config.format(value), Config.format(value, header) | the text as a String, without writing anything |
Config.table(key, value, ...) | a table with the keys in the order given; the helper for write and format |
The order of the entries is the order of the Map they come from: Config.table and LinkedHashMap keep the order given, a HashMap does not. List.of and Arrays.asList are written as arrays.
What gets written
| Java value | in the file |
|---|---|
String, Character, enum | a string: "text" (escapes written for you) |
Integer, Short, Byte | 25565 |
Long | 100L |
Double, Float | 0.75, 1.0E10 |
Boolean | true, false |
null | left out of a table; kept as null inside an array |
Map, JTable, a record, an object with public fields | a table: { key: value }; the comments of a record go in the file too |
List, JArray, other collections, Java arrays | an array: [1, "two"] |
Values are checked as they are written. A NaN or infinite number, an object that is none of the above (a BigInteger, a Path, a class without public fields), an object that contains itself and a table with two keys that become the same text throw IllegalArgumentException with the reason, and no file is touched.
Keys are written as strings. A key that is a plain name (letters, digits and _, not starting with a digit, not a keyword) is written bare; any other key is quoted: "my key": 1, "class": 1.
The layout follows the value. A table or array that holds another non-empty table or array goes over lines, one entry per line, indented by four spaces. Anything else goes on one line if it fits in 100 columns. A comment on a component gets a blank line around its declaration.
Top-level variables
When every top-level key is a plain name, the file is a list of declarations, one per key, in order. Each gets the type of its value: int, long, double, boolean, String, or dyn for tables and arrays. Otherwise, for example when a key has a space or is a keyword, the file is return { ... }; with the whole table as its value. Both forms read back as the same table. A top level that is not a table is written as return <value>;.
Config.write(Path.of("list.jmc"), List.of(1, 2, 3)); // return [1, 2, 3];
Config.format(Config.table("ban list", List.of("x"))); // return { "ban list": ["x"] };Writing a config of this size takes a couple of microseconds and allocates a few kilobytes; the file system is the slow part.
Changing values in a file
Config.update changes the values of top-level variables and leaves everything else in the file - comments, blank lines, the other variables and their expressions - exactly as it was. This is the way to save a setting into a file that people also edit by hand:
Config.update(file, "port", 25566);
Config.update(file, Map.of("dev", true, "motd", List.of("Back online")));// Server settings
boolean dev = true; // was false
int port = 25566; // was `dev ? 25566 : 25565`: the expression gives way to the value
dyn motd = ["Back online"];
String name = "unchanged"; // not mentioned: not touched- The expression of the variable is replaced by the value. A typed declaration keeps its type when the value fits (
long big = 6;) and changes it otherwise (String port = "x";). - A variable the file does not declare is appended at the end. A
nullvalue (in a map that allows one, notMap.of) removes the declaration, with the comment lines right above it. - A file that does not exist is created from the changes.
Config.update(String, Map)does the same on config text and gives the new text.
The new text is read back before the file is written and every changed variable is checked. Two kinds of file are refused with an IllegalStateException, and left as they were: a config that sets the variable again lower down (if (dev) { port = 1; } - its declaration is not where the value comes from) and a config that is return <value>;, which has no top-level variables. Write such files whole with Config.write.
Writing a loaded config back
A value read with Config.load is an ordinary JTable (or List, or whatever the file returns), so it can be changed and written whole:
JTable cfg = (JTable) Config.load(Path.of("server.jmc"));
cfg.put("port", 25566);
Config.write(Path.of("server.jmc"), cfg);This loses the comments, and expressions become their values: int permLevel = isDev ? 10 : 0; is written as int permLevel = 0;. It suits files that the program owns. For a file that people edit, use Config.update.