Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
PermissibleBase.java
1 package org.bukkit.permissions;
2 
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.LinkedList;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.logging.Level;
10 import org.bukkit.Bukkit;
11 import org.bukkit.plugin.Plugin;
12 
13 /**
14  * Base Permissible for use in any Permissible object via proxy or extension
15  */
16 public class PermissibleBase implements Permissible {
17  private ServerOperator opable = null;
18  private Permissible parent = this;
19  private final List<PermissionAttachment> attachments = new LinkedList<PermissionAttachment>();
20  private final Map<String, PermissionAttachmentInfo> permissions = new HashMap<String, PermissionAttachmentInfo>();
21 
22  public PermissibleBase(ServerOperator opable) {
23  this.opable = opable;
24 
25  if (opable instanceof Permissible) {
26  this.parent = (Permissible) opable;
27  }
28 
30  }
31 
32  public boolean isOp() {
33  if (opable == null) {
34  return false;
35  } else {
36  return opable.isOp();
37  }
38  }
39 
40  public void setOp(boolean value) {
41  if (opable == null) {
42  throw new UnsupportedOperationException("Cannot change op value as no ServerOperator is set");
43  } else {
44  opable.setOp(value);
45  }
46  }
47 
48  public boolean isPermissionSet(String name) {
49  if (name == null) {
50  throw new IllegalArgumentException("Permission name cannot be null");
51  }
52 
53  return permissions.containsKey(name.toLowerCase());
54  }
55 
56  public boolean isPermissionSet(Permission perm) {
57  if (perm == null) {
58  throw new IllegalArgumentException("Permission cannot be null");
59  }
60 
61  return isPermissionSet(perm.getName());
62  }
63 
64  public boolean hasPermission(String inName) {
65  if (inName == null) {
66  throw new IllegalArgumentException("Permission name cannot be null");
67  }
68 
69  String name = inName.toLowerCase();
70 
71  if (isPermissionSet(name)) {
72  return permissions.get(name).getValue();
73  } else {
75 
76  if (perm != null) {
77  return perm.getDefault().getValue(isOp());
78  } else {
79  return Permission.DEFAULT_PERMISSION.getValue(isOp());
80  }
81  }
82  }
83 
84  public boolean hasPermission(Permission perm) {
85  if (perm == null) {
86  throw new IllegalArgumentException("Permission cannot be null");
87  }
88 
89  String name = perm.getName().toLowerCase();
90 
91  if (isPermissionSet(name)) {
92  return permissions.get(name).getValue();
93  }
94  return perm.getDefault().getValue(isOp());
95  }
96 
97  public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value) {
98  if (name == null) {
99  throw new IllegalArgumentException("Permission name cannot be null");
100  } else if (plugin == null) {
101  throw new IllegalArgumentException("Plugin cannot be null");
102  } else if (!plugin.isEnabled()) {
103  throw new IllegalArgumentException("Plugin " + plugin.getDescription().getFullName() + " is disabled");
104  }
105 
106  PermissionAttachment result = addAttachment(plugin);
107  result.setPermission(name, value);
108 
110 
111  return result;
112  }
113 
115  if (plugin == null) {
116  throw new IllegalArgumentException("Plugin cannot be null");
117  } else if (!plugin.isEnabled()) {
118  throw new IllegalArgumentException("Plugin " + plugin.getDescription().getFullName() + " is disabled");
119  }
120 
121  PermissionAttachment result = new PermissionAttachment(plugin, parent);
122 
123  attachments.add(result);
125 
126  return result;
127  }
128 
129  public void removeAttachment(PermissionAttachment attachment) {
130  if (attachment == null) {
131  throw new IllegalArgumentException("Attachment cannot be null");
132  }
133 
134  if (attachments.contains(attachment)) {
135  attachments.remove(attachment);
137 
138  if (ex != null) {
139  ex.attachmentRemoved(attachment);
140  }
141 
143  } else {
144  throw new IllegalArgumentException("Given attachment is not part of Permissible object " + parent);
145  }
146  }
147 
148  public void recalculatePermissions() {
149  clearPermissions();
150  Set<Permission> defaults = Bukkit.getServer().getPluginManager().getDefaultPermissions(isOp());
152 
153  for (Permission perm : defaults) {
154  String name = perm.getName().toLowerCase();
155  permissions.put(name, new PermissionAttachmentInfo(parent, name, null, true));
157  calculateChildPermissions(perm.getChildren(), false, null);
158  }
159 
160  for (PermissionAttachment attachment : attachments) {
161  calculateChildPermissions(attachment.getPermissions(), false, attachment);
162  }
163  }
164 
165  public synchronized void clearPermissions() {
166  Set<String> perms = permissions.keySet();
167 
168  for (String name : perms) {
170  }
171 
174 
175  permissions.clear();
176  }
177 
178  private void calculateChildPermissions(Map<String, Boolean> children, boolean invert, PermissionAttachment attachment) {
179  Set<String> keys = children.keySet();
180 
181  for (String name : keys) {
182  Permission perm = Bukkit.getServer().getPluginManager().getPermission(name);
183  boolean value = children.get(name) ^ invert;
184  String lname = name.toLowerCase();
185 
186  permissions.put(lname, new PermissionAttachmentInfo(parent, lname, attachment, value));
187  Bukkit.getServer().getPluginManager().subscribeToPermission(name, parent);
188 
189  if (perm != null) {
190  calculateChildPermissions(perm.getChildren(), !value, attachment);
191  }
192  }
193  }
194 
195  public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks) {
196  if (name == null) {
197  throw new IllegalArgumentException("Permission name cannot be null");
198  } else if (plugin == null) {
199  throw new IllegalArgumentException("Plugin cannot be null");
200  } else if (!plugin.isEnabled()) {
201  throw new IllegalArgumentException("Plugin " + plugin.getDescription().getFullName() + " is disabled");
202  }
203 
204  PermissionAttachment result = addAttachment(plugin, ticks);
205 
206  if (result != null) {
207  result.setPermission(name, value);
208  }
209 
210  return result;
211  }
212 
213  public PermissionAttachment addAttachment(Plugin plugin, int ticks) {
214  if (plugin == null) {
215  throw new IllegalArgumentException("Plugin cannot be null");
216  } else if (!plugin.isEnabled()) {
217  throw new IllegalArgumentException("Plugin " + plugin.getDescription().getFullName() + " is disabled");
218  }
219 
220  PermissionAttachment result = addAttachment(plugin);
221 
222  if (Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new RemoveAttachmentRunnable(result), ticks) == -1) {
223  Bukkit.getServer().getLogger().log(Level.WARNING, "Could not add PermissionAttachment to " + parent + " for plugin " + plugin.getDescription().getFullName() + ": Scheduler returned -1");
224  result.remove();
225  return null;
226  } else {
227  return result;
228  }
229  }
230 
231  public Set<PermissionAttachmentInfo> getEffectivePermissions() {
232  return new HashSet<PermissionAttachmentInfo>(permissions.values());
233  }
234 
235  private class RemoveAttachmentRunnable implements Runnable {
236  private PermissionAttachment attachment;
237 
238  public RemoveAttachmentRunnable(PermissionAttachment attachment) {
239  this.attachment = attachment;
240  }
241 
242  public void run() {
243  attachment.remove();
244  }
245  }
246 }
void attachmentRemoved(PermissionAttachment attachment)
void unsubscribeFromDefaultPerms(boolean op, Permissible permissible)
Set< Permission > getDefaultPermissions(boolean op)
PermissionDefault getDefault()
Definition: Permission.java:95
PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks)
PermissionAttachment addAttachment(Plugin plugin)
PluginManager getPluginManager()
void removeAttachment(PermissionAttachment attachment)
static Server getServer()
Definition: Bukkit.java:50
PluginDescriptionFile getDescription()
PermissionAttachment addAttachment(Plugin plugin, int ticks)
PermissionAttachment addAttachment(Plugin plugin, String name, boolean value)
void unsubscribeFromPermission(String permission, Permissible permissible)
Set< PermissionAttachmentInfo > getEffectivePermissions()
void subscribeToDefaultPerms(boolean op, Permissible permissible)
void subscribeToPermission(String permission, Permissible permissible)
boolean hasPermission(Permission perm)
void setPermission(String name, boolean value)
boolean isPermissionSet(Permission perm)
Logger getLogger()
Permission getPermission(String name)
int scheduleSyncDelayedTask(Plugin plugin, Runnable task, long delay)
BukkitScheduler getScheduler()