diff options
author | ElgarL <ElgarL@palmergames.com> | 2011-11-02 22:33:29 +0000 |
---|---|---|
committer | ElgarL <ElgarL@palmergames.com> | 2011-11-02 22:33:29 +0000 |
commit | e873ebd67d3d1379b8a0a8cf18d7b2db9d9320f3 (patch) | |
tree | 54d40ef381b6717218a7762f869b53cba353030c /EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds | |
parent | 57f9bc06b0169326a5065640eeb977d7f32c1c86 (diff) | |
download | Essentials-e873ebd67d3d1379b8a0a8cf18d7b2db9d9320f3.tar Essentials-e873ebd67d3d1379b8a0a8cf18d7b2db9d9320f3.tar.gz Essentials-e873ebd67d3d1379b8a0a8cf18d7b2db9d9320f3.tar.lz Essentials-e873ebd67d3d1379b8a0a8cf18d7b2db9d9320f3.tar.xz Essentials-e873ebd67d3d1379b8a0a8cf18d7b2db9d9320f3.zip |
GM will now check to see if it's data files have been changed at each
scheduled save.
If the files have been altered (on disc) it will reload, so long as
the in-memory data hasn't changed.
If the files on Disc have changed AND there have been changes to it's
in-memory data it will show a warning.
You then MUST issue a '/mansave force' to overwrite the disc files,
or a '/manload' to overwrite the memory data.
Diffstat (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds')
-rw-r--r-- | EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java | 94 |
1 files changed, 74 insertions, 20 deletions
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java index f5b55a25a..8ea8e28c9 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java @@ -160,47 +160,97 @@ public class WorldsHolder { public void reloadWorld(String worldName) { getWorldData(worldName).reload(); } + + /** + * Wrapper to retain backwards compatibility + * (call this function to auto overwrite files) + */ + public void saveChanges() { + saveChanges(true); + } /** * */ - public void saveChanges() { + public void saveChanges(boolean overwrite) { ArrayList<WorldDataHolder> alreadyDone = new ArrayList<WorldDataHolder>(); + Tasks.removeOldFiles(plugin, plugin.getBackupFolder()); + for (OverloadedWorldHolder w : worldsData.values()) { if (alreadyDone.contains(w)) { continue; } - Tasks.removeOldFiles(plugin, plugin.getBackupFolder()); if (w == null) { GroupManager.logger.severe("WHAT HAPPENED?"); continue; } if (w.haveGroupsChanged()) { - //String groupsFolderName = w.getGroupsFile().getParentFile().getName(); - File backupGroups = new File(plugin.getBackupFolder(), "bkp_" + w.getName() + "_g_" + Tasks.getDateString() + ".yml"); - try { - Tasks.copy(w.getGroupsFile(), backupGroups); - } catch (IOException ex) { - GroupManager.logger.log(Level.SEVERE, null, ex); - } - WorldDataHolder.writeGroups(w, w.getGroupsFile()); - w.removeGroupsChangedFlag(); + if (overwrite || (!overwrite && (w.getTimeStampGroups() >= w.getGroupsFile().lastModified()))) { + // Backup Groups file + backupFile(w,true); + + WorldDataHolder.writeGroups(w, w.getGroupsFile()); + //w.removeGroupsChangedFlag(); + } else { + // Newer file found. + GroupManager.logger.log(Level.WARNING, "Newer Groups file found for " + w.getName() + ", but we have local changes!"); + throw new IllegalStateException("Unable to save unless you issue a '/mansave force'"); + } + } else { + //Check for newer file as no local changes. + if (w.getTimeStampGroups() < w.getGroupsFile().lastModified()) { + System.out.print("Newer Groups file found (Loading changes)!"); + // Backup Users file + backupFile(w,false); + w.reloadGroups(); + } } if (w.haveUsersChanged()) { - File backupUsers = new File(plugin.getBackupFolder(), "bkp_" + w.getName() + "_u_" + Tasks.getDateString() + ".yml"); - try { - Tasks.copy(w.getUsersFile(), backupUsers); - } catch (IOException ex) { - GroupManager.logger.log(Level.SEVERE, null, ex); - } - WorldDataHolder.writeUsers(w, w.getUsersFile()); - w.removeUsersChangedFlag(); + if (overwrite || (!overwrite && (w.getTimeStampUsers() >= w.getUsersFile().lastModified()))) { + // Backup Users file + backupFile(w,false); + + WorldDataHolder.writeUsers(w, w.getUsersFile()); + //w.removeUsersChangedFlag(); + } else { + // Newer file found. + GroupManager.logger.log(Level.WARNING, "Newer Users file found for " + w.getName() + ", but we have local changes!"); + throw new IllegalStateException("Unable to save unless you issue a '/mansave force'"); + } + } else { + //Check for newer file as no local changes. + if (w.getTimeStampUsers() < w.getUsersFile().lastModified()) { + System.out.print("Newer Users file found (Loading changes)!"); + // Backup Users file + backupFile(w,false); + w.reloadUsers(); + } } alreadyDone.add(w); } // Write Global Groups if (GroupManager.getGlobalGroups().haveGroupsChanged()) { - GroupManager.getGlobalGroups().writeGroups(); + GroupManager.getGlobalGroups().writeGroups(overwrite); + } else { + if (GroupManager.getGlobalGroups().getTimeStampGroups() < GroupManager.getGlobalGroups().getGlobalGroupsFile().lastModified()) { + System.out.print("Newer GlobalGroups file found (Loading changes)!"); + GroupManager.getGlobalGroups().load(); + } + } + } + + /** + * Backup the Groups/Users file + * @param w + * @param groups + */ + private void backupFile(OverloadedWorldHolder w, Boolean groups) { + + File backupFile = new File(plugin.getBackupFolder(), "bkp_" + w.getName() + (groups ? "_g_" : "_u_") + Tasks.getDateString() + ".yml"); + try { + Tasks.copy((groups ? w.getGroupsFile() : w.getUsersFile()), backupFile); + } catch (IOException ex) { + GroupManager.logger.log(Level.SEVERE, null, ex); } } @@ -407,6 +457,10 @@ public class WorldsHolder { } try { OverloadedWorldHolder thisWorldData = new OverloadedWorldHolder(WorldDataHolder.load(worldName, groupsFile, usersFile)); + + // Set the file TimeStamps as it will be default from the initial load. + thisWorldData.setTimeStamps(); + if (thisWorldData != null) { GroupManager.logger.finest("Successful load of world " + worldName + "..."); worldsData.put(worldName.toLowerCase(), thisWorldData); |