mirror of
https://github.com/MineLittlePony/MineLittlePony.git
synced 2025-02-13 16:24:23 +01:00
Ignore case for keys and fix types when deserializing
This commit is contained in:
parent
f29b4af99d
commit
329e02ec56
2 changed files with 13 additions and 9 deletions
|
@ -17,7 +17,7 @@ public abstract class Config {
|
||||||
|
|
||||||
protected void initWith(Setting<?>... settings) {
|
protected void initWith(Setting<?>... settings) {
|
||||||
for (Setting<?> s : settings) {
|
for (Setting<?> s : settings) {
|
||||||
entries.putIfAbsent(s.name(), s.getDefault());
|
entries.putIfAbsent(s.name().toLowerCase(), s.getDefault());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ public abstract class Config {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.def = def;
|
this.def = def;
|
||||||
|
|
||||||
entries.putIfAbsent(name(), def);
|
entries.putIfAbsent(name().toLowerCase(), def);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -76,7 +76,7 @@ public abstract class Config {
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
default T get() {
|
default T get() {
|
||||||
T t = (T)config().entries.computeIfAbsent(name(), k -> getDefault());
|
T t = (T)config().entries.computeIfAbsent(name().toLowerCase(), k -> getDefault());
|
||||||
|
|
||||||
if (t == null) {
|
if (t == null) {
|
||||||
t = getDefault();
|
t = getDefault();
|
||||||
|
@ -92,7 +92,7 @@ public abstract class Config {
|
||||||
*/
|
*/
|
||||||
default void set(@Nullable T value) {
|
default void set(@Nullable T value) {
|
||||||
value = value == null ? getDefault() : value;
|
value = value == null ? getDefault() : value;
|
||||||
config().entries.put(name(), value);
|
config().entries.put(name().toLowerCase(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.minelittlepony.settings;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
@ -9,7 +10,6 @@ import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class JsonConfig extends Config {
|
public class JsonConfig extends Config {
|
||||||
|
@ -41,11 +41,15 @@ public class JsonConfig extends Config {
|
||||||
try {
|
try {
|
||||||
if (Files.exists(file)) {
|
if (Files.exists(file)) {
|
||||||
try (BufferedReader s = Files.newBufferedReader(file)) {
|
try (BufferedReader s = Files.newBufferedReader(file)) {
|
||||||
Map<String, Object> parsed = gson.fromJson(s, HashMap.class);
|
gson.fromJson(s, JsonObject.class).entrySet().forEach(entry -> {
|
||||||
|
String key = entry.getKey().toLowerCase();
|
||||||
|
|
||||||
if (parsed != null) {
|
if (entries.containsKey(key)) {
|
||||||
entries = parsed;
|
Object value = gson.getAdapter(entries.get(key).getClass()).fromJsonTree(entry.getValue());
|
||||||
}
|
|
||||||
|
entries.put(key, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
} catch (IOException ignored) { }
|
} catch (IOException ignored) { }
|
||||||
}
|
}
|
||||||
configFile = file;
|
configFile = file;
|
||||||
|
|
Loading…
Reference in a new issue