Raxen/src/main/java/me/unurled/raxen/config/GlyphConfig.java

74 lines
2.5 KiB
Java

package me.unurled.raxen.config;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import me.unurled.raxen.Raxen;
import me.unurled.raxen.components.glyph.GlyphBuilder;
import me.unurled.raxen.components.glyph.GlyphParser;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
public class GlyphConfig {
private Raxen main;
private File glyphFolder;
public GlyphConfig(Raxen main) {
this.main = main;
validatesConfig();
}
public void validatesConfig() {
glyphFolder = new File(main.getDataFolder(), "items");
if (!glyphFolder.exists()) {
glyphFolder.mkdirs();
}
}
public Map<File, Map<String, GlyphBuilder>> parseGlyphConfigs() {
Map<File, Map<String, GlyphBuilder>> parseMap = new LinkedHashMap<>();
List<File> config = Arrays.stream(getGlyphFiles())
.filter(file -> file.getName().endsWith(".yml"))
.toList();
for (File file : config) {
parseMap.put(file, parseGlyphConfig(YamlConfiguration.loadConfiguration(file), file));
}
return parseMap;
}
public Map<String, GlyphBuilder> parseGlyphConfig(YamlConfiguration config, File file) {
Map<String, GlyphParser> parseMap = new LinkedHashMap<>();
for (String glyphSelection : config.getKeys(false)) {
if (!config.isConfigurationSection(glyphSelection)) {
continue;
}
ConfigurationSection glyphSection = config.getConfigurationSection(glyphSelection);
parseMap.put(glyphSelection, new GlyphParser(glyphSection));
}
boolean configUpdated = false;
Map<String, GlyphBuilder> map = new LinkedHashMap<>();
for (Map.Entry<String, GlyphParser> entry : parseMap.entrySet()) {
GlyphParser parser = entry.getValue();
try {
map.put(entry.getKey(), parser.buildGlyph());
} catch (Exception e) {
e.printStackTrace();
}
}
return map;
}
private File[] getGlyphFiles() {
if (!glyphFolder.exists()) {
glyphFolder.mkdirs();
return new File[0];
}
File[] itemConfigs = Arrays.stream(glyphFolder.listFiles()).filter(File::isFile)
.toArray(File[]::new);
Arrays.sort(itemConfigs);
return itemConfigs;
}
}