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

118 lines
3.8 KiB
Java
Raw Normal View History

2016-05-18 01:27:04 +02:00
package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
2016-05-18 01:27:04 +02:00
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
2017-08-06 02:23:47 +02:00
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Team;
2016-05-18 01:27:04 +02:00
import java.util.*;
2016-05-18 01:27:04 +02:00
/**
* @author libraryaddict
*/
2016-11-28 22:47:48 +01:00
public abstract class DisguiseBaseCommand implements CommandExecutor {
private static final Map<Class<? extends DisguiseBaseCommand>, String> disguiseCommands;
static {
HashMap<Class<? extends DisguiseBaseCommand>, String> map = new HashMap<>();
map.put(DisguiseCommand.class, "Disguise");
map.put(DisguiseEntityCommand.class, "DisguiseEntity");
map.put(DisguisePlayerCommand.class, "DisguisePlayer");
map.put(DisguiseRadiusCommand.class, "DisguiseRadius");
map.put(DisguiseModifyCommand.class, "DisguiseModify");
map.put(DisguiseModifyEntityCommand.class, "DisguiseModifyEntity");
map.put(DisguiseModifyPlayerCommand.class, "DisguiseModifyPlayer");
map.put(DisguiseModifyRadiusCommand.class, "DisguiseModifyRadius");
disguiseCommands = map;
}
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();
}
2017-06-19 19:06:35 +02:00
return new ArrayList<>(new HashSet<>(list));
2016-11-28 15:01:06 +01:00
}
2017-08-06 02:23:47 +02:00
protected String getDisplayName(CommandSender player) {
Team team = ((Player) player).getScoreboard().getEntryTeam(player.getName());
2019-01-25 05:14:40 +01:00
return (team == null ? "" : team.getColor() + team.getPrefix()) + player.getName() +
(team == null ? "" : team.getSuffix());
2017-08-06 02:23:47 +02:00
}
protected ArrayList<String> getAllowedDisguises(DisguisePermissions permissions) {
2016-05-18 01:27:04 +02:00
ArrayList<String> allowedDisguises = new ArrayList<>();
for (DisguisePerm type : permissions.getAllowed()) {
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
return allowedDisguises;
}
2016-11-28 15:01:06 +01:00
protected String[] getArgs(String[] args) {
2017-06-11 23:36:54 +02:00
ArrayList<String> newArgs = new ArrayList<>();
2016-11-28 15:01:06 +01:00
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
}
protected static final Map<Class<? extends DisguiseBaseCommand>, String> getCommandNames() {
return disguiseCommands;
}
public final String getPermNode() {
String name = getCommandNames().get(this.getClass());
if (name == null) {
2016-11-28 22:47:48 +01:00
throw new UnsupportedOperationException("Unknown disguise command, perm node not found");
}
return name;
2016-11-28 22:47:48 +01:00
}
protected DisguisePermissions getPermissions(CommandSender sender) {
return DisguiseParser.getPermissions(sender, getPermNode());
2016-05-18 01:27:04 +02:00
}
protected boolean isInteger(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;
}
}
protected abstract void sendCommandUsage(CommandSender sender, DisguisePermissions disguisePermissions);
2016-05-18 01:27:04 +02:00
}