LibsDisguises/src/me/libraryaddict/disguise/commands/DisguiseBaseCommand.java

108 lines
3.3 KiB
Java
Raw Normal View History

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;
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;
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();
}
return new ArrayList<String>(new HashSet<String>(list));
2016-11-28 15:01:06 +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<>();
for (DisguisePerm type : hashMap.keySet()) {
if (type.isUnknown())
continue;
allowedDisguises.add(type.toReadable().replaceAll(" ", "_"));
2016-05-18 01:27:04 +02:00
}
2016-05-18 01:27:04 +02:00
Collections.sort(allowedDisguises, String.CASE_INSENSITIVE_ORDER);
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");
}
protected HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> getPermissions(CommandSender sender) {
return DisguiseParser.getPermissions(sender, "libsdisguises." + getPermNode() + ".");
2016-05-18 01:27:04 +02:00
}
protected boolean isNumeric(String string) {
try {
2016-05-18 01:27:04 +02:00
Integer.parseInt(string);
return true;
}
catch (Exception ex) {
2016-05-18 01:27:04 +02:00
return false;
}
}
public boolean passesCheck(CommandSender sender, HashMap<ArrayList<String>, Boolean> theirPermissions,
ArrayList<String> usedOptions) {
return DisguiseParser.passesCheck(sender, theirPermissions, usedOptions);
2016-11-28 15:01:06 +01:00
}
2016-05-18 01:27:04 +02:00
protected abstract void sendCommandUsage(CommandSender sender,
HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> map);
2016-05-18 01:27:04 +02:00
}