Skip to content

Commit 1b14feb

Browse files
committed
update config README
1 parent 371de49 commit 1b14feb

File tree

1 file changed

+83
-30
lines changed

1 file changed

+83
-30
lines changed

libraries/config/README.md

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ The Config API provides a framework for building and storing configs for your mo
44

55
## Creating a config
66

7-
The easiest way to create a config is to extend the `net.ornithemc.osl.config.api.config.BaseConfig` class:
7+
The easiest way to create a config is to extend the `net.ornithemc.osl.config.api.config.BaseConfig` class.
8+
In order to make serialization work, you must register all your options in the `init` method. With the `registerOptions(String group, Option... options)` method, you can register an entire option group at once.
9+
An option group is a collection of options with a name. It's a useful tool to categoryize your options if you have
10+
many. You can of course also add all your options in just one group.
811

912
```java
1013
package com.example;
@@ -90,6 +93,11 @@ The API allows you to provide custom option types, so long as you register the r
9093
Network serializers must be provided, as well as file serializers for the type your config uses.
9194
These serializers should be registered in your mod's entrypoint.
9295

96+
Custom options of immutable types are the most straightforward. Immutable types are classes that do not
97+
allow their data to be modified. Immutable types include primitives such as `boolean`, `float`, and `int`,
98+
but classes such as `String`, `UUID`, and `Path` are also examples of immutable types. Options for these
99+
types should made by extending the `BaseOption` class.
100+
93101
```java
94102
package com.example;
95103

@@ -108,66 +116,111 @@ public class CookieOption extends BaseOption<Cookie> {
108116
}
109117
}
110118
```
119+
120+
Custom options of mutable types are more involved. Mutable types are classes that allow their data to be modified.
121+
Mutable types include collections like `List` and `Map`, but classes such as Minecraft's `NbtList` and `NbtCompound`
122+
are also examples of mutable types. Options for these types should be made by extending the `ModifiableOption` class.
123+
Using this class does require that your objects of your custom type can be converted to a 'modifiable view' (which it
124+
most likely is by default) and an 'unmodifiable view'. The reason for this is that the config should be able keep track
125+
of when the option's value is modified. Providing an unmodifiable view allows the option to force modifications to happen
126+
through the option's `modify` method.
127+
111128
```java
112129
package com.example;
113130

114-
import java.io.IOException;
131+
import java.util.function.Predicate;
115132

116-
import net.minecraft.nbt.NbtCompound;
117-
import net.minecraft.network.PacketByteBuf;
133+
import net.ornithemc.osl.config.api.config.option.ModifiableOption;
118134

119-
import net.ornithemc.osl.config.api.serdes.SerializationSettings;
120-
import net.ornithemc.osl.config.api.serdes.config.option.JsonOptionSerializer;
135+
public class CookieOption extends ModifiableOption<Cookie> {
136+
137+
public CookieOption(String name, String description, Cookie defaultValue) {
138+
super(name, description, defaultValue);
139+
}
140+
141+
public CookieOption(String name, String description, Cookie defaultValue, Predicate<Cookie> validator) {
142+
super(name, description, defaultValue, validator);
143+
}
144+
145+
@Override
146+
protected Cookie modifiable(Cookie value) {
147+
// convert to modifiable view
148+
}
149+
150+
@Override
151+
protected Cookie unmodifiable(Cookie value) {
152+
// convert to unmodifiable view
153+
}
154+
}
155+
```
156+
157+
Option serialization can be done in two ways: through custom object serializers or through custom option serializers.
158+
The former is recommended, but in some cases (such as generic options) the latter is preferable. Custom serializers
159+
should all be registered in your mod's entrypoint.
160+
161+
If you're registering a custom object serializer, then registering the option serializer simplifies to a one-liner.
162+
163+
```java
164+
package com.example;
165+
166+
import java.io.IOException;
167+
168+
import net.ornithemc.osl.config.api.serdes.JsonSerializer;
169+
import net.ornithemc.osl.config.api.serdes.JsonSerializers;
121170
import net.ornithemc.osl.config.api.serdes.config.option.JsonOptionSerializers;
122-
import net.ornithemc.osl.config.api.serdes.config.option.NbtOptionSerializer;
123-
import net.ornithemc.osl.config.api.serdes.config.option.NbtOptionSerializers;
124-
import net.ornithemc.osl.config.api.serdes.config.option.NetworkOptionSerializer;
125-
import net.ornithemc.osl.config.api.serdes.config.option.NetworkOptionSerializers;
126171
import net.ornithemc.osl.core.api.json.JsonFile;
127172
import net.ornithemc.osl.entrypoints.api.ModInitializer;
128173

129174
public class ExampleInitializer implements ModInitializer {
130175

131176
@Override
132177
public void init() {
133-
JsonOptionSerializers.register(CookieOption.class, new JsonOptionSerializer<CookieOption>() {
178+
JsonSerializers.register(Cookie.class, new JsonSerializer<Cookie>() {
134179

135180
@Override
136-
public void serialize(CookieOption option, SerializationSettings settings, JsonFile json) throws IOException {
181+
public void serialize(Cookie value, JsonFile json) throws IOException {
137182
// write value to json
138183
}
139184

140185
@Override
141-
public void deserialize(CookieOption option, SerializationSettings settings, JsonFile json) throws IOException {
186+
public void deserialize(Cookie value, JsonFile json) throws IOException {
142187
// read value from json
143188
}
144189
});
145-
NbtOptionSerializers.register(CookieOption.class, new NbtOptionSerializer<CookieOption>() {
190+
JsonOptionSerializers.register(CookieOption.class, Cookie.class);
191+
}
192+
}
193+
```
146194

147-
@Override
148-
public void serialize(CookieOption option, SerializationSettings settings, NbtCompound nbt) throws IOException {
149-
// write value to nbt
150-
}
195+
A custom option serializer may look as follows.
151196

152-
@Override
153-
public void deserialize(CookieOption option, SerializationSettings settings, NbtCompound nbt) throws IOException {
154-
// read value from nbt
155-
}
156-
});
157-
NetworkOptionSerializers.register(CookieOption.class, new NetworkOptionSerializer<CookieOption>() {
197+
```java
198+
package com.example;
199+
200+
import java.io.IOException;
201+
202+
import net.ornithemc.osl.config.api.serdes.SerializationSettings;
203+
import net.ornithemc.osl.config.api.serdes.config.option.JsonOptionSerializer;
204+
import net.ornithemc.osl.config.api.serdes.config.option.JsonOptionSerializers;
205+
import net.ornithemc.osl.core.api.json.JsonFile;
206+
import net.ornithemc.osl.entrypoints.api.ModInitializer;
207+
208+
public class ExampleInitializer implements ModInitializer {
209+
210+
@Override
211+
public void init() {
212+
JsonOptionSerializers.register(CookieOption.class, new JsonOptionSerializer<CookieOption>() {
158213

159214
@Override
160-
public void serialize(CookieOption option, SerializationSettings settings, PacketByteBuf buffer) throws IOException {
161-
// write value to buffer
215+
public void serialize(CookieOption option, SerializationSettings settings, JsonFile json) throws IOException {
216+
// write value to json
162217
}
163218

164219
@Override
165-
public void deserialize(CookieOption option, SerializationSettings settings, PacketByteBuf buffer) throws IOException {
166-
// read value from buffer
220+
public void deserialize(CookieOption option, SerializationSettings settings, JsonFile json) throws IOException {
221+
// read value from json
167222
}
168223
});
169-
170-
...
171224
}
172225
}
173226
```

0 commit comments

Comments
 (0)