-
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathConfigNullFieldsFix.java
More file actions
47 lines (38 loc) · 1.45 KB
/
ConfigNullFieldsFix.java
File metadata and controls
47 lines (38 loc) · 1.45 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
package de.hysky.skyblocker.config;
import java.lang.reflect.Field;
import org.slf4j.Logger;
import com.mojang.logging.LogUtils;
import dev.isxander.yacl3.config.v2.api.SerialEntry;
/**
* While this sounds like a data fixer it isn't. - It's the only reasonable solution to deal with the mine field
* that is YACL's null handling.
*/
public class ConfigNullFieldsFix {
private static final Logger LOGGER = LogUtils.getLogger();
public static void init() {
SkyblockerConfigManager.update(config -> {
try {
fixNullFields(config, new SkyblockerConfig());
} catch (Exception e) {
LOGGER.error("[Skyblocker Config Null Fields Fixer] Failed to ensure that the config has no null fields! You may encounter crashes :(", e);
}
});
}
/**
* Traverse through every config field to ensure that is isn't null, if it is then reset the value.
*/
private static void fixNullFields(Object config, Object defaultConfig) throws Exception {
for (Field field : config.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(SerialEntry.class)) {
field.setAccessible(true);
Object configValue = field.get(config);
Object defaultValue = field.get(defaultConfig);
if (configValue == null && defaultValue != null) {
field.set(config, defaultValue);
} else if (configValue != null && defaultValue != null && SkyblockerConfigManager.isConfigClass(field.getType())) {
fixNullFields(configValue, defaultValue);
}
}
}
}
}