Fix tab complete showing values they can't access

This commit is contained in:
libraryaddict 2022-02-17 20:57:14 +13:00
parent 145c1057c3
commit 49fe9d51f0
2 changed files with 44 additions and 13 deletions

@ -31,6 +31,7 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -172,12 +173,15 @@ public abstract class DisguiseBaseCommand implements CommandExecutor {
List<String> tabs = new ArrayList<>(); List<String> tabs = new ArrayList<>();
ParamInfo info = null; ParamInfo info = null;
String methodName = null;
if (allArgs.length == startsAt) { if (allArgs.length == startsAt) {
if (disguisePerm.getType() == DisguiseType.FALLING_BLOCK) { if (disguisePerm.getType() == DisguiseType.FALLING_BLOCK) {
info = ParamInfoManager.getParamInfoItemBlock(); info = ParamInfoManager.getParamInfoItemBlock();
methodName = "setBlock";
} else if (disguisePerm.getType() == DisguiseType.DROPPED_ITEM) { } else if (disguisePerm.getType() == DisguiseType.DROPPED_ITEM) {
info = ParamInfoManager.getParamInfo(ItemStack.class); info = ParamInfoManager.getParamInfo(ItemStack.class);
methodName = "setItemstack";
} }
} else if (allArgs.length > startsAt) { } else if (allArgs.length > startsAt) {
// Check what argument was used before the current argument to see what we're displaying // Check what argument was used before the current argument to see what we're displaying
@ -185,6 +189,7 @@ public abstract class DisguiseBaseCommand implements CommandExecutor {
String prevArg = allArgs[allArgs.length - 1]; String prevArg = allArgs[allArgs.length - 1];
info = ParamInfoManager.getParamInfo(disguisePerm, prevArg); info = ParamInfoManager.getParamInfo(disguisePerm, prevArg);
methodName = prevArg;
if (info != null && !info.isParam(boolean.class)) { if (info != null && !info.isParam(boolean.class)) {
addMethods = false; addMethods = false;
@ -198,17 +203,36 @@ public abstract class DisguiseBaseCommand implements CommandExecutor {
// If the previous argument is a method // If the previous argument is a method
if (info != null) { if (info != null) {
Collection<String> wantToUse = null;
// If there is a list of default values // If there is a list of default values
if (info.hasValues()) { if (info.hasValues()) {
tabs.addAll(info.getEnums(currentArg)); wantToUse = info.getEnums(currentArg);
} else if (info.isParam(String.class)) { } else if (info.isParam(String.class)) {
wantToUse = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
// If command user cannot see player online, don't tab-complete name // If command user cannot see player online, don't tab-complete name
if (commandSender instanceof Player && !((Player) commandSender).canSee(player)) { if (commandSender instanceof Player && !((Player) commandSender).canSee(player)) {
continue; continue;
} }
tabs.add(player.getName()); wantToUse.add(player.getName());
}
}
if (wantToUse != null) {
HashMap<String, HashMap<String, Boolean>> allowedOptions = DisguisePermissions.getDisguiseOptions(commandSender, getPermNode(), disguisePerm);
HashMap<String, Boolean> methodValues = allowedOptions.get(methodName.toLowerCase(Locale.ENGLISH));
if (methodValues != null) {
for (String value : wantToUse) {
if (!DisguisePermissions.hasMethodOption(methodValues, value)) {
continue;
}
tabs.add(value);
}
} }
} }
} }

@ -551,6 +551,23 @@ public class DisguisePermissions {
return returns; return returns;
} }
/**
* Returns true if the string is found in the map, or it's not a whitelisted setup
* <p>
* Returns if command user can access the disguise creation permission type
*/
public static boolean hasMethodOption(HashMap<String, Boolean> methodValues, String value) {
value = value.toLowerCase(Locale.ENGLISH);
// If they were explictly defined, can just return the value
if (methodValues.containsKey(value)) {
return methodValues.get(value);
}
// If there is at least one whitelisted value, then they needed the whitelist to use it
return !methodValues.containsValue(true);
}
/** /**
* Returns true if the string is found in the map, or it's not a whitelisted setup * Returns true if the string is found in the map, or it's not a whitelisted setup
* <p> * <p>
@ -564,16 +581,6 @@ public class DisguisePermissions {
return true; return true;
} }
HashMap<String, Boolean> map = disguiseOptions.get(method); return hasMethodOption(disguiseOptions.get(method), value);
value = value.toLowerCase(Locale.ENGLISH);
// If they were explictly defined, can just return the value
if (map.containsKey(value)) {
return map.get(value);
}
// If there is at least one whitelisted value, then they needed the whitelist to use it
return !map.containsValue(true);
} }
} }