Added new method so that you can't get a commentless config

This commit is contained in:
libraryaddict 2014-05-31 05:29:40 +12:00
parent 0e828eaf07
commit 2cb8d90158

@ -1,8 +1,13 @@
package me.libraryaddict.disguise; package me.libraryaddict.disguise;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import me.libraryaddict.disguise.commands.*; import me.libraryaddict.disguise.commands.*;
@ -24,9 +29,6 @@ import me.libraryaddict.disguise.utilities.DisguiseValues;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Ageable; import org.bukkit.entity.Ageable;
import org.bukkit.entity.Damageable; import org.bukkit.entity.Damageable;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -43,24 +45,27 @@ public class LibsDisguises extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
saveDefaultConfig(); saveDefaultConfig();
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml")); File configFile = new File(getDataFolder(), "config.yml");
boolean needToSaveConfig = false;
InputStream stream = null; InputStream stream = null;
FileReader reader = null;
String toWrite = "";
String toRead = "";
try { try {
stream = getClassLoader().getResource("config.yml").openStream(); stream = getClassLoader().getResource("config.yml").openStream();
YamlConfiguration internalConfig = YamlConfiguration.loadConfiguration(stream); toWrite = read(new InputStreamReader(stream));
for (String option : internalConfig.getKeys(false)) { reader = new FileReader(configFile);
if (internalConfig.isConfigurationSection(option)) { toRead = read(reader);
ConfigurationSection section = internalConfig.getConfigurationSection(option);
for (String secondOption : section.getKeys(false)) { if (!toRead.equals(toWrite)) {
if (!config.contains(option + "." + secondOption)) { try {
config.set(option + "." + secondOption, section.get(secondOption)); FileWriter writer = new FileWriter(configFile);
needToSaveConfig = true; try {
} writer.write(toWrite);
} finally {
writer.close();
} }
} else if (!config.contains(option)) { } catch (IOException e) {
config.set(option, getConfig().get(option)); e.printStackTrace();
needToSaveConfig = true;
} }
} }
} catch (IOException e) { } catch (IOException e) {
@ -73,14 +78,15 @@ public class LibsDisguises extends JavaPlugin {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
if (needToSaveConfig) {
try { try {
config.save(new File(getDataFolder(), "config.yml")); if (reader != null) {
reader.close();
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
PacketsManager.init(this); PacketsManager.init(this);
DisguiseUtilities.init(this); DisguiseUtilities.init(this);
DisguiseConfig.setSoundsEnabled(getConfig().getBoolean("DisguiseSounds")); DisguiseConfig.setSoundsEnabled(getConfig().getBoolean("DisguiseSounds"));
@ -136,6 +142,52 @@ public class LibsDisguises extends JavaPlugin {
registerValues(); registerValues();
} }
private String read(Reader reader) {
String toWrite = "";
try {
BufferedReader input = new BufferedReader(reader);
String currentPath = "";
try {
String line;
while ((line = input.readLine()) != null) {
if (line.replace(" ", "").startsWith("#")) {
toWrite += line;
} else if (line.contains(":")) {
if (line.substring(line.indexOf(":") + 1).equals("")) {
currentPath = line.substring(0, line.length() - 1) + ".";
toWrite += line;
} else {
if (!line.startsWith(" ")) {
currentPath = "";
}
String obj = line.substring(0, line.indexOf(":")).replace(" ", "");
Object value = getConfig().get(currentPath + obj);
if (value instanceof String) {
value = "'" + value + "'";
}
toWrite += (currentPath.length() == 0 ? "" : " ") + obj + ": " + value;
}
}
if (input.ready()) {
toWrite += "\n";
}
}
} finally {
input.close();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return toWrite;
}
/** /**
* Here we create a nms entity for each disguise. Then grab their default values in their datawatcher. Then their sound volume * Here we create a nms entity for each disguise. Then grab their default values in their datawatcher. Then their sound volume
* for mob noises. As well as setting their watcher class and entity size. * for mob noises. As well as setting their watcher class and entity size.