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

124 lines
4.1 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
2017-08-06 02:23:47 +02:00
import org.bukkit.Bukkit;
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;
2017-08-06 02:23:47 +02:00
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
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();
}
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());
return (team == null ? "" : team.getPrefix()) + player.getName() + (team == null ? "" : team.getSuffix());
}
2017-06-11 23:36:54 +02: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) {
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
}
public final String getPermNode() {
2016-11-28 22:47:48 +01:00
if (this instanceof DisguiseCommand) {
return "disguise";
2017-06-11 23:36:54 +02:00
} else if (this instanceof DisguiseEntityCommand) {
2016-11-28 22:47:48 +01:00
return "disguiseentity";
2017-06-11 23:36:54 +02:00
} else if (this instanceof DisguisePlayerCommand) {
2016-11-28 22:47:48 +01:00
return "disguiseplayer";
2017-06-11 23:36:54 +02:00
} else if (this instanceof DisguiseRadiusCommand) {
2016-11-28 22:47:48 +01:00
return "disguiseradius";
2017-06-11 23:36:54 +02:00
} else if (this instanceof DisguiseModifyCommand) {
return "disguisemodify";
2017-06-11 23:36:54 +02:00
} else if (this instanceof DisguiseModifyEntityCommand) {
return "disguisemodifyentity";
2017-06-11 23:36:54 +02:00
} else if (this instanceof DisguiseModifyPlayerCommand) {
return "disguisemodifyplayer";
2017-06-11 23:36:54 +02:00
} else if (this instanceof DisguiseModifyRadiusCommand) {
return "disguisemodifyradius";
2017-06-11 23:36:54 +02:00
} else {
2016-11-28 22:47:48 +01:00
throw new UnsupportedOperationException("Unknown disguise command, perm node not found");
}
2016-11-28 22:47:48 +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
}
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
}