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
This commit is contained in:
libraryaddict 2014-06-14 20:16:48 +12:00
parent 4d1092f41d
commit f777b33917
7 changed files with 216 additions and 149 deletions

@ -1,7 +1,11 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import java.util.ArrayList;
import java.util.HashMap;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.DisguiseListener; import me.libraryaddict.disguise.DisguiseListener;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.BaseDisguiseCommand; import me.libraryaddict.disguise.utilities.BaseDisguiseCommand;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -55,7 +59,7 @@ public class DisguiseCloneCommand extends BaseDisguiseCommand {
/** /**
* Send the player the information * Send the player the information
*/ */
protected void sendCommandUsage(CommandSender sender) { protected void sendCommandUsage(CommandSender sender, HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map) {
sender.sendMessage(ChatColor.DARK_GREEN sender.sendMessage(ChatColor.DARK_GREEN
+ "Right click a entity to get a disguise reference you can pass to other disguise commands!"); + "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"); sender.sendMessage(ChatColor.DARK_GREEN + "Beware however, the reference bypasses all permissions checks");

@ -1,10 +1,12 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.BaseDisguiseCommand; import me.libraryaddict.disguise.utilities.BaseDisguiseCommand;
@ -24,7 +26,7 @@ public class DisguiseCommand extends BaseDisguiseCommand {
} }
Disguise disguise; Disguise disguise;
try { try {
disguise = parseDisguise(sender, args); disguise = parseDisguise(sender, args, getPermissions(sender));
} catch (Exception ex) { } catch (Exception ex) {
if (ex.getMessage() != null && !ChatColor.getLastColors(ex.getMessage()).equals("")) { if (ex.getMessage() != null && !ChatColor.getLastColors(ex.getMessage()).equals("")) {
sender.sendMessage(ex.getMessage()); sender.sendMessage(ex.getMessage());
@ -53,8 +55,8 @@ public class DisguiseCommand extends BaseDisguiseCommand {
/** /**
* Send the player the information * Send the player the information
*/ */
protected void sendCommandUsage(CommandSender sender) { protected void sendCommandUsage(CommandSender sender, HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map) {
ArrayList<String> allowedDisguises = getAllowedDisguises(sender); ArrayList<String> allowedDisguises = getAllowedDisguises(map);
sender.sendMessage(ChatColor.DARK_GREEN + "Choose a disguise to become the disguise!"); sender.sendMessage(ChatColor.DARK_GREEN + "Choose a disguise to become the disguise!");
sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN
+ StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)); + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN));

@ -1,10 +1,12 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.DisguiseListener; import me.libraryaddict.disguise.DisguiseListener;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.BaseDisguiseCommand; import me.libraryaddict.disguise.utilities.BaseDisguiseCommand;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -28,7 +30,7 @@ public class DisguiseEntityCommand extends BaseDisguiseCommand {
} }
Disguise disguise; Disguise disguise;
try { try {
disguise = parseDisguise(sender, args); disguise = parseDisguise(sender, args, getPermissions(sender));
} catch (Exception ex) { } catch (Exception ex) {
if (ex.getMessage() != null && !ChatColor.getLastColors(ex.getMessage()).equals("")) { if (ex.getMessage() != null && !ChatColor.getLastColors(ex.getMessage()).equals("")) {
sender.sendMessage(ex.getMessage()); sender.sendMessage(ex.getMessage());
@ -46,8 +48,8 @@ public class DisguiseEntityCommand extends BaseDisguiseCommand {
/** /**
* Send the player the information * Send the player the information
*/ */
protected void sendCommandUsage(CommandSender sender) { protected void sendCommandUsage(CommandSender sender, HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map) {
ArrayList<String> allowedDisguises = getAllowedDisguises(sender); ArrayList<String> allowedDisguises = getAllowedDisguises(map);
sender.sendMessage(ChatColor.DARK_GREEN + "Choose a disguise then right click a entity to disguise it!"); 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 sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN
+ StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)); + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN));

@ -117,10 +117,9 @@ public class DisguiseHelpCommand extends BaseDisguiseCommand {
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
for (String node : new String[] { "disguise", "disguiseradius", "disguiseentity", "disguiseplayer" }) { for (String node : new String[] { "disguise", "disguiseradius", "disguiseentity", "disguiseplayer" }) {
ArrayList<String> allowedDisguises = getAllowedDisguises(sender, "libsdisguises." + node + "."); if (!getPermissions(sender, "libsdisguises." + node + ".").isEmpty()) {
if (!allowedDisguises.isEmpty()) {
if (args.length == 0) { if (args.length == 0) {
sendCommandUsage(sender); sendCommandUsage(sender, null);
return true; return true;
} else { } else {
EnumHelp help = null; EnumHelp help = null;
@ -219,7 +218,7 @@ public class DisguiseHelpCommand extends BaseDisguiseCommand {
/** /**
* Send the player the information * Send the player the information
*/ */
protected void sendCommandUsage(CommandSender sender) { protected void sendCommandUsage(CommandSender sender, HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map) {
sender.sendMessage(ChatColor.RED + "/disguisehelp <DisguiseType> " + ChatColor.GREEN sender.sendMessage(ChatColor.RED + "/disguisehelp <DisguiseType> " + ChatColor.GREEN
+ "- View the options you can set on a disguise"); + "- View the options you can set on a disguise");
for (EnumHelp s : enumHelp) { for (EnumHelp s : enumHelp) {

@ -1,10 +1,12 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.BaseDisguiseCommand; import me.libraryaddict.disguise.utilities.BaseDisguiseCommand;
@ -19,13 +21,13 @@ public class DisguisePlayerCommand extends BaseDisguiseCommand {
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
ArrayList<String> allowedDisguises = getAllowedDisguises(sender); HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map = getPermissions(sender);
if (allowedDisguises.isEmpty()) { if (map.isEmpty()) {
sender.sendMessage(ChatColor.RED + "You are forbidden to use this command."); sender.sendMessage(ChatColor.RED + "You are forbidden to use this command.");
return true; return true;
} }
if (args.length == 0) { if (args.length == 0) {
sendCommandUsage(sender); sendCommandUsage(sender, map);
return true; return true;
} }
if (args.length == 1) { if (args.length == 1) {
@ -41,7 +43,7 @@ public class DisguisePlayerCommand extends BaseDisguiseCommand {
System.arraycopy(args, 1, newArgs, 0, newArgs.length); System.arraycopy(args, 1, newArgs, 0, newArgs.length);
Disguise disguise; Disguise disguise;
try { try {
disguise = parseDisguise(sender, newArgs); disguise = parseDisguise(sender, newArgs, map);
} catch (Exception ex) { } catch (Exception ex) {
if (ex.getMessage() != null && !ChatColor.getLastColors(ex.getMessage()).equals("")) { if (ex.getMessage() != null && !ChatColor.getLastColors(ex.getMessage()).equals("")) {
sender.sendMessage(ex.getMessage()); sender.sendMessage(ex.getMessage());
@ -77,8 +79,8 @@ public class DisguisePlayerCommand extends BaseDisguiseCommand {
/** /**
* Send the player the information * Send the player the information
*/ */
protected void sendCommandUsage(CommandSender sender) { protected void sendCommandUsage(CommandSender sender, HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map) {
ArrayList<String> allowedDisguises = getAllowedDisguises(sender); ArrayList<String> allowedDisguises = getAllowedDisguises(map);
sender.sendMessage(ChatColor.DARK_GREEN + "Disguise another player!"); sender.sendMessage(ChatColor.DARK_GREEN + "Disguise another player!");
sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN
+ StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)); + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN));

@ -2,10 +2,12 @@ package me.libraryaddict.disguise.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.BaseDisguiseCommand; import me.libraryaddict.disguise.utilities.BaseDisguiseCommand;
import me.libraryaddict.disguise.utilities.ClassGetter; 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!"); sender.sendMessage(ChatColor.RED + "You may not use this command from the console!");
return true; return true;
} }
ArrayList<String> allowedDisguises = getAllowedDisguises(sender); HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map = getPermissions(sender);
if (allowedDisguises.isEmpty()) { if (map.isEmpty()) {
sender.sendMessage(ChatColor.RED + "You are forbidden to use this command."); sender.sendMessage(ChatColor.RED + "You are forbidden to use this command.");
return true; return true;
} }
if (args.length == 0) { if (args.length == 0) {
sendCommandUsage(sender); sendCommandUsage(sender, map);
return true; return true;
} }
if (args[0].equalsIgnoreCase("entitytype") || args[0].equalsIgnoreCase("entitytypes")) { 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); System.arraycopy(args, starting + 1, newArgs, 0, newArgs.length);
Disguise disguise; Disguise disguise;
try { try {
disguise = parseDisguise(sender, newArgs); disguise = parseDisguise(sender, newArgs, map);
} catch (Exception ex) { } catch (Exception ex) {
if (ex.getMessage() != null && !ChatColor.getLastColors(ex.getMessage()).equals("")) { if (ex.getMessage() != null && !ChatColor.getLastColors(ex.getMessage()).equals("")) {
sender.sendMessage(ex.getMessage()); sender.sendMessage(ex.getMessage());
@ -147,8 +149,8 @@ public class DisguiseRadiusCommand extends BaseDisguiseCommand {
/** /**
* Send the player the information * Send the player the information
*/ */
protected void sendCommandUsage(CommandSender sender) { protected void sendCommandUsage(CommandSender sender, HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map) {
ArrayList<String> allowedDisguises = getAllowedDisguises(sender); ArrayList<String> allowedDisguises = getAllowedDisguises(map);
sender.sendMessage(ChatColor.DARK_GREEN + "Disguise all entities in a radius! Caps at 30 blocks!"); 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 sender.sendMessage(ChatColor.DARK_GREEN + "You can use the disguises: " + ChatColor.GREEN
+ StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)); + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN));

@ -3,8 +3,7 @@ package me.libraryaddict.disguise.utilities;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashMap;
import java.util.Iterator;
import me.libraryaddict.disguise.disguisetypes.AnimalColor; import me.libraryaddict.disguise.disguisetypes.AnimalColor;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
@ -25,79 +24,179 @@ import org.bukkit.potion.PotionEffectType;
public abstract class BaseDisguiseCommand implements CommandExecutor { public abstract class BaseDisguiseCommand implements CommandExecutor {
protected ArrayList<String> getAllowedDisguises(CommandSender sender) { protected ArrayList<String> getAllowedDisguises(HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> hashMap) {
String permissionNode = "libsdisguises." + getClass().getSimpleName().replace("Command", "").toLowerCase() + "."; ArrayList<String> allowedDisguises = new ArrayList<String>();
return getAllowedDisguises(sender, permissionNode); for (DisguiseType type : hashMap.keySet()) {
allowedDisguises.add(type.toReadable().replace(" ", "_"));
}
Collections.sort(allowedDisguises, String.CASE_INSENSITIVE_ORDER);
return allowedDisguises;
} }
protected ArrayList<String> getAllowedDisguises(CommandSender sender, String permissionNode) { protected HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> getPermissions(CommandSender sender) {
HashSet<String> singleAllowed = new HashSet<String>(); return getPermissions(sender, "libsdisguises." + getClass().getSimpleName().replace("Command", "").toLowerCase() + ".");
HashSet<String> singleForbidden = new HashSet<String>(); }
HashSet<String> globalForbidden = new HashSet<String>();
HashSet<String> globalAllowed = new HashSet<String>(); /**
* Get perms for the node. Returns a hashmap of allowed disguisetypes and their options
*/
protected HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> getPermissions(CommandSender sender,
String permissionNode) {
HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> singleDisguises = new HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>>();
HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> rangeDisguises = new HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>>();
HashMap<String, Boolean> perms = new HashMap<String, Boolean>();
for (PermissionAttachmentInfo permission : sender.getEffectivePermissions()) { for (PermissionAttachmentInfo permission : sender.getEffectivePermissions()) {
String perm = permission.getPermission().toLowerCase(); 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()); perm = perm.substring(permissionNode.length());
String disguiseType = perm.split("\\.")[0]; String disguiseType = perm.split("\\.")[0];
HashSet<String> perms; try {
for (DisguiseType type : DisguiseType.values()) { DisguiseType type = DisguiseType.valueOf(disguiseType.toUpperCase());
if (type.getEntityType() == null) { if (type.getEntityType() == null)
continue; continue;
} HashMap<ArrayList<String>, Boolean> list;
Class entityClass = type.getEntityType().getEntityClass(); if (singleDisguises.containsKey(type)) {
String name = type.name().toLowerCase(); list = singleDisguises.get(type);
if (!perm.equalsIgnoreCase(name)) {
perms = (permission.getValue() ? globalAllowed : globalForbidden);
} else { } else {
perms = (permission.getValue() ? singleAllowed : singleForbidden); list = new HashMap<ArrayList<String>, Boolean>();
singleDisguises.put(type, list);
} }
if (perm.equals("mob")) { HashMap<ArrayList<String>, Boolean> map1 = getOptions(perm);
if (type.isMob()) { list.put(map1.keySet().iterator().next(), map1.values().iterator().next());
perms.add(name); } catch (Exception ex) {
for (DisguiseType type : DisguiseType.values()) {
if (type.getEntityType() == null) {
continue;
} }
} else if (perm.equals("animal") || perm.equals("animals")) { HashMap<ArrayList<String>, Boolean> options = null;
if (Animals.class.isAssignableFrom(entityClass)) { Class entityClass = type.getEntityType().getEntityClass();
perms.add(name); 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 (options != null) {
if (Monster.class.isAssignableFrom(entityClass)) { HashMap<ArrayList<String>, Boolean> list;
perms.add(name); if (rangeDisguises.containsKey(type)) {
list = rangeDisguises.get(type);
} else {
list = new HashMap<ArrayList<String>, Boolean>();
rangeDisguises.put(type, list);
}
HashMap<ArrayList<String>, 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<DisguiseType, HashMap<ArrayList<String>, Boolean>> map = new HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>>();
for (DisguiseType type : DisguiseType.values()) { for (DisguiseType type : DisguiseType.values()) {
if (type.getEntityType() == null) { HashMap<ArrayList<String>, Boolean> temp = new HashMap<ArrayList<String>, Boolean>();
continue; if (singleDisguises.containsKey(type)) {
temp.putAll(singleDisguises.get(type));
} }
if (!globalAllowed.contains(type.name().toLowerCase())) { if (rangeDisguises.containsKey(type)) {
if (sender.hasPermission(permissionNode + "*") temp.putAll(rangeDisguises.get(type));
|| sender.hasPermission(permissionNode + type.name().toLowerCase())) { }
globalAllowed.add(type.name().toLowerCase()); if (!temp.isEmpty()) {
} map.put(type, temp);
} }
} }
globalAllowed.removeAll(globalForbidden); return map;
singleAllowed.addAll(globalAllowed); }
singleAllowed.removeAll(singleForbidden);
ArrayList<String> disguiseTypes = new ArrayList<String>(singleAllowed); private HashMap<ArrayList<String>, Boolean> getOptions(String perm) {
Collections.sort(disguiseTypes, String.CASE_INSENSITIVE_ORDER); ArrayList<String> list = new ArrayList<String>();
return disguiseTypes; 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<ArrayList<String>, Boolean> options = new HashMap<ArrayList<String>, Boolean>();
options.put(list, isRemove);
return options;
} }
protected boolean isDouble(String string) { 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<HashSet<String>> getPermissions(CommandSender sender, String disguiseType) {
HashSet<HashSet<String>> perms = new HashSet<HashSet<String>>();
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<String>>();
HashSet<String> p = new HashSet<String>();
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 * 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 * commandsender is purely used for checking permissions. Would defeat the purpose otherwise. To reach this point, the
* disguise has been feed a proper disguisetype. * disguise has been feed a proper disguisetype.
*/ */
protected Disguise parseDisguise(CommandSender sender, String[] args) throws Exception { protected Disguise parseDisguise(CommandSender sender, String[] args,
ArrayList<String> allowedDisguises = getAllowedDisguises(sender); HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map) throws Exception {
if (allowedDisguises.isEmpty()) { if (map.isEmpty()) {
throw new Exception(ChatColor.RED + "You are forbidden to use this command."); throw new Exception(ChatColor.RED + "You are forbidden to use this command.");
} }
if (args.length == 0) { if (args.length == 0) {
sendCommandUsage(sender); sendCommandUsage(sender, map);
throw new Exception(); throw new Exception();
} }
// How many args to skip due to the disugise being constructed // How many args to skip due to the disugise being constructed
@ -172,7 +237,7 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
int toSkip = 1; int toSkip = 1;
ArrayList<String> usedOptions = new ArrayList<String>(); ArrayList<String> usedOptions = new ArrayList<String>();
Disguise disguise = null; Disguise disguise = null;
HashSet<HashSet<String>> optionPermissions; HashMap<ArrayList<String>, Boolean> optionPermissions;
if (args[0].startsWith("@")) { if (args[0].startsWith("@")) {
if (sender.hasPermission("libsdisguises.disguise.disguiseclone")) { if (sender.hasPermission("libsdisguises.disguise.disguiseclone")) {
disguise = DisguiseUtilities.getClonedDisguise(args[0].toLowerCase()); disguise = DisguiseUtilities.getClonedDisguise(args[0].toLowerCase());
@ -182,7 +247,8 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
} else { } else {
throw new Exception(ChatColor.RED + "You do not have perimssion to use disguise references!"); 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<ArrayList<String>, Boolean>());
} else { } else {
DisguiseType disguiseType = null; DisguiseType disguiseType = null;
if (args[0].equalsIgnoreCase("p")) { 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 throw new Exception(ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[0] + ChatColor.RED
+ " doesn't exist!"); + " 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."); 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 (disguiseType.isPlayer()) {// If he is doing a player disguise
if (args.length == 1) { if (args.length == 1) {
// He needs to give the player name // He needs to give the player name
@ -398,38 +464,28 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
return value; return value;
} }
private void doCheck(HashSet<HashSet<String>> optionPermissions, ArrayList<String> usedOptions) throws Exception { private boolean passesCheck(HashMap<ArrayList<String>, Boolean> map1, ArrayList<String> usedOptions) {
if (!optionPermissions.isEmpty()) { boolean hasPermission = false;
boolean hasPermission = true; for (ArrayList<String> list : map1.keySet()) {
for (HashSet<String> perms : optionPermissions) { boolean myPerms = true;
HashSet<String> cloned = (HashSet<String>) perms.clone(); for (String option : usedOptions) {
Iterator<String> itel = cloned.iterator(); if (!(map1.get(list) && list.contains("*")) && (list.contains(option) != map1.get(list))) {
while (itel.hasNext()) { myPerms = false;
String perm = itel.next(); break;
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;
}
} }
} }
if (!hasPermission) { if (myPerms) {
throw new Exception(ChatColor.RED + "You do not have the permission to use the option " hasPermission = true;
+ usedOptions.get(usedOptions.size() - 1));
} }
} }
return hasPermission;
}
private void doCheck(HashMap<ArrayList<String>, Boolean> optionPermissions, ArrayList<String> 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) { 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<DisguiseType, HashMap<ArrayList<String>, Boolean>> map);
} }