From 2203037b00eeb8cd762f2ecf265f3629451c5c6d Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sun, 3 Aug 2025 02:40:13 +0200 Subject: [PATCH] Adds a warning when trying to save books containing the title author separator in title or author --- .../bookswithoutborders/command/CommandSave.java | 8 +++++++- .../bookswithoutborders/utility/BookHelper.java | 14 +++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java index 9487395..400ca62 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java @@ -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); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java index 84292b6..054153d 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java @@ -121,13 +121,17 @@ public final class BookHelper { * @param book

The book to get the file of

* @param player

The player trying to do something with the book

* @return

The book file

+ * @throws IllegalArgumentException

If the book title or author contains the title author separator

*/ @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);