From 0f67d42c9965ecbacc52e7f938dcae0c978d3dbd Mon Sep 17 00:00:00 2001 From: ElgarL Date: Mon, 31 Oct 2011 08:34:57 +0000 Subject: Global Groups yml and class --- EssentialsGroupManager/src/globalgroups.yml | 181 +++++++++++++ .../org/anjocaido/groupmanager/GlobalGroups.java | 291 +++++++++++++++++++++ 2 files changed, 472 insertions(+) create mode 100644 EssentialsGroupManager/src/globalgroups.yml create mode 100644 EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java (limited to 'EssentialsGroupManager') diff --git a/EssentialsGroupManager/src/globalgroups.yml b/EssentialsGroupManager/src/globalgroups.yml new file mode 100644 index 000000000..43c225b6a --- /dev/null +++ b/EssentialsGroupManager/src/globalgroups.yml @@ -0,0 +1,181 @@ +groups: + + g:bukkit_default: + permissions: + - bukkit.broadcast.user + + g:essentials_default: + permissions: + - essentials.help + - essentials.helpop + - essentials.list + - essentials.motd + - essentials.rules + - essentials.spawn + - groupmanager.notify.self + + g:essentials_builder: + permissions: + - essentials.afk + - essentials.back + - essentials.back.ondeath + - essentials.balance + - essentials.balance.others + - essentials.balancetop + - essentials.chat.color + - essentials.chat.shout + - essentials.chat.question + - essentials.compass + - essentials.depth + - essentials.home + - essentials.ignore + - essentials.kit + - essentials.kit.tools + - essentials.mail + - essentials.mail.send + - essentials.me + - essentials.msg + - essentials.nick + - essentials.pay + - essentials.ping + - essentials.powertool + - essentials.protect + - essentials.sethome + - essentials.signs.use.* + - essentials.signs.create.disposal + - essentials.signs.create.mail + - essentials.signs.create.protection + - essentials.signs.create.trade + - essentials.signs.break.disposal + - essentials.signs.break.mail + - essentials.signs.break.protection + - essentials.signs.break.trade + - essentials.suicide + - essentials.time + - essentials.tpa + - essentials.tpaccept + - essentials.tpahere + - essentials.tpdeny + - essentials.warp + - essentials.warp.list + - essentials.worth + + g:essentials_moderator: + permissions: + - essentials.ban + - essentials.ban.notify + - essentials.banip + - essentials.broadcast + - essentials.clearinventory + - essentials.delwarp + - essentials.eco.loan + - essentials.ext + - essentials.getpos + - essentials.helpop.recieve + - essentials.home.others + - essentials.invsee + - essentials.jails + - essentials.jump + - essentials.kick + - essentials.kick.notify + - essentials.kill + - essentials.mute + - essentials.nick.others + - essentials.realname + - essentials.setwarp + - essentials.signs.create.* + - essentials.signs.break.* + - essentials.spawner + - essentials.thunder + - essentials.time + - essentials.time.set + - essentials.protect.alerts + - essentials.protect.admin + - essentials.protect.ownerinfo + - essentials.ptime + - essentials.ptime.others + - essentials.togglejail + - essentials.top + - essentials.tp + - essentials.tphere + - essentials.tppos + - essentials.tptoggle + - essentials.unban + - essentials.unbanip + - essentials.weather + - essentials.whois + - essentials.world + - groupmanager.listgroups + - groupmanager.mandemote + - groupmanager.manpromote + - groupmanager.manselect + - groupmanager.manuadd + - groupmanager.manudel + - groupmanager.manwhois + - groupmanager.notify.other + + g:essentials_admin: + permissions: + - -essentials.backup + - -essentials.essentials + - -essentials.setspawn + - -essentials.reloadall + - -essentials.plugin + - essentials.* + - groupmanager.mantogglevalidate + - groupmanager.mansave + - groupmanager.mangcheckp + - groupmanager.manglistp + - groupmanager.manucheckp + - groupmanager.manulistp + + g:bukkit_moderator: + permissions: + - bukkit.command.ban + - bukkit.command.ban.ip + - bukkit.command.ban.player + - bukkit.command.gamemode + - bukkit.command.kick + - bukkit.command.unban + - bukkit.command.unban.ip + - bukkit.command.unban.player + + g:bukkit_admin: + permissions: + - bPermissions.admin + - bPermissions.demote.admin + - bPermissions.gui + - bPermissions.iplock.lock + - bPermissions.promote.admin + - bukkit.broadcast + - bukkit.broadcast.admin + - bukkit.command + - bukkit.command.give + - bukkit.command.help + - bukkit.command.kill + - bukkit.command.list + - bukkit.command.me + - bukkit.command.op + - bukkit.command.op.give + - bukkit.command.op.take + - bukkit.command.plugins + - bukkit.command.reload + - bukkit.command.save + - bukkit.command.save.disable + - bukkit.command.save.enable + - bukkit.command.save.perform + - bukkit.command.say + - bukkit.command.stop + - bukkit.command.teleport + - bukkit.command.tell + - bukkit.command.time + - bukkit.command.time.add + - bukkit.command.time.set + - bukkit.command.version + - bukkit.command.whitelist + - bukkit.command.whitelist.add + - bukkit.command.whitelist.disable + - bukkit.command.whitelist.enable + - bukkit.command.whitelist.list + - bukkit.command.whitelist.reload + - bukkit.command.whitelist.remove diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java new file mode 100644 index 000000000..72bedc106 --- /dev/null +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/GlobalGroups.java @@ -0,0 +1,291 @@ +package org.anjocaido.groupmanager; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.logging.Level; + +import org.anjocaido.groupmanager.data.Group; +import org.anjocaido.groupmanager.utils.PermissionCheckResult; +import org.anjocaido.groupmanager.utils.Tasks; +import org.bukkit.configuration.file.YamlConfiguration; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + + + +/** + * @author ElgarL + * + */ +public class GlobalGroups { + + private GroupManager plugin; + private YamlConfiguration GGroups; + + private Map groups; + /** + * + */ + protected boolean haveGroupsChanged = false; + + public GlobalGroups(GroupManager plugin) { + this.plugin = plugin; + load(); + } + + /** + * @return the haveGroupsChanged + */ + public boolean haveGroupsChanged() { + if (this.haveGroupsChanged) { + return true; + } + for (Group g : groups.values()) { + if (g.isChanged()) { + return true; + } + } + return false; + } + + /** + * @param haveGroupsChanged the haveGroupsChanged to set + */ + public void setGroupsChanged(boolean haveGroupsChanged) { + this.haveGroupsChanged = haveGroupsChanged; + } + + @SuppressWarnings("unchecked") + public void load() { + + GGroups = new YamlConfiguration(); + groups = new HashMap(); + + // READ globalGroups FILE + File GlobalGroupsFile = new File(plugin.getDataFolder(), + "globalgroups.yml"); + + if (!GlobalGroupsFile.exists()) { + try { + // Create a new file if it doesn't exist. + Tasks.copy(plugin.getResourceAsStream("globalgroups.yml"), + GlobalGroupsFile); + } catch (IOException ex) { + GroupManager.logger.log(Level.SEVERE, null, ex); + } + } + + try { + GGroups.load(GlobalGroupsFile); + } catch (Exception ex) { + throw new IllegalArgumentException( + "The following file couldn't pass on Parser.\n" + + GlobalGroupsFile.getPath(), ex); + } + + // Read all global groups + Map allGroups = (Map) GGroups + .getConfigurationSection("groups").getValues(false); + + // Load each groups permissions list. + if (allGroups != null) + for (String groupName : allGroups.keySet()) { + Group newGroup = new Group(groupName.toLowerCase()); + Object permissions = GGroups.get("groups." + groupName + + ".permissions"); + + if (permissions instanceof List) { + for (String permission : (List) permissions) { + newGroup.addPermission(permission); + } + } else if (permissions instanceof String) { + newGroup.addPermission((String) permissions); + } + + // Push a new group + addGroup(newGroup); + } + + GlobalGroupsFile = null; + + } + + /** + * Write a dataHolder in a specified file + * @param ph + * @param groupsFile + */ + + public void writeGroups() { + + File GlobalGroupsFile = new File(plugin.getDataFolder(), + "globalgroups.yml"); + + Map root = new HashMap(); + + Map groupsMap = new HashMap(); + root.put("groups", groupsMap); + for (String groupKey : groups.keySet()) { + Group group = groups.get(groupKey); + + Map aGroupMap = new HashMap(); + groupsMap.put(group.getName(), aGroupMap); + + aGroupMap.put("permissions", group.getPermissionList()); + } + + if (!root.isEmpty()) { + DumperOptions opt = new DumperOptions(); + opt.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + final Yaml yaml = new Yaml(opt); + try { + yaml.dump(root, new OutputStreamWriter(new FileOutputStream(GlobalGroupsFile), "UTF-8")); + } catch (UnsupportedEncodingException ex) { + } catch (FileNotFoundException ex) { + } + } + + } + + /** + * Add a new group if it doesn't already exist. + * + * @param newGroup + */ + public Group addGroup(Group newGroup) { + // Push a new group + if (!groups.containsKey(newGroup.getName().toLowerCase())) { + groups.put(newGroup.getName().toLowerCase(), newGroup); + this.setGroupsChanged(true); + return newGroup; + } + return null; + } + + /** + * Delete a group if it exist. + * + * @param newGroup + */ + public boolean removeGroup(String groupName) { + // Push a new group + if (groups.containsKey(groupName.toLowerCase())) { + groups.remove(groupName.toLowerCase()); + this.setGroupsChanged(true); + return true; + } + return false; + } + + + /** + * Returns true if the Global Group exists in the globalgroups.yml + * + * @param groupName + * @return + */ + 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 + */ + public boolean hasPermission(String groupName, String permissionNode) { + + if (!hasGroup(groupName.toLowerCase())) + return false; + + return groups.get(groupName.toLowerCase()).hasSamePermissionNode( + permissionNode); + + } + + /** + * Returns a PermissionCheckResult of the permission node for the group to + * be tested against. + * + * @param groupName + * @param permissionNode + * @return + */ + public PermissionCheckResult checkPermission(String groupName, + String permissionNode) { + + PermissionCheckResult result = new PermissionCheckResult(); + result.askedPermission = permissionNode; + result.resultType = PermissionCheckResult.Type.NOTFOUND; + + if (!hasGroup(groupName.toLowerCase())) + 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 + */ + public List getGroupsPermissions(String groupName) { + if (!hasGroup(groupName.toLowerCase())) + return null; + + return groups.get(groupName.toLowerCase()).getPermissionList(); + } + + /** + * Returns a Set of all global group names. + * + * @return + */ + public Set getGlobalGroups() { + return groups.keySet(); + } + + /** + * + * @return a collection of the groups + */ + public Collection getGroupList() { + return groups.values(); + } + + /** + * Returns the Global Group or null if it doesn't exist. + * + * @param groupName + * @return + */ + public Group getGroup(String groupName) { + if (!hasGroup(groupName.toLowerCase())) + return null; + + return groups.get(groupName.toLowerCase()); + + } + +} \ No newline at end of file -- cgit v1.2.3