Makes givebook support filenames with spaces
This commit is contained in:
@@ -46,6 +46,8 @@ public class CommandDelete implements TabExecutor {
|
||||
return true;
|
||||
}
|
||||
|
||||
//TODO: Add parsing of books containing spaces
|
||||
|
||||
//Delete the file
|
||||
if (arguments.length == 1) {
|
||||
List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, deletePublic);
|
||||
|
@@ -6,6 +6,7 @@ import net.knarcraft.bookswithoutborders.utility.BookLoader;
|
||||
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
||||
import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper;
|
||||
import net.knarcraft.knarlib.util.TabCompletionHelper;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -52,16 +53,19 @@ public class CommandGive implements TabExecutor {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (arguments.length == 1 || arguments.length > 4) {
|
||||
int argumentCount = arguments.length;
|
||||
|
||||
if (arguments.length == 1) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "Incorrect number of arguments for this command!");
|
||||
return false;
|
||||
}
|
||||
|
||||
//Organize and parse input
|
||||
String bookIdentifier = arguments[0];
|
||||
String receivingPlayerName = arguments[1];
|
||||
String bookIdentifier;
|
||||
String receivingPlayerName;
|
||||
String copies = "1";
|
||||
String isSigned = "true";
|
||||
|
||||
if (arguments.length == 4) {
|
||||
copies = arguments[2];
|
||||
isSigned = arguments[3];
|
||||
@@ -73,6 +77,24 @@ public class CommandGive implements TabExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
if (argumentCount > 3 && InputCleaningHelper.isInt(arguments[argumentCount - 2]) && InputCleaningHelper.isBoolean(arguments[argumentCount - 1])) {
|
||||
receivingPlayerName = arguments[argumentCount - 3];
|
||||
isSigned = arguments[argumentCount - 1];
|
||||
copies = arguments[argumentCount - 2];
|
||||
bookIdentifier = InputCleaningHelper.mergeArguments(arguments, 3);
|
||||
} else if (argumentCount > 2 && InputCleaningHelper.isBoolean(arguments[argumentCount - 1])) {
|
||||
isSigned = arguments[argumentCount - 1];
|
||||
receivingPlayerName = arguments[argumentCount - 2];
|
||||
bookIdentifier = InputCleaningHelper.mergeArguments(arguments, 2);
|
||||
} else if (argumentCount > 2 && InputCleaningHelper.isInt(arguments[argumentCount - 1])) {
|
||||
copies = arguments[argumentCount - 1];
|
||||
receivingPlayerName = arguments[argumentCount - 2];
|
||||
bookIdentifier = InputCleaningHelper.mergeArguments(arguments, 2);
|
||||
} else {
|
||||
receivingPlayerName = arguments[argumentCount - 1];
|
||||
bookIdentifier = InputCleaningHelper.mergeArguments(arguments, 1);
|
||||
}
|
||||
|
||||
//Try and find the target player
|
||||
Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(receivingPlayerName);
|
||||
if (receivingPlayer == null) {
|
||||
@@ -112,43 +134,31 @@ public class CommandGive implements TabExecutor {
|
||||
* Performs the actual tab completion
|
||||
*
|
||||
* @param sender <p>The sender of the command</p>
|
||||
* @param args <p>The arguments given</p>
|
||||
* @param arguments <p>The arguments given</p>
|
||||
* @param listPublic <p>Whether to list public files or player files</p>
|
||||
* @return <p>A list of available choices</p>
|
||||
*/
|
||||
@Nullable
|
||||
protected List<String> doTabCompletion(@NotNull CommandSender sender, @NotNull String[] args, boolean listPublic) {
|
||||
protected List<String> doTabCompletion(@NotNull CommandSender sender, @NotNull String[] arguments, boolean listPublic) {
|
||||
Server server = booksWithoutBorders.getServer();
|
||||
|
||||
int argumentCount = args.length;
|
||||
|
||||
if (argumentCount > 2) {
|
||||
//Don't continue with autocompletion if the recipient is invalid
|
||||
if (server.getPlayer(args[1]) == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
int argumentCount = arguments.length;
|
||||
|
||||
if (argumentCount == 1) {
|
||||
//Return list of books
|
||||
return TabCompletionHelper.filterMatchingContains(BooksWithoutBorders.getAvailableBooks(sender, listPublic),
|
||||
args[0]);
|
||||
} else if (argumentCount == 2) {
|
||||
//Return online players
|
||||
return null;
|
||||
} else if (argumentCount == 3) {
|
||||
//Number of copies
|
||||
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleansAndNumbers(1, 3), args[2]);
|
||||
} else if (argumentCount == 4) {
|
||||
//Signed
|
||||
try {
|
||||
Integer.parseInt(args[2]);
|
||||
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleans(), args[3]);
|
||||
} catch (NumberFormatException e) {
|
||||
return new ArrayList<>();
|
||||
return TabCompletionHelper.filterMatchingContains(BooksWithoutBorders.getAvailableBooks(sender, listPublic), arguments[0]);
|
||||
} else if (argumentCount > 2 && InputCleaningHelper.isBoolean(arguments[argumentCount - 2])) {
|
||||
return List.of();
|
||||
} else if (argumentCount > 2 && server.getPlayer(arguments[argumentCount - 3]) != null && InputCleaningHelper.isInt(arguments[argumentCount - 2])) {
|
||||
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleans(), arguments[argumentCount - 1]);
|
||||
} else if (argumentCount > 2 && server.getPlayer(arguments[argumentCount - 2]) != null) {
|
||||
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleansAndNumbers(1, 3), arguments[argumentCount - 1]);
|
||||
} else {
|
||||
List<String> players = new ArrayList<>();
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
players.add(player.getName());
|
||||
}
|
||||
return TabCompletionHelper.filterMatchingStartsWith(players, arguments[argumentCount - 1]);
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -60,18 +60,19 @@ public class CommandLoad implements TabExecutor {
|
||||
String isSigned = "true";
|
||||
|
||||
if (argumentCount > 1) {
|
||||
if (argumentCount > 2 && isInt(arguments[argumentCount - 2]) && isBoolean(arguments[argumentCount - 1])) {
|
||||
if (argumentCount > 2 && InputCleaningHelper.isInt(arguments[argumentCount - 2]) &&
|
||||
InputCleaningHelper.isBoolean(arguments[argumentCount - 1])) {
|
||||
isSigned = arguments[argumentCount - 1];
|
||||
copies = arguments[argumentCount - 2];
|
||||
bookIdentifier = mergeArguments(arguments, 2);
|
||||
} else if (isBoolean(arguments[argumentCount - 1])) {
|
||||
bookIdentifier = InputCleaningHelper.mergeArguments(arguments, 2);
|
||||
} else if (InputCleaningHelper.isBoolean(arguments[argumentCount - 1])) {
|
||||
isSigned = arguments[argumentCount - 1];
|
||||
bookIdentifier = mergeArguments(arguments, 1);
|
||||
} else if (isInt(arguments[argumentCount - 1])) {
|
||||
bookIdentifier = InputCleaningHelper.mergeArguments(arguments, 1);
|
||||
} else if (InputCleaningHelper.isInt(arguments[argumentCount - 1])) {
|
||||
copies = arguments[argumentCount - 1];
|
||||
bookIdentifier = mergeArguments(arguments, 1);
|
||||
bookIdentifier = InputCleaningHelper.mergeArguments(arguments, 1);
|
||||
} else {
|
||||
bookIdentifier = mergeArguments(arguments, 0);
|
||||
bookIdentifier = InputCleaningHelper.mergeArguments(arguments, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,26 +101,6 @@ public class CommandLoad implements TabExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given input is a boolean
|
||||
*
|
||||
* @param input <p>The input to validate</p>
|
||||
* @return <p>True if the given input is a boolean</p>
|
||||
*/
|
||||
private boolean isBoolean(@NotNull String input) {
|
||||
return input.matches("(true|false)");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given input is an integer
|
||||
*
|
||||
* @param input <p>The input to validate</p>
|
||||
* @return <p>True if the given input is an integer</p>
|
||||
*/
|
||||
private boolean isInt(@NotNull String input) {
|
||||
return input.matches("[0-9]+");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
|
||||
@NotNull String[] arguments) {
|
||||
@@ -139,29 +120,13 @@ public class CommandLoad implements TabExecutor {
|
||||
int argumentCount = arguments.length;
|
||||
if (argumentCount == 1) {
|
||||
return TabCompletionHelper.filterMatchingContains(BooksWithoutBorders.getAvailableBooks(sender, loadPublic), arguments[0]);
|
||||
} else if (isBoolean(arguments[argumentCount - 2])) {
|
||||
} else if (InputCleaningHelper.isBoolean(arguments[argumentCount - 2])) {
|
||||
return List.of();
|
||||
} else if (isInt(arguments[argumentCount - 2])) {
|
||||
} else if (InputCleaningHelper.isInt(arguments[argumentCount - 2])) {
|
||||
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleans(), arguments[argumentCount - 1]);
|
||||
} else {
|
||||
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleansAndNumbers(1, 3), arguments[argumentCount - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges all arguments to a string with spaces
|
||||
*
|
||||
* @param arguments <p>The arguments to merge</p>
|
||||
* @param stripLastN <p>How many of the last arguments to ignore</p>
|
||||
* @return <p>The merged arguments</p>
|
||||
*/
|
||||
@NotNull
|
||||
private String mergeArguments(String[] arguments, int stripLastN) {
|
||||
StringBuilder builder = new StringBuilder(arguments[0]);
|
||||
for (int i = 1; i < arguments.length - stripLastN; i++) {
|
||||
builder.append(" ").append(arguments[i]);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -50,22 +50,22 @@ public abstract class BookIndex {
|
||||
}
|
||||
|
||||
// Parse book author from input
|
||||
for (int i = 0; i < arguments.length; i++) {
|
||||
String author = InputCleaningHelper.parseAuthorSpecifier(arguments[i]);
|
||||
for (int authorIndex = 0; authorIndex < arguments.length; authorIndex++) {
|
||||
String author = InputCleaningHelper.parseAuthorSpecifier(arguments[authorIndex]);
|
||||
if (author == null) {
|
||||
continue;
|
||||
}
|
||||
for (int j = i + 1; j < arguments.length; j++) {
|
||||
int pageNumber = InputCleaningHelper.parsePageNumber(arguments[j]);
|
||||
if (pageNumber > 0) {
|
||||
int authorEnd = j - 1;
|
||||
StringBuilder builder = new StringBuilder(author);
|
||||
for (int k = i + 1; k <= authorEnd; k++) {
|
||||
builder.append(" ").append(arguments[k]);
|
||||
}
|
||||
AuthorBookIndex.printBooks(sender, selectPublic, command, pageNumber, builder.toString());
|
||||
return true;
|
||||
for (int pageIndex = authorIndex + 1; pageIndex < arguments.length; pageIndex++) {
|
||||
int pageNumber = InputCleaningHelper.parsePageNumber(arguments[pageIndex]);
|
||||
if (pageNumber <= 0) {
|
||||
continue;
|
||||
}
|
||||
StringBuilder builder = new StringBuilder(author);
|
||||
for (int authorPartCounter = authorIndex + 1; authorPartCounter < pageIndex; authorPartCounter++) {
|
||||
builder.append(" ").append(arguments[authorPartCounter]);
|
||||
}
|
||||
AuthorBookIndex.printBooks(sender, selectPublic, command, pageNumber, builder.toString());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -87,4 +87,40 @@ public final class InputCleaningHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given input is a boolean
|
||||
*
|
||||
* @param input <p>The input to validate</p>
|
||||
* @return <p>True if the given input is a boolean</p>
|
||||
*/
|
||||
public static boolean isBoolean(@NotNull String input) {
|
||||
return input.matches("(true|false)");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given input is an integer
|
||||
*
|
||||
* @param input <p>The input to validate</p>
|
||||
* @return <p>True if the given input is an integer</p>
|
||||
*/
|
||||
public static boolean isInt(@NotNull String input) {
|
||||
return input.matches("[0-9]+");
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges all arguments to a string with spaces
|
||||
*
|
||||
* @param arguments <p>The arguments to merge</p>
|
||||
* @param stripLastN <p>How many of the last arguments to ignore</p>
|
||||
* @return <p>The merged arguments</p>
|
||||
*/
|
||||
@NotNull
|
||||
public static String mergeArguments(String[] arguments, int stripLastN) {
|
||||
StringBuilder builder = new StringBuilder(arguments[0]);
|
||||
for (int i = 1; i < arguments.length - stripLastN; i++) {
|
||||
builder.append(" ").append(arguments[i]);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user