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 playerThe player trying to do something with the book
* @returnThe book file
+ * @throws IllegalArgumentExceptionIf 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);