summaryrefslogtreecommitdiffstats
path: root/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java
diff options
context:
space:
mode:
Diffstat (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java')
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java278
1 files changed, 189 insertions, 89 deletions
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java
index 04d9e86be..1335ecbe3 100644
--- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java
@@ -24,22 +24,22 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
+
/**
* @author ElgarL
- *
+ *
*/
-public class GlobalGroups {
-
+public class GlobalGroups
+{
private GroupManager plugin;
private YamlConfiguration GGroups;
-
private Map<String, Group> groups;
-
protected long timeStampGroups = 0;
protected boolean haveGroupsChanged = false;
protected File GlobalGroupsFile = null;
- public GlobalGroups(GroupManager plugin) {
+ public GlobalGroups(GroupManager plugin)
+ {
this.plugin = plugin;
load();
@@ -48,13 +48,17 @@ public class GlobalGroups {
/**
* @return the haveGroupsChanged
*/
- public boolean haveGroupsChanged() {
+ public boolean haveGroupsChanged()
+ {
- if (this.haveGroupsChanged) {
+ if (this.haveGroupsChanged)
+ {
return true;
}
- for (Group g : groups.values()) {
- if (g.isChanged()) {
+ for (Group g : groups.values())
+ {
+ if (g.isChanged())
+ {
return true;
}
}
@@ -64,7 +68,8 @@ public class GlobalGroups {
/**
* @return the timeStampGroups
*/
- public long getTimeStampGroups() {
+ public long getTimeStampGroups()
+ {
return timeStampGroups;
}
@@ -72,22 +77,24 @@ public class GlobalGroups {
/**
* @param timeStampGroups the timeStampGroups to set
*/
- protected void setTimeStampGroups(long timeStampGroups) {
+ protected void setTimeStampGroups(long timeStampGroups)
+ {
this.timeStampGroups = timeStampGroups;
}
/**
- * @param haveGroupsChanged
- * the haveGroupsChanged to set
+ * @param haveGroupsChanged the haveGroupsChanged to set
*/
- public void setGroupsChanged(boolean haveGroupsChanged) {
+ public void setGroupsChanged(boolean haveGroupsChanged)
+ {
this.haveGroupsChanged = haveGroupsChanged;
}
@SuppressWarnings("unchecked")
- public void load() {
+ public void load()
+ {
GGroups = new YamlConfiguration();
@@ -95,39 +102,53 @@ public class GlobalGroups {
// READ globalGroups FILE
if (GlobalGroupsFile == null)
+ {
GlobalGroupsFile = new File(plugin.getDataFolder(), "globalgroups.yml");
+ }
- if (!GlobalGroupsFile.exists()) {
- try {
+ if (!GlobalGroupsFile.exists())
+ {
+ try
+ {
// Create a new file if it doesn't exist.
Tasks.copy(plugin.getResourceAsStream("globalgroups.yml"), GlobalGroupsFile);
- } catch (IOException ex) {
+ }
+ catch (IOException ex)
+ {
GroupManager.logger.log(Level.SEVERE, null, ex);
}
}
- try {
+ try
+ {
GGroups.load(GlobalGroupsFile);
- } catch (Exception ex) {
+ }
+ catch (Exception ex)
+ {
throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + GlobalGroupsFile.getPath(), ex);
}
// Clear out old groups
resetGlobalGroups();
- if (!GGroups.getKeys(false).isEmpty()) {
+ 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) {
+ 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);
}
// Load each groups permissions list.
- if (allGroups != null) {
+ if (allGroups != null)
+ {
Iterator<String> groupItr = allGroups.keySet().iterator();
String groupName;
@@ -137,12 +158,16 @@ public class GlobalGroups {
* loop each group entry
* and read it's data.
*/
- while (groupItr.hasNext()) {
- try {
+ while (groupItr.hasNext())
+ {
+ try
+ {
groupCount++;
// Attempt to fetch the next group name.
groupName = groupItr.next();
- } catch (Exception ex) {
+ }
+ catch (Exception ex)
+ {
throw new IllegalArgumentException("Invalid group name for GlobalGroup entry (" + groupCount + ") in file: " + GlobalGroupsFile.getPath(), ex);
}
@@ -156,33 +181,56 @@ public class GlobalGroups {
element = GGroups.get("groups." + groupName + ".permissions");
if (element != null)
- if (element instanceof List) {
- try {
- for (String node : (List<String>) element) {
+ {
+ if (element instanceof List)
+ {
+ try
+ {
+ for (String node : (List<String>)element)
+ {
if ((node != null) && !node.isEmpty())
+ {
newGroup.addPermission(node);
+ }
}
- } catch (ClassCastException ex) {
+ }
+ catch (ClassCastException ex)
+ {
throw new IllegalArgumentException("Invalid permission node for global group: " + groupName, ex);
}
- } else if (element instanceof String) {
+ }
+ else if (element instanceof String)
+ {
if ((element != null) && !((String)element).isEmpty())
- newGroup.addPermission((String) element);
- } else
+ {
+ 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) {
+ {
+ 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));
+ for (String key : ((MemorySection)element).getKeys(false))
+ {
+ vars.put(key, ((MemorySection)element).get(key));
}
newGroup.setVariables(vars);
- } else
+ }
+ else
+ {
throw new IllegalArgumentException("Unknown type of info node for global group: " + groupName);
+ }
+ }
// Push a new group
addGroup(newGroup);
@@ -200,18 +248,21 @@ public class GlobalGroups {
/**
* Write the globalgroups.yml file
*/
-
- public void writeGroups(boolean overwrite) {
+ public void writeGroups(boolean overwrite)
+ {
// File GlobalGroupsFile = new File(plugin.getDataFolder(), "globalgroups.yml");
- if (haveGroupsChanged()) {
- if (overwrite || (!overwrite && (getTimeStampGroups() >= GlobalGroupsFile.lastModified()))) {
+ 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()) {
+ for (String groupKey : groups.keySet())
+ {
Group group = groups.get(groupKey);
// Group header
@@ -222,7 +273,8 @@ public class GlobalGroups {
Map<String, Object> infoMap = new HashMap<String, Object>();
aGroupMap.put("info", infoMap);
- for (String infoKey : group.getVariables().getVarKeyList()) {
+ for (String infoKey : group.getVariables().getVarKeyList())
+ {
infoMap.put(infoKey, group.getVariables().getVarObject(infoKey));
}
@@ -230,26 +282,37 @@ public class GlobalGroups {
aGroupMap.put("permissions", group.getPermissionList());
}
- if (!root.isEmpty()) {
+ if (!root.isEmpty())
+ {
DumperOptions opt = new DumperOptions();
opt.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
final Yaml yaml = new Yaml(opt);
- try {
+ try
+ {
yaml.dump(root, new OutputStreamWriter(new FileOutputStream(GlobalGroupsFile), "UTF-8"));
- } catch (UnsupportedEncodingException ex) {
- } catch (FileNotFoundException ex) {
+ }
+ catch (UnsupportedEncodingException ex)
+ {
+ }
+ catch (FileNotFoundException ex)
+ {
}
}
setTimeStampGroups(GlobalGroupsFile.lastModified());
- } else {
+ }
+ 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'");
}
removeGroupsChangedFlag();
- } else {
+ }
+ else
+ {
// Check for newer file as no local changes.
- if (getTimeStampGroups() < GlobalGroupsFile.lastModified()) {
+ if (getTimeStampGroups() < GlobalGroupsFile.lastModified())
+ {
System.out.print("Newer GlobalGroups file found (Loading changes)!");
// Backup GlobalGroups file
backupFile();
@@ -261,28 +324,34 @@ public class GlobalGroups {
/**
* Backup the BlobalGroups file
- *
+ *
* @param w
*/
- private void backupFile() {
+ private void backupFile()
+ {
File backupFile = new File(plugin.getBackupFolder(), "bkp_ggroups_" + Tasks.getDateString() + ".yml");
- try {
+ try
+ {
Tasks.copy(GlobalGroupsFile, backupFile);
- } catch (IOException ex) {
+ }
+ 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) {
+ public void addGroup(Group groupToAdd)
+ {
// Create a new group if it already exists
- if (hasGroup(groupToAdd.getName())) {
+ if (hasGroup(groupToAdd.getName()))
+ {
groupToAdd = groupToAdd.clone();
removeGroup(groupToAdd.getName());
}
@@ -290,18 +359,22 @@ public class GlobalGroups {
newGroup(groupToAdd);
haveGroupsChanged = true;
if (GroupManager.isLoaded())
+ {
GroupManagerEventHandler.callEvent(groupToAdd, GMGroupEvent.Action.GROUP_ADDED);
+ }
}
/**
* Creates a new group if it doesn't already exist.
- *
+ *
* @param newGroup
*/
- public Group newGroup(Group newGroup) {
+ public Group newGroup(Group newGroup)
+ {
// Push a new group
- if (!groups.containsKey(newGroup.getName().toLowerCase())) {
+ if (!groups.containsKey(newGroup.getName().toLowerCase()))
+ {
groups.put(newGroup.getName().toLowerCase(), newGroup);
this.setGroupsChanged(true);
return newGroup;
@@ -311,17 +384,21 @@ public class GlobalGroups {
/**
* Delete a group if it exist.
- *
+ *
* @param groupName
*/
- public boolean removeGroup(String groupName) {
+ public boolean removeGroup(String groupName)
+ {
// Push a new group
- if (groups.containsKey(groupName.toLowerCase())) {
+ if (groups.containsKey(groupName.toLowerCase()))
+ {
groups.remove(groupName.toLowerCase());
this.setGroupsChanged(true);
if (GroupManager.isLoaded())
+ {
GroupManagerEventHandler.callEvent(groupName.toLowerCase(), GMGroupEvent.Action.GROUP_REMOVED);
+ }
return true;
}
return false;
@@ -329,80 +406,96 @@ public class GlobalGroups {
/**
* Returns true if the Global Group exists in the globalgroups.yml
- *
+ *
* @param groupName
* @return true if the group exists
*/
- public boolean hasGroup(String groupName) {
+ public boolean hasGroup(String groupName)
+ {
return groups.containsKey(groupName.toLowerCase());
}
/**
* Returns true if the group has the correct permission node.
- *
+ *
* @param groupName
* @param permissionNode
* @return true if node exists
*/
- public boolean hasPermission(String groupName, String permissionNode) {
+ public boolean hasPermission(String groupName, String permissionNode)
+ {
if (!hasGroup(groupName))
+ {
return false;
+ }
return groups.get(groupName.toLowerCase()).hasSamePermissionNode(permissionNode);
}
/**
- * Returns a PermissionCheckResult of the permission node for the group to
- * be tested against.
- *
+ * Returns a PermissionCheckResult of the permission node for the group to be tested against.
+ *
* @param groupName
* @param permissionNode
* @return PermissionCheckResult object
*/
- public PermissionCheckResult checkPermission(String groupName, String permissionNode) {
+ public PermissionCheckResult checkPermission(String groupName, String permissionNode)
+ {
PermissionCheckResult result = new PermissionCheckResult();
result.askedPermission = permissionNode;
result.resultType = PermissionCheckResult.Type.NOTFOUND;
if (!hasGroup(groupName))
+ {
return result;
+ }
Group tempGroup = groups.get(groupName.toLowerCase());
if (tempGroup.hasSamePermissionNode(permissionNode))
+ {
result.resultType = PermissionCheckResult.Type.FOUND;
+ }
if (tempGroup.hasSamePermissionNode("-" + permissionNode))
+ {
result.resultType = PermissionCheckResult.Type.NEGATION;
+ }
if (tempGroup.hasSamePermissionNode("+" + permissionNode))
+ {
result.resultType = PermissionCheckResult.Type.EXCEPTION;
+ }
return result;
}
/**
* Returns a List of all permission nodes for this group null if none
- *
+ *
* @param groupName
* @return List of all group names
*/
- public List<String> getGroupsPermissions(String groupName) {
+ public List<String> getGroupsPermissions(String groupName)
+ {
if (!hasGroup(groupName))
+ {
return null;
+ }
return groups.get(groupName.toLowerCase()).getPermissionList();
}
/**
* Returns a Set of all global group names.
- *
+ *
* @return Set containing all group names.
*/
- public Set<String> getGlobalGroups() {
+ public Set<String> getGlobalGroups()
+ {
return groups.keySet();
}
@@ -410,30 +503,35 @@ public class GlobalGroups {
/**
* Resets GlobalGroups.
*/
- public void resetGlobalGroups() {
+ public void resetGlobalGroups()
+ {
this.groups = new HashMap<String, Group>();
}
/**
- *
+ *
* @return a collection of the groups
*/
- public Collection<Group> getGroupList() {
+ public Collection<Group> getGroupList()
+ {
return groups.values();
}
/**
* Returns the Global Group or null if it doesn't exist.
- *
+ *
* @param groupName
* @return Group object
*/
- public Group getGroup(String groupName) {
+ public Group getGroup(String groupName)
+ {
if (!hasGroup(groupName))
+ {
return null;
+ }
return groups.get(groupName.toLowerCase());
@@ -442,20 +540,22 @@ public class GlobalGroups {
/**
* @return the globalGroupsFile
*/
- public File getGlobalGroupsFile() {
+ public File getGlobalGroupsFile()
+ {
return GlobalGroupsFile;
}
/**
- *
- */
- public void removeGroupsChangedFlag() {
+ *
+ */
+ public void removeGroupsChangedFlag()
+ {
setGroupsChanged(false);
- for (Group g : groups.values()) {
+ for (Group g : groups.values())
+ {
g.flagAsSaved();
}
}
-
} \ No newline at end of file