001 package org.bukkit.permissions;
002
003 import java.util.HashMap;
004 import java.util.Map;
005
006 /**
007 * Represents the possible default values for permissions
008 */
009 public enum PermissionDefault {
010 TRUE("true"),
011 FALSE("false"),
012 OP("op", "isop", "operator", "isoperator", "admin", "isadmin"),
013 NOT_OP("!op", "notop", "!operator", "notoperator", "!admin", "notadmin");
014
015 private final String[] names;
016 private final static Map<String, PermissionDefault> lookup = new HashMap<String, PermissionDefault>();
017
018 private PermissionDefault(String... names) {
019 this.names = names;
020 }
021
022 /**
023 * Calculates the value of this PermissionDefault for the given operator
024 * value
025 *
026 * @param op If the target is op
027 * @return True if the default should be true, or false
028 */
029 public boolean getValue(boolean op) {
030 switch (this) {
031 case TRUE:
032 return true;
033 case FALSE:
034 return false;
035 case OP:
036 return op;
037 case NOT_OP:
038 return !op;
039 default:
040 return false;
041 }
042 }
043
044 /**
045 * Looks up a PermissionDefault by name
046 *
047 * @param name Name of the default
048 * @return Specified value, or null if not found
049 */
050 public static PermissionDefault getByName(String name) {
051 return lookup.get(name.toLowerCase().replaceAll("[^a-z!]", ""));
052 }
053
054 @Override
055 public String toString() {
056 return names[0];
057 }
058
059 static {
060 for (PermissionDefault value : values()) {
061 for (String name : value.names) {
062 lookup.put(name, value);
063 }
064 }
065 }
066 }