2016-05-18 01:27:04 +02:00
|
|
|
package me.libraryaddict.disguise.commands;
|
|
|
|
|
2019-11-03 06:07:12 +01:00
|
|
|
import me.libraryaddict.disguise.LibsDisguises;
|
2019-02-18 06:52:54 +01:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
2019-11-03 06:07:12 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.LibsPremium;
|
2018-10-05 01:04:30 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
|
|
|
|
import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
|
2018-10-23 23:13:13 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
|
2019-05-18 08:54:51 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.parser.params.ParamInfoManager;
|
2019-02-18 06:52:54 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
|
|
|
|
import org.bukkit.Bukkit;
|
2019-11-03 06:07:12 +01:00
|
|
|
import org.bukkit.ChatColor;
|
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
|
|
|
|
2019-02-18 06:52:54 +01:00
|
|
|
import java.lang.reflect.Method;
|
2018-10-05 01:04:30 +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 {
|
2018-10-23 23:13:13 +02:00
|
|
|
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
|
|
|
|
2019-11-03 06:07:12 +01:00
|
|
|
protected boolean isNotPremium(CommandSender sender) {
|
|
|
|
if (sender instanceof Player &&
|
|
|
|
(!LibsPremium.isPremium() || LibsPremium.getPaidInformation() == LibsPremium.getPluginInformation())) {
|
|
|
|
sender.sendMessage(ChatColor.RED + "Please purchase Lib's Disguises to enable player commands");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-18 06:52:54 +01:00
|
|
|
protected List<String> getTabDisguiseTypes(CommandSender sender, DisguisePermissions perms, String[] allArgs,
|
|
|
|
int startsAt, String currentArg) {
|
|
|
|
// If not enough arguments to get current disguise type
|
|
|
|
if (allArgs.length <= startsAt) {
|
|
|
|
return getAllowedDisguises(perms);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get current disguise type
|
|
|
|
DisguisePerm disguiseType = DisguiseParser.getDisguisePerm(allArgs[startsAt]);
|
|
|
|
|
|
|
|
// If disguise type isn't found, return nothing
|
|
|
|
if (disguiseType == null) {
|
|
|
|
return new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If current argument is just after the disguise type, and disguise type is a player which is not a custom
|
|
|
|
// disguise
|
|
|
|
if (allArgs.length == startsAt + 1 && disguiseType.getType() == DisguiseType.PLAYER &&
|
|
|
|
!disguiseType.isCustomDisguise()) {
|
|
|
|
ArrayList<String> tabs = new ArrayList<>();
|
|
|
|
|
|
|
|
// Add all player names to tab list
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
|
|
// If command user cannot see player online, don't tab-complete name
|
|
|
|
if (sender instanceof Player && !((Player) sender).canSee(player)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tabs.add(player.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return tablist
|
|
|
|
return tabs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getTabDisguiseOptions(sender, perms, disguiseType, allArgs, startsAt + (disguiseType.isPlayer() ? 2 : 1),
|
|
|
|
currentArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param perms What permissions they can use
|
|
|
|
* @param disguisePerm The disguise permission they're using
|
|
|
|
* @param allArgs All the arguments in the command
|
|
|
|
* @param startsAt What index this starts at
|
|
|
|
* @return a list of viable disguise options
|
|
|
|
*/
|
|
|
|
protected List<String> getTabDisguiseOptions(CommandSender commandSender, DisguisePermissions perms,
|
|
|
|
DisguisePerm disguisePerm, String[] allArgs, int startsAt, String currentArg) {
|
|
|
|
ArrayList<String> usedOptions = new ArrayList<>();
|
|
|
|
|
|
|
|
Method[] methods = ParamInfoManager.getDisguiseWatcherMethods(disguisePerm.getWatcherClass());
|
|
|
|
|
|
|
|
// Find which methods the disguiser has already used
|
|
|
|
for (int i = startsAt; i < allArgs.length; i++) {
|
|
|
|
for (Method method : methods) {
|
|
|
|
String arg = allArgs[i];
|
|
|
|
|
|
|
|
if (!method.getName().equalsIgnoreCase(arg)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
usedOptions.add(arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the disguiser has used options that they have not been granted to use, ignore them
|
|
|
|
if (!perms.isAllowedDisguise(disguisePerm, usedOptions)) {
|
|
|
|
return new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return getTabDisguiseSubOptions(commandSender, perms, disguisePerm, allArgs, startsAt, currentArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected List<String> getTabDisguiseSubOptions(CommandSender commandSender, DisguisePermissions perms,
|
|
|
|
DisguisePerm disguisePerm, String[] allArgs, int startsAt, String currentArg) {
|
|
|
|
boolean addMethods = true;
|
|
|
|
List<String> tabs = new ArrayList<>();
|
|
|
|
|
|
|
|
// Check what argument was used before the current argument to see what we're displaying
|
|
|
|
if (allArgs.length > startsAt) {
|
|
|
|
String prevArg = allArgs[allArgs.length - 1];
|
|
|
|
|
|
|
|
ParamInfo info = ParamInfoManager.getParamInfo(disguisePerm, prevArg);
|
|
|
|
|
|
|
|
// If the previous argument is a method
|
|
|
|
if (info != null) {
|
|
|
|
if (!info.isParam(boolean.class)) {
|
|
|
|
addMethods = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is a list of default values
|
|
|
|
if (info.hasValues()) {
|
|
|
|
tabs.addAll(info.getEnums(currentArg));
|
|
|
|
} else if (info.isParam(String.class)) {
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
|
|
// If command user cannot see player online, don't tab-complete name
|
|
|
|
if (commandSender instanceof Player && !((Player) commandSender).canSee(player)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tabs.add(player.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addMethods) {
|
|
|
|
// If this is a method, add. Else if it can be a param of the previous argument, add.
|
|
|
|
for (Method method : ParamInfoManager.getDisguiseWatcherMethods(disguisePerm.getWatcherClass())) {
|
|
|
|
tabs.add(method.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tabs;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected List<String> filterTabs(List<String> list, String[] origArgs) {
|
2016-11-28 15:01:06 +01:00
|
|
|
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-29 11:19:13 +01:00
|
|
|
if (team == null) {
|
|
|
|
return player.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return team.getPrefix() + team.getColor() + player.getName() + team.getSuffix();
|
2017-08-06 02:23:47 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
protected ArrayList<String> getAllowedDisguises(DisguisePermissions permissions) {
|
2016-05-18 01:27:04 +02:00
|
|
|
ArrayList<String> allowedDisguises = new ArrayList<>();
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
for (DisguisePerm type : permissions.getAllowed()) {
|
2016-11-30 18:38:43 +01:00
|
|
|
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
|
|
|
return allowedDisguises;
|
|
|
|
}
|
|
|
|
|
2019-02-18 06:52:54 +01:00
|
|
|
/**
|
|
|
|
* @param args
|
|
|
|
* @return Array of strings excluding current argument
|
|
|
|
*/
|
|
|
|
protected String[] getPreviousArgs(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
|
|
|
}
|
|
|
|
|
2019-02-18 06:52:54 +01:00
|
|
|
protected String getCurrentArg(String[] args) {
|
|
|
|
if (args.length == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return args[args.length - 1].trim();
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
protected static final Map<Class<? extends DisguiseBaseCommand>, String> getCommandNames() {
|
|
|
|
return disguiseCommands;
|
|
|
|
}
|
|
|
|
|
2017-02-19 15:53:03 +01:00
|
|
|
public final String getPermNode() {
|
2018-10-23 23:13:13 +02:00
|
|
|
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");
|
2017-02-19 15:53:03 +01:00
|
|
|
}
|
2018-10-23 23:13:13 +02:00
|
|
|
|
|
|
|
return name;
|
2016-11-28 22:47:48 +01:00
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
protected DisguisePermissions getPermissions(CommandSender sender) {
|
|
|
|
return DisguiseParser.getPermissions(sender, getPermNode());
|
2016-05-18 01:27:04 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
protected boolean isInteger(String string) {
|
2016-11-25 13:07:02 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
protected abstract void sendCommandUsage(CommandSender sender, DisguisePermissions disguisePermissions);
|
2016-05-18 01:27:04 +02:00
|
|
|
}
|