Config load error check, remove Towny dep

This commit is contained in:
Norbi Peti 2019-12-16 22:46:23 +01:00
parent cecd8df991
commit 5da07565b0
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 33 additions and 13 deletions

View file

@ -18,6 +18,7 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="library" name="Maven: org.reflections:reflections:0.9.10" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:annotations:2.0.1" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" />

View file

@ -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)

View file

@ -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 &amp; 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());
}
}