From 677534c0b7319691c3139559943b2a5c4e8d2c88 Mon Sep 17 00:00:00 2001 From: ElgarL Date: Fri, 13 Apr 2012 13:58:05 +0100 Subject: Fixed the 'last' nested throw/catch and rework all user and group reading to be certain to trap all errors. --- .../src/org/anjocaido/groupmanager/GlobalGroups.java | 3 +++ 1 file changed, 3 insertions(+) (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java index 6fa8b0d1d..904055d31 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java @@ -142,6 +142,9 @@ public class GlobalGroups { throw new IllegalArgumentException("Invalid group name for GlobalGroup entry (" + groupCount + ") in file: " + GlobalGroupsFile.getPath(), ex); } + /* + * Create a new group with this name. + */ Group newGroup = new Group(groupName.toLowerCase()); Object element; -- cgit v1.2.3 From a028abe03630c1990105f5dbfef5ba44bf11602d Mon Sep 17 00:00:00 2001 From: ElgarL Date: Fri, 13 Apr 2012 14:40:26 +0100 Subject: Update all code formatting to use tabs for indentation. --- .../org/anjocaido/groupmanager/GlobalGroups.java | 192 +++++++++++---------- 1 file changed, 104 insertions(+), 88 deletions(-) (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java') diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java index 904055d31..647d729e5 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java @@ -24,8 +24,6 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; - - /** * @author ElgarL * @@ -42,6 +40,7 @@ public class GlobalGroups { protected File GlobalGroupsFile = null; public GlobalGroups(GroupManager plugin) { + this.plugin = plugin; load(); } @@ -50,6 +49,7 @@ public class GlobalGroups { * @return the haveGroupsChanged */ public boolean haveGroupsChanged() { + if (this.haveGroupsChanged) { return true; } @@ -65,20 +65,24 @@ public class GlobalGroups { * @return the timeStampGroups */ public long getTimeStampGroups() { + return timeStampGroups; } + /** * @param timeStampGroups the timeStampGroups to set */ protected void setTimeStampGroups(long timeStampGroups) { + this.timeStampGroups = timeStampGroups; } - + /** * @param haveGroupsChanged * the haveGroupsChanged to set */ public void setGroupsChanged(boolean haveGroupsChanged) { + this.haveGroupsChanged = haveGroupsChanged; } @@ -86,7 +90,7 @@ public class GlobalGroups { public void load() { GGroups = new YamlConfiguration(); - + GroupManager.setLoaded(false); // READ globalGroups FILE @@ -110,47 +114,47 @@ public class GlobalGroups { // Clear out old groups resetGlobalGroups(); - + if (!GGroups.getKeys(false).isEmpty()) { // Read all global groups Map allGroups = new HashMap(); - + try { allGroups = (Map) GGroups.getConfigurationSection("groups").getValues(false); } catch (Exception ex) { - //ex.printStackTrace(); - throw new IllegalArgumentException("Your " + GlobalGroupsFile.getPath() + " file is invalid. See console for details.", ex); - } - + // ex.printStackTrace(); + throw new IllegalArgumentException("Your " + GlobalGroupsFile.getPath() + " file is invalid. See console for details.", ex); + } + // Load each groups permissions list. if (allGroups != null) { - + Iterator groupItr = allGroups.keySet().iterator(); - String groupName; - Integer groupCount = 0; - - /* - * loop each group entry - * and read it's data. - */ - while (groupItr.hasNext()) { - try { - groupCount++; - // Attempt to fetch the next group name. - groupName = groupItr.next(); - } catch (Exception ex) { + String groupName; + Integer groupCount = 0; + + /* + * loop each group entry + * and read it's data. + */ + while (groupItr.hasNext()) { + try { + groupCount++; + // Attempt to fetch the next group name. + groupName = groupItr.next(); + } catch (Exception ex) { throw new IllegalArgumentException("Invalid group name for GlobalGroup entry (" + groupCount + ") in file: " + GlobalGroupsFile.getPath(), ex); } - /* - * Create a new group with this name. - */ - Group newGroup = new Group(groupName.toLowerCase()); + /* + * Create a new group with this name. + */ + Group newGroup = new Group(groupName.toLowerCase()); Object element; - + // Permission nodes element = GGroups.get("groups." + groupName + ".permissions"); - + if (element != null) if (element instanceof List) { try { @@ -164,31 +168,31 @@ public class GlobalGroups { newGroup.addPermission((String) element); } else throw new IllegalArgumentException("Unknown type of permission node for global group: " + groupName); - + // Info nodes element = GGroups.get("groups." + groupName + ".info"); - + if (element != null) if (element instanceof MemorySection) { Map vars = new HashMap(); for (String key : ((MemorySection) element).getKeys(false)) { - vars.put(key, ((MemorySection) element).get(key)); - } + vars.put(key, ((MemorySection) element).get(key)); + } newGroup.setVariables(vars); } else throw new IllegalArgumentException("Unknown type of info node for global group: " + groupName); - + // Push a new group addGroup(newGroup); } } - + removeGroupsChangedFlag(); } - + setTimeStampGroups(GlobalGroupsFile.lastModified()); GroupManager.setLoaded(true); - //GlobalGroupsFile = null; + // GlobalGroupsFile = null; } /** @@ -197,33 +201,33 @@ public class GlobalGroups { public void writeGroups(boolean overwrite) { - //File GlobalGroupsFile = new File(plugin.getDataFolder(), "globalgroups.yml"); + // File GlobalGroupsFile = new File(plugin.getDataFolder(), "globalgroups.yml"); if (haveGroupsChanged()) { if (overwrite || (!overwrite && (getTimeStampGroups() >= GlobalGroupsFile.lastModified()))) { Map root = new HashMap(); - + Map groupsMap = new HashMap(); root.put("groups", groupsMap); for (String groupKey : groups.keySet()) { Group group = groups.get(groupKey); - + // Group header Map aGroupMap = new HashMap(); groupsMap.put(group.getName(), aGroupMap); - + // Info nodes Map infoMap = new HashMap(); - aGroupMap.put("info", infoMap); - - for (String infoKey : group.getVariables().getVarKeyList()) { - infoMap.put(infoKey, group.getVariables().getVarObject(infoKey)); - } - - // Permission nodes + aGroupMap.put("info", infoMap); + + for (String infoKey : group.getVariables().getVarKeyList()) { + infoMap.put(infoKey, group.getVariables().getVarObject(infoKey)); + } + + // Permission nodes aGroupMap.put("permissions", group.getPermissionList()); } - + if (!root.isEmpty()) { DumperOptions opt = new DumperOptions(); opt.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); @@ -236,53 +240,55 @@ public class GlobalGroups { } setTimeStampGroups(GlobalGroupsFile.lastModified()); } else { - // Newer file found. - GroupManager.logger.log(Level.WARNING, "Newer GlobalGroups file found, but we have local changes!"); - throw new IllegalStateException("Unable to save unless you issue a '/mansave force'"); - } + // Newer file found. + GroupManager.logger.log(Level.WARNING, "Newer GlobalGroups file found, but we have local changes!"); + throw new IllegalStateException("Unable to save unless you issue a '/mansave force'"); + } removeGroupsChangedFlag(); } else { - //Check for newer file as no local changes. - if (getTimeStampGroups() < GlobalGroupsFile.lastModified()) { - System.out.print("Newer GlobalGroups file found (Loading changes)!"); - // Backup GlobalGroups file - backupFile(); - load(); - } - } + // Check for newer file as no local changes. + if (getTimeStampGroups() < GlobalGroupsFile.lastModified()) { + System.out.print("Newer GlobalGroups file found (Loading changes)!"); + // Backup GlobalGroups file + backupFile(); + load(); + } + } } - + /** - * Backup the BlobalGroups file - * @param w - */ - private void backupFile() { - - File backupFile = new File(plugin.getBackupFolder(), "bkp_ggroups_" + Tasks.getDateString() + ".yml"); - try { - Tasks.copy(GlobalGroupsFile, backupFile); - } catch (IOException ex) { - GroupManager.logger.log(Level.SEVERE, null, ex); - } - } - + * Backup the BlobalGroups file + * + * @param w + */ + private void backupFile() { + + File backupFile = new File(plugin.getBackupFolder(), "bkp_ggroups_" + Tasks.getDateString() + ".yml"); + try { + Tasks.copy(GlobalGroupsFile, backupFile); + } catch (IOException ex) { + GroupManager.logger.log(Level.SEVERE, null, ex); + } + } + /** * Adds a group, or replaces an existing one. * * @param groupToAdd */ public void addGroup(Group groupToAdd) { + // Create a new group if it already exists if (hasGroup(groupToAdd.getName())) { groupToAdd = groupToAdd.clone(); removeGroup(groupToAdd.getName()); } - + newGroup(groupToAdd); - haveGroupsChanged = true; - if (GroupManager.isLoaded()) - GroupManagerEventHandler.callEvent(groupToAdd, GMGroupEvent.Action.GROUP_ADDED); + haveGroupsChanged = true; + if (GroupManager.isLoaded()) + GroupManagerEventHandler.callEvent(groupToAdd, GMGroupEvent.Action.GROUP_ADDED); } /** @@ -291,6 +297,7 @@ public class GlobalGroups { * @param newGroup */ public Group newGroup(Group newGroup) { + // Push a new group if (!groups.containsKey(newGroup.getName().toLowerCase())) { groups.put(newGroup.getName().toLowerCase(), newGroup); @@ -306,6 +313,7 @@ public class GlobalGroups { * @param groupName */ public boolean removeGroup(String groupName) { + // Push a new group if (groups.containsKey(groupName.toLowerCase())) { groups.remove(groupName.toLowerCase()); @@ -324,6 +332,7 @@ public class GlobalGroups { * @return true if the group exists */ public boolean hasGroup(String groupName) { + return groups.containsKey(groupName.toLowerCase()); } @@ -379,6 +388,7 @@ public class GlobalGroups { * @return List of all group names */ public List getGroupsPermissions(String groupName) { + if (!hasGroup(groupName)) return null; @@ -391,6 +401,7 @@ public class GlobalGroups { * @return Set containing all group names. */ public Set getGlobalGroups() { + return groups.keySet(); } @@ -398,14 +409,16 @@ public class GlobalGroups { * Resets GlobalGroups. */ public void resetGlobalGroups() { + this.groups = new HashMap(); } - + /** * * @return a collection of the groups */ public Collection getGroupList() { + return groups.values(); } @@ -416,6 +429,7 @@ public class GlobalGroups { * @return Group object */ public Group getGroup(String groupName) { + if (!hasGroup(groupName)) return null; @@ -427,17 +441,19 @@ public class GlobalGroups { * @return the globalGroupsFile */ public File getGlobalGroupsFile() { + return GlobalGroupsFile; } - + /** * */ - public void removeGroupsChangedFlag() { - setGroupsChanged(false); - for (Group g : groups.values()) { - g.flagAsSaved(); - } - } + public void removeGroupsChangedFlag() { + + setGroupsChanged(false); + for (Group g : groups.values()) { + g.flagAsSaved(); + } + } } \ No newline at end of file -- cgit v1.2.3