Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 134 lines 4.1 kB view raw
1package org.bukkit.permissions; 2 3import org.bukkit.plugin.Plugin; 4 5import java.util.Map; 6import java.util.TreeMap; 7 8/** 9 * Holds information about a permission attachment on a {@link Permissible} object 10 */ 11public class PermissionAttachment { 12 private PermissionRemovedExecutor removed; 13 private final TreeMap<String, Boolean> permissions = new TreeMap<String, Boolean>(); 14 private final Permissible permissible; 15 private final Plugin plugin; 16 17 public PermissionAttachment(Plugin plugin, Permissible Permissible) { 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 {@link Permissible}. May be null. 39 * 40 * @param ex Object to be called when this is removed 41 */ 42 public void setRemovalCallback(PermissionRemovedExecutor ex) { 43 removed = ex; 44 } 45 46 /** 47 * Gets the class that was previously set to be called when this attachment was removed from a {@link Permissible}. May be null. 48 * 49 * @return Object to be called when this is removed 50 */ 51 public PermissionRemovedExecutor getRemovalCallback() { 52 return removed; 53 } 54 55 /** 56 * Gets the Permissible that this is attached to 57 * 58 * @return Permissible containing this attachment 59 */ 60 public Permissible getPermissible() { 61 return permissible; 62 } 63 64 /** 65 * Gets a copy of all set permissions and values contained within this attachment. 66 * <p> 67 * This map may be modified but will not affect the attachment, as it is a copy. 68 * 69 * @return Copy of all permissions and values expressed by this attachment 70 */ 71 public Map<String, Boolean> getPermissions() { 72 return (Map<String, Boolean>) permissions.clone(); 73 } 74 75 /** 76 * Sets a permission to the given value, by its fully qualified name 77 * 78 * @param name Name of the permission 79 * @param value New value of the permission 80 */ 81 public void setPermission(String name, boolean value) { 82 permissions.put(name.toLowerCase(), value); 83 permissible.recalculatePermissions(); 84 } 85 86 /** 87 * Sets a permission to the given value 88 * 89 * @param perm Permission to set 90 * @param value New value of the permission 91 */ 92 public void setPermission(Permission perm, boolean value) { 93 setPermission(perm.getName(), value); 94 permissible.recalculatePermissions(); 95 } 96 97 /** 98 * Removes the specified permission from this attachment. 99 * <p> 100 * If the permission does not exist in this attachment, nothing will happen. 101 * 102 * @param name Name of the permission to remove 103 */ 104 public void unsetPermission(String name) { 105 permissions.remove(name.toLowerCase()); 106 permissible.recalculatePermissions(); 107 } 108 109 /** 110 * Removes the specified permission from this attachment. 111 * <p> 112 * If the permission does not exist in this attachment, nothing will happen. 113 * 114 * @param perm Permission to remove 115 */ 116 public void unsetPermission(Permission perm) { 117 unsetPermission(perm.getName()); 118 permissible.recalculatePermissions(); 119 } 120 121 /** 122 * Removes this attachment from its registered {@link Permissible} 123 * 124 * @return true if the permissible was removed successfully, false if it did not exist 125 */ 126 public boolean remove() { 127 try { 128 permissible.removeAttachment(this); 129 return true; 130 } catch (IllegalArgumentException ex) { 131 return false; 132 } 133 } 134}