diff options
author | KHobbits <rob@khobbits.co.uk> | 2012-04-15 19:41:06 +0100 |
---|---|---|
committer | KHobbits <rob@khobbits.co.uk> | 2012-04-15 19:41:06 +0100 |
commit | 26a0cd0761777c93cb5282455bc8de4d2697cecd (patch) | |
tree | 18f7b41a0f2dfc84174b6e7fdbca41c49e3a5b02 /EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java | |
parent | 75b7f7ac228e06c42cea3357bb649f8e8fc85f0b (diff) | |
parent | 1599a36642014d2b3284288676999a39b417a841 (diff) | |
download | Essentials-26a0cd0761777c93cb5282455bc8de4d2697cecd.tar Essentials-26a0cd0761777c93cb5282455bc8de4d2697cecd.tar.gz Essentials-26a0cd0761777c93cb5282455bc8de4d2697cecd.tar.lz Essentials-26a0cd0761777c93cb5282455bc8de4d2697cecd.tar.xz Essentials-26a0cd0761777c93cb5282455bc8de4d2697cecd.zip |
Merge remote branch 'remotes/origin/groupmanager'
Diffstat (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java')
-rw-r--r-- | EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java | 189 |
1 files changed, 104 insertions, 85 deletions
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java index 6fa8b0d1d..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,44 +114,47 @@ public class GlobalGroups { // Clear out old groups
resetGlobalGroups();
-
+
if (!GGroups.getKeys(false).isEmpty()) {
// Read all global groups
Map<String, Object> allGroups = new HashMap<String, Object>();
-
+
try {
allGroups = (Map<String, Object>) 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<String> 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);
}
- 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 {
@@ -161,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<String, Object> vars = new HashMap<String, Object>();
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;
}
/**
@@ -194,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<String, Object> root = new HashMap<String, Object>();
-
+
Map<String, Object> groupsMap = new HashMap<String, Object>();
root.put("groups", groupsMap);
for (String groupKey : groups.keySet()) {
Group group = groups.get(groupKey);
-
+
// Group header
Map<String, Object> aGroupMap = new HashMap<String, Object>();
groupsMap.put(group.getName(), aGroupMap);
-
+
// Info nodes
Map<String, Object> infoMap = new HashMap<String, Object>();
- 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);
@@ -233,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);
}
/**
@@ -288,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);
@@ -303,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());
@@ -321,6 +332,7 @@ public class GlobalGroups { * @return true if the group exists
*/
public boolean hasGroup(String groupName) {
+
return groups.containsKey(groupName.toLowerCase());
}
@@ -376,6 +388,7 @@ public class GlobalGroups { * @return List of all group names
*/
public List<String> getGroupsPermissions(String groupName) {
+
if (!hasGroup(groupName))
return null;
@@ -388,6 +401,7 @@ public class GlobalGroups { * @return Set containing all group names.
*/
public Set<String> getGlobalGroups() {
+
return groups.keySet();
}
@@ -395,14 +409,16 @@ public class GlobalGroups { * Resets GlobalGroups.
*/
public void resetGlobalGroups() {
+
this.groups = new HashMap<String, Group>();
}
-
+
/**
*
* @return a collection of the groups
*/
public Collection<Group> getGroupList() {
+
return groups.values();
}
@@ -413,6 +429,7 @@ public class GlobalGroups { * @return Group object
*/
public Group getGroup(String groupName) {
+
if (!hasGroup(groupName))
return null;
@@ -424,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 |