Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.permissions;
2
3import org.bukkit.Bukkit;
4
5import java.util.LinkedHashMap;
6import java.util.Map;
7import java.util.Map.Entry;
8import java.util.Set;
9
10/**
11 * Represents a unique permission that may be attached to a {@link Permissible}
12 */
13public class Permission {
14 private final String name;
15 private final Map<String, Boolean> children = new LinkedHashMap<String, Boolean>();
16 private PermissionDefault defaultValue = PermissionDefault.FALSE;
17 private String description;
18
19 public Permission(String name) {
20 this(name, null, null, null);
21 }
22
23 public Permission(String name, String description) {
24 this(name, description, null, null);
25 }
26
27 public Permission(String name, PermissionDefault defaultValue) {
28 this(name, null, defaultValue, null);
29 }
30
31 public Permission(String name, String description, PermissionDefault defaultValue) {
32 this(name, description, defaultValue, null);
33 }
34
35 public Permission(String name, Map<String, Boolean> children) {
36 this(name, null, null, children);
37 }
38
39 public Permission(String name, String description, Map<String, Boolean> children) {
40 this(name, description, null, children);
41 }
42
43 public Permission(String name, PermissionDefault defaultValue, Map<String, Boolean> children) {
44 this(name, null, defaultValue, children);
45 }
46
47 public Permission(String name, String description, PermissionDefault defaultValue, Map<String, Boolean> children) {
48 this.name = name;
49 this.description = (description == null) ? "" : description;
50 this.defaultValue = (defaultValue == null) ? defaultValue.FALSE : defaultValue;
51
52 if (children != null) {
53 this.children.putAll(children);
54 }
55
56 recalculatePermissibles();
57 }
58
59 /**
60 * Returns the unique fully qualified name of this Permission
61 *
62 * @return Fully qualified name
63 */
64 public String getName() {
65 return name;
66 }
67
68 /**
69 * Gets the children of this permission.
70 * <p>
71 * If you change this map in any form, you must call {@link #recalculatePermissibles()} to recalculate all {@link Permissible}s
72 *
73 * @return Permission children
74 */
75 public Map<String, Boolean> getChildren() {
76 return children;
77 }
78
79 /**
80 * Gets the default value of this permission.
81 *
82 * @return Default value of this permission.
83 */
84 public PermissionDefault getDefault() {
85 return defaultValue;
86 }
87
88 /**
89 * Sets the default value of this permission.
90 * <p>
91 * This will not be saved to disk, and is a temporary operation until the server reloads permissions.
92 * Changing this default will cause all {@link Permissible}s that contain this permission to recalculate their permissions
93 *
94 * @param value The new default to set
95 */
96 public void setDefault(PermissionDefault value) {
97 if (defaultValue == null) {
98 throw new IllegalArgumentException("Default value cannot be null");
99 }
100
101 defaultValue = value;
102 recalculatePermissibles();
103 }
104
105 /**
106 * Gets a brief description of this permission, if set
107 *
108 * @return Brief description of this permission
109 */
110 public String getDescription() {
111 return description;
112 }
113
114 /**
115 * Sets the description of this permission.
116 * <p>
117 * This will not be saved to disk, and is a temporary operation until the server reloads permissions.
118 *
119 * @param value The new description to set
120 */
121 public void setDescription(String value) {
122 if (value == null) {
123 description = "";
124 } else {
125 description = value;
126 }
127 }
128
129 /**
130 * Gets a set containing every {@link Permissible} that has this permission.
131 * <p>
132 * This set cannot be modified.
133 *
134 * @return Set containing permissibles with this permission
135 */
136 public Set<Permissible> getPermissibles() {
137 return Bukkit.getServer().getPluginManager().getPermissionSubscriptions(name);
138 }
139
140 /**
141 * Recalculates all {@link Permissible}s that contain this permission.
142 * <p>
143 * This should be called after modifying the children, and is automatically called after modifying the default value
144 */
145 public void recalculatePermissibles() {
146 Set<Permissible> perms = getPermissibles();
147
148 Bukkit.getServer().getPluginManager().recalculatePermissionDefaults(this);
149
150 for (Permissible p : perms) {
151 p.recalculatePermissions();
152 }
153 }
154
155 /**
156 * Loads a Permission from a map of data, usually used from retrieval from a yaml file.
157 * <p>
158 * The data may contain the following keys:
159 * default: Boolean true or false. If not specified, false.
160 * children: Map<String, Boolean> of child permissions. If not specified, empty list.
161 * description: Short string containing a very small description of this description. If not specified, empty string.
162 *
163 * @param name Name of the permission
164 * @param data Map of keys
165 * @return Permission object
166 */
167 public static Permission loadPermission(String name, Map<String, Object> data) {
168 if (name == null) {
169 throw new IllegalArgumentException("Name cannot be null");
170 }
171 if (data == null) {
172 throw new IllegalArgumentException("Data cannot be null");
173 }
174 String desc = null;
175 PermissionDefault def = null;
176 Map<String, Boolean> children = null;
177
178 if (data.containsKey("default")) {
179 try {
180 PermissionDefault value = PermissionDefault.getByName(data.get("default").toString());
181 if (value != null) {
182 def = value;
183 } else {
184 throw new IllegalArgumentException("'default' key contained unknown value");
185 }
186 } catch (ClassCastException ex) {
187 throw new IllegalArgumentException("'default' key is of wrong type", ex);
188 }
189 }
190
191 if (data.containsKey("children")) {
192 try {
193 children = extractChildren(data);
194 } catch (ClassCastException ex) {
195 throw new IllegalArgumentException("'children' key is of wrong type", ex);
196 }
197 }
198
199 if (data.containsKey("description")) {
200 try {
201 desc = (String) data.get("description");
202 } catch (ClassCastException ex) {
203 throw new IllegalArgumentException("'description' key is of wrong type", ex);
204 }
205 }
206
207 return new Permission(name, desc, def, children);
208 }
209
210 private static Map<String, Boolean> extractChildren(Map<String, Object> data) {
211 Map<String, Boolean> input = (Map<String, Boolean>) data.get("children");
212 Set<Entry<String, Boolean>> entries = input.entrySet();
213
214 for (Map.Entry<String, Boolean> entry : entries) {
215 if (!(entry.getValue() instanceof Boolean)) {
216 throw new IllegalArgumentException("Child '" + entry.getKey() + "' contains invalid value");
217 }
218 }
219
220 return input;
221 }
222}