200 lines
6.2 KiB
Java
200 lines
6.2 KiB
Java
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<String> plugins;
|
|
private static Map<String, List<String>> permissions;
|
|
|
|
private TabCompleteHelper() {
|
|
|
|
}
|
|
|
|
/**
|
|
* Finds tab complete values that match the start of the typed text
|
|
*
|
|
* @param values <p>The values to filter</p>
|
|
* @param typedText <p>The text the player has started typing</p>
|
|
* @return <p>The given string values that start with the player's typed text</p>
|
|
*/
|
|
public static List<String> filterMatchingStartsWith(@NotNull List<String> 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<String> 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 <p>The available boolean values</p>
|
|
*/
|
|
public static List<String> getBooleans() {
|
|
List<String> booleans = new ArrayList<>();
|
|
booleans.add("true");
|
|
booleans.add("false");
|
|
return booleans;
|
|
}
|
|
|
|
/**
|
|
* Gets the available option states for tab completion
|
|
*
|
|
* @return <p>The available option states</p>
|
|
*/
|
|
public static List<String> getOptionStates() {
|
|
List<String> optionStates = getBooleans();
|
|
optionStates.add("default");
|
|
return optionStates;
|
|
}
|
|
|
|
/**
|
|
* Gets the names of all registered paid signs
|
|
*
|
|
* @return <p>The names of all registered paid signs</p>
|
|
*/
|
|
public static List<String> getPaidSignNames() {
|
|
Set<String> paidSignNames = PaidSigns.getInstance().getSignManager().getAllPaidSigns().keySet();
|
|
List<String> quotedNames = new ArrayList<>(paidSignNames.size());
|
|
for (String paidSignName : paidSignNames) {
|
|
quotedNames.add("\"" + paidSignName + "\"");
|
|
}
|
|
return quotedNames;
|
|
}
|
|
|
|
/**
|
|
* Gets all the choices for lines on a sign
|
|
*
|
|
* @return <p>All the line indices of a sign</p>
|
|
*/
|
|
public static List<String> getSignLines() {
|
|
List<String> 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 <p>Some valid costs</p>
|
|
*/
|
|
public static List<String> getCosts() {
|
|
List<String> 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 <p>Some example paid sign names</p>
|
|
*/
|
|
public static List<String> getExamplePaidSignNames() {
|
|
List<String> 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 <p>Some example "stringToMatch" values</p>
|
|
*/
|
|
public static List<String> getExampleConditionStrings() {
|
|
List<String> 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 <p>The full permission node typed by the player</p>
|
|
* @return <p>All known valid auto-complete options</p>
|
|
*/
|
|
public static List<String> tabCompletePermission(String typedNode) {
|
|
if (plugins == null) {
|
|
loadAvailablePermissions();
|
|
}
|
|
List<String> output;
|
|
if (typedNode.contains(".")) {
|
|
List<String> 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 <p>The permission to load</p>
|
|
*/
|
|
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<String> permissionList = permissions.computeIfAbsent(path, k -> new ArrayList<>());
|
|
permissionList.add(permissionName);
|
|
|
|
loadPermission(path);
|
|
}
|
|
}
|
|
|
|
}
|