Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
PermissionAttachment.java
1 package org.bukkit.permissions;
2 
3 import java.util.LinkedHashMap;
4 import java.util.Map;
5 import org.bukkit.plugin.Plugin;
6 
7 /**
8  * Holds information about a permission attachment on a {@link Permissible}
9  * object
10  */
11 public class PermissionAttachment {
12  private PermissionRemovedExecutor removed;
13  private final Map<String, Boolean> permissions = new LinkedHashMap<String, Boolean>();
14  private final Permissible permissible;
15  private final Plugin plugin;
16 
18  if (plugin == null) {
19  throw new IllegalArgumentException("Plugin cannot be null");
20  } else if (!plugin.isEnabled()) {
21  throw new IllegalArgumentException("Plugin " + plugin.getDescription().getFullName() + " is disabled");
22  }
23 
24  this.permissible = Permissible;
25  this.plugin = plugin;
26  }
27 
28  /**
29  * Gets the plugin responsible for this attachment
30  *
31  * @return Plugin responsible for this permission attachment
32  */
33  public Plugin getPlugin() {
34  return plugin;
35  }
36 
37  /**
38  * Sets an object to be called for when this attachment is removed from a
39  * {@link Permissible}. May be null.
40  *
41  * @param ex Object to be called when this is removed
42  */
44  removed = ex;
45  }
46 
47  /**
48  * Gets the class that was previously set to be called when this
49  * attachment was removed from a {@link Permissible}. May be null.
50  *
51  * @return Object to be called when this is removed
52  */
54  return removed;
55  }
56 
57  /**
58  * Gets the Permissible that this is attached to
59  *
60  * @return Permissible containing this attachment
61  */
62  public Permissible getPermissible() {
63  return permissible;
64  }
65 
66  /**
67  * Gets a copy of all set permissions and values contained within this
68  * attachment.
69  * <p>
70  * This map may be modified but will not affect the attachment, as it is a
71  * copy.
72  *
73  * @return Copy of all permissions and values expressed by this attachment
74  */
75  public Map<String, Boolean> getPermissions() {
76  return new LinkedHashMap<String, Boolean>(permissions);
77  }
78 
79  /**
80  * Sets a permission to the given value, by its fully qualified name
81  *
82  * @param name Name of the permission
83  * @param value New value of the permission
84  */
85  public void setPermission(String name, boolean value) {
86  permissions.put(name.toLowerCase(), value);
87  permissible.recalculatePermissions();
88  }
89 
90  /**
91  * Sets a permission to the given value
92  *
93  * @param perm Permission to set
94  * @param value New value of the permission
95  */
96  public void setPermission(Permission perm, boolean value) {
97  setPermission(perm.getName(), value);
98  }
99 
100  /**
101  * Removes the specified permission from this attachment.
102  * <p>
103  * If the permission does not exist in this attachment, nothing will
104  * happen.
105  *
106  * @param name Name of the permission to remove
107  */
108  public void unsetPermission(String name) {
109  permissions.remove(name.toLowerCase());
110  permissible.recalculatePermissions();
111  }
112 
113  /**
114  * Removes the specified permission from this attachment.
115  * <p>
116  * If the permission does not exist in this attachment, nothing will
117  * happen.
118  *
119  * @param perm Permission to remove
120  */
121  public void unsetPermission(Permission perm) {
122  unsetPermission(perm.getName());
123  }
124 
125  /**
126  * Removes this attachment from its registered {@link Permissible}
127  *
128  * @return true if the permissible was removed successfully, false if it
129  * did not exist
130  */
131  public boolean remove() {
132  try {
133  permissible.removeAttachment(this);
134  return true;
135  } catch (IllegalArgumentException ex) {
136  return false;
137  }
138  }
139 }
void setRemovalCallback(PermissionRemovedExecutor ex)
PluginDescriptionFile getDescription()
void setPermission(Permission perm, boolean value)
void setPermission(String name, boolean value)