Adds a warning when trying to save books containing the title author separator in title or author
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good

This commit is contained in:
2025-08-03 02:40:13 +02:00
parent 6b44ada84a
commit 2203037b00
2 changed files with 20 additions and 2 deletions

View File

@@ -94,7 +94,13 @@ public class CommandSave implements TabExecutor {
}
//Generate book filename
String fileName = BookHelper.getBookFile(book, player, saveToPublicFolder);
String fileName;
try {
fileName = BookHelper.getBookFile(book, player, saveToPublicFolder);
} catch (IllegalArgumentException exception) {
BooksWithoutBorders.sendErrorMessage(player, exception.getMessage());
return;
}
//Make sure the used folders exist
File file = new File(savePath);

View File

@@ -121,13 +121,17 @@ public final class BookHelper {
* @param book <p>The book to get the file of</p>
* @param player <p>The player trying to do something with the book</p>
* @return <p>The book file</p>
* @throws IllegalArgumentException <p>If the book title or author contains the title author separator</p>
*/
@NotNull
public static String getBookFile(@NotNull BookMeta book, @NotNull Player player, boolean isPublic) {
public static String getBookFile(@NotNull BookMeta book, @NotNull Player player, boolean isPublic) throws IllegalArgumentException {
String titleAuthorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
String bookName;
if (book.hasTitle()) {
bookName = book.getTitle();
if (bookName == null) {
bookName = "Untitled";
}
} else {
bookName = "Untitled";
}
@@ -140,6 +144,14 @@ public final class BookHelper {
authorName = player.getName();
} else {
authorName = book.getAuthor();
if (authorName == null) {
authorName = "Unknown";
}
}
if (bookName.contains(titleAuthorSeparator) || authorName.contains(titleAuthorSeparator)) {
throw new IllegalArgumentException("The author or title contains the title author separator. Saving this " +
"book would lead to unexpected problems.");
}
return fixName(cleanString(bookName + titleAuthorSeparator + authorName), false);