-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathConfig.java
More file actions
453 lines (402 loc) · 13 KB
/
Config.java
File metadata and controls
453 lines (402 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, only version 3 of the License.
*
* FalsePatternLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.config;
import org.intellij.lang.annotations.Language;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.function.BiConsumer;
/**
* A modern configuration system to replace the old and obtuse forge config system.
* <br>
* Note that just annotating a configuration class with {@link Config} is not enough, you MUST also register it using
* {@link ConfigurationManager#initialize(Class[])} or {@link ConfigurationManager#initialize(BiConsumer, Class[])}!
* <br>
* For each config field, the following annotations are available:
* <br>
* <br>
* <code>@Default*</code> - you MUST add these annotations, they define the default value of the given config
* (not having a default is an error.)
* <br>
* {@link Comment} - you SHOULD add this annotation to every config field and the config class itself, it will
* add a comment to the config file for users.
* <br>
* {@link LangKey} - you MAY add this to provide localization support.
* <br>
* {@link Ignore} - you MUST add this annotation to additional fields you don't want put in the config file
* <br>
* {@link RangeInt}, {@link RangeDouble} - you SHOULD add this to Int/IntList, Double/DoubleList configs respectively
* to define upper/lower bounds for the values.
* <br>
* {@link StringMaxLength} - you MAY add this to String/StringList configs if you know a definite maximum length
* for them.
* <br>
* {@link Pattern} - you MAY add this to String/StringList configs to restrict the values using regex.
* <br>
* {@link ListMaxLength} - you MAY add this to *List configs to limit their maximum length.
* <br>
* {@link ListFixedLength} - you MAY add this to *List configs to lock them to a fixed length.
* <br>
* {@link Name} - you SHOULD add this to config properties to give them user-friendly names in the config. SHOULD be
* "camelCase".
* <br>
* {@link RequiresMcRestart}, {@link RequiresWorldRestart} - You MUST add these to configs if they require a game or
* world reload accordingly, to avoid broken state!
* You MUST NOT add these to configs that are meant to be changed by the user while actively playing.
* <br>
* {@link Synchronize} - You MAY add this to the config class.
* Allows the config to be synchronized from server to client.
* Useful for keeping gameplay-specific state consistent.
* <br>
* {@link NoSync} - You MUST add this to fields that you don't want to be synchronized by {@link Synchronize}.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Config {
/**
* The mod id that this configuration is associated with.
* If {@link #customPath()} is not set, this is used for the config file name (.minecraft/config/modid.cfg)
* @see #customPath()
*/
String modid();
/**
* Root element category, defaults to "general". MUST NOT be an empty string.
*/
String category() default "general";
/**
* Define category migrations here.
* Used to rename config categories without losing data.
* @see #pathMigrations()
*
* @since 1.5.0
*/
String[] categoryMigrations() default {};
/**
* A custom configuration file path. Relative to the .minecraft/config folder.
* The .cfg is automatically appended if no file extension is provided.
* <br>
* Examples:
* <br>
* customPath = "MyMod/magic" -> .minecraft/config/MyMod/magic.cfg
* <br>
* customPath = "mymod.config" -> .minecraft/config/mymod.config
* <br>
* @see #modid()
*
* @since 1.5.0
*/
String customPath() default "";
/**
* Define file migration paths here.
* Used to relocate config files from older versions of the mod to a new location.
* <br>
* NOTE: This only moves the current category, so you can even use this to split up/combine files. If every
* category from an old config file has been migrated, the old config file is automatically deleted.
* <br>
* Only the FIRST successful migration is executed, and no migration happens if the new config file already contains
* the category of this config.
* @see #customPath()
* @see #categoryMigrations()
*
* @since 1.5.0
*/
String[] pathMigrations() default {};
// region Common
/**
* The description of the configuration or the category. Should be in the primary language of the mod.
* @see LangKey
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
@interface Comment {
String[] value();
}
/**
* The lang file key of this configuration. Used in config GUIs.
* <br>
* <code>{@link LangKey#value()} + ".tooltip"</code> is used for the hover tooltips. If the localization is not
* present, defaults to {@link Comment#value()}.
* <br>
* If left empty. it's autogenerated from the modid, category, and config name. (config.MODID.CATEGORY.NAME)
* <br>
* Also applies to categories, where it gets autogenerated from the modid and category (config.MODID.CATEGORY)
* @see Comment
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
@interface LangKey {
String value() default "";
}
/**
* If you have extra fields in the config class used for anything else other than configuring, you must annotate
* the using this so that the config engine doesn't pick them up.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Ignore {}
/**
* The name of this config property in the config file. If not specified, the field's name will be used instead.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Name {
String value();
/**
* Use this to migrate from old config names. "" is resolved to the name of the field
*
* @since 1.5.0
*/
String[] migrations() default {};
}
/**
* Whether the specific configuration needs a minecraft restart to be applied.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
@interface RequiresMcRestart {}
/**
* Whether the specific configuration needs a world/server rejoin to be applied.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
@interface RequiresWorldRestart {}
// endregion
// region Sync
/**
* Signals that this configuration class should be synchronized between the client and the server when
* joining a multiplayer instance.
* <p>
* Note that synchronization ALWAYS happens FROM the server TO the client. The server should NEVER attempt to get
* configuration values from a client. This is to avoid malicious clients manipulating the server configs.
*
* @since 0.10.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Synchronize {}
/**
* Use this to mark config fields you don't want to synchronize in a class marked with {@link Synchronize}.
*
* @since 0.10.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface NoSync {}
// endregion
// region Boolean
/**
* The default value for a boolean field. Not having a default is deprecated since 0.10, and will be strongly
* enforced in 0.11+!
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultBoolean {
boolean value();
}
// endregion
// region Int
/**
* The default value for an int field.
* @see RangeInt
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultInt {
int value();
}
/**
* The range of possible values an int config can have.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface RangeInt {
int min() default Integer.MIN_VALUE;
int max() default Integer.MAX_VALUE;
}
// endregion
// region Double
/**
* The default value for a double field.
* @see RangeDouble
*
* @since 0.10.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultDouble {
double value();
}
/**
* The range of possible values a double config can have.
*
* @since 0.10.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface RangeDouble {
double min() default -Double.MAX_VALUE;
double max() default Double.MAX_VALUE;
}
// endregion
// region String
/**
* The default value for a String field.
* @see StringMaxLength
* @see Pattern
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultString {
String value();
}
/**
* This annotation limits the maximum number of characters present in a string configuration.
* <p>
* When used with a string list, this limit will apply to each element individually, not to the size of the list as a whole.
*
* @since 0.10.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface StringMaxLength {
int value();
}
/**
* A regex pattern for restricting the allowed strings.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Pattern {
@Language("RegExp")
String value();
}
// endregion
// region Enum
/**
* The default value for an Enum field.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultEnum {
String value();
}
// endregion
// region Lists
/**
* The default value for a boolean array field.
* @see ListFixedLength
* @see ListMaxLength
*
* @since 0.10.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultBooleanList {
boolean[] value();
}
/**
* The default value for an int array field.
* @see ListFixedLength
* @see ListMaxLength
* @see RangeInt
*
* @since 0.10.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultIntList {
int[] value();
}
/**
* The default value for a double array field.
* @see ListFixedLength
* @see ListMaxLength
* @see RangeDouble
*
* @since 0.10.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultDoubleList {
double[] value();
}
/**
* The default value for a string array field.
* @see ListFixedLength
* @see ListMaxLength
* @see StringMaxLength
* @see Pattern
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultStringList {
String[] value();
}
/**
* If this annotation is present, the list in the config will be forced to have exactly the amount of elements as
* the default value.
* @see ListMaxLength
*
* @since 0.10.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface ListFixedLength {}
/**
* This annotation limits the maximum number of elements present in an array configuration. Only effective if
* {@link ListFixedLength} is NOT present.
*
* @since 0.10.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface ListMaxLength {
int value();
}
// endregion
}