From 49fe9d51f09dd1298bf6a88ea71c3c575fd04c48 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Thu, 17 Feb 2022 20:57:14 +1300 Subject: [PATCH] Fix tab complete showing values they can't access --- .../commands/DisguiseBaseCommand.java | 28 ++++++++++++++++-- .../utilities/parser/DisguisePermissions.java | 29 ++++++++++++------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/plugin/src/main/java/me/libraryaddict/disguise/commands/DisguiseBaseCommand.java b/plugin/src/main/java/me/libraryaddict/disguise/commands/DisguiseBaseCommand.java index 8840e77d..fb3eea95 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/commands/DisguiseBaseCommand.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/commands/DisguiseBaseCommand.java @@ -31,6 +31,7 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -172,12 +173,15 @@ public abstract class DisguiseBaseCommand implements CommandExecutor { List tabs = new ArrayList<>(); ParamInfo info = null; + String methodName = null; if (allArgs.length == startsAt) { if (disguisePerm.getType() == DisguiseType.FALLING_BLOCK) { info = ParamInfoManager.getParamInfoItemBlock(); + methodName = "setBlock"; } else if (disguisePerm.getType() == DisguiseType.DROPPED_ITEM) { info = ParamInfoManager.getParamInfo(ItemStack.class); + methodName = "setItemstack"; } } else if (allArgs.length > startsAt) { // 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]; info = ParamInfoManager.getParamInfo(disguisePerm, prevArg); + methodName = prevArg; if (info != null && !info.isParam(boolean.class)) { addMethods = false; @@ -198,17 +203,36 @@ public abstract class DisguiseBaseCommand implements CommandExecutor { // If the previous argument is a method if (info != null) { + Collection wantToUse = null; + // If there is a list of default values if (info.hasValues()) { - tabs.addAll(info.getEnums(currentArg)); + wantToUse = info.getEnums(currentArg); } else if (info.isParam(String.class)) { + wantToUse = new ArrayList<>(); + 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()); + wantToUse.add(player.getName()); + } + } + + if (wantToUse != null) { + HashMap> allowedOptions = DisguisePermissions.getDisguiseOptions(commandSender, getPermNode(), disguisePerm); + HashMap methodValues = allowedOptions.get(methodName.toLowerCase(Locale.ENGLISH)); + + if (methodValues != null) { + for (String value : wantToUse) { + if (!DisguisePermissions.hasMethodOption(methodValues, value)) { + continue; + } + + tabs.add(value); + } } } } diff --git a/plugin/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguisePermissions.java b/plugin/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguisePermissions.java index 029d805b..8a50467d 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguisePermissions.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguisePermissions.java @@ -551,6 +551,23 @@ public class DisguisePermissions { return returns; } + /** + * Returns true if the string is found in the map, or it's not a whitelisted setup + *

+ * Returns if command user can access the disguise creation permission type + */ + public static boolean hasMethodOption(HashMap 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 *

@@ -564,16 +581,6 @@ public class DisguisePermissions { return true; } - HashMap map = disguiseOptions.get(method); - - 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); + return hasMethodOption(disguiseOptions.get(method), value); } }