From 5774aaa6a0a78ca0069c8cd866e24c914e39ebbf Mon Sep 17 00:00:00 2001 From: Dinnerbone Date: Tue, 20 Sep 2011 18:42:28 +0100 Subject: Permission registration in plugin.yml can now be easier, see https://gist.github.com/32dca3e937c1c42a4ed2 - also added "default-permission" option. --- .../java/org/bukkit/permissions/Permission.java | 143 +++++++++++++++++++-- .../org/bukkit/plugin/PluginDescriptionFile.java | 36 +++--- 2 files changed, 153 insertions(+), 26 deletions(-) (limited to 'src/main') 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 children = new LinkedHashMap(); - private PermissionDefault defaultValue = PermissionDefault.FALSE; + private PermissionDefault defaultValue = DEFAULT_PERMISSION; private String description; public Permission(String name) { @@ -152,6 +158,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 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 loadPermissions(Map> data, String error, PermissionDefault def) { + List result = new ArrayList(); + + for (Map.Entry> 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. * @@ -165,6 +233,24 @@ public class Permission { * @return Permission object */ public static Permission loadPermission(String name, Map 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 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 data, PermissionDefault def, List 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 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 extractChildren(Map data) { - Map input = (Map)data.get("children"); - Set> entries = input.entrySet(); - for (Map.Entry entry : entries) { - if (!(entry.getValue() instanceof Boolean)) { - throw new IllegalArgumentException("Child '" + entry.getKey() + "' contains invalid value"); + private static Map extractChildren(Map data, String name, PermissionDefault def, List output) { + Map input = (Map)data.get("children"); + Map children = new LinkedHashMap(); + + for (Map.Entry 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)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 permissions = new ArrayList(); + private List permissions = new ArrayList(); + 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 getPermissions() { + public List getPermissions() { return permissions; } + public PermissionDefault getPermissionDefault() { + return defaultPerm; + } + private void loadMap(Map 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> perms = (Map>) 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> perms) { - Set 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); - } - } - } } -- cgit v1.2.3