Adding configuration base (implement to Enum)
This commit is contained in:
parent
44f74edd67
commit
9e20f15e71
138
src/main/java/fr/molzonas/mzcore/MZConfig.java
Normal file
138
src/main/java/fr/molzonas/mzcore/MZConfig.java
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
package fr.molzonas.mzcore;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public class MZConfig {
|
||||||
|
private final JavaPlugin plugin;
|
||||||
|
private final FileConfiguration config;
|
||||||
|
private final ConcurrentHashMap<MZConfigKey, Object> cache = new ConcurrentHashMap<>();
|
||||||
|
@Setter @Getter
|
||||||
|
private boolean cacheEnabled = true;
|
||||||
|
|
||||||
|
public MZConfig(JavaPlugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.config = this.plugin.getConfig();
|
||||||
|
}
|
||||||
|
public MZConfig(JavaPlugin plugin, FileConfiguration config) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reload() {
|
||||||
|
cache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ensureDefault(MZConfigKey[] keys) {
|
||||||
|
for (MZConfigKey k : keys) {
|
||||||
|
if (!config.isSet(k.getPath())) config.set(k.getPath(), k.getDefaultValue());
|
||||||
|
}
|
||||||
|
plugin.saveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> T get(MZConfigKey key) {
|
||||||
|
return (T) this.get(key, key.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean exists(MZConfigKey key) {
|
||||||
|
return config.isSet(key.getPath());
|
||||||
|
}
|
||||||
|
public boolean exists(String path) {
|
||||||
|
return config.isSet(path);
|
||||||
|
}
|
||||||
|
public String get(String path) {
|
||||||
|
return config.getString(path);
|
||||||
|
}
|
||||||
|
public <T> T get(String path, Class<T> expectedType) {
|
||||||
|
return expectedType.cast(coerce(config.get(path), expectedType));
|
||||||
|
}
|
||||||
|
public <T> T get(MZConfigKey key, Class<T> expectedType) {
|
||||||
|
if (this.isCacheEnabled() && cache.containsKey(key)) {
|
||||||
|
return expectedType.cast(coerce(cache.get(key), expectedType));
|
||||||
|
}
|
||||||
|
Object raw = config.get(key.getPath(), key.getDefaultValue());
|
||||||
|
if (raw == null) return null;
|
||||||
|
if (this.isCacheEnabled()) cache.put(key, raw);
|
||||||
|
return expectedType.cast(coerce(raw, expectedType));
|
||||||
|
}
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> List<T> getList(MZConfigKey key) {
|
||||||
|
return (List<T>) getList(key, key.getType());
|
||||||
|
}
|
||||||
|
public <T> List<T> getList(MZConfigKey key, Class<T> expectedType) {
|
||||||
|
Object raw = config.get(key.getPath());
|
||||||
|
List<?> list;
|
||||||
|
if (raw instanceof List) {
|
||||||
|
list = (List<?>) raw;
|
||||||
|
} else if (raw == null) {
|
||||||
|
Object defaultValue = key.getDefaultValue();
|
||||||
|
list = (defaultValue instanceof List<?> dl) ? dl : List.of();
|
||||||
|
} else {
|
||||||
|
list = List.of(raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<T> result = new ArrayList<>(list.size());
|
||||||
|
for (Object o : list) {
|
||||||
|
if (o == null) continue;
|
||||||
|
try {
|
||||||
|
Object c = coerce(o, expectedType);
|
||||||
|
result.add(expectedType.cast(c));
|
||||||
|
} catch (ClassCastException e) {
|
||||||
|
plugin.getLogger().warning("Config '" + key.getPath() + "' [" + e + "] is invalid.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private Object coerce(Object raw, Class<?> target) {
|
||||||
|
if (raw == null || target.isInstance(raw)) return raw;
|
||||||
|
if (target == String.class) return parseString(raw);
|
||||||
|
if (target == Boolean.class) return parseBoolean(raw);
|
||||||
|
if (target == Integer.class) return parseInteger(raw);
|
||||||
|
if (target == Double.class) return parseDouble(raw);
|
||||||
|
if (target == Float.class) return parseFloat(raw);
|
||||||
|
if (target == Long.class) return parseLong(raw);
|
||||||
|
return raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String parseString(Object raw) {
|
||||||
|
return raw == null ? "" : String.valueOf(raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Boolean parseBoolean(Object raw) {
|
||||||
|
return raw != null && Boolean.parseBoolean(parseString(raw));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Integer parseInteger(Object raw) {
|
||||||
|
if (raw == null) return null;
|
||||||
|
if (raw instanceof Number n) return n.intValue();
|
||||||
|
return Integer.parseInt(parseString(raw));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Long parseLong(Object raw) {
|
||||||
|
if (raw == null) return null;
|
||||||
|
if (raw instanceof Number n) return n.longValue();
|
||||||
|
return Long.parseLong(parseString(raw));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Double parseDouble(Object raw) {
|
||||||
|
if (raw == null) return null;
|
||||||
|
if (raw instanceof Number n) return n.doubleValue();
|
||||||
|
else return Double.parseDouble(parseString(raw));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Float parseFloat(Object raw) {
|
||||||
|
if (raw == null) return null;
|
||||||
|
if (raw instanceof Number n) return n.floatValue();
|
||||||
|
return Float.parseFloat(parseString(raw));
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/main/java/fr/molzonas/mzcore/MZConfigKey.java
Normal file
18
src/main/java/fr/molzonas/mzcore/MZConfigKey.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package fr.molzonas.mzcore;
|
||||||
|
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
|
||||||
|
public interface MZConfigKey {
|
||||||
|
String getPath();
|
||||||
|
Object getDefaultValue();
|
||||||
|
Class<?> getType();
|
||||||
|
|
||||||
|
|
||||||
|
default Object getRaw(FileConfiguration cfg) {
|
||||||
|
return cfg.isSet(getPath()) ? cfg.get(getPath()) : getDefaultValue();
|
||||||
|
}
|
||||||
|
default <T> T get(FileConfiguration cfg, Class<T> expected) {
|
||||||
|
Object raw = getRaw(cfg);
|
||||||
|
return raw == null ? null : expected.cast(raw);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +0,0 @@
|
|||||||
package fr.molzonas.mzcore;
|
|
||||||
|
|
||||||
public class MZProperties {
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user