Permission registration in plugin.yml can now be easier, see https://gist.github.com/32dca3e937c1c42a4ed2 - also added "default-permission" option.

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot
2011-09-20 18:42:28 +01:00
parent 44188b546a
commit acd8d918f1
2 changed files with 154 additions and 27 deletions

View File

@@ -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) {
@@ -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<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.
*
@@ -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);
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();
if (data.containsKey("parents")) {
try {
Object parents = data.get("parents");
for (Map.Entry<String, Boolean> entry : entries) {
if (!(entry.getValue() instanceof Boolean)) {
throw new IllegalArgumentException("Child '" + entry.getKey() + "' contains invalid value");
if (parents instanceof String) {
result.addParent((String)parents, true);
}
} catch (ClassCastException ex) {
throw new IllegalArgumentException("'parents' key is of wrong type", ex);
}
}
return input;
return result;
}
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 children;
}
}