Makes givebook support filenames with spaces

This commit is contained in:
2025-08-08 15:05:54 +02:00
parent b963f83dee
commit b5bff2400b
5 changed files with 100 additions and 87 deletions

View File

@@ -46,6 +46,8 @@ public class CommandDelete implements TabExecutor {
return true; return true;
} }
//TODO: Add parsing of books containing spaces
//Delete the file //Delete the file
if (arguments.length == 1) { if (arguments.length == 1) {
List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, deletePublic); List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, deletePublic);

View File

@@ -6,6 +6,7 @@ import net.knarcraft.bookswithoutborders.utility.BookLoader;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper; import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper;
import net.knarcraft.knarlib.util.TabCompletionHelper; import net.knarcraft.knarlib.util.TabCompletionHelper;
import org.bukkit.Bukkit;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@@ -52,16 +53,19 @@ public class CommandGive implements TabExecutor {
return true; 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!"); BooksWithoutBorders.sendErrorMessage(sender, "Incorrect number of arguments for this command!");
return false; return false;
} }
//Organize and parse input //Organize and parse input
String bookIdentifier = arguments[0]; String bookIdentifier;
String receivingPlayerName = arguments[1]; String receivingPlayerName;
String copies = "1"; String copies = "1";
String isSigned = "true"; String isSigned = "true";
if (arguments.length == 4) { if (arguments.length == 4) {
copies = arguments[2]; copies = arguments[2];
isSigned = arguments[3]; 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 //Try and find the target player
Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(receivingPlayerName); Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(receivingPlayerName);
if (receivingPlayer == null) { if (receivingPlayer == null) {
@@ -112,43 +134,31 @@ public class CommandGive implements TabExecutor {
* Performs the actual tab completion * Performs the actual tab completion
* *
* @param sender <p>The sender of the command</p> * @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> * @param listPublic <p>Whether to list public files or player files</p>
* @return <p>A list of available choices</p> * @return <p>A list of available choices</p>
*/ */
@Nullable @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(); Server server = booksWithoutBorders.getServer();
int argumentCount = args.length; int argumentCount = arguments.length;
if (argumentCount > 2) {
//Don't continue with autocompletion if the recipient is invalid
if (server.getPlayer(args[1]) == null) {
return new ArrayList<>();
}
}
if (argumentCount == 1) { if (argumentCount == 1) {
//Return list of books return TabCompletionHelper.filterMatchingContains(BooksWithoutBorders.getAvailableBooks(sender, listPublic), arguments[0]);
return TabCompletionHelper.filterMatchingContains(BooksWithoutBorders.getAvailableBooks(sender, listPublic), } else if (argumentCount > 2 && InputCleaningHelper.isBoolean(arguments[argumentCount - 2])) {
args[0]); return List.of();
} else if (argumentCount == 2) { } else if (argumentCount > 2 && server.getPlayer(arguments[argumentCount - 3]) != null && InputCleaningHelper.isInt(arguments[argumentCount - 2])) {
//Return online players return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleans(), arguments[argumentCount - 1]);
return null; } else if (argumentCount > 2 && server.getPlayer(arguments[argumentCount - 2]) != null) {
} else if (argumentCount == 3) { return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleansAndNumbers(1, 3), arguments[argumentCount - 1]);
//Number of copies } else {
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleansAndNumbers(1, 3), args[2]); List<String> players = new ArrayList<>();
} else if (argumentCount == 4) { for (Player player : Bukkit.getOnlinePlayers()) {
//Signed players.add(player.getName());
try {
Integer.parseInt(args[2]);
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleans(), args[3]);
} catch (NumberFormatException e) {
return new ArrayList<>();
} }
return TabCompletionHelper.filterMatchingStartsWith(players, arguments[argumentCount - 1]);
} }
return new ArrayList<>();
} }
/** /**

View File

@@ -60,18 +60,19 @@ public class CommandLoad implements TabExecutor {
String isSigned = "true"; String isSigned = "true";
if (argumentCount > 1) { 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]; isSigned = arguments[argumentCount - 1];
copies = arguments[argumentCount - 2]; copies = arguments[argumentCount - 2];
bookIdentifier = mergeArguments(arguments, 2); bookIdentifier = InputCleaningHelper.mergeArguments(arguments, 2);
} else if (isBoolean(arguments[argumentCount - 1])) { } else if (InputCleaningHelper.isBoolean(arguments[argumentCount - 1])) {
isSigned = arguments[argumentCount - 1]; isSigned = arguments[argumentCount - 1];
bookIdentifier = mergeArguments(arguments, 1); bookIdentifier = InputCleaningHelper.mergeArguments(arguments, 1);
} else if (isInt(arguments[argumentCount - 1])) { } else if (InputCleaningHelper.isInt(arguments[argumentCount - 1])) {
copies = arguments[argumentCount - 1]; copies = arguments[argumentCount - 1];
bookIdentifier = mergeArguments(arguments, 1); bookIdentifier = InputCleaningHelper.mergeArguments(arguments, 1);
} else { } 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 @Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
@NotNull String[] arguments) { @NotNull String[] arguments) {
@@ -139,29 +120,13 @@ public class CommandLoad implements TabExecutor {
int argumentCount = arguments.length; int argumentCount = arguments.length;
if (argumentCount == 1) { if (argumentCount == 1) {
return TabCompletionHelper.filterMatchingContains(BooksWithoutBorders.getAvailableBooks(sender, loadPublic), arguments[0]); 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(); return List.of();
} else if (isInt(arguments[argumentCount - 2])) { } else if (InputCleaningHelper.isInt(arguments[argumentCount - 2])) {
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleans(), arguments[argumentCount - 1]); return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleans(), arguments[argumentCount - 1]);
} else { } else {
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleansAndNumbers(1, 3), arguments[argumentCount - 1]); 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();
}
} }

View File

@@ -50,24 +50,24 @@ public abstract class BookIndex {
} }
// Parse book author from input // Parse book author from input
for (int i = 0; i < arguments.length; i++) { for (int authorIndex = 0; authorIndex < arguments.length; authorIndex++) {
String author = InputCleaningHelper.parseAuthorSpecifier(arguments[i]); String author = InputCleaningHelper.parseAuthorSpecifier(arguments[authorIndex]);
if (author == null) { if (author == null) {
continue; continue;
} }
for (int j = i + 1; j < arguments.length; j++) { for (int pageIndex = authorIndex + 1; pageIndex < arguments.length; pageIndex++) {
int pageNumber = InputCleaningHelper.parsePageNumber(arguments[j]); int pageNumber = InputCleaningHelper.parsePageNumber(arguments[pageIndex]);
if (pageNumber > 0) { if (pageNumber <= 0) {
int authorEnd = j - 1; continue;
}
StringBuilder builder = new StringBuilder(author); StringBuilder builder = new StringBuilder(author);
for (int k = i + 1; k <= authorEnd; k++) { for (int authorPartCounter = authorIndex + 1; authorPartCounter < pageIndex; authorPartCounter++) {
builder.append(" ").append(arguments[k]); builder.append(" ").append(arguments[authorPartCounter]);
} }
AuthorBookIndex.printBooks(sender, selectPublic, command, pageNumber, builder.toString()); AuthorBookIndex.printBooks(sender, selectPublic, command, pageNumber, builder.toString());
return true; return true;
} }
} }
}
return false; return false;
} }

View File

@@ -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();
}
} }