Browse Source

!!This build is brokeh!!
- Updated Settings and Strings loaders.
- Seperated the loading and saving of setting files.
- Implemented better and automatic updating of configurations.

gamerx 8 years ago
parent
commit
9049b36968

+ 2 - 1
.gitignore

@@ -20,6 +20,7 @@
 /bin
 /dist
 /manifest.mf
+changes.txt
 
 # Mac filesystem dust
 /.DS_Store
@@ -31,4 +32,4 @@
 *.iml
 *.ipr
 *.iws
-.idea/
+.idea/

+ 0 - 7
pom.xml

@@ -38,13 +38,6 @@
                 <directory>${basedir}/src/main/resources/</directory>
                 <includes>
                     <include>config.yml</include>
-                </includes>
-            </resource>
-            <resource>
-                <targetPath>settings/</targetPath>
-                <filtering>true</filtering>
-                <directory>${basedir}/src/main/resources/</directory>
-                <includes>
                     <include>strings.yml</include>
                 </includes>
             </resource>

+ 15 - 15
src/main/java/net/tgxn/bukkit/backup/BackupMain.java

@@ -3,15 +3,12 @@ package net.tgxn.bukkit.backup;
 import com.nijiko.permissions.PermissionHandler;
 import com.nijikokun.bukkit.Permissions.Permissions;
 
-import java.io.FileNotFoundException;
-import java.io.IOException;
 import net.tgxn.bukkit.backup.utils.*;
 import net.tgxn.bukkit.backup.config.*;
 import net.tgxn.bukkit.backup.listeners.*;
 import net.tgxn.bukkit.backup.threading.PrepareBackup;
 
 import org.bukkit.Server;
-import org.bukkit.configuration.InvalidConfigurationException;
 import org.bukkit.event.Event.Priority;
 import org.bukkit.event.Event.Type;
 import org.bukkit.plugin.Plugin;
@@ -21,7 +18,7 @@ import org.bukkit.plugin.java.JavaPlugin;
 import java.io.File;
 
 /**
- * The plugin's main class.
+ * Main class file for Backup.
  *
  * @author Domenic Horner (gamerx)
  */
@@ -29,6 +26,7 @@ public class BackupMain extends JavaPlugin {
     
     public static PermissionHandler permissionsHandler;
     public int mainBackupTaskID = -2;
+    public File mainDataFolder;
     
     private static Strings strings;
     private static Settings settings;
@@ -36,19 +34,21 @@ public class BackupMain extends JavaPlugin {
     
     @Override
     public void onLoad() {
-        // Initalize logger.
+        
+        // Initalize main data folder.
+        mainDataFolder = this.getDataFolder();
+        
+        // Initalize log utilities.
         LogUtils.initLogUtils(this);
         
         // Perform datafile check.
-        SharedUtils.checkFolderAndCreate(this.getDataFolder());
-        
-        // Load plugin's string settings.  
-        strings = new Strings(this);
-
+        SharedUtils.checkFolderAndCreate(mainDataFolder);
         
-        // Load plugin's main configuration.
-        settings = new Settings(this, new File(this.getDataFolder(), "config.yml"), strings);
+        // Load plugin configuration.
+        strings = new Strings(this, new File(mainDataFolder, "strings.yml"));
+        settings = new Settings(this, new File(mainDataFolder, "config.yml"), strings);
         
+        // Finish init of LogUtils.
         LogUtils.finishInitLogUtils(settings.getBooleanProperty("displaylog"));
         
         // Do folder checking for backups folder.
@@ -59,14 +59,14 @@ public class BackupMain extends JavaPlugin {
     @Override
     public void onEnable() {
         
-        // Set up permissions for plugin.
+        // Initalize permissions handler.
         initPermissions();
 
-        // Get server and player managers.
+        // Get server and plugin manager instances.
         Server pluginServer = getServer();
         PluginManager pluginManager = pluginServer.getPluginManager();
 
-        // Create new backup instance.
+        // Create new "PrepareBackup" instance.
         prepareBackup = new PrepareBackup(pluginServer, settings, strings);
 
         // Initalize plugin listeners.

+ 66 - 50
src/main/java/net/tgxn/bukkit/backup/config/Settings.java

@@ -1,52 +1,47 @@
 package net.tgxn.bukkit.backup.config;
 
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
 import net.tgxn.bukkit.backup.utils.*;
 
 import org.bukkit.configuration.InvalidConfigurationException;
 import org.bukkit.plugin.Plugin;
-
 import org.bukkit.configuration.file.FileConfiguration;
 import org.bukkit.configuration.file.YamlConfiguration;
 
-
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.logging.Level;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
 
 /**
  * Loads all settings for the plugin.
  * 
  * @author gamerx
  */
-public class Settings {
+public final class Settings {
+
+    private Plugin plugin;
+    private Strings strings;
     
     private File configFile;
-    private FileConfiguration fileConfiguration;
+    private FileConfiguration fileSettingConfiguration;
     
-    private Plugin plugin;
-    private Strings strings;
-    public boolean outOfDate = false;
     
     public Settings(Plugin plugin, File configFile, Strings strings) {
-        
         this.plugin = plugin;
         this.configFile = configFile;
         this.strings = strings;
-
-        // Load the properties.
+        
+        checkAndCreate();
+        
         loadProperties();
+        
+        checkConfigVersion();
     }
     
-    /**
-     * Load the properties from the configFile, create if needed.
-     */
-    private void loadProperties() {
-                
-        // Check for the config file, have it created if needed.
+    public void checkAndCreate() {
         try {
             if (!configFile.exists()) {
                 LogUtils.sendLog(Level.WARNING, strings.getString("newconfigfile"));
@@ -55,69 +50,93 @@ public class Settings {
         } catch (SecurityException | NullPointerException se) {
             LogUtils.exceptionLog(se.getStackTrace());
         }
+    }
+    
+    /**
+     * Load the properties from the configFile, create if needed.
+     */
+    private void loadProperties() {
         
-        fileConfiguration = new YamlConfiguration();
+        fileSettingConfiguration = new YamlConfiguration();
         try {
             // Attempt to load configuration.
-            fileConfiguration.load(configFile);
+            fileSettingConfiguration.load(configFile);
         } catch (IOException | InvalidConfigurationException ex) {
             LogUtils.exceptionLog(ex.getStackTrace(), "Failed to load settings.");
             
         }
+    }
+    
+    private void saveProperties() {
+        
+        // Check they are loaded.
+        if (fileSettingConfiguration == null) {
+            return;
+        }
         
-        // Check version of the config file.
-        checkConfigVersion();
+        // Attempt to save configuration to file forcibly.
+        try {
+            fileSettingConfiguration.save(configFile);
+        } catch (IOException ex) {
+            LogUtils.exceptionLog(ex.getStackTrace(), "Error saving config file.");
+        }
     }
     
     /**
-     * Checks the version in the config file, and suggests the user runs the update command.
+     * Checks configuration version and then based on the outcome, either runs the update, or returns false.
+     * 
+     * @return False for no update done, True for update done.
      */
-    public void checkConfigVersion() {
-
-        boolean needToUpdate = false;
-
-        // Check config is loaded.
-        if (fileConfiguration != null) {
+    public boolean checkConfigVersion() {
+        
+        boolean doUpgrade = false;
+        
+        // Check configuration is loaded.
+        if (fileSettingConfiguration != null) {
 
-            // Get the version information.
-            String configVersion = fileConfiguration.getString("version", plugin.getDescription().getVersion());
+            // Get the version information from the file.
+            String configVersion = fileSettingConfiguration.getString("version", plugin.getDescription().getVersion());
             String pluginVersion = plugin.getDescription().getVersion();
 
             // Check we got a version from the config file.
             if (configVersion == null) {
                 LogUtils.sendLog(strings.getString("failedtogetpropsver"), Level.SEVERE, true);
-                needToUpdate = true;
+                doUpgrade = true;
             }
 
             // Check if the config is outdated.
             if (!configVersion.equals(pluginVersion))
-                needToUpdate = true;
+                doUpgrade = true;
 
             // After we have checked the versions, we have determined that we need to update.
-            if (needToUpdate) {
-                LogUtils.sendLog(Level.SEVERE, strings.getString("configoutdated"));
-                outOfDate = true;
-            } else {
-                outOfDate = false;
+            if (doUpgrade) {
+                LogUtils.sendLog(Level.SEVERE, strings.getString("configupdate"));
+                doConfigurationUpdate();
             }
         }
+        return doUpgrade;
     }
     
-    public void doConfUpdate() {
-        createDefaultSettings();
+    public void doConfigurationUpdate() {
         loadProperties();
+        createDefaultSettings();
+        fileSettingConfiguration.set("version", this.plugin.getDescription().getVersion());
+        saveProperties();
     }
     
     /**
      * Load the properties file from the JAR and place it in the backup DIR.
      */
     private void createDefaultSettings() {
+        
+        if (configFile.exists()) {
+            configFile.delete();
+        }
+        
         BufferedReader bReader = null;
         BufferedWriter bWriter = null;
         String line;
-
         try {
-
             // Open a stream to the properties file in the jar, because we can only access over the class loader.
             bReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/settings/config.yml")));
             bWriter = new BufferedWriter(new FileWriter(configFile));
@@ -128,7 +147,7 @@ public class Settings {
                 bWriter.newLine();
             }
         } catch (IOException ioe) {
-            LogUtils.exceptionLog(ioe.getStackTrace());
+            LogUtils.exceptionLog(ioe.getStackTrace(), "Error opening stream.");
         }
         
         finally {
@@ -139,11 +158,8 @@ public class Settings {
                 if (bWriter != null) {
                     bWriter.close();
                 }
-                
-                
-                
             } catch (IOException ioe) {
-                LogUtils.exceptionLog(ioe.getStackTrace());
+                LogUtils.exceptionLog(ioe.getStackTrace(), "Error closing stream.");
             }
         }
     }
@@ -156,7 +172,7 @@ public class Settings {
      * @return The value of the property, defaults to -1.
      */
     public int getIntProperty(String property) {
-        return fileConfiguration.getInt(property, -1);
+        return fileSettingConfiguration.getInt(property, -1);
     }
 
     /**
@@ -166,7 +182,7 @@ public class Settings {
      * @return The value of the property, defaults to true.
      */
     public boolean getBooleanProperty(String property) {
-        return fileConfiguration.getBoolean(property, true);
+        return fileSettingConfiguration.getBoolean(property, true);
     }
 
     /**
@@ -176,7 +192,7 @@ public class Settings {
      * @return The value of the property.
      */
     public String getStringProperty(String property) {
-        return fileConfiguration.getString(property, "");
+        return fileSettingConfiguration.getString(property, "");
     }
     
     /**

+ 49 - 40
src/main/java/net/tgxn/bukkit/backup/config/Strings.java

@@ -1,19 +1,19 @@
 package net.tgxn.bukkit.backup.config;
 
+import net.tgxn.bukkit.backup.utils.LogUtils;
+
+import org.bukkit.plugin.Plugin;
+import org.bukkit.configuration.InvalidConfigurationException;
+import org.bukkit.configuration.file.FileConfiguration;
+import org.bukkit.configuration.file.YamlConfiguration;
+
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
-
 import java.io.FileNotFoundException;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStreamReader;
-import net.tgxn.bukkit.backup.utils.LogUtils;
-import org.bukkit.configuration.InvalidConfigurationException;
-import org.bukkit.configuration.file.FileConfiguration;
-import org.bukkit.configuration.file.YamlConfiguration;
-
-import org.bukkit.plugin.Plugin;
 
 
 /**
@@ -25,7 +25,7 @@ public class Strings {
     
     private Plugin plugin;
     private File stringsFile;
-    private FileConfiguration fileConfiguration;
+    private FileConfiguration fileStringConfiguration;
     
     /**
      * Loads the strings configuration file.
@@ -33,31 +33,53 @@ public class Strings {
      * 
      * @param plugin The plugin this is for.
      */
-    public Strings(Plugin plugin) {
-        
+    public Strings(Plugin plugin, File stringsFile) {
         this.plugin = plugin;
-        stringsFile = new File(plugin.getDataFolder(), "strings.yml");
-        
-        checkForStringsFile();
+        this.stringsFile = stringsFile;
         
+        checkAndCreate();
         loadStrings();
-
     }
-    
-    private void checkForStringsFile() {
-                // Check for the config file, have it created if needed.
+
+    private void checkAndCreate() {
+        // Check for the config file, have it created if needed.
         try {
             if (!stringsFile.exists()) {
-                //LogUtils.sendLog(Level.WARNING, strings.getString("newconfigfile"));
                 createDefaultStrings();
             }
         } catch (SecurityException | NullPointerException se) {
-            LogUtils.exceptionLog(se.getStackTrace());
+            LogUtils.exceptionLog(se.getStackTrace(), "Error checking strings file.");
+        }
+    }
+    
+    
+    
+    private void loadStrings() {
+        fileStringConfiguration = new YamlConfiguration();
+        try {
+            fileStringConfiguration.load(stringsFile);
+        } catch (FileNotFoundException ex) {
+            LogUtils.exceptionLog(ex.getStackTrace(), "Error loading strings file.");
+        } catch (IOException | InvalidConfigurationException ex) {
+            LogUtils.exceptionLog(ex.getStackTrace(), "Error loading strings file.");
+        }
+    }
+    
+    private void saveStrings() {
+        if(fileStringConfiguration == null)
+            return;
+        try {
+            fileStringConfiguration.save(stringsFile);
+        } catch (IOException ex) {
+            LogUtils.exceptionLog(ex.getStackTrace(), "Error saving strings file.");
         }
     }
     
     private void createDefaultStrings() {
     
+        if(stringsFile.exists())
+            stringsFile.delete();
+        
     /**
      * Load the properties file from the JAR and place it in the backup DIR.
      */
@@ -78,7 +100,7 @@ public class Strings {
                 bWriter.newLine();
             }
         } catch (IOException ioe) {
-            LogUtils.exceptionLog(ioe.getStackTrace());
+            LogUtils.exceptionLog(ioe.getStackTrace(), "Error opening streams.");
         }
         
         finally {
@@ -89,31 +111,19 @@ public class Strings {
                 if (bWriter != null) {
                     bWriter.close();
                 }
-                
-                
-                
             } catch (IOException ioe) {
-                LogUtils.exceptionLog(ioe.getStackTrace());
+                LogUtils.exceptionLog(ioe.getStackTrace(), "Error closing streams.");
             }
         }
     
 
     }
     
-    private void loadStrings() {
-
-        
-        // Create the new strings file.
-        fileConfiguration = new YamlConfiguration();
-        try {
-            fileConfiguration.load(stringsFile);
-        } catch (FileNotFoundException ex) {
-        } catch (IOException | InvalidConfigurationException ex) {
-        }
+    public void doConfigUpdate() {
+        loadStrings();
         
     }
     
-    
     /**
      * Gets a value of the string property.
      * 
@@ -123,13 +133,13 @@ public class Strings {
     public String getString(String property) {
         
         // Get string for this name.
-        String string = fileConfiguration.getString(property);
+        String string = fileStringConfiguration.getString(property);
         
         // If we cannot find a string for this, return default.
         if (string != null)
             return colorizeString(string);
         else
-            return fileConfiguration.getString("stringnotfound") + property;
+            return fileStringConfiguration.getString("stringnotfound") + property;
     }
     
     /**
@@ -142,13 +152,13 @@ public class Strings {
     public String getString(String property, String option) {
         
         // Get string for this name.
-        String string = fileConfiguration.getString(property);
+        String string = fileStringConfiguration.getString(property);
         
         // If we cannot find a string for this, return default.
         if (string != null)
             return colorizeString(string.replaceAll("%%ARG%%", option));
         else
-            return fileConfiguration.getString("stringnotfound") + property;
+            return fileStringConfiguration.getString("stringnotfound") + property;
     }
     
     /**
@@ -166,5 +176,4 @@ public class Strings {
         else
             return "";
     }
-
 }

+ 2 - 2
src/main/java/net/tgxn/bukkit/backup/listeners/CommandListener.java

@@ -270,9 +270,9 @@ public class CommandListener extends PlayerListener implements CommandExecutor {
     }
 
     private void updateConfig(Player player) {
-        if(settings.outOfDate) {
+        if(settings.checkConfigVersion()) {
             player.sendMessage(strings.getString("updatingconf"));
-            settings.doConfUpdate();
+            settings.doConfigurationUpdate();
         } else {
             player.sendMessage(strings.getString("confuptodate"));
         }

+ 0 - 5
src/main/java/net/tgxn/bukkit/backup/utils/SharedUtils.java

@@ -1,11 +1,6 @@
 package net.tgxn.bukkit.backup.utils;
 
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStreamReader;
 
 /**
  * I don't like repeating myself.

+ 1 - 1
src/main/resources/config.yml

@@ -35,7 +35,7 @@ enableautosave: true
 #======== File & Location Settings ==========#
 
 ## Should all the worlds be split into their own files? (Default: false)
-# Makes this compatible with WorldEdit.
+# Needs to be true to be compatible with WorldEdit.
 splitbackup: false
 
 ## Whether to ZIP Backups or not. (Default: true)

+ 1 - 1
src/main/resources/strings.yml

@@ -14,7 +14,7 @@ reloadedok           : '[Backup] Reloaded %%ARG%% successfully!'
 #========= Startup Strings ==========#
 
 failedtogetpropsver  : Failed to retrieve version from config file, I suggest upgrading!
-configoutdated       : Your config file is outdated, run '/backup updateconf' in-game to upgrade it.
+configupdate         : Your configuration is outdated, upgrading it...
 zipdisabled          : You have disabled backup compression.
 createbudir          : Created the folder for backups.
 defaultperms         : No permissions plugin detected, defaulting to OP.