Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
Permission.java
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;
13 
14 /**
15  * Represents a unique permission that may be attached to a {@link
16  * Permissible}
17  */
18 public class Permission {
19  public static final PermissionDefault DEFAULT_PERMISSION = PermissionDefault.OP;
20 
21  private final String name;
22  private final Map<String, Boolean> children = new LinkedHashMap<String, Boolean>();
23  private PermissionDefault defaultValue = DEFAULT_PERMISSION;
24  private String description;
25 
26  public Permission(String name) {
27  this(name, null, null, null);
28  }
29 
30  public Permission(String name, String description) {
31  this(name, description, null, null);
32  }
33 
34  public Permission(String name, PermissionDefault defaultValue) {
35  this(name, null, defaultValue, null);
36  }
37 
38  public Permission(String name, String description, PermissionDefault defaultValue) {
39  this(name, description, defaultValue, null);
40  }
41 
42  public Permission(String name, Map<String, Boolean> children) {
43  this(name, null, null, children);
44  }
45 
46  public Permission(String name, String description, Map<String, Boolean> children) {
47  this(name, description, null, children);
48  }
49 
50  public Permission(String name, PermissionDefault defaultValue, Map<String, Boolean> children) {
51  this(name, null, defaultValue, children);
52  }
53 
54  public Permission(String name, String description, PermissionDefault defaultValue, Map<String, Boolean> children) {
55  this.name = name;
56  this.description = (description == null) ? "" : description;
57 
58  if (defaultValue != null) {
59  this.defaultValue = defaultValue;
60  }
61 
62  if (children != null) {
63  this.children.putAll(children);
64  }
65 
67  }
68 
69  /**
70  * Returns the unique fully qualified name of this Permission
71  *
72  * @return Fully qualified name
73  */
74  public String getName() {
75  return name;
76  }
77 
78  /**
79  * Gets the children of this permission.
80  * <p>
81  * If you change this map in any form, you must call {@link
82  * #recalculatePermissibles()} to recalculate all {@link Permissible}s
83  *
84  * @return Permission children
85  */
86  public Map<String, Boolean> getChildren() {
87  return children;
88  }
89 
90  /**
91  * Gets the default value of this permission.
92  *
93  * @return Default value of this permission.
94  */
96  return defaultValue;
97  }
98 
99  /**
100  * Sets the default value of this permission.
101  * <p>
102  * This will not be saved to disk, and is a temporary operation until the
103  * server reloads permissions. Changing this default will cause all {@link
104  * Permissible}s that contain this permission to recalculate their
105  * permissions
106  *
107  * @param value The new default to set
108  */
109  public void setDefault(PermissionDefault value) {
110  if (defaultValue == null) {
111  throw new IllegalArgumentException("Default value cannot be null");
112  }
113 
114  defaultValue = value;
116  }
117 
118  /**
119  * Gets a brief description of this permission, if set
120  *
121  * @return Brief description of this permission
122  */
123  public String getDescription() {
124  return description;
125  }
126 
127  /**
128  * Sets the description of this permission.
129  * <p>
130  * This will not be saved to disk, and is a temporary operation until the
131  * server reloads permissions.
132  *
133  * @param value The new description to set
134  */
135  public void setDescription(String value) {
136  if (value == null) {
137  description = "";
138  } else {
139  description = value;
140  }
141  }
142 
143  /**
144  * Gets a set containing every {@link Permissible} that has this
145  * permission.
146  * <p>
147  * This set cannot be modified.
148  *
149  * @return Set containing permissibles with this permission
150  */
151  public Set<Permissible> getPermissibles() {
153  }
154 
155  /**
156  * Recalculates all {@link Permissible}s that contain this permission.
157  * <p>
158  * This should be called after modifying the children, and is
159  * automatically called after modifying the default value
160  */
161  public void recalculatePermissibles() {
162  Set<Permissible> perms = getPermissibles();
163 
165 
166  for (Permissible p : perms) {
167  p.recalculatePermissions();
168  }
169  }
170 
171  /**
172  * Adds this permission to the specified parent permission.
173  * <p>
174  * If the parent permission does not exist, it will be created and
175  * registered.
176  *
177  * @param name Name of the parent permission
178  * @param value The value to set this permission to
179  * @return Parent permission it created or loaded
180  */
181  public Permission addParent(String name, boolean value) {
183  String lname = name.toLowerCase();
184 
185  Permission perm = pm.getPermission(lname);
186 
187  if (perm == null) {
188  perm = new Permission(lname);
189  pm.addPermission(perm);
190  }
191 
192  addParent(perm, value);
193 
194  return perm;
195  }
196 
197  /**
198  * Adds this permission to the specified parent permission.
199  *
200  * @param perm Parent permission to register with
201  * @param value The value to set this permission to
202  */
203  public void addParent(Permission perm, boolean value) {
204  perm.getChildren().put(getName(), value);
206  }
207 
208  /**
209  * Loads a list of Permissions from a map of data, usually used from
210  * retrieval from a yaml file.
211  * <p>
212  * The data may contain a list of name:data, where the data contains the
213  * following keys:
214  * <ul>
215  * <li>default: Boolean true or false. If not specified, false.
216  * <li>children: Map<String, Boolean> of child permissions. If not
217  * specified, empty list.
218  * <li>description: Short string containing a very small description of
219  * this description. If not specified, empty string.
220  * </ul>
221  *
222  * @param data Map of permissions
223  * @param error An error message to show if a permission is invalid.
224  * @param def Default permission value to use if missing
225  * @return Permission object
226  */
227  public static List<Permission> loadPermissions(Map<?, ?> data, String error, PermissionDefault def) {
228  List<Permission> result = new ArrayList<Permission>();
229 
230  for (Map.Entry<?, ?> entry : data.entrySet()) {
231  try {
232  result.add(Permission.loadPermission(entry.getKey().toString(), (Map<?, ?>) entry.getValue(), def, result));
233  } catch (Throwable ex) {
234  Bukkit.getServer().getLogger().log(Level.SEVERE, String.format(error, entry.getKey()), ex);
235  }
236  }
237 
238  return result;
239  }
240 
241  /**
242  * Loads a Permission from a map of data, usually used from retrieval from
243  * a yaml file.
244  * <p>
245  * The data may contain the following keys:
246  * <ul>
247  * <li>default: Boolean true or false. If not specified, false.
248  * <li>children: Map<String, Boolean> of child permissions. If not
249  * specified, empty list.
250  * <li>description: Short string containing a very small description of
251  * this description. If not specified, empty string.
252  *
253  * @param name Name of the permission
254  * @param data Map of keys
255  * @return Permission object
256  */
257  public static Permission loadPermission(String name, Map<String, Object> data) {
258  return loadPermission(name, data, DEFAULT_PERMISSION, null);
259  }
260 
261  /**
262  * Loads a Permission from a map of data, usually used from retrieval from
263  * a yaml file.
264  * <p>
265  * The data may contain the following keys:
266  * <ul>
267  * <li>default: Boolean true or false. If not specified, false.
268  * <li>children: Map<String, Boolean> of child permissions. If not
269  * specified, empty list.
270  * <li>description: Short string containing a very small description of
271  * this description. If not specified, empty string.
272  * </ul>
273  *
274  * @param name Name of the permission
275  * @param data Map of keys
276  * @param def Default permission value to use if not set
277  * @param output A list to append any created child-Permissions to, may be null
278  * @return Permission object
279  */
280  public static Permission loadPermission(String name, Map<?, ?> data, PermissionDefault def, List<Permission> output) {
281  Validate.notNull(name, "Name cannot be null");
282  Validate.notNull(data, "Data cannot be null");
283 
284  String desc = null;
285  Map<String, Boolean> children = null;
286 
287  if (data.get("default") != null) {
288  PermissionDefault value = PermissionDefault.getByName(data.get("default").toString());
289  if (value != null) {
290  def = value;
291  } else {
292  throw new IllegalArgumentException("'default' key contained unknown value");
293  }
294  }
295 
296  if (data.get("children") != null) {
297  Object childrenNode = data.get("children");
298  if (childrenNode instanceof Iterable) {
299  children = new LinkedHashMap<String, Boolean>();
300  for (Object child : (Iterable<?>) childrenNode) {
301  if (child != null) {
302  children.put(child.toString(), Boolean.TRUE);
303  }
304  }
305  } else if (childrenNode instanceof Map) {
306  children = extractChildren((Map<?,?>) childrenNode, name, def, output);
307  } else {
308  throw new IllegalArgumentException("'children' key is of wrong type");
309  }
310  }
311 
312  if (data.get("description") != null) {
313  desc = data.get("description").toString();
314  }
315 
316  return new Permission(name, desc, def, children);
317  }
318 
319  private static Map<String, Boolean> extractChildren(Map<?, ?> input, String name, PermissionDefault def, List<Permission> output) {
320  Map<String, Boolean> children = new LinkedHashMap<String, Boolean>();
321 
322  for (Map.Entry<?, ?> entry : input.entrySet()) {
323  if ((entry.getValue() instanceof Boolean)) {
324  children.put(entry.getKey().toString(), (Boolean) entry.getValue());
325  } else if ((entry.getValue() instanceof Map)) {
326  try {
327  Permission perm = loadPermission(entry.getKey().toString(), (Map<?, ?>) entry.getValue(), def, output);
328  children.put(perm.getName(), Boolean.TRUE);
329 
330  if (output != null) {
331  output.add(perm);
332  }
333  } catch (Throwable ex) {
334  throw new IllegalArgumentException("Permission node '" + entry.getKey().toString() + "' in child of " + name + " is invalid", ex);
335  }
336  } else {
337  throw new IllegalArgumentException("Child '" + entry.getKey().toString() + "' contains invalid value");
338  }
339  }
340 
341  return children;
342  }
343 }
void addParent(Permission perm, boolean value)
void setDescription(String value)
Map< String, Boolean > getChildren()
Definition: Permission.java:86
PermissionDefault getDefault()
Definition: Permission.java:95
void setDefault(PermissionDefault value)
PluginManager getPluginManager()
static PermissionDefault getByName(String name)
static Server getServer()
Definition: Bukkit.java:50
void recalculatePermissionDefaults(Permission perm)
void addPermission(Permission perm)
static List< Permission > loadPermissions(Map<?,?> data, String error, PermissionDefault def)
Set< Permissible > getPermissionSubscriptions(String permission)
static Permission loadPermission(String name, Map< String, Object > data)
Logger getLogger()
Permission addParent(String name, boolean value)
Set< Permissible > getPermissibles()
Permission getPermission(String name)
static Permission loadPermission(String name, Map<?,?> data, PermissionDefault def, List< Permission > output)