Allow the lang.yml to have comments within it

This commit is contained in:
gomeow
2013-06-13 00:01:27 -07:00
parent 1635a84952
commit 9ad6332cd2
@@ -1,8 +1,10 @@
package com.drtshock.playervaults; package com.drtshock.playervaults;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -230,26 +232,48 @@ public class PlayerVaults extends JavaPlugin {
/** /**
* Load the lang.yml file. * Load the lang.yml file.
* @return The lang.yml config.
*/ */
public YamlConfiguration loadLang() { public void loadLang() {
File lang = new File(getDataFolder(), "lang.yml"); File lang = new File(getDataFolder(), "lang.yml");
OutputStream out = null;
InputStream defLangStream = this.getResource("lang.yml");
if (!lang.exists()) { if (!lang.exists()) {
try { try {
getDataFolder().mkdir(); getDataFolder().mkdir();
lang.createNewFile(); lang.createNewFile();
InputStream defConfigStream = this.getResource("lang.yml"); if (defLangStream != null) {
if (defConfigStream != null) { out = new FileOutputStream(lang);
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream); int read = 0;
defConfig.save(lang); byte[] bytes = new byte[1024];
while((read = defLangStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defLangStream);
Lang.setFile(defConfig); Lang.setFile(defConfig);
return defConfig; return;
} }
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); // So they notice e.printStackTrace(); // So they notice
log.severe("[PlayerVaults] Couldn't create language file."); log.severe("[PlayerVaults] Couldn't create language file.");
log.severe("[PlayerVaults] This is a fatal error. Now disabling"); log.severe("[PlayerVaults] This is a fatal error. Now disabling");
this.setEnabled(false); // Without it loaded, we can't send them messages this.setEnabled(false); // Without it loaded, we can't send them messages
} finally {
if (defLangStream != null) {
try {
defLangStream.close();
} catch(IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch(IOException e) {
e.printStackTrace();
}
}
} }
} }
YamlConfiguration conf = YamlConfiguration.loadConfiguration(lang); YamlConfiguration conf = YamlConfiguration.loadConfiguration(lang);
@@ -268,7 +292,6 @@ public class PlayerVaults extends JavaPlugin {
log.log(Level.WARNING, "PlayerVaults: Report this stack trace to drtshock and gomeow."); log.log(Level.WARNING, "PlayerVaults: Report this stack trace to drtshock and gomeow.");
e.printStackTrace(); e.printStackTrace();
} }
return conf;
} }
/** /**