From f777b33917293195b3dcb11278d7068466b03154 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 14 Jun 2014 20:16:48 +1200 Subject: [PATCH] Read desc Change all the permissions code to a 'better' version By default people can no longer use options without the option perm or * DisguiseType returns is now nicer looking --- .../commands/DisguiseCloneCommand.java | 6 +- .../disguise/commands/DisguiseCommand.java | 8 +- .../commands/DisguiseEntityCommand.java | 8 +- .../commands/DisguiseHelpCommand.java | 7 +- .../commands/DisguisePlayerCommand.java | 14 +- .../commands/DisguiseRadiusCommand.java | 14 +- .../utilities/BaseDisguiseCommand.java | 308 +++++++++++------- 7 files changed, 216 insertions(+), 149 deletions(-) diff --git a/src/me/libraryaddict/disguise/commands/DisguiseCloneCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseCloneCommand.java index 13722134..ef1373d6 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseCloneCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseCloneCommand.java @@ -1,7 +1,11 @@ package me.libraryaddict.disguise.commands; +import java.util.ArrayList; +import java.util.HashMap; + import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseListener; +import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.utilities.BaseDisguiseCommand; import org.apache.commons.lang.StringUtils; @@ -55,7 +59,7 @@ public class DisguiseCloneCommand extends BaseDisguiseCommand { /** * Send the player the information */ - protected void sendCommandUsage(CommandSender sender) { + protected void sendCommandUsage(CommandSender sender, HashMap, Boolean>> map) { sender.sendMessage(ChatColor.DARK_GREEN + "Right click a entity to get a disguise reference you can pass to other disguise commands!"); sender.sendMessage(ChatColor.DARK_GREEN + "Beware however, the reference bypasses all permissions checks"); diff --git a/src/me/libraryaddict/disguise/commands/DisguiseCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseCommand.java index 1833d5d2..722a6d48 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseCommand.java @@ -1,10 +1,12 @@ package me.libraryaddict.disguise.commands; import java.util.ArrayList; +import java.util.HashMap; import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.disguisetypes.Disguise; +import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.utilities.BaseDisguiseCommand; @@ -24,7 +26,7 @@ public class DisguiseCommand extends BaseDisguiseCommand { } Disguise disguise; try { - disguise = parseDisguise(sender, args); + disguise = parseDisguise(sender, args, getPermissions(sender)); } catch (Exception ex) { if (ex.getMessage() != null && !ChatColor.getLastColors(ex.getMessage()).equals("")) { sender.sendMessage(ex.getMessage()); @@ -53,8 +55,8 @@ public class DisguiseCommand extends BaseDisguiseCommand { /** * Send the player the information */ - protected void sendCommandUsage(CommandSender sender) { - ArrayList allowedDisguises = getAllowedDisguises(sender); + protected void sendCommandUsage(CommandSender sender, HashMap, Boolean>> map) { + ArrayList allowedDisguises = getAllowedDisguises(map); sender.sendMessage(ChatColor.DARK_GREEN + "Choose a disguise to become the disguise!"); sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)); diff --git a/src/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java index e9148c4a..f9d27c18 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java @@ -1,10 +1,12 @@ package me.libraryaddict.disguise.commands; import java.util.ArrayList; +import java.util.HashMap; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseListener; import me.libraryaddict.disguise.disguisetypes.Disguise; +import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.utilities.BaseDisguiseCommand; import org.apache.commons.lang.StringUtils; @@ -28,7 +30,7 @@ public class DisguiseEntityCommand extends BaseDisguiseCommand { } Disguise disguise; try { - disguise = parseDisguise(sender, args); + disguise = parseDisguise(sender, args, getPermissions(sender)); } catch (Exception ex) { if (ex.getMessage() != null && !ChatColor.getLastColors(ex.getMessage()).equals("")) { sender.sendMessage(ex.getMessage()); @@ -46,8 +48,8 @@ public class DisguiseEntityCommand extends BaseDisguiseCommand { /** * Send the player the information */ - protected void sendCommandUsage(CommandSender sender) { - ArrayList allowedDisguises = getAllowedDisguises(sender); + protected void sendCommandUsage(CommandSender sender, HashMap, Boolean>> map) { + ArrayList allowedDisguises = getAllowedDisguises(map); sender.sendMessage(ChatColor.DARK_GREEN + "Choose a disguise then right click a entity to disguise it!"); sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)); diff --git a/src/me/libraryaddict/disguise/commands/DisguiseHelpCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseHelpCommand.java index 2ff4fb66..1a5f7eb4 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseHelpCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseHelpCommand.java @@ -117,10 +117,9 @@ public class DisguiseHelpCommand extends BaseDisguiseCommand { @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { for (String node : new String[] { "disguise", "disguiseradius", "disguiseentity", "disguiseplayer" }) { - ArrayList allowedDisguises = getAllowedDisguises(sender, "libsdisguises." + node + "."); - if (!allowedDisguises.isEmpty()) { + if (!getPermissions(sender, "libsdisguises." + node + ".").isEmpty()) { if (args.length == 0) { - sendCommandUsage(sender); + sendCommandUsage(sender, null); return true; } else { EnumHelp help = null; @@ -219,7 +218,7 @@ public class DisguiseHelpCommand extends BaseDisguiseCommand { /** * Send the player the information */ - protected void sendCommandUsage(CommandSender sender) { + protected void sendCommandUsage(CommandSender sender, HashMap, Boolean>> map) { sender.sendMessage(ChatColor.RED + "/disguisehelp " + ChatColor.GREEN + "- View the options you can set on a disguise"); for (EnumHelp s : enumHelp) { diff --git a/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java b/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java index ccba8de0..e4e5f912 100644 --- a/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java @@ -1,10 +1,12 @@ package me.libraryaddict.disguise.commands; import java.util.ArrayList; +import java.util.HashMap; import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.disguisetypes.Disguise; +import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.utilities.BaseDisguiseCommand; @@ -19,13 +21,13 @@ public class DisguisePlayerCommand extends BaseDisguiseCommand { @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - ArrayList allowedDisguises = getAllowedDisguises(sender); - if (allowedDisguises.isEmpty()) { + HashMap, Boolean>> map = getPermissions(sender); + if (map.isEmpty()) { sender.sendMessage(ChatColor.RED + "You are forbidden to use this command."); return true; } if (args.length == 0) { - sendCommandUsage(sender); + sendCommandUsage(sender, map); return true; } if (args.length == 1) { @@ -41,7 +43,7 @@ public class DisguisePlayerCommand extends BaseDisguiseCommand { System.arraycopy(args, 1, newArgs, 0, newArgs.length); Disguise disguise; try { - disguise = parseDisguise(sender, newArgs); + disguise = parseDisguise(sender, newArgs, map); } catch (Exception ex) { if (ex.getMessage() != null && !ChatColor.getLastColors(ex.getMessage()).equals("")) { sender.sendMessage(ex.getMessage()); @@ -77,8 +79,8 @@ public class DisguisePlayerCommand extends BaseDisguiseCommand { /** * Send the player the information */ - protected void sendCommandUsage(CommandSender sender) { - ArrayList allowedDisguises = getAllowedDisguises(sender); + protected void sendCommandUsage(CommandSender sender, HashMap, Boolean>> map) { + ArrayList allowedDisguises = getAllowedDisguises(map); sender.sendMessage(ChatColor.DARK_GREEN + "Disguise another player!"); sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)); diff --git a/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java index 1c993600..04eccb31 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java @@ -2,10 +2,12 @@ package me.libraryaddict.disguise.commands; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.disguisetypes.Disguise; +import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.utilities.BaseDisguiseCommand; import me.libraryaddict.disguise.utilities.ClassGetter; @@ -38,13 +40,13 @@ public class DisguiseRadiusCommand extends BaseDisguiseCommand { sender.sendMessage(ChatColor.RED + "You may not use this command from the console!"); return true; } - ArrayList allowedDisguises = getAllowedDisguises(sender); - if (allowedDisguises.isEmpty()) { + HashMap, Boolean>> map = getPermissions(sender); + if (map.isEmpty()) { sender.sendMessage(ChatColor.RED + "You are forbidden to use this command."); return true; } if (args.length == 0) { - sendCommandUsage(sender); + sendCommandUsage(sender, map); return true; } if (args[0].equalsIgnoreCase("entitytype") || args[0].equalsIgnoreCase("entitytypes")) { @@ -97,7 +99,7 @@ public class DisguiseRadiusCommand extends BaseDisguiseCommand { System.arraycopy(args, starting + 1, newArgs, 0, newArgs.length); Disguise disguise; try { - disguise = parseDisguise(sender, newArgs); + disguise = parseDisguise(sender, newArgs, map); } catch (Exception ex) { if (ex.getMessage() != null && !ChatColor.getLastColors(ex.getMessage()).equals("")) { sender.sendMessage(ex.getMessage()); @@ -147,8 +149,8 @@ public class DisguiseRadiusCommand extends BaseDisguiseCommand { /** * Send the player the information */ - protected void sendCommandUsage(CommandSender sender) { - ArrayList allowedDisguises = getAllowedDisguises(sender); + protected void sendCommandUsage(CommandSender sender, HashMap, Boolean>> map) { + ArrayList allowedDisguises = getAllowedDisguises(map); sender.sendMessage(ChatColor.DARK_GREEN + "Disguise all entities in a radius! Caps at 30 blocks!"); sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)); diff --git a/src/me/libraryaddict/disguise/utilities/BaseDisguiseCommand.java b/src/me/libraryaddict/disguise/utilities/BaseDisguiseCommand.java index dfa80f19..66d9433d 100644 --- a/src/me/libraryaddict/disguise/utilities/BaseDisguiseCommand.java +++ b/src/me/libraryaddict/disguise/utilities/BaseDisguiseCommand.java @@ -3,8 +3,7 @@ package me.libraryaddict.disguise.utilities; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; +import java.util.HashMap; import me.libraryaddict.disguise.disguisetypes.AnimalColor; import me.libraryaddict.disguise.disguisetypes.Disguise; @@ -25,79 +24,179 @@ import org.bukkit.potion.PotionEffectType; public abstract class BaseDisguiseCommand implements CommandExecutor { - protected ArrayList getAllowedDisguises(CommandSender sender) { - String permissionNode = "libsdisguises." + getClass().getSimpleName().replace("Command", "").toLowerCase() + "."; - return getAllowedDisguises(sender, permissionNode); + protected ArrayList getAllowedDisguises(HashMap, Boolean>> hashMap) { + ArrayList allowedDisguises = new ArrayList(); + for (DisguiseType type : hashMap.keySet()) { + allowedDisguises.add(type.toReadable().replace(" ", "_")); + } + Collections.sort(allowedDisguises, String.CASE_INSENSITIVE_ORDER); + return allowedDisguises; } - protected ArrayList getAllowedDisguises(CommandSender sender, String permissionNode) { - HashSet singleAllowed = new HashSet(); - HashSet singleForbidden = new HashSet(); - HashSet globalForbidden = new HashSet(); - HashSet globalAllowed = new HashSet(); + protected HashMap, Boolean>> getPermissions(CommandSender sender) { + return getPermissions(sender, "libsdisguises." + getClass().getSimpleName().replace("Command", "").toLowerCase() + "."); + } + + /** + * Get perms for the node. Returns a hashmap of allowed disguisetypes and their options + */ + protected HashMap, Boolean>> getPermissions(CommandSender sender, + String permissionNode) { + + HashMap, Boolean>> singleDisguises = new HashMap, Boolean>>(); + HashMap, Boolean>> rangeDisguises = new HashMap, Boolean>>(); + HashMap perms = new HashMap(); + for (PermissionAttachmentInfo permission : sender.getEffectivePermissions()) { String perm = permission.getPermission().toLowerCase(); - if (perm.startsWith(permissionNode)) { + if (perm.startsWith(permissionNode) && (!perms.containsKey(perm) || !permission.getValue())) { + perms.put(perm, permission.getValue()); + } + } + if (!perms.containsKey(permissionNode + "*") && sender.hasPermission(permissionNode + "*")) { + perms.put(permissionNode + "*", true); + } + + for (String perm : perms.keySet()) { + if (perms.get(perm)) { perm = perm.substring(permissionNode.length()); String disguiseType = perm.split("\\.")[0]; - HashSet perms; - for (DisguiseType type : DisguiseType.values()) { - if (type.getEntityType() == null) { + try { + DisguiseType type = DisguiseType.valueOf(disguiseType.toUpperCase()); + if (type.getEntityType() == null) continue; - } - Class entityClass = type.getEntityType().getEntityClass(); - String name = type.name().toLowerCase(); - if (!perm.equalsIgnoreCase(name)) { - perms = (permission.getValue() ? globalAllowed : globalForbidden); + HashMap, Boolean> list; + if (singleDisguises.containsKey(type)) { + list = singleDisguises.get(type); } else { - perms = (permission.getValue() ? singleAllowed : singleForbidden); + list = new HashMap, Boolean>(); + singleDisguises.put(type, list); } - if (perm.equals("mob")) { - if (type.isMob()) { - perms.add(name); + HashMap, Boolean> map1 = getOptions(perm); + list.put(map1.keySet().iterator().next(), map1.values().iterator().next()); + } catch (Exception ex) { + for (DisguiseType type : DisguiseType.values()) { + if (type.getEntityType() == null) { + continue; } - } else if (perm.equals("animal") || perm.equals("animals")) { - if (Animals.class.isAssignableFrom(entityClass)) { - perms.add(name); + HashMap, Boolean> options = null; + Class entityClass = type.getEntityType().getEntityClass(); + if (disguiseType.equals("mob")) { + if (type.isMob()) { + options = getOptions(perm); + } + } else if (disguiseType.equals("animal") || disguiseType.equals("animals")) { + if (Animals.class.isAssignableFrom(entityClass)) { + options = getOptions(perm); + } + } else if (disguiseType.equals("monster") || disguiseType.equals("monsters")) { + if (Monster.class.isAssignableFrom(entityClass)) { + options = getOptions(perm); + } + } else if (disguiseType.equals("misc")) { + if (type.isMisc()) { + options = getOptions(perm); + } + } else if (disguiseType.equals("ageable")) { + if (Ageable.class.isAssignableFrom(entityClass)) { + options = getOptions(perm); + } + } else if (disguiseType.equals("*")) { + options = getOptions(perm); } - } else if (perm.equals("monster") || perm.equals("monsters")) { - if (Monster.class.isAssignableFrom(entityClass)) { - perms.add(name); + if (options != null) { + HashMap, Boolean> list; + if (rangeDisguises.containsKey(type)) { + list = rangeDisguises.get(type); + } else { + list = new HashMap, Boolean>(); + rangeDisguises.put(type, list); + } + HashMap, Boolean> map1 = getOptions(perm); + list.put(map1.keySet().iterator().next(), map1.values().iterator().next()); } - } else if (perm.equals("misc")) { - if (type.isMisc()) { - perms.add(name); - } - } else if (perm.equals("ageable")) { - if (Ageable.class.isAssignableFrom(entityClass)) { - perms.add(name); - } - } else if (disguiseType.equals("*")) { - perms.add(name); - } else if (disguiseType.equals(name)) { - perms.add(name); } } } } - // This does the disguises for OP's. + for (String perm : perms.keySet()) { + if (!perms.get(perm)) { + perm = perm.substring(permissionNode.length()); + String disguiseType = perm.split("\\.")[0]; + try { + DisguiseType type = DisguiseType.valueOf(disguiseType.toUpperCase()); + singleDisguises.remove(type); + } catch (Exception ex) { + for (DisguiseType type : DisguiseType.values()) { + if (type.getEntityType() == null) { + continue; + } + boolean foundHim = false; + Class entityClass = type.getEntityType().getEntityClass(); + if (disguiseType.equals("mob")) { + if (type.isMob()) { + foundHim = true; + } + } else if (disguiseType.equals("animal") || disguiseType.equals("animals")) { + if (Animals.class.isAssignableFrom(entityClass)) { + foundHim = true; + } + } else if (disguiseType.equals("monster") || disguiseType.equals("monsters")) { + if (Monster.class.isAssignableFrom(entityClass)) { + foundHim = true; + } + } else if (disguiseType.equals("misc")) { + if (type.isMisc()) { + foundHim = true; + } + } else if (disguiseType.equals("ageable")) { + if (Ageable.class.isAssignableFrom(entityClass)) { + foundHim = true; + } + } else if (disguiseType.equals("*")) { + foundHim = true; + } + if (foundHim) { + rangeDisguises.remove(type); + } + } + } + } + } + HashMap, Boolean>> map = new HashMap, Boolean>>(); for (DisguiseType type : DisguiseType.values()) { - if (type.getEntityType() == null) { - continue; + HashMap, Boolean> temp = new HashMap, Boolean>(); + if (singleDisguises.containsKey(type)) { + temp.putAll(singleDisguises.get(type)); } - if (!globalAllowed.contains(type.name().toLowerCase())) { - if (sender.hasPermission(permissionNode + "*") - || sender.hasPermission(permissionNode + type.name().toLowerCase())) { - globalAllowed.add(type.name().toLowerCase()); - } + if (rangeDisguises.containsKey(type)) { + temp.putAll(rangeDisguises.get(type)); + } + if (!temp.isEmpty()) { + map.put(type, temp); } } - globalAllowed.removeAll(globalForbidden); - singleAllowed.addAll(globalAllowed); - singleAllowed.removeAll(singleForbidden); - ArrayList disguiseTypes = new ArrayList(singleAllowed); - Collections.sort(disguiseTypes, String.CASE_INSENSITIVE_ORDER); - return disguiseTypes; + return map; + } + + private HashMap, Boolean> getOptions(String perm) { + ArrayList list = new ArrayList(); + boolean isRemove = true; + String[] split = perm.split("\\."); + for (int i = 1; i < split.length; i++) { + String option = split[i]; + boolean value = option.startsWith("-"); + if (value) { + option = option.substring(1); + isRemove = false; + } + if (option.equals("baby")) + option = "setbaby"; + list.add(option); + } + HashMap, Boolean> options = new HashMap, Boolean>(); + options.put(list, isRemove); + return options; } protected boolean isDouble(String string) { @@ -118,52 +217,18 @@ public abstract class BaseDisguiseCommand implements CommandExecutor { } } - /** - * Returns null if they have all perms. Else they can only use the options in the returned - */ - protected HashSet> getPermissions(CommandSender sender, String disguiseType) { - HashSet> perms = new HashSet>(); - String permNode = "libsdisguises." + getClass().getSimpleName().replace("Command", "").toLowerCase() + "."; - for (PermissionAttachmentInfo permission : sender.getEffectivePermissions()) { - if (permission.getValue()) { - String s = permission.getPermission().toLowerCase(); - if (s.startsWith(permNode + disguiseType) || s.startsWith(permNode + "*")) { - if (s.startsWith(permNode + disguiseType)) - s = s.substring((permNode + disguiseType).length()); - else if (s.startsWith(permNode + "*")) { - s = s.substring((permNode + "*").length()); - if (s.length() == 0) - continue; - } - if (s.length() == 0) - return new HashSet>(); - HashSet p = new HashSet(); - for (String str : s.split("\\.")) { - if (str.equals("baby")) - str = "setbaby"; - if (str.equals("-baby")) - str = "-setbaby"; - p.add(str); - } - perms.add(p); - } - } - } - return perms; - } - /** * Returns the disguise if it all parsed correctly. Returns a exception with a complete message if it didn't. The * commandsender is purely used for checking permissions. Would defeat the purpose otherwise. To reach this point, the * disguise has been feed a proper disguisetype. */ - protected Disguise parseDisguise(CommandSender sender, String[] args) throws Exception { - ArrayList allowedDisguises = getAllowedDisguises(sender); - if (allowedDisguises.isEmpty()) { + protected Disguise parseDisguise(CommandSender sender, String[] args, + HashMap, Boolean>> map) throws Exception { + if (map.isEmpty()) { throw new Exception(ChatColor.RED + "You are forbidden to use this command."); } if (args.length == 0) { - sendCommandUsage(sender); + sendCommandUsage(sender, map); throw new Exception(); } // How many args to skip due to the disugise being constructed @@ -172,7 +237,7 @@ public abstract class BaseDisguiseCommand implements CommandExecutor { int toSkip = 1; ArrayList usedOptions = new ArrayList(); Disguise disguise = null; - HashSet> optionPermissions; + HashMap, Boolean> optionPermissions; if (args[0].startsWith("@")) { if (sender.hasPermission("libsdisguises.disguise.disguiseclone")) { disguise = DisguiseUtilities.getClonedDisguise(args[0].toLowerCase()); @@ -182,7 +247,8 @@ public abstract class BaseDisguiseCommand implements CommandExecutor { } else { throw new Exception(ChatColor.RED + "You do not have perimssion to use disguise references!"); } - optionPermissions = this.getPermissions(sender, disguise.getType().name().toLowerCase()); + optionPermissions = (map.containsKey(disguise.getType()) ? map.get(disguise.getType()) + : new HashMap, Boolean>()); } else { DisguiseType disguiseType = null; if (args[0].equalsIgnoreCase("p")) { @@ -202,10 +268,10 @@ public abstract class BaseDisguiseCommand implements CommandExecutor { throw new Exception(ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[0] + ChatColor.RED + " doesn't exist!"); } - if (!allowedDisguises.contains(disguiseType.name().toLowerCase())) { + if (!map.containsKey(disguiseType)) { throw new Exception(ChatColor.RED + "You are forbidden to use this disguise."); } - optionPermissions = this.getPermissions(sender, disguiseType.name().toLowerCase()); + optionPermissions = map.get(disguiseType); if (disguiseType.isPlayer()) {// If he is doing a player disguise if (args.length == 1) { // He needs to give the player name @@ -398,38 +464,28 @@ public abstract class BaseDisguiseCommand implements CommandExecutor { return value; } - private void doCheck(HashSet> optionPermissions, ArrayList usedOptions) throws Exception { - if (!optionPermissions.isEmpty()) { - boolean hasPermission = true; - for (HashSet perms : optionPermissions) { - HashSet cloned = (HashSet) perms.clone(); - Iterator itel = cloned.iterator(); - while (itel.hasNext()) { - String perm = itel.next(); - if (perm.startsWith("-")) { - itel.remove(); - if (usedOptions.contains(perm.substring(1))) { - hasPermission = false; - break; - } - } - } - // If this wasn't modified by the above check - if (perms.size() == cloned.size()) { - // If there is a option used that the perms don't allow - if (!perms.containsAll(usedOptions)) { - hasPermission = false; - } else { - // The perms allow it. Return true - return; - } + private boolean passesCheck(HashMap, Boolean> map1, ArrayList usedOptions) { + boolean hasPermission = false; + for (ArrayList list : map1.keySet()) { + boolean myPerms = true; + for (String option : usedOptions) { + if (!(map1.get(list) && list.contains("*")) && (list.contains(option) != map1.get(list))) { + myPerms = false; + break; } } - if (!hasPermission) { - throw new Exception(ChatColor.RED + "You do not have the permission to use the option " - + usedOptions.get(usedOptions.size() - 1)); + if (myPerms) { + hasPermission = true; } } + return hasPermission; + } + + private void doCheck(HashMap, Boolean> optionPermissions, ArrayList usedOptions) throws Exception { + if (!passesCheck(optionPermissions, usedOptions)) { + throw new Exception(ChatColor.RED + "You do not have the permission to use the option " + + usedOptions.get(usedOptions.size() - 1)); + } } private Exception parseToException(String expectedValue, String receivedInstead, String methodName) { @@ -459,5 +515,5 @@ public abstract class BaseDisguiseCommand implements CommandExecutor { } } - protected abstract void sendCommandUsage(CommandSender sender); + protected abstract void sendCommandUsage(CommandSender sender, HashMap, Boolean>> map); }