Skip to content

Commit 5f047bb

Browse files
config: Add forceDefault option
1 parent 19036ae commit 5f047bb

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

libraries/config/src/main/java/com/fancyinnovations/config/Config.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@ public void addField(ConfigField<?> field) {
4545
*/
4646
public <T> T get(String path) {
4747
ConfigField<?> field = fields.get(path);
48-
if (field != null) {
49-
Object value = values.computeIfAbsent(path, k -> field.defaultValue());
50-
return (T) field.type().cast(value);
48+
if (field == null) {
49+
return null;
5150
}
5251

53-
return null;
52+
if (field.forceDefault()) {
53+
return (T) field.defaultValue();
54+
}
55+
56+
Object value = values.computeIfAbsent(path, k -> field.defaultValue());
57+
return (T) field.type().cast(value);
5458
}
5559

5660
/**

libraries/config/src/main/java/com/fancyinnovations/config/ConfigField.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
* @param description a description of the field
99
* @param forRemoval indicates if this field should be removed
1010
* @param defaultValue the default value of the field
11+
* @param forceDefault indicates if the default value should be enforced
1112
* @param type the class type of the field value
1213
*/
1314
public record ConfigField<T>(
1415
String path,
1516
String description,
1617
boolean forRemoval,
1718
T defaultValue,
19+
boolean forceDefault,
1820
Class<T> type
1921
) {
2022
}

plugins/fancyholograms/src/main/java/de/oliver/fancyholograms/config/FHConfiguration.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ public final class FHConfiguration implements HologramConfiguration {
2020

2121
public void init() {
2222
config = new Config(FancyHologramsPlugin.get().getFancyLogger(), CONFIG_FILE_PATH);
23-
23+
2424
config.addField(new ConfigField<>(
2525
MUTE_VERSION_NOTIFICATION_PATH,
2626
"Whether version notifications are muted.",
2727
false,
2828
false,
29+
false,
2930
Boolean.class
3031
));
3132

@@ -34,6 +35,7 @@ public void init() {
3435
"Whether autosave is enabled.",
3536
false,
3637
true,
38+
false,
3739
Boolean.class
3840
));
3941

@@ -42,6 +44,7 @@ public void init() {
4244
"The interval at which autosave is performed in minutes.",
4345
false,
4446
15,
47+
false,
4548
Integer.class
4649
));
4750

@@ -50,6 +53,7 @@ public void init() {
5053
"Whether the plugin should save holograms when they are changed.",
5154
false,
5255
true,
56+
false,
5357
Boolean.class
5458
));
5559

@@ -58,6 +62,7 @@ public void init() {
5862
"The default visibility distance for holograms.",
5963
false,
6064
20,
65+
false,
6166
Integer.class
6267
));
6368

@@ -66,6 +71,7 @@ public void init() {
6671
"Whether the plugin should register its commands.",
6772
false,
6873
true,
74+
false,
6975
Boolean.class
7076
));
7177

@@ -74,6 +80,7 @@ public void init() {
7480
"The log level for the plugin (DEBUG, INFO, WARN, ERROR).",
7581
false,
7682
"INFO",
83+
false,
7784
String.class
7885
));
7986

0 commit comments

Comments
 (0)