|
@@ -1,7 +1,9 @@
|
|
|
package com.gmail.nossr50.config;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
@@ -9,69 +11,69 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
import com.gmail.nossr50.mcMMO;
|
|
|
|
|
|
public abstract class ConfigLoader {
|
|
|
+ protected static final mcMMO plugin = mcMMO.p;
|
|
|
protected String fileName;
|
|
|
protected File configFile;
|
|
|
- protected File dataFolder;
|
|
|
- protected final mcMMO plugin;
|
|
|
protected FileConfiguration config;
|
|
|
|
|
|
- public ConfigLoader(mcMMO plugin, String fileName){
|
|
|
- this.plugin = plugin;
|
|
|
+ public ConfigLoader(String relativePath, String fileName){
|
|
|
this.fileName = fileName;
|
|
|
- dataFolder = plugin.getDataFolder();
|
|
|
- configFile = new File(dataFolder, File.separator + fileName);
|
|
|
- config = YamlConfiguration.loadConfiguration(configFile);
|
|
|
+ configFile = new File(plugin.getDataFolder(), relativePath + File.separator + fileName);
|
|
|
+ load();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Load this config file.
|
|
|
- */
|
|
|
- public void load() {
|
|
|
+ public ConfigLoader(String fileName){
|
|
|
+ this.fileName = fileName;
|
|
|
+ configFile = new File(plugin.getDataFolder(), fileName);
|
|
|
+ load();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void load() {
|
|
|
if (!configFile.exists()) {
|
|
|
- dataFolder.mkdir();
|
|
|
- saveConfig();
|
|
|
+ plugin.getLogger().info("Creating mcMMO " + fileName + " File...");
|
|
|
+ createFile();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ plugin.getLogger().info("Loading mcMMO " + fileName + " File...");
|
|
|
}
|
|
|
|
|
|
- addDefaults();
|
|
|
- loadKeys();
|
|
|
+ config = YamlConfiguration.loadConfiguration(configFile);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Save this config file.
|
|
|
- */
|
|
|
- private void saveConfig() {
|
|
|
- try {
|
|
|
- config.save(configFile);
|
|
|
- }
|
|
|
- catch (IOException ex) {
|
|
|
- plugin.getLogger().severe("Could not save config to " + configFile + ex);
|
|
|
+ protected abstract void loadKeys();
|
|
|
+
|
|
|
+ protected void createFile() {
|
|
|
+ if (configFile.exists()) {
|
|
|
+ return;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- protected void saveIfNotExist() {
|
|
|
- if (!configFile.exists()) {
|
|
|
- if (plugin.getResource(fileName) != null) {
|
|
|
- plugin.saveResource(fileName, false);
|
|
|
+ configFile.getParentFile().mkdirs();
|
|
|
+
|
|
|
+ InputStream inputStream = plugin.getResource(fileName);
|
|
|
+
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ copyStreamToFile(inputStream, configFile);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
- rereadFromDisk();
|
|
|
+ else {
|
|
|
+ plugin.getLogger().severe("Missing ressource file: '" + configFile.getName() + "' please notify the plugin authors");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- protected void rereadFromDisk() {
|
|
|
- config = YamlConfiguration.loadConfiguration(configFile);
|
|
|
- }
|
|
|
+ private static void copyStreamToFile(InputStream inputStream, File file) throws Exception {
|
|
|
+ OutputStream outputStream = new FileOutputStream(file);
|
|
|
|
|
|
- /**
|
|
|
- * Add the defaults to this config file.
|
|
|
- */
|
|
|
- protected void addDefaults() {
|
|
|
- config.options().copyDefaults(true);
|
|
|
- saveConfig();
|
|
|
- }
|
|
|
+ int read = 0;
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
|
|
|
- /**
|
|
|
- * Load the keys from this config file.
|
|
|
- */
|
|
|
- protected abstract void loadKeys();
|
|
|
+ while ((read = inputStream.read(bytes)) != -1) {
|
|
|
+ outputStream.write(bytes, 0, read);
|
|
|
+ }
|
|
|
|
|
|
+ inputStream.close();
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
}
|