diff options
Diffstat (limited to 'src/main/java/org')
-rw-r--r-- | src/main/java/org/bukkit/permissions/Permission.java | 143 | ||||
-rw-r--r-- | src/main/java/org/bukkit/plugin/PluginDescriptionFile.java | 36 |
2 files changed, 153 insertions, 26 deletions
diff --git a/src/main/java/org/bukkit/permissions/Permission.java b/src/main/java/org/bukkit/permissions/Permission.java index ea6ade6b..18818f23 100644 --- a/src/main/java/org/bukkit/permissions/Permission.java +++ b/src/main/java/org/bukkit/permissions/Permission.java @@ -1,19 +1,25 @@ package org.bukkit.permissions; +import java.util.ArrayList; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.logging.Level; import org.bukkit.Bukkit; +import org.bukkit.plugin.PluginManager; /** * Represents a unique permission that may be attached to a {@link Permissible} */ public class Permission { + public static final PermissionDefault DEFAULT_PERMISSION = PermissionDefault.FALSE; + private final String name; private final Map<String, Boolean> children = new LinkedHashMap<String, Boolean>(); - private PermissionDefault defaultValue = PermissionDefault.FALSE; + private PermissionDefault defaultValue = DEFAULT_PERMISSION; private String description; public Permission(String name) { @@ -153,6 +159,68 @@ public class Permission { } /** + * Adds this permission to the specified parent permission. + * + * If the parent permission does not exist, it will be created and registered. + * + * @param name Name of the parent permission + * @param value The value to set this permission to + * @return Parent permission it created or loaded + */ + public Permission addParent(String name, boolean value) { + PluginManager pm = Bukkit.getServer().getPluginManager(); + String lname = name.toLowerCase(); + + Permission perm = pm.getPermission(lname); + + if (perm == null) { + perm = new Permission(lname); + pm.addPermission(perm); + } + + addParent(perm, value); + + return perm; + } + + /** + * Adds this permission to the specified parent permission. + * + * @param perm Parent permission to register with + * @param value The value to set this permission to + */ + public void addParent(Permission perm, boolean value) { + perm.getChildren().put(getName(), value); + perm.recalculatePermissibles(); + } + + /** + * Loads a list of Permissions from a map of data, usually used from retrieval from a yaml file. + * + * The data may contain a list of name:data, where the data contains the following keys: + * default: Boolean true or false. If not specified, false. + * children: Map<String, Boolean> of child permissions. If not specified, empty list. + * description: Short string containing a very small description of this description. If not specified, empty string. + * + * @param data Map of permissions + * @param def Default permission value to use if missing + * @return Permission object + */ + public static List<Permission> loadPermissions(Map<String, Map<String, Object>> data, String error, PermissionDefault def) { + List<Permission> result = new ArrayList<Permission>(); + + for (Map.Entry<String, Map<String, Object>> entry : data.entrySet()) { + try { + result.add(Permission.loadPermission(entry.getKey(), entry.getValue(), def, result)); + } catch (Throwable ex) { + Bukkit.getServer().getLogger().log(Level.SEVERE, String.format(error, entry.getKey()), ex); + } + } + + return result; + } + + /** * Loads a Permission from a map of data, usually used from retrieval from a yaml file. * * The data may contain the following keys: @@ -165,6 +233,24 @@ public class Permission { * @return Permission object */ public static Permission loadPermission(String name, Map<String, Object> data) { + return loadPermission(name, data, DEFAULT_PERMISSION, null); + } + + /** + * Loads a Permission from a map of data, usually used from retrieval from a yaml file. + * + * The data may contain the following keys: + * default: Boolean true or false. If not specified, false. + * children: Map<String, Boolean> of child permissions. If not specified, empty list. + * description: Short string containing a very small description of this description. If not specified, empty string. + * + * @param name Name of the permission + * @param data Map of keys + * @param def Default permission value to use if not set + * @param output A list to append any created child-Permissions to, may be null + * @return Permission object + */ + public static Permission loadPermission(String name, Map<String, Object> data, PermissionDefault def, List<Permission> output) { if (name == null) { throw new IllegalArgumentException("Name cannot be null"); } @@ -172,7 +258,6 @@ public class Permission { throw new IllegalArgumentException("Data cannot be null"); } String desc = null; - PermissionDefault def = null; Map<String, Boolean> children = null; if (data.containsKey("default")) { @@ -190,7 +275,7 @@ public class Permission { if (data.containsKey("children")) { try { - children = extractChildren(data); + children = extractChildren(data, name, def, output); } catch (ClassCastException ex) { throw new IllegalArgumentException("'children' key is of wrong type", ex); } @@ -204,19 +289,55 @@ public class Permission { } } - return new Permission(name, desc, def, children); + Permission result = new Permission(name, desc, def, children); + + if (data.containsKey("parents")) { + try { + Object parents = data.get("parents"); + + if (parents instanceof String) { + result.addParent((String)parents, true); + } + } catch (ClassCastException ex) { + throw new IllegalArgumentException("'parents' key is of wrong type", ex); + } + } + + return result; } - private static Map<String, Boolean> extractChildren(Map<String, Object> data) { - Map<String, Boolean> input = (Map<String, Boolean>)data.get("children"); - Set<Entry<String, Boolean>> entries = input.entrySet(); - for (Map.Entry<String, Boolean> entry : entries) { - if (!(entry.getValue() instanceof Boolean)) { - throw new IllegalArgumentException("Child '" + entry.getKey() + "' contains invalid value"); + private static Map<String, Boolean> extractChildren(Map<String, Object> data, String name, PermissionDefault def, List<Permission> output) { + Map<String, Object> input = (Map<String, Object>)data.get("children"); + Map<String, Boolean> children = new LinkedHashMap(); + + for (Map.Entry<String, Object> entry : input.entrySet()) { + if ((entry.getValue() instanceof Boolean)) { + children.put(entry.getKey(), (Boolean)entry.getValue()); + } else if ((entry.getValue() instanceof Map)) { + try { + System.out.println("Going to make new child " + (String)entry.getKey() + " perm for " + name); + + try + { + Permission perm = loadPermission((String)entry.getKey(), (Map<String, Object>)entry.getValue(), def, output); + children.put(perm.getName(), Boolean.valueOf(true)); + + if (output != null) { + output.add(perm); + } + } + catch (Throwable ex) { + Bukkit.getServer().getLogger().log(Level.SEVERE, "Permission node '" + (String)entry.getKey() + "' in child of " + name + " is invalid", ex); + } + } catch (ClassCastException ex) { + throw new IllegalArgumentException("Child '" + (String)entry.getKey() + "' contains invalid map type"); + } + } else { + throw new IllegalArgumentException("Child '" + (String)entry.getKey() + "' contains invalid value"); } } - return input; + return children; } } diff --git a/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java b/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java index 4d61c613..50351883 100644 --- a/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java +++ b/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java @@ -5,11 +5,13 @@ import java.io.Reader; import java.io.Writer; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.logging.Level; import org.bukkit.Bukkit; import org.bukkit.permissions.Permission; +import org.bukkit.permissions.PermissionDefault; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; @@ -29,7 +31,8 @@ public final class PluginDescriptionFile { private String website = null; private boolean database = false; private PluginLoadOrder order = PluginLoadOrder.POSTWORLD; - private ArrayList<Permission> permissions = new ArrayList<Permission>(); + private List<Permission> permissions = new ArrayList<Permission>(); + private PermissionDefault defaultPerm = PermissionDefault.OP; @SuppressWarnings("unchecked") public PluginDescriptionFile(final InputStream stream) throws InvalidDescriptionException { @@ -143,10 +146,14 @@ public final class PluginDescriptionFile { this.database = database; } - public ArrayList<Permission> getPermissions() { + public List<Permission> getPermissions() { return permissions; } + public PermissionDefault getPermissionDefault() { + return defaultPerm; + } + private void loadMap(Map<String, Object> map) throws InvalidDescriptionException { try { name = map.get("name").toString(); @@ -257,11 +264,21 @@ public final class PluginDescriptionFile { } } + if (map.containsKey("default-permission")) { + try { + defaultPerm = defaultPerm.getByName((String)map.get("default-permission")); + } catch (ClassCastException ex) { + throw new InvalidDescriptionException(ex, "default-permission is of wrong type"); + } catch (IllegalArgumentException ex) { + throw new InvalidDescriptionException(ex, "default-permission is not a valid choice"); + } + } + if (map.containsKey("permissions")) { try { Map<String, Map<String, Object>> perms = (Map<String, Map<String, Object>>) map.get("permissions"); - loadPermissions(perms); + permissions = Permission.loadPermissions(perms, "Permission node '%s' in plugin description file for " + getFullName() + " is invalid", defaultPerm); } catch (ClassCastException ex) { throw new InvalidDescriptionException(ex, "permissions are of wrong type"); } @@ -276,6 +293,7 @@ public final class PluginDescriptionFile { map.put("version", version); map.put("database", database); map.put("order", order.toString()); + map.put("default-permission", defaultPerm.toString()); if (commands != null) { map.put("command", commands); @@ -301,16 +319,4 @@ public final class PluginDescriptionFile { return map; } - - private void loadPermissions(Map<String, Map<String, Object>> perms) { - Set<String> keys = perms.keySet(); - - for (String name : keys) { - try { - permissions.add(Permission.loadPermission(name, perms.get(name))); - } catch (Throwable ex) { - Bukkit.getServer().getLogger().log(Level.SEVERE, "Permission node '" + name + "' in plugin description file for " + getFullName() + " is invalid", ex); - } - } - } } |