package com.alttd.hunger_games.config; import com.alttd.hunger_games.Main; import com.google.common.collect.ImmutableMap; import lombok.extern.slf4j.Slf4j; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @Slf4j @SuppressWarnings({"unused", "SameParameterValue"}) abstract class AbstractConfig { File file; YamlConfiguration yaml; AbstractConfig(Main main, String filename) { init(new File(main.getDataFolder(), filename), filename); } AbstractConfig(File file, String filename) { init(new File(file.getPath() + File.separator + filename), filename); } private void init(File file, String filename) { this.file = file; this.yaml = new YamlConfiguration(); try { yaml.load(file); } catch (IOException ignore) { } catch (InvalidConfigurationException ex) { log.error("Could not load {}, please correct your syntax errors", filename, ex); throw new RuntimeException(ex); } yaml.options().copyDefaults(true); } void readConfig(Class clazz, Object instance) { for (Class declaredClass : clazz.getDeclaredClasses()) { for (Method method : declaredClass.getDeclaredMethods()) { if (!Modifier.isPrivate(method.getModifiers())) { continue; } if (method.getParameterTypes().length != 0 || method.getReturnType() != Void.TYPE) { continue; } try { method.setAccessible(true); method.invoke(instance); } catch (InvocationTargetException ex) { throw new RuntimeException(ex.getCause()); } catch (Exception ex) { log.error("Error invoking {}.", method, ex); } } } save(); } private void save() { try { yaml.save(file); } catch (IOException ex) { log.error("Could not save {}.", file.toString(), ex); } } void set(String prefix, String path, Object val) { path = prefix + path; yaml.addDefault(path, val); yaml.set(path, val); save(); } String getString(String prefix, String path, String def) { path = prefix + path; yaml.addDefault(path, def); return yaml.getString(path, yaml.getString(path)); } boolean getBoolean(String prefix, String path, boolean def) { path = prefix + path; yaml.addDefault(path, def); return yaml.getBoolean(path, yaml.getBoolean(path)); } int getInt(String prefix, String path, int def) { path = prefix + path; yaml.addDefault(path, def); return yaml.getInt(path, yaml.getInt(path)); } public long getLong(String prefix, String path, long def) { path = prefix + path; yaml.addDefault(path, def); return yaml.getLong(path, yaml.getLong(path)); } double getDouble(String prefix, String path, double def) { path = prefix + path; yaml.addDefault(path, def); return yaml.getDouble(path, yaml.getDouble(path)); } List getList(String prefix, String path, T def) { path = prefix + path; yaml.addDefault(path, def); List list = yaml.getList(path, yaml.getList(path)); return list == null ? null : list.stream().map(Object::toString).collect(Collectors.toList()); } List getStringList(String prefix, String path, List def) { path = prefix + path; yaml.addDefault(path, def); return yaml.getStringList(path); } @NonNull Map getMap(String prefix, @NonNull String path, final @Nullable Map def) { path = prefix + path; final ImmutableMap.Builder builder = ImmutableMap.builder(); if (def != null && yaml.getConfigurationSection(path) == null) { yaml.addDefault(path, def.isEmpty() ? new HashMap<>() : def); return def; } final ConfigurationSection section = yaml.getConfigurationSection(path); if (section != null) { for (String key : section.getKeys(false)) { @SuppressWarnings("unchecked") final T val = (T) section.get(key); if (val != null) { builder.put(key, val); } } } return builder.build(); } ConfigurationSection getConfigurationSection(String path) { return yaml.getConfigurationSection(path); } }