Created
October 28, 2024 18:08
-
-
Save MeexReay/64059ac7c41562976d41ec26ca29f1ef to your computer and use it in GitHub Desktop.
Simple YAML config manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.themixray.config; | |
import org.yaml.snakeyaml.Yaml; | |
import java.io.*; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class UnrealConfig extends UnrealObject<String> { | |
private static final Yaml yaml = new Yaml(); | |
private final File file; | |
public UnrealConfig(Object plugin, Path dataDirectory, String filename) { | |
super(new HashMap<>()); | |
file = Paths.get(dataDirectory.toFile().getPath(), filename).toFile(); | |
if (!dataDirectory.toFile().exists()) { | |
dataDirectory.toFile().mkdir(); | |
} | |
if (!file.exists()) { | |
try (InputStream stream = plugin.getClass().getClassLoader().getResourceAsStream(filename)) { | |
assert stream != null; | |
Files.copy(stream, file.toPath()); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
reload(); | |
} | |
public void reload() { | |
try { | |
clear(); | |
putAll(yaml.load(new FileInputStream(file))); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void save() { | |
try { | |
yaml.dump(this,new FileWriter(file)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public Map<String,Object> clone() { | |
return new HashMap<>(this); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.themixray.config; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class UnrealObject<K> extends HashMap<K,Object> { | |
public UnrealObject(Map<K,Object> map) { | |
putAll(map); | |
} | |
public Integer getInteger(K key) { | |
if (!containsKey(key)) return null; | |
return ((Number)get(key)).intValue(); | |
} | |
public Float getFloat(K key) { | |
if (!containsKey(key)) return null; | |
return ((Number)get(key)).floatValue(); | |
} | |
public Double getDouble(K key) { | |
if (!containsKey(key)) return null; | |
return ((Number)get(key)).doubleValue(); | |
} | |
public Long getLong(K key) { | |
if (!containsKey(key)) return null; | |
return ((Number)get(key)).longValue(); | |
} | |
public Short getShort(K key) { | |
if (!containsKey(key)) return null; | |
return ((Number)get(key)).shortValue(); | |
} | |
public Byte getByte(K key) { | |
if (!containsKey(key)) return null; | |
return ((Number)get(key)).byteValue(); | |
} | |
public String getString(K key) { | |
return (String) get(key); | |
} | |
public Boolean getBoolean(K key) { | |
return (Boolean) get(key); | |
} | |
public UnrealObject getMap(K key) { | |
if (!containsKey(key)) return null; | |
return new UnrealObject((Map<String,Object>)get(key)); | |
} | |
public UnrealObject getList(K key) { | |
if (!containsKey(key)) return null; | |
Map<Integer,Object> m = new HashMap<>(); | |
int i = 0; | |
for (Object o : ((List<Object>) get(key))) { | |
m.put(i,o); | |
i++; | |
} | |
return new UnrealObject(m); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment