From 37faacdc6bb2666b6a7409b572587344db0fae06 Mon Sep 17 00:00:00 2001 From: destro174 <40720638+destro174@users.noreply.github.com> Date: Sat, 8 Jan 2022 14:20:07 +0100 Subject: [PATCH] switch config to configurate --- .../afkdectector/config/AbstractConfig.java | 151 ------------------ .../com/alttd/afkdectector/config/Config.java | 8 +- .../afkdectector/config/MessagesConfig.java | 9 +- 3 files changed, 13 insertions(+), 155 deletions(-) delete mode 100644 src/main/java/com/alttd/afkdectector/config/AbstractConfig.java diff --git a/src/main/java/com/alttd/afkdectector/config/AbstractConfig.java b/src/main/java/com/alttd/afkdectector/config/AbstractConfig.java deleted file mode 100644 index fde4450..0000000 --- a/src/main/java/com/alttd/afkdectector/config/AbstractConfig.java +++ /dev/null @@ -1,151 +0,0 @@ -package com.alttd.afkdectector.config; - -import com.alttd.afkdectector.AFKDetector; -import com.alttd.afkdectector.util.Logger; -import com.google.common.collect.ImmutableMap; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.inventory.ItemStack; -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.*; - -@SuppressWarnings({"unused", "SameParameterValue"}) -abstract class AbstractConfig { - File file; - YamlConfiguration yaml; - File configPath; - - AbstractConfig(String filename) { - this.configPath = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + AFKDetector.getInstance().getName()); - this.file = new File(configPath, filename); - this.yaml = new YamlConfiguration(); - if (!this.file.getParentFile().exists()) { - if(!this.file.getParentFile().mkdirs()) { - return; - } - } - try { - yaml.load(file); - } catch (IOException ignore) { - } catch (InvalidConfigurationException ex) { - Logger.severe(String.format("Could not load %s, please correct your syntax errors", filename)); - throw new RuntimeException(ex); - } - yaml.options().copyDefaults(true); - } - - void readConfig(Class clazz, Object instance) { - for (Method method : clazz.getDeclaredMethods()) { - if (Modifier.isPrivate(method.getModifiers())) { - if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) { - try { - method.setAccessible(true); - method.invoke(instance); - } catch (InvocationTargetException ex) { - throw new RuntimeException(ex.getCause()); - } catch (Exception ex) { - Logger.severe("Error invoking " + method); - ex.printStackTrace(); - } - } - } - } - - try { - yaml.save(file); - } catch (IOException ex) { - Logger.severe("Could not save " + file); - ex.printStackTrace(); - } - } - - void set(String path, Object val) { - yaml.addDefault(path, val); - yaml.set(path, val); - } - - String getString(String path, String def) { - yaml.addDefault(path, def); - return yaml.getString(path, yaml.getString(path)); - } - - boolean getBoolean(String path, boolean def) { - yaml.addDefault(path, def); - return yaml.getBoolean(path, yaml.getBoolean(path)); - } - - int getInt(String path, int def) { - yaml.addDefault(path, def); - return yaml.getInt(path, yaml.getInt(path)); - } - - double getDouble(String path, double def) { - yaml.addDefault(path, def); - return yaml.getDouble(path, yaml.getDouble(path)); - } - - List getList(String path, T def) { - yaml.addDefault(path, def); - return yaml.getList(path, yaml.getList(path)); - } - - @NonNull - Map getMap(final @NonNull String path, final @Nullable Map def) { - 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(); - } - - ItemStack getItemStack(String path, ItemStack def) { - yaml.addDefault(path, def); - return yaml.getItemStack(path); - } - - void setLocations(String path, ArrayList locs) { - ArrayList list = new ArrayList<>(); - for(Location l : locs) { - String toSave = l.getWorld().getName() + ":" + l.getX() + ":" + l.getY() + ":" + l.getZ(); - list.add(toSave); - } - yaml.set(path, list); - } - - List getLocations(String path){ - List attempts = yaml.getStringList(path); - - if (attempts == null || attempts.isEmpty()) { - return Collections.emptyList(); - } - List locations = new ArrayList<>(); - - for (String attempt : attempts) { - String[] parts = attempt.split(":"); - locations.add(new Location(Bukkit.getWorld(parts[0]), Double.parseDouble(parts[1]), Double.parseDouble(parts[2]), Double.parseDouble(parts[3]))); - } - return locations; - } - -} diff --git a/src/main/java/com/alttd/afkdectector/config/Config.java b/src/main/java/com/alttd/afkdectector/config/Config.java index 0602adb..9fe53d5 100644 --- a/src/main/java/com/alttd/afkdectector/config/Config.java +++ b/src/main/java/com/alttd/afkdectector/config/Config.java @@ -1,11 +1,15 @@ package com.alttd.afkdectector.config; +import com.alttd.afkdectector.AFKDetector; +import com.alttd.galaxy.configuration.AbstractConfiguration; import org.bukkit.Bukkit; +import java.io.File; + @SuppressWarnings("unused") -public class Config extends AbstractConfig { +public class Config extends AbstractConfiguration { private Config() { - super("config.yml"); + super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + AFKDetector.getInstance().getName()), "config.yml"); } static Config config; diff --git a/src/main/java/com/alttd/afkdectector/config/MessagesConfig.java b/src/main/java/com/alttd/afkdectector/config/MessagesConfig.java index 578b4b6..3fd8f48 100644 --- a/src/main/java/com/alttd/afkdectector/config/MessagesConfig.java +++ b/src/main/java/com/alttd/afkdectector/config/MessagesConfig.java @@ -1,9 +1,14 @@ package com.alttd.afkdectector.config; +import com.alttd.afkdectector.AFKDetector; +import com.alttd.galaxy.configuration.AbstractConfiguration; + +import java.io.File; + @SuppressWarnings("unused") -public class MessagesConfig extends AbstractConfig { +public class MessagesConfig extends AbstractConfiguration { private MessagesConfig() { - super("messages.yml"); + super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + AFKDetector.getInstance().getName()), "messages.yml"); } static MessagesConfig config;