diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/CleanupUnusedClaimTask.java b/src/main/java/me/ryanhamshire/GriefPrevention/CleanupUnusedClaimTask.java index 988354a..a791336 100644 --- a/src/main/java/me/ryanhamshire/GriefPrevention/CleanupUnusedClaimTask.java +++ b/src/main/java/me/ryanhamshire/GriefPrevention/CleanupUnusedClaimTask.java @@ -18,7 +18,7 @@ package me.ryanhamshire.GriefPrevention; -import me.ryanhamshire.GriefPrevention.alttd.config.AlttdConfig; +import me.ryanhamshire.GriefPrevention.alttd.config.Config; import me.ryanhamshire.GriefPrevention.events.ClaimExpirationEvent; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; @@ -95,7 +95,7 @@ class CleanupUnusedClaimTask implements Runnable //make a copy of this player's claim list Vector claims = new Vector<>(ownerData.getClaims()); // Alternative logic for deleting claims - if(AlttdConfig.alternativeClaimExpiring) { + if(Config.alternativeClaimExpiring) { for (Claim claim : claims) { // remove all subclaims a claim has @@ -109,7 +109,7 @@ class CleanupUnusedClaimTask implements Runnable claim.setPermission("public", ClaimPermission.Build); // make the claim an (expiring) admin claim GriefPrevention.instance.dataStore.changeClaimOwner(claim, null); - AlttdConfig.addExpiringClaim(claim.id); + Config.addExpiringClaim(claim.id); } GriefPrevention.AddLogEntry(" All of " + claim.getOwnerName() + "'s claims have expired and converted to a temp claim.", CustomLogEntryTypes.AdminActivity); GriefPrevention.AddLogEntry("earliestPermissibleLastLogin#getTime: " + earliestPermissibleLastLogin.getTime(), CustomLogEntryTypes.Debug, true); diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/alttd/config/AbstractConfig.java b/src/main/java/me/ryanhamshire/GriefPrevention/alttd/config/AbstractConfig.java new file mode 100644 index 0000000..636448c --- /dev/null +++ b/src/main/java/me/ryanhamshire/GriefPrevention/alttd/config/AbstractConfig.java @@ -0,0 +1,175 @@ +package me.ryanhamshire.GriefPrevention.alttd.config; + +import com.google.common.collect.ImmutableMap; +import me.ryanhamshire.GriefPrevention.GriefPrevention; +import me.ryanhamshire.GriefPrevention.alttd.util.Logger; +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.awt.*; +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.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@SuppressWarnings({"unused", "SameParameterValue"}) +abstract class AbstractConfig { + File file; + YamlConfiguration yaml; + File configPath; + + AbstractConfig(String filename) { + this.configPath = GriefPrevention.instance.getDataFolder(); + 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.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; + } + + Color getColor(String path, Color def) { + yaml.addDefault(path, colorToHex(def)); + return hexToColor(yaml.getString(path, yaml.getString(path))); + } + + String colorToHex(final Color color) { + return Integer.toHexString(color.getRGB() & 0x00FFFFFF); + } + + Color hexToColor(final String hex) { + if (hex == null) { + return Color.RED; + } + String stripped = hex.replace("#", ""); + int rgb = (int) Long.parseLong(stripped, 16); + return new Color(rgb); + } + +} diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/alttd/config/AlttdConfig.java b/src/main/java/me/ryanhamshire/GriefPrevention/alttd/config/Config.java similarity index 79% rename from src/main/java/me/ryanhamshire/GriefPrevention/alttd/config/AlttdConfig.java rename to src/main/java/me/ryanhamshire/GriefPrevention/alttd/config/Config.java index 09117fd..600aec1 100644 --- a/src/main/java/me/ryanhamshire/GriefPrevention/alttd/config/AlttdConfig.java +++ b/src/main/java/me/ryanhamshire/GriefPrevention/alttd/config/Config.java @@ -1,66 +1,29 @@ package me.ryanhamshire.GriefPrevention.alttd.config; -import com.alttd.galaxy.configuration.AbstractConfiguration; -import me.ryanhamshire.GriefPrevention.GriefPrevention; import me.ryanhamshire.GriefPrevention.alttd.util.Logger; -import org.spongepowered.configurate.ConfigurationNode; import java.awt.*; import java.io.IOException; import java.util.HashMap; -import java.util.Map; import java.util.concurrent.TimeUnit; @SuppressWarnings("unused") -public class AlttdConfig extends AbstractConfiguration -{ +public class Config extends AbstractConfig { - private AlttdConfig() { - super(GriefPrevention.instance.getDataFolder(),"alttdconfig.yml"); + private Config() { + super("alttdconfig.yml"); } - static AlttdConfig config; + static Config config; static int version; public static void reload() { - config = new AlttdConfig(); + config = new Config(); version = config.getInt("config-version", 1); config.set("config-version", 1); - config.readConfig(AlttdConfig.class, null); - } - - private HashMap getMap(String path, HashMap def) { - set(path, def); - ConfigurationNode node = config.getNode(path); - HashMap map = new HashMap<>(); - for (Map.Entry entry : node.childrenMap().entrySet()) { - try { - map.put(Long.parseLong(entry.getKey().toString()), Long.parseLong(entry.getValue().getString())); - } catch (NumberFormatException exception) { - // handle - } - } - return map; - } - - Color getColor(String path, Color def) { - config.set(path, colorToHex(def)); - return hexToColor(config.getString(path, colorToHex(def))); - } - - String colorToHex(final Color color) { - return Integer.toHexString(color.getRGB() & 0x00FFFFFF); - } - - Color hexToColor(final String hex) { - if (hex == null) { - return Color.RED; - } - String stripped = hex.replace("#", ""); - int rgb = (int) Long.parseLong(stripped, 16); - return new Color(rgb); + config.readConfig(Config.class, null); } public static boolean DEBUG_MODE = false; @@ -76,18 +39,23 @@ public class AlttdConfig extends AbstractConfiguration expireCheckRate = config.getInt(node + ".expire-check-rate", expireCheckRate); // todo create an alternative way of loading these in expiringClaims.clear(); - config.getMap(node + ".claims", new HashMap<>()) - .forEach((key, value) -> { - try { - expiringClaims.put(Long.parseLong(key.toString()), value); - } catch (NumberFormatException ignored) {} - }); + config.getMap(node + ".claims", new HashMap()) + .forEach((key, value) -> { + try { + expiringClaims.put(Long.parseLong(key), value); + } catch (NumberFormatException ignored) {} + }); } public static void addExpiringClaim(Long id) { expiringClaims.put(id, System.currentTimeMillis() + TimeUnit.DAYS.toMillis(alternativeClaimExpireDays)); config.set("alternative-claim-expiring.claims", expiringClaims); - config.saveConfig(); + try { + config.yaml.save(config.file); + } catch (IOException ex) { + Logger.severe("Could not save " + config.file.getName()); + ex.printStackTrace(); + } } public static String CONTROL_LABEL = "GriefPrevention"; @@ -103,7 +71,7 @@ public class AlttdConfig extends AbstractConfiguration public static Color FILL_COLOR = Color.GREEN; public static double FILL_OPACITY = 0.2D; - public static Color ADMIN_STROKE_COLOR = Color.BLUE; + public static Color ADMIN_STROKE_COLOR = Color.BLUE; public static int ADMIN_STROKE_WEIGHT = 1; public static double ADMIN_STROKE_OPACITY = 1.0D; public static Color ADMIN_FILL_COLOR = Color.BLUE; @@ -168,10 +136,9 @@ public class AlttdConfig extends AbstractConfiguration public static int ignoreClaimWarningDelay = 20 * 600; public static String ignoreClaimWarningMessage = " has had ignore claims on since