Clean up tab completion code for repeated statements

This commit is contained in:
libraryaddict 2019-02-18 18:52:54 +13:00
parent b813c5b899
commit 2cf86b9cd8
12 changed files with 191 additions and 501 deletions

View File

@ -1,13 +1,18 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.parser.DisguiseParser; import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.parser.DisguisePerm; import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions; import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
import me.libraryaddict.disguise.utilities.parser.ParamInfoManager;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
import java.lang.reflect.Method;
import java.util.*; import java.util.*;
/** /**
@ -31,7 +36,124 @@ public abstract class DisguiseBaseCommand implements CommandExecutor {
disguiseCommands = map; disguiseCommands = map;
} }
protected ArrayList<String> filterTabs(ArrayList<String> list, String[] origArgs) { 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) {
if (origArgs.length == 0) if (origArgs.length == 0)
return list; return list;
@ -73,7 +195,11 @@ public abstract class DisguiseBaseCommand implements CommandExecutor {
return allowedDisguises; return allowedDisguises;
} }
protected String[] getArgs(String[] args) { /**
* @param args
* @return Array of strings excluding current argument
*/
protected String[] getPreviousArgs(String[] args) {
ArrayList<String> newArgs = new ArrayList<>(); ArrayList<String> newArgs = new ArrayList<>();
for (int i = 0; i < args.length - 1; i++) { for (int i = 0; i < args.length - 1; i++) {
@ -88,6 +214,14 @@ public abstract class DisguiseBaseCommand implements CommandExecutor {
return newArgs.toArray(new String[0]); return newArgs.toArray(new String[0]);
} }
protected String getCurrentArg(String[] args) {
if (args.length == 0) {
return "";
}
return args[args.length - 1].trim();
}
protected static final Map<Class<? extends DisguiseBaseCommand>, String> getCommandNames() { protected static final Map<Class<? extends DisguiseBaseCommand>, String> getCommandNames() {
return disguiseCommands; return disguiseCommands;
} }

View File

@ -70,7 +70,7 @@ public class DisguiseCloneCommand extends DisguiseBaseCommand implements TabComp
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) { public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) {
ArrayList<String> tabs = new ArrayList<>(); ArrayList<String> tabs = new ArrayList<>();
String[] args = getArgs(origArgs); String[] args = getPreviousArgs(origArgs);
if (args.length == 0) { if (args.length == 0) {
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {

View File

@ -3,14 +3,13 @@ package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
import me.libraryaddict.disguise.utilities.translations.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -18,7 +17,6 @@ import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -98,82 +96,11 @@ public class DisguiseCommand extends DisguiseBaseCommand implements TabCompleter
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) { public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) {
ArrayList<String> tabs = new ArrayList<>(); ArrayList<String> tabs = new ArrayList<>();
String[] args = getArgs(origArgs); String[] args = getPreviousArgs(origArgs);
DisguisePermissions perms = getPermissions(sender); DisguisePermissions perms = getPermissions(sender);
if (args.length == 0) { return filterTabs(getTabDisguiseTypes(sender, perms, args, 0, getCurrentArg(origArgs)), origArgs);
tabs.addAll(getAllowedDisguises(perms));
} else {
DisguisePerm disguiseType = DisguiseParser.getDisguisePerm(args[0]);
if (disguiseType == null)
return filterTabs(tabs, origArgs);
// No disguisetype specificied, cannot help.
if (args.length == 1 && disguiseType.getType() == DisguiseType.PLAYER) {
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());
}
} else {
ArrayList<String> usedOptions = new ArrayList<>();
for (Method method : ParamInfoManager.getDisguiseWatcherMethods(disguiseType.getWatcherClass())) {
for (int i = disguiseType.getType() == DisguiseType.PLAYER ? 2 : 1; i < args.length; i++) {
String arg = args[i];
if (!method.getName().equalsIgnoreCase(arg))
continue;
usedOptions.add(arg);
}
}
if (perms.isAllowedDisguise(disguiseType, usedOptions)) {
boolean addMethods = true;
if (args.length > 1) {
String prevArg = args[args.length - 1];
ParamInfo info = ParamInfoManager.getParamInfo(disguiseType, prevArg);
if (info != null) {
if (!info.isParam(boolean.class)) {
addMethods = false;
}
if (info.hasValues()) {
tabs.addAll(info.getEnums(origArgs[origArgs.length - 1]));
} else if (info.isParam(String.class)) {
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());
}
}
}
}
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(disguiseType.getWatcherClass())) {
tabs.add(method.getName());
}
}
}
}
}
return filterTabs(tabs, origArgs);
} }
/** /**

View File

@ -3,13 +3,12 @@ package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
import me.libraryaddict.disguise.utilities.translations.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -17,7 +16,6 @@ import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -73,83 +71,11 @@ public class DisguiseEntityCommand extends DisguiseBaseCommand implements TabCom
return tabs; return tabs;
} }
String[] args = getArgs(origArgs); String[] args = getPreviousArgs(origArgs);
DisguisePermissions perms = getPermissions(sender); DisguisePermissions perms = getPermissions(sender);
if (args.length == 0) { return filterTabs(getTabDisguiseTypes(sender, perms, args, 0, getCurrentArg(origArgs)), origArgs);
for (String type : getAllowedDisguises(perms)) {
tabs.add(type);
}
} else {
DisguisePerm disguiseType = DisguiseParser.getDisguisePerm(args[0]);
if (disguiseType == null)
return filterTabs(tabs, origArgs);
if (args.length == 1 && disguiseType.getType() == DisguiseType.PLAYER) {
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());
}
} else {
ArrayList<String> usedOptions = new ArrayList<>();
for (Method method : ParamInfoManager.getDisguiseWatcherMethods(disguiseType.getWatcherClass())) {
for (int i = disguiseType.getType() == DisguiseType.PLAYER ? 2 : 1; i < args.length; i++) {
String arg = args[i];
if (!method.getName().equalsIgnoreCase(arg))
continue;
usedOptions.add(arg);
}
}
if (perms.isAllowedDisguise(disguiseType, usedOptions)) {
boolean addMethods = true;
if (args.length > 1) {
String prevArg = args[args.length - 1];
ParamInfo info = ParamInfoManager.getParamInfo(disguiseType, prevArg);
if (info != null) {
if (!info.isParam(boolean.class)) {
addMethods = false;
}
if (info.hasValues()) {
tabs.addAll(info.getEnums(origArgs[origArgs.length - 1]));
} else if (info.isParam(String.class)) {
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());
}
}
}
}
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(disguiseType.getWatcherClass())) {
tabs.add(method.getName());
}
}
}
}
}
return filterTabs(tabs, origArgs);
} }
/** /**

View File

@ -129,7 +129,7 @@ public class DisguiseHelpCommand extends DisguiseBaseCommand implements TabCompl
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) { public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) {
ArrayList<String> tabs = new ArrayList<>(); ArrayList<String> tabs = new ArrayList<>();
String[] args = getArgs(origArgs); String[] args = getPreviousArgs(origArgs);
for (String node : getCommandNames().values()) { for (String node : getCommandNames().values()) {
DisguisePermissions perms = DisguiseParser.getPermissions(sender, node); DisguisePermissions perms = DisguiseParser.getPermissions(sender, node);

View File

@ -2,13 +2,13 @@ package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
import me.libraryaddict.disguise.utilities.translations.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -16,7 +16,6 @@ import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -77,70 +76,21 @@ public class DisguiseModifyCommand extends DisguiseBaseCommand implements TabCom
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) { public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) {
ArrayList<String> tabs = new ArrayList<>();
if (!(sender instanceof Player)) if (!(sender instanceof Player))
return tabs; return new ArrayList<>();
Disguise disguise = DisguiseAPI.getDisguise((Player) sender, (Entity) sender); Disguise disguise = DisguiseAPI.getDisguise((Player) sender, (Entity) sender);
if (disguise == null) if (disguise == null)
return tabs; return new ArrayList<>();
String[] args = getArgs(origArgs); String[] args = getPreviousArgs(origArgs);
DisguisePermissions perms = getPermissions(sender); DisguisePermissions perms = getPermissions(sender);
DisguisePerm disguiseType = new DisguisePerm(disguise.getType()); DisguisePerm disguiseType = new DisguisePerm(disguise.getType());
ArrayList<String> usedOptions = new ArrayList<>(); List<String> tabs = getTabDisguiseOptions(sender, perms, disguiseType, args, 0, getCurrentArg(origArgs));
for (Method method : ParamInfoManager.getDisguiseWatcherMethods(disguiseType.getWatcherClass())) {
for (int i = disguiseType.getType() == DisguiseType.PLAYER ? 2 : 1; i < args.length; i++) {
String arg = args[i];
if (!method.getName().equalsIgnoreCase(arg))
continue;
usedOptions.add(arg);
}
}
if (perms.isAllowedDisguise(disguiseType, usedOptions)) {
boolean addMethods = true;
if (args.length > 0) {
String prevArg = args[args.length - 1];
ParamInfo info = ParamInfoManager.getParamInfo(disguiseType, prevArg);
if (info != null) {
if (!info.isParam(boolean.class)) {
addMethods = false;
}
if (info.hasValues()) {
tabs.addAll(info.getEnums(origArgs[origArgs.length - 1]));
} else if (info.isParam(String.class)) {
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());
}
}
}
}
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(disguiseType.getWatcherClass())) {
tabs.add(method.getName());
}
}
}
return filterTabs(tabs, origArgs); return filterTabs(tabs, origArgs);
} }

View File

@ -5,18 +5,14 @@ import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.parser.DisguisePerm; import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions; import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
import me.libraryaddict.disguise.utilities.parser.ParamInfoManager;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import me.libraryaddict.disguise.utilities.translations.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -51,54 +47,22 @@ public class DisguiseModifyEntityCommand extends DisguiseBaseCommand implements
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) { public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) {
ArrayList<String> tabs = new ArrayList<>();
if (!(sender instanceof Player)) { if (!(sender instanceof Player)) {
return tabs; return new ArrayList<>();
} }
String[] args = getArgs(origArgs); String[] args = getPreviousArgs(origArgs);
DisguisePermissions perms = getPermissions(sender); DisguisePermissions perms = getPermissions(sender);
if (!perms.hasPermissions()) { if (!perms.hasPermissions()) {
return tabs; return new ArrayList<>();
} }
List<String> tabs = new ArrayList<>();
for (DisguisePerm perm : perms.getAllowed()) { for (DisguisePerm perm : perms.getAllowed()) {
boolean addMethods = true; tabs.addAll(getTabDisguiseSubOptions(sender, perms, perm, args, 0, getCurrentArg(args)));
if (args.length > 0) {
String prevArg = args[args.length - 1];
ParamInfo info = ParamInfoManager.getParamInfo(perm.getType(), prevArg);
if (info != null) {
if (!info.isParam(boolean.class)) {
addMethods = false;
}
if (info.hasValues()) {
tabs.addAll(info.getEnums(origArgs[origArgs.length - 1]));
} else if (info.isParam(String.class)) {
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());
}
}
}
}
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(perm.getType().getWatcherClass())) {
tabs.add(method.getName());
}
}
} }
return filterTabs(tabs, origArgs); return filterTabs(tabs, origArgs);

View File

@ -3,9 +3,11 @@ package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
import me.libraryaddict.disguise.utilities.translations.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.parser.*;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -14,7 +16,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -93,7 +94,7 @@ public class DisguiseModifyPlayerCommand extends DisguiseBaseCommand implements
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) { public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) {
ArrayList<String> tabs = new ArrayList<>(); ArrayList<String> tabs = new ArrayList<>();
String[] args = getArgs(origArgs); String[] args = getPreviousArgs(origArgs);
DisguisePermissions perms = getPermissions(sender); DisguisePermissions perms = getPermissions(sender);
@ -114,7 +115,6 @@ public class DisguiseModifyPlayerCommand extends DisguiseBaseCommand implements
Player player = Bukkit.getPlayer(args[0]); Player player = Bukkit.getPlayer(args[0]);
if (player == null) { if (player == null) {
sender.sendMessage(LibsMsg.CANNOT_FIND_PLAYER.get(args[0]));
return tabs; return tabs;
} }
@ -127,60 +127,12 @@ public class DisguiseModifyPlayerCommand extends DisguiseBaseCommand implements
disguise = DisguiseAPI.getDisguise(player); disguise = DisguiseAPI.getDisguise(player);
if (disguise == null) { if (disguise == null) {
sender.sendMessage(LibsMsg.DMODPLAYER_NODISGUISE.get(player.getName()));
return tabs; return tabs;
} }
DisguisePerm disguiseType = new DisguisePerm(disguise.getType()); DisguisePerm disguiseType = new DisguisePerm(disguise.getType());
ArrayList<String> usedOptions = new ArrayList<>(); tabs.addAll(getTabDisguiseOptions(sender, perms, disguiseType, args, 1, getCurrentArg(args)));
for (Method method : ParamInfoManager.getDisguiseWatcherMethods(disguiseType.getWatcherClass())) {
for (int i = 1; i < args.length; i++) {
String arg = args[i];
if (!method.getName().equalsIgnoreCase(arg))
continue;
usedOptions.add(arg);
}
}
if (perms.isAllowedDisguise(disguiseType, usedOptions)) {
boolean addMethods = true;
if (args.length > 1) {
String prevArg = args[args.length - 1];
ParamInfo info = ParamInfoManager.getParamInfo(disguiseType, prevArg);
if (info != null) {
if (!info.isParam(boolean.class)) {
addMethods = false;
}
if (info.hasValues()) {
tabs.addAll(info.getEnums(origArgs[origArgs.length - 1]));
} else if (info.isParam(String.class)) {
for (Player p : Bukkit.getOnlinePlayers()) {
// If command user cannot see player online, don't tab-complete name
if (sender instanceof Player && !((Player) sender).canSee(p)) {
continue;
}
tabs.add(p.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(disguiseType.getWatcherClass())) {
tabs.add(method.getName());
}
}
}
} }
return filterTabs(tabs, origArgs); return filterTabs(tabs, origArgs);

View File

@ -5,11 +5,9 @@ import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.*;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import me.libraryaddict.disguise.utilities.translations.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.translations.TranslateType; import me.libraryaddict.disguise.utilities.translations.TranslateType;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
@ -186,7 +184,7 @@ public class DisguiseModifyRadiusCommand extends DisguiseBaseCommand implements
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) { public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) {
ArrayList<String> tabs = new ArrayList<>(); ArrayList<String> tabs = new ArrayList<>();
String[] args = getArgs(origArgs); String[] args = getPreviousArgs(origArgs);
DisguisePermissions perms = getPermissions(sender); DisguisePermissions perms = getPermissions(sender);
@ -201,7 +199,6 @@ public class DisguiseModifyRadiusCommand extends DisguiseBaseCommand implements
int starting = 0; int starting = 0;
if (!isInteger(args[0])) { if (!isInteger(args[0])) {
for (DisguiseType t : DisguiseType.values()) { for (DisguiseType t : DisguiseType.values()) {
if (t.toReadable().replaceAll(" ", "").equalsIgnoreCase(args[0].replaceAll("_", ""))) { if (t.toReadable().replaceAll(" ", "").equalsIgnoreCase(args[0].replaceAll("_", ""))) {
starting = 2; starting = 2;
@ -214,7 +211,7 @@ public class DisguiseModifyRadiusCommand extends DisguiseBaseCommand implements
return filterTabs(tabs, origArgs); return filterTabs(tabs, origArgs);
} }
if (!isInteger(args[starting])) { if (args.length <= starting || !isInteger(args[starting])) {
return filterTabs(tabs, origArgs); return filterTabs(tabs, origArgs);
} }
@ -225,6 +222,8 @@ public class DisguiseModifyRadiusCommand extends DisguiseBaseCommand implements
radius = maxRadius; radius = maxRadius;
} }
starting++;
ArrayList<String> usedOptions = new ArrayList<>(); ArrayList<String> usedOptions = new ArrayList<>();
for (Entity entity : getNearbyEntities(sender, radius)) { for (Entity entity : getNearbyEntities(sender, radius)) {
@ -244,40 +243,10 @@ public class DisguiseModifyRadiusCommand extends DisguiseBaseCommand implements
} }
} }
if (perms.isAllowedDisguise(new DisguisePerm(disguiseType), usedOptions)) { DisguisePerm perm = new DisguisePerm(disguiseType);
boolean addMethods = true;
if (args.length > 1 + starting) { if (perms.isAllowedDisguise(perm, usedOptions)) {
String prevArg = args[args.length - 1]; tabs.addAll(getTabDisguiseSubOptions(sender, perms, perm, args, starting, getCurrentArg(args)));
ParamInfo info = ParamInfoManager.getParamInfo(disguiseType, prevArg);
if (info != null) {
if (!info.isParam(boolean.class)) {
addMethods = false;
}
if (info.hasValues()) {
tabs.addAll(info.getEnums(origArgs[origArgs.length - 1]));
} else if (info.isParam(String.class)) {
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());
}
}
}
}
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(disguiseType.getWatcherClass())) {
tabs.add(method.getName());
}
}
} }
} }

View File

@ -6,8 +6,9 @@ import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
import me.libraryaddict.disguise.utilities.translations.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -18,7 +19,6 @@ import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -140,7 +140,7 @@ public class DisguisePlayerCommand extends DisguiseBaseCommand implements TabCom
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) { public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) {
ArrayList<String> tabs = new ArrayList<>(); ArrayList<String> tabs = new ArrayList<>();
String[] args = getArgs(origArgs); String[] args = getPreviousArgs(origArgs);
DisguisePermissions perms = getPermissions(sender); DisguisePermissions perms = getPermissions(sender);
@ -153,74 +153,8 @@ public class DisguisePlayerCommand extends DisguiseBaseCommand implements TabCom
tabs.add(player.getName()); tabs.add(player.getName());
} }
} else if (args.length == 1) {
tabs.addAll(getAllowedDisguises(perms));
} else { } else {
DisguisePerm disguiseType = DisguiseParser.getDisguisePerm(args[1]); tabs.addAll(getTabDisguiseTypes(sender, perms, args, 1, getCurrentArg(origArgs)));
if (disguiseType == null)
return filterTabs(tabs, origArgs);
if (args.length == 2 && disguiseType.getType() == DisguiseType.PLAYER) {
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());
}
} else {
ArrayList<String> usedOptions = new ArrayList<>();
for (Method method : ParamInfoManager.getDisguiseWatcherMethods(disguiseType.getWatcherClass())) {
for (int i = disguiseType.getType() == DisguiseType.PLAYER ? 3 : 2; i < args.length; i++) {
String arg = args[i];
if (!method.getName().equalsIgnoreCase(arg))
continue;
usedOptions.add(arg);
}
}
if (perms.isAllowedDisguise(disguiseType, usedOptions)) {
boolean addMethods = true;
if (args.length > 2) {
String prevArg = args[args.length - 1];
ParamInfo info = ParamInfoManager.getParamInfo(disguiseType, prevArg);
if (info != null) {
if (!info.isParam(boolean.class)) {
addMethods = false;
}
if (info.hasValues()) {
tabs.addAll(info.getEnums(origArgs[origArgs.length - 1]));
} else if (info.isParam(String.class)) {
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());
}
}
}
}
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(disguiseType.getWatcherClass())) {
tabs.add(method.getName());
}
}
}
}
} }
return filterTabs(tabs, origArgs); return filterTabs(tabs, origArgs);

View File

@ -3,16 +3,15 @@ package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
import me.libraryaddict.disguise.utilities.reflection.ClassGetter; import me.libraryaddict.disguise.utilities.reflection.ClassGetter;
import me.libraryaddict.disguise.utilities.translations.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.translations.TranslateType; import me.libraryaddict.disguise.utilities.translations.TranslateType;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
@ -24,7 +23,6 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -230,7 +228,7 @@ public class DisguiseRadiusCommand extends DisguiseBaseCommand implements TabCom
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) { public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) {
ArrayList<String> tabs = new ArrayList<>(); ArrayList<String> tabs = new ArrayList<>();
String[] args = getArgs(origArgs); String[] args = getPreviousArgs(origArgs);
DisguisePermissions perms = getPermissions(sender); DisguisePermissions perms = getPermissions(sender);
@ -258,77 +256,7 @@ public class DisguiseRadiusCommand extends DisguiseBaseCommand implements TabCom
return filterTabs(tabs, origArgs); return filterTabs(tabs, origArgs);
} }
if (args.length == starting) { tabs.addAll(getTabDisguiseTypes(sender, perms, args, starting, getCurrentArg(origArgs)));
tabs.addAll(getAllowedDisguises(perms));
} else {
DisguisePerm disguiseType = DisguiseParser.getDisguisePerm(args[starting]);
if (disguiseType == null)
return filterTabs(tabs, origArgs);
if (args.length == 1 + starting && disguiseType.getType() == DisguiseType.PLAYER) {
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());
}
} else {
ArrayList<String> usedOptions = new ArrayList<>();
for (Method method : ParamInfoManager.getDisguiseWatcherMethods(disguiseType.getWatcherClass())) {
for (int i = disguiseType.getType() == DisguiseType.PLAYER ? starting + 2 : starting + 1;
i < args.length; i++) {
String arg = args[i];
if (!method.getName().equalsIgnoreCase(arg))
continue;
usedOptions.add(arg);
}
}
if (perms.isAllowedDisguise(disguiseType, usedOptions)) {
boolean addMethods = true;
if (args.length > 1 + starting) {
String prevArg = args[args.length - 1];
ParamInfo info = ParamInfoManager.getParamInfo(disguiseType, prevArg);
if (info != null) {
if (!info.isParam(boolean.class)) {
addMethods = false;
}
if (info.hasValues()) {
tabs.addAll(info.getEnums(origArgs[origArgs.length - 1]));
} else if (info.isParam(String.class)) {
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());
}
}
}
}
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(disguiseType.getWatcherClass())) {
tabs.add(method.getName());
}
}
}
}
}
return filterTabs(tabs, origArgs); return filterTabs(tabs, origArgs);
} }

View File

@ -12,6 +12,7 @@ import java.util.Objects;
public class DisguisePerm { public class DisguisePerm {
private DisguiseType disguiseType; private DisguiseType disguiseType;
private String permName; private String permName;
private boolean customDisguise;
public DisguisePerm(DisguiseType disguiseType) { public DisguisePerm(DisguiseType disguiseType) {
this.disguiseType = disguiseType; this.disguiseType = disguiseType;
@ -20,6 +21,11 @@ public class DisguisePerm {
public DisguisePerm(DisguiseType disguiseType, String disguisePerm) { public DisguisePerm(DisguiseType disguiseType, String disguisePerm) {
this.disguiseType = disguiseType; this.disguiseType = disguiseType;
permName = disguisePerm; permName = disguisePerm;
customDisguise = true;
}
public boolean isCustomDisguise() {
return customDisguise;
} }
public Class getEntityClass() { public Class getEntityClass() {