2016-05-18 01:27:04 +02:00
|
|
|
package me.libraryaddict.disguise.commands;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
2016-11-30 18:38:43 +01:00
|
|
|
import java.util.HashSet;
|
2016-11-28 15:01:06 +01:00
|
|
|
import java.util.Iterator;
|
2016-05-18 01:27:04 +02:00
|
|
|
|
|
|
|
import org.bukkit.command.CommandExecutor;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
|
2016-11-30 18:38:43 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseParser;
|
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseParser.DisguisePerm;
|
2016-05-18 01:27:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author libraryaddict
|
|
|
|
*/
|
2016-11-28 22:47:48 +01:00
|
|
|
public abstract class DisguiseBaseCommand implements CommandExecutor {
|
2016-11-28 15:01:06 +01:00
|
|
|
|
|
|
|
protected ArrayList<String> filterTabs(ArrayList<String> list, String[] origArgs) {
|
|
|
|
if (origArgs.length == 0)
|
|
|
|
return list;
|
|
|
|
|
|
|
|
Iterator<String> itel = list.iterator();
|
|
|
|
String label = origArgs[origArgs.length - 1].toLowerCase();
|
|
|
|
|
|
|
|
while (itel.hasNext()) {
|
|
|
|
String name = itel.next();
|
|
|
|
|
|
|
|
if (name.toLowerCase().startsWith(label))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
itel.remove();
|
|
|
|
}
|
|
|
|
|
2016-11-30 18:38:43 +01:00
|
|
|
return new ArrayList<String>(new HashSet<String>(list));
|
2016-11-28 15:01:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-30 18:38:43 +01:00
|
|
|
protected ArrayList<String> getAllowedDisguises(HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> hashMap) {
|
2016-05-18 01:27:04 +02:00
|
|
|
ArrayList<String> allowedDisguises = new ArrayList<>();
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-30 18:38:43 +01:00
|
|
|
for (DisguisePerm type : hashMap.keySet()) {
|
|
|
|
if (type.isUnknown())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
allowedDisguises.add(type.toReadable().replaceAll(" ", "_"));
|
2016-05-18 01:27:04 +02:00
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
Collections.sort(allowedDisguises, String.CASE_INSENSITIVE_ORDER);
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
return allowedDisguises;
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:01:06 +01:00
|
|
|
protected String[] getArgs(String[] args) {
|
|
|
|
ArrayList<String> newArgs = new ArrayList<String>();
|
|
|
|
|
|
|
|
for (int i = 0; i < args.length - 1; i++) {
|
|
|
|
String s = args[i];
|
|
|
|
|
|
|
|
if (s.trim().isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
newArgs.add(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newArgs.toArray(new String[0]);
|
2016-05-18 01:27:04 +02:00
|
|
|
}
|
|
|
|
|
2016-11-28 22:47:48 +01:00
|
|
|
public String getPermNode() {
|
|
|
|
if (this instanceof DisguiseCommand) {
|
|
|
|
return "disguise";
|
|
|
|
}
|
|
|
|
else if (this instanceof DisguiseEntityCommand) {
|
|
|
|
return "disguiseentity";
|
|
|
|
}
|
|
|
|
else if (this instanceof DisguisePlayerCommand) {
|
|
|
|
return "disguiseplayer";
|
|
|
|
}
|
|
|
|
else if (this instanceof DisguiseRadiusCommand) {
|
|
|
|
return "disguiseradius";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new UnsupportedOperationException("Unknown disguise command, perm node not found");
|
|
|
|
}
|
|
|
|
|
2016-11-30 18:38:43 +01:00
|
|
|
protected HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> getPermissions(CommandSender sender) {
|
|
|
|
return DisguiseParser.getPermissions(sender, "libsdisguises." + getPermNode() + ".");
|
2016-05-18 01:27:04 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
protected boolean isNumeric(String string) {
|
|
|
|
try {
|
2016-05-18 01:27:04 +02:00
|
|
|
Integer.parseInt(string);
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (Exception ex) {
|
2016-05-18 01:27:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:01:06 +01:00
|
|
|
public boolean passesCheck(HashMap<ArrayList<String>, Boolean> theirPermissions, ArrayList<String> usedOptions) {
|
|
|
|
boolean hasPermission = false;
|
|
|
|
|
|
|
|
for (ArrayList<String> list : theirPermissions.keySet()) {
|
|
|
|
boolean myPerms = true;
|
|
|
|
|
|
|
|
for (String option : usedOptions) {
|
|
|
|
if (!(theirPermissions.get(list) && list.contains("*"))
|
|
|
|
&& (list.contains(option) != theirPermissions.get(list))) {
|
|
|
|
myPerms = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (myPerms) {
|
|
|
|
hasPermission = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasPermission;
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
protected abstract void sendCommandUsage(CommandSender sender,
|
2016-11-30 18:38:43 +01:00
|
|
|
HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> map);
|
2016-05-18 01:27:04 +02:00
|
|
|
}
|