Read desc

Added new permission system.
This should allow people to use * better
This commit is contained in:
libraryaddict 2014-04-18 19:50:48 +12:00
parent 5f03b75eb7
commit 4ebf7bdf51

View File

@ -12,9 +12,13 @@ import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.MiscDisguise; import me.libraryaddict.disguise.disguisetypes.MiscDisguise;
import me.libraryaddict.disguise.disguisetypes.MobDisguise; import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise; import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Monster;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
@ -27,27 +31,51 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
} }
protected ArrayList<String> getAllowedDisguises(CommandSender sender, String permissionNode) { protected ArrayList<String> getAllowedDisguises(CommandSender sender, String permissionNode) {
ArrayList<String> names = new ArrayList<String>(); HashSet<String> singleAllowed = new HashSet<String>();
ArrayList<String> forbiddenDisguises = new ArrayList<String>(); HashSet<String> singleForbidden = new HashSet<String>();
HashSet<String> globalForbidden = new HashSet<String>();
HashSet<String> globalAllowed = new HashSet<String>();
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)) {
perm = perm.substring(permissionNode.length()); perm = perm.substring(permissionNode.length());
String disguiseType = perm.split("\\.")[0];
HashSet<String> perms;
for (DisguiseType type : DisguiseType.values()) { for (DisguiseType type : DisguiseType.values()) {
if (type.getEntityType() == null) { if (type.getEntityType() == null) {
continue; continue;
} }
Class entityClass = type.getEntityType().getEntityClass();
String name = type.name().toLowerCase(); String name = type.name().toLowerCase();
if (perm.split("\\.")[0].equals("*") && permission.getValue()) { if (!perm.equalsIgnoreCase(name)) {
if (!names.contains(name)) perms = (permission.getValue() ? globalAllowed : globalForbidden);
names.add(name); } else {
} else if (perm.split("\\.")[0].equals(name)) { perms = (permission.getValue() ? singleAllowed : singleForbidden);
if (permission.getValue()) { }
if (!names.contains(name)) if (perm.equals("mob")) {
names.add(name); if (type.isMob()) {
} else { perms.add(name);
forbiddenDisguises.add(name);
} }
} else if (perm.equals("animal") || perm.equals("animals")) {
if (Animals.class.isAssignableFrom(entityClass)) {
perms.add(name);
}
} else if (perm.equals("monster") || perm.equals("monsters")) {
if (Monster.class.isAssignableFrom(entityClass)) {
perms.add(name);
}
} 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);
} }
} }
} }
@ -57,16 +85,19 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
if (type.getEntityType() == null) { if (type.getEntityType() == null) {
continue; continue;
} }
if (!names.contains(type.name().toLowerCase())) { if (!globalAllowed.contains(type.name().toLowerCase())) {
if (sender.hasPermission(permissionNode + "*") if (sender.hasPermission(permissionNode + "*")
|| sender.hasPermission(permissionNode + type.name().toLowerCase())) { || sender.hasPermission(permissionNode + type.name().toLowerCase())) {
names.add(type.name().toLowerCase()); globalAllowed.add(type.name().toLowerCase());
} }
} }
} }
names.removeAll(forbiddenDisguises); globalAllowed.removeAll(globalForbidden);
Collections.sort(names, String.CASE_INSENSITIVE_ORDER); globalAllowed.removeAll(singleForbidden);
return names; singleAllowed.addAll(globalAllowed);
ArrayList<String> disguiseTypes = new ArrayList<String>(singleAllowed);
Collections.sort(disguiseTypes, String.CASE_INSENSITIVE_ORDER);
return disguiseTypes;
} }
protected boolean isDouble(String string) { protected boolean isDouble(String string) {