package net.knarcraft.paidsigns.utility; import net.knarcraft.paidsigns.PaidSigns; import org.bukkit.Bukkit; import org.bukkit.permissions.Permission; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.StringJoiner; /** * A helper class for providing common tab complete options */ public final class TabCompleteHelper { private static List plugins; private static Map> permissions; private TabCompleteHelper() { } /** * Finds tab complete values that match the start of the typed text * * @param values

The values to filter

* @param typedText

The text the player has started typing

* @return

The given string values that start with the player's typed text

*/ public static List filterMatchingStartsWith(@NotNull List values, String typedText) { //This little trick makes sure tab-completion works for paid sign names if (!values.isEmpty() && values.get(0).startsWith("\"")) { typedText = "\"" + typedText; } List configValues = new ArrayList<>(); for (String value : values) { if (value.toLowerCase().startsWith(typedText.toLowerCase())) { configValues.add(value); } } return configValues; } /** * Gets the available boolean values for tab completion * * @return

The available boolean values

*/ public static List getBooleans() { List booleans = new ArrayList<>(); booleans.add("true"); booleans.add("false"); return booleans; } /** * Gets the available option states for tab completion * * @return

The available option states

*/ public static List getOptionStates() { List optionStates = getBooleans(); optionStates.add("default"); return optionStates; } /** * Gets the names of all registered paid signs * * @return

The names of all registered paid signs

*/ public static List getPaidSignNames() { Set paidSignNames = PaidSigns.getInstance().getSignManager().getAllPaidSigns().keySet(); List quotedNames = new ArrayList<>(paidSignNames.size()); for (String paidSignName : paidSignNames) { quotedNames.add("\"" + paidSignName + "\""); } return quotedNames; } /** * Gets all the choices for lines on a sign * * @return

All the line indices of a sign

*/ public static List getSignLines() { List lines = new ArrayList<>(); for (int i = 1; i < 5; i++) { lines.add(String.valueOf(i)); } return lines; } /** * Gets some valid costs as example values * * @return

Some valid costs

*/ public static List getCosts() { List costs = new ArrayList<>(); costs.add("1"); costs.add("5"); costs.add("10"); costs.add("15"); return costs; } /** * Gets some example paid sign names for auto-completion * * @return

Some example paid sign names

*/ public static List getExamplePaidSignNames() { List names = new ArrayList<>(); names.add("sign1"); names.add("\"lift up sign\""); names.add("\"lift down sign\""); return names; } /** * Gets some example condition matching strings for tab completion * * @return

Some example "stringToMatch" values

*/ public static List getExampleConditionStrings() { List conditionStrings = new ArrayList<>(); conditionStrings.add("[Gate]"); conditionStrings.add("\"[Lift Up]\""); conditionStrings.add("\"[Lift Down]\""); return conditionStrings; } /** * Gets the tab complete value for the permission typed * * @param typedNode

The full permission node typed by the player

* @return

All known valid auto-complete options

*/ public static List tabCompletePermission(String typedNode) { if (plugins == null) { loadAvailablePermissions(); } List output; if (typedNode.contains(".")) { List matchingPermissions = permissions.get(typedNode.substring(0, typedNode.lastIndexOf("."))); if (matchingPermissions == null) { output = new ArrayList<>(); } else { //Filter by the typed text output = TabCompleteHelper.filterMatchingStartsWith(matchingPermissions, typedNode); } } else { output = plugins; } //Add previous permissions in the comma-separated lists as a prefix return output; } /** * Loads all permissions available from bukkit plugins */ private static void loadAvailablePermissions() { plugins = new ArrayList<>(); permissions = new HashMap<>(); for (Permission permission : Bukkit.getPluginManager().getPermissions()) { loadPermission(permission.getName()); } } /** * Loads a given permission into the proper lists and maps * * @param permissionName

The permission to load

*/ private static void loadPermission(String permissionName) { String[] permissionParts = permissionName.split("\\."); if (permissionParts.length == 1 && !plugins.contains(permissionParts[0])) { plugins.add(permissionParts[0]); } else if (permissionParts.length > 1) { StringJoiner pathJoiner = new StringJoiner("."); for (int j = 0; j < permissionParts.length - 1; j++) { pathJoiner.add(permissionParts[j]); } String path = pathJoiner.toString(); List permissionList = permissions.computeIfAbsent(path, k -> new ArrayList<>()); permissionList.add(permissionName); loadPermission(path); } } }