Skip to content

Instantly share code, notes, and snippets.

@lachlandcp
Last active July 9, 2016 01:56
Show Gist options
  • Save lachlandcp/0e5d3939c08d7ec07a489ff1e8d767aa to your computer and use it in GitHub Desktop.
Save lachlandcp/0e5d3939c08d7ec07a489ff1e8d767aa to your computer and use it in GitHub Desktop.
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
// args = ["tp", "colonies", "ci"]; length: 3
List<String> returnArgs = new ArrayList<String>();
List<String> allOptions = somehowGetPossibleArguments();
String[] protectionNameStrings = new String[args.length - 1]{};
// args, 1 = point in args array to start at, copying to protectionNameStrings, point in protectionNameStrings array to copy to, array length - 1
System.arraycopy(args, 1, protectionNameStrings, 0, args.length - 1);
// protectionNameStrings = ["colonies", "ci"]; length: 2 (ideally, i havent acrtually tested this code)
// Join all protectionNameStrings to one string
StringBuilder protectionName = new StringBuilder();
for (String s : protectionNameStrings){
protectionName.append(s);
}
for (String option : allOptions) {
// Check if option starts with protectionName.toString()
// "colonies city" starts with "colonies ci"
if (option.toLowerCase().startsWith(protectionName.toString())) {
// This gets complicated. Need to return argument FROM the current unfinished argument's position.
// So if the command is "/bp tp protection with long na...", we'd need to only finish the "name" argument, or if it is "/bp tp protection with lo...", we'd need to finish "long name"
// option = "colonies city"
List<String> allPieces = Arrays.asList(option.toLowerCase().split(" "));
// allPieces = ["colonies", "city"]; length: 2
List<String> validPieces = new ArrayList<String>();
// System.arraycopy(["colonies", "city"], 1, [], 0, 1 /* 2 - (2 - 1) */);
// System.arraycopy(["colonies", "city", "resort"], 1, [], 0, 2 /* 3 - (2 - 1)*/);
// How many user provided: protectionNameStrings.size() - 1
// How many the command requires: allPieces.size()
// How many we provide back: allPieces.size() - (protectionNameStrings.size() - 1)
System.arraycopy(allPieces, protectionNameStrings.size() - 1, validPieces, 0, allPieces.size() - (protectionNameStrings.size() - 1));
// Join all valid pieces to one string
StringBuilder validPiecesString = new StringBuilder();
for (String s : validPieces){
validPiecesString.append(s);
}
returnArgs.add(validPiecesString);
}
}
return returnArgs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment