From 5da07565b0204781f8fd97d6c0374ea817d6ae30 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 16 Dec 2019 22:46:23 +0100 Subject: [PATCH] Config load error check, remove Towny dep --- BuildConfigUpdater/BuildConfigUpdater.iml | 1 + .../lib/architecture/ButtonPlugin.java | 36 ++++++++++++++----- .../architecture/CommentedConfiguration.java | 9 ++--- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/BuildConfigUpdater/BuildConfigUpdater.iml b/BuildConfigUpdater/BuildConfigUpdater.iml index 67aa057..04dfdc0 100644 --- a/BuildConfigUpdater/BuildConfigUpdater.iml +++ b/BuildConfigUpdater/BuildConfigUpdater.iml @@ -18,6 +18,7 @@ + diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java index 6a18977..2bba1c8 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java @@ -7,11 +7,13 @@ import buttondevteam.lib.chat.Command2MC; import buttondevteam.lib.chat.TBMCChatAPI; import lombok.AccessLevel; import lombok.Getter; +import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; +import java.io.IOException; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -51,7 +53,10 @@ public abstract class ButtonPlugin extends JavaPlugin { @Override public final void onEnable() { - loadConfig(); + if (!loadConfig()) { + getLogger().warning("Please fix the issues and restart the server to load the plugin."); + return; + } try { pluginEnable(); } catch (Exception e) { @@ -61,10 +66,14 @@ public abstract class ButtonPlugin extends JavaPlugin { IHaveConfig.pregenConfig(this, null); } - private void loadConfig() { - var section = getConfig().getConfigurationSection("global"); - if (section == null) section = getConfig().createSection("global"); + private boolean loadConfig() { + var config = getConfig(); + if (config == null) + return false; + var section = config.getConfigurationSection("global"); + if (section == null) section = config.createSection("global"); iConfig = new IHaveConfig(section, this::saveConfig); + return true; } @Override @@ -101,9 +110,14 @@ public abstract class ButtonPlugin extends JavaPlugin { } var file = new File(getDataFolder(), "config.yml"); var yaml = new CommentedConfiguration(file); - if (file.exists() && !yaml.load()) { - getLogger().warning("Failed to load config! Check for syntax errors."); - return false; + if (file.exists()) { + try { + yaml.load(file); + } catch (IOException | InvalidConfigurationException e) { + getLogger().warning("Failed to load config! Check for syntax errors."); + e.printStackTrace(); + return false; + } } this.yaml = yaml; var res = getTextResource("configHelp.yml"); @@ -126,8 +140,12 @@ public abstract class ButtonPlugin extends JavaPlugin { @Override public void saveConfig() { - if (yaml != null) - yaml.save(); + try { + if (yaml != null) + yaml.save(); + } catch (Exception e) { + TBMCCoreAPI.SendException("Failed to save config", e); + } } @Retention(RetentionPolicy.RUNTIME) diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/CommentedConfiguration.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/CommentedConfiguration.java index f7351ff..62cafb2 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/CommentedConfiguration.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/CommentedConfiguration.java @@ -1,6 +1,5 @@ package buttondevteam.lib.architecture; -import com.palmergames.util.FileMgmt; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConstructor; @@ -11,10 +10,12 @@ import org.yaml.snakeyaml.representer.Representer; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.HashMap; /** * A copy of Towny's CommentedConfiguration: https://github.com/TownyAdvanced/Towny/blob/master/src/com/palmergames/bukkit/config/CommentedConfiguration.java + * Modified to remove dependency on the FileMgmt class * * @author dumptruckman & Articdive */ @@ -46,7 +47,7 @@ public class CommentedConfiguration extends YamlConfiguration { //TODO: Remove F return loaded; } - public void save() { + public void save() throws IOException { boolean saved = true; @@ -61,7 +62,7 @@ public class CommentedConfiguration extends YamlConfiguration { //TODO: Remove F // if there's comments to add and it saved fine, we need to add comments if (!comments.isEmpty() && saved) { // String array of each line in the config file - String[] yamlContents = FileMgmt.convertFileToString(file).split("[" + System.getProperty("line.separator") + "]"); + String[] yamlContents = Files.readAllLines(file.toPath()).toArray(new String[0]); // This will hold the newly formatted line StringBuilder newContents = new StringBuilder(); @@ -173,7 +174,7 @@ public class CommentedConfiguration extends YamlConfiguration { //TODO: Remove F while (newContents.toString().startsWith(" " + System.getProperty("line.separator"))) { newContents = new StringBuilder(newContents.toString().replaceFirst(" " + System.getProperty("line.separator"), "")); } - FileMgmt.stringToFile(newContents.toString(), file); + Files.write(file.toPath(), newContents.toString().getBytes()); } }