Bukkit  1.4.7-R1.0
 All Classes Namespaces Files Functions Variables Enumerator Pages
Permission.java
Go to the documentation of this file.
1 package org.bukkit.permissions;
2 
3 import java.util.ArrayList;
4 import java.util.LinkedHashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8 import java.util.logging.Level;
9 
10 import org.apache.commons.lang.Validate;
11 import org.bukkit.Bukkit;
12 import org.bukkit.plugin.PluginManager;
13 
17 public class Permission {
19 
20  private final String name;
21  private final Map<String, Boolean> children = new LinkedHashMap<String, Boolean>();
22  private PermissionDefault defaultValue = DEFAULT_PERMISSION;
23  private String description;
24 
25  public Permission(String name) {
26  this(name, null, null, null);
27  }
28 
29  public Permission(String name, String description) {
30  this(name, description, null, null);
31  }
32 
33  public Permission(String name, PermissionDefault defaultValue) {
34  this(name, null, defaultValue, null);
35  }
36 
37  public Permission(String name, String description, PermissionDefault defaultValue) {
38  this(name, description, defaultValue, null);
39  }
40 
41  public Permission(String name, Map<String, Boolean> children) {
42  this(name, null, null, children);
43  }
44 
45  public Permission(String name, String description, Map<String, Boolean> children) {
46  this(name, description, null, children);
47  }
48 
49  public Permission(String name, PermissionDefault defaultValue, Map<String, Boolean> children) {
50  this(name, null, defaultValue, children);
51  }
52 
53  public Permission(String name, String description, PermissionDefault defaultValue, Map<String, Boolean> children) {
54  this.name = name;
55  this.description = (description == null) ? "" : description;
56 
57  if (defaultValue != null) {
58  this.defaultValue = defaultValue;
59  }
60 
61  if (children != null) {
62  this.children.putAll(children);
63  }
64 
66  }
67 
73  public String getName() {
74  return name;
75  }
76 
84  public Map<String, Boolean> getChildren() {
85  return children;
86  }
87 
94  return defaultValue;
95  }
96 
105  public void setDefault(PermissionDefault value) {
106  if (defaultValue == null) {
107  throw new IllegalArgumentException("Default value cannot be null");
108  }
109 
110  defaultValue = value;
112  }
113 
119  public String getDescription() {
120  return description;
121  }
122 
130  public void setDescription(String value) {
131  if (value == null) {
132  description = "";
133  } else {
134  description = value;
135  }
136  }
137 
145  public Set<Permissible> getPermissibles() {
147  }
148 
154  public void recalculatePermissibles() {
155  Set<Permissible> perms = getPermissibles();
156 
158 
159  for (Permissible p : perms) {
160  p.recalculatePermissions();
161  }
162  }
163 
173  public Permission addParent(String name, boolean value) {
175  String lname = name.toLowerCase();
176 
177  Permission perm = pm.getPermission(lname);
178 
179  if (perm == null) {
180  perm = new Permission(lname);
181  pm.addPermission(perm);
182  }
183 
184  addParent(perm, value);
185 
186  return perm;
187  }
188 
195  public void addParent(Permission perm, boolean value) {
196  perm.getChildren().put(getName(), value);
198  }
199 
213  public static List<Permission> loadPermissions(Map<?, ?> data, String error, PermissionDefault def) {
214  List<Permission> result = new ArrayList<Permission>();
215 
216  for (Map.Entry<?, ?> entry : data.entrySet()) {
217  try {
218  result.add(Permission.loadPermission(entry.getKey().toString(), (Map<?, ?>) entry.getValue(), def, result));
219  } catch (Throwable ex) {
220  Bukkit.getServer().getLogger().log(Level.SEVERE, String.format(error, entry.getKey()), ex);
221  }
222  }
223 
224  return result;
225  }
226 
239  public static Permission loadPermission(String name, Map<String, Object> data) {
240  return loadPermission(name, data, DEFAULT_PERMISSION, null);
241  }
242 
257  public static Permission loadPermission(String name, Map<?, ?> data, PermissionDefault def, List<Permission> output) {
258  Validate.notNull(name, "Name cannot be null");
259  Validate.notNull(data, "Data cannot be null");
260 
261  String desc = null;
262  Map<String, Boolean> children = null;
263 
264  if (data.get("default") != null) {
265  PermissionDefault value = PermissionDefault.getByName(data.get("default").toString());
266  if (value != null) {
267  def = value;
268  } else {
269  throw new IllegalArgumentException("'default' key contained unknown value");
270  }
271  }
272 
273  if (data.get("children") != null) {
274  Object childrenNode = data.get("children");
275  if (childrenNode instanceof Iterable) {
276  children = new LinkedHashMap<String, Boolean>();
277  for (Object child : (Iterable<?>) childrenNode) {
278  if (child != null) {
279  children.put(child.toString(), Boolean.TRUE);
280  }
281  }
282  } else if (childrenNode instanceof Map) {
283  children = extractChildren((Map<?,?>) childrenNode, name, def, output);
284  } else {
285  throw new IllegalArgumentException("'children' key is of wrong type");
286  }
287  }
288 
289  if (data.get("description") != null) {
290  desc = data.get("description").toString();
291  }
292 
293  return new Permission(name, desc, def, children);
294  }
295 
296  private static Map<String, Boolean> extractChildren(Map<?, ?> input, String name, PermissionDefault def, List<Permission> output) {
297  Map<String, Boolean> children = new LinkedHashMap<String, Boolean>();
298 
299  for (Map.Entry<?, ?> entry : input.entrySet()) {
300  if ((entry.getValue() instanceof Boolean)) {
301  children.put(entry.getKey().toString(), (Boolean) entry.getValue());
302  } else if ((entry.getValue() instanceof Map)) {
303  try {
304  Permission perm = loadPermission(entry.getKey().toString(), (Map<?, ?>) entry.getValue(), def, output);
305  children.put(perm.getName(), Boolean.TRUE);
306 
307  if (output != null) {
308  output.add(perm);
309  }
310  } catch (Throwable ex) {
311  throw new IllegalArgumentException("Permission node '" + entry.getKey().toString() + "' in child of " + name + " is invalid", ex);
312  }
313  } else {
314  throw new IllegalArgumentException("Child '" + entry.getKey().toString() + "' contains invalid value");
315  }
316  }
317 
318  return children;
319  }
320 }