Made Permissions mutable, added methods to view which Permissibles are subscribed to which Permission

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot
2011-07-20 18:05:04 +01:00
parent 2be5996714
commit bfd0ab1851
5 changed files with 282 additions and 16 deletions

View File

@@ -154,12 +154,14 @@ public class PermissibleBase implements Permissible {
private synchronized void calculatePermissions() {
if (dirtyPermissions) {
permissions.clear();
clearPermissions();
Set<Permission> defaults = Bukkit.getServer().getPluginManager().getDefaultPermissions(isOp());
Bukkit.getServer().getPluginManager().subscribeToDefaultPerms(isOp(), this);
for (Permission perm : defaults) {
String name = perm.getName().toLowerCase();
permissions.put(name, new PermissionAttachmentInfo(this, name, null, true));
Bukkit.getServer().getPluginManager().subscribeToPermission(name, this);
calculateChildPermissions(perm.getChildren(), false, null);
}
@@ -171,6 +173,18 @@ public class PermissibleBase implements Permissible {
}
}
private synchronized void clearPermissions() {
Set<String> perms = permissions.keySet();
for (String name : perms) {
Bukkit.getServer().getPluginManager().unsubscribeFromPermission(name, this);
}
Bukkit.getServer().getPluginManager().unsubscribeFromDefaultPerms(isOp(), this);
permissions.clear();
}
private void calculateChildPermissions(Map<String, Boolean> children, boolean invert, PermissionAttachment attachment) {
Set<String> keys = children.keySet();
@@ -180,6 +194,7 @@ public class PermissibleBase implements Permissible {
String lname = name.toLowerCase();
permissions.put(lname, new PermissionAttachmentInfo(this, lname, attachment, value));
Bukkit.getServer().getPluginManager().subscribeToPermission(name, this);
if (perm != null) {
calculateChildPermissions(perm.getChildren(), !value, attachment);

View File

@@ -5,6 +5,7 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.bukkit.Bukkit;
/**
* Represents a unique permission that may be attached to a {@link Permissible}
@@ -51,6 +52,8 @@ public class Permission {
if (children != null) {
this.children.putAll(children);
}
recalculatePermissibles();
}
/**
@@ -65,12 +68,12 @@ public class Permission {
/**
* Gets the children of this permission.
*
* This is a copy and changes will not be saved.
* If you change this map in any form, you must call {@link #recalculatePermissibles()} to recalculate all {@link Permissible}s
*
* @return Permission children
*/
public Map<String, Boolean> getChildren() {
return new LinkedHashMap<String, Boolean>(children);
return children;
}
/**
@@ -82,6 +85,23 @@ public class Permission {
return defaultValue;
}
/**
* Sets the default value of this permission.
*
* This will not be saved to disk, and is a temporary operation until the server reloads permissions.
* Changing this default will cause all {@link Permissible}s that contain this permission to recalculate their permissions
*
* @param value The new default to set
*/
public void setDefault(PermissionDefault value) {
if (defaultValue == null) {
throw new IllegalArgumentException("Default value cannot be null");
}
defaultValue = value;
recalculatePermissibles();
}
/**
* Gets a brief description of this permission, if set
*
@@ -91,6 +111,47 @@ public class Permission {
return description;
}
/**
* Sets the description of this permission.
*
* This will not be saved to disk, and is a temporary operation until the server reloads permissions.
*
* @param value The new description to set
*/
public void setDescription(String value) {
if (value == null) {
description = "";
} else {
description = value;
}
}
/**
* Gets a set containing every {@link Permissible} that has this permission.
*
* This set cannot be modified.
*
* @return Set containing permissibles with this permission
*/
public Set<Permissible> getPermissibles() {
return Bukkit.getServer().getPluginManager().getPermissionSubscriptions(name);
}
/**
* Recalculates all {@link Permissible}s that contain this permission.
*
* This should be called after modifying the children, and is automatically called after modifying the default value
*/
public void recalculatePermissibles() {
Set<Permissible> perms = getPermissibles();
Bukkit.getServer().getPluginManager().recalculatePermissionDefaults(this);
for (Permissible p : perms) {
p.recalculatePermissions();
}
}
/**
* Loads a Permission from a map of data, usually used from retrieval from a yaml file.
*