From 8c61d801e2cbc2e043522ffb351bb61372fa283a Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 21 Aug 2025 14:37:52 +0200 Subject: [PATCH] Retains display name of saved books --- .../config/StaticMessage.java | 4 +- .../utility/BookToFromTextHelper.java | 112 +++++++++--------- 2 files changed, 61 insertions(+), 55 deletions(-) diff --git a/src/main/java/net/knarcraft/bookswithoutborders/config/StaticMessage.java b/src/main/java/net/knarcraft/bookswithoutborders/config/StaticMessage.java index a7a42f3..6861faf 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/config/StaticMessage.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/config/StaticMessage.java @@ -26,7 +26,9 @@ public enum StaticMessage { DEBUG_AES_INVALID_PADDING_CIPHER("Invalid AES padding during Cipher generation"), DEBUG_AES_INVALID_KEY_SPECIFICATION("Invalid AES key specification"), WARNING_USER_BOOK_MIGRATION_IMPOSSIBLE("Unable to migrate player book directory for player {player}"), - ; + EXCEPTION_BOOK_LOAD_NULL("Text file's first line was null"), + EXCEPTION_BOOK_LOAD_FAILED("Unable to read text file"), + EXCEPTION_BOOK_UNKNOWN_EXTENSION("Trying to load a book file with an unrecognized extension"); private final @NotNull String messageString; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java index de4770a..e86388b 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java @@ -1,6 +1,7 @@ package net.knarcraft.bookswithoutborders.utility; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.config.StaticMessage; import net.knarcraft.bookswithoutborders.config.translation.Formatting; import net.knarcraft.bookswithoutborders.encryption.AESConfiguration; import net.knarcraft.bookswithoutborders.encryption.EncryptionStyle; @@ -30,6 +31,24 @@ public final class BookToFromTextHelper { private BookToFromTextHelper() { } + /** + * Loads a book from a file + * + * @param file

The file to load

+ * @param bookMetadata

The book metadata to use for saving the book

+ * @return

The book metadata of the loaded book

+ */ + @Nullable + public static BookMeta bookFromFile(@NotNull File file, @NotNull BookMeta bookMetadata) { + if (file.getName().endsWith(".txt")) { + return bookFromTXT(file, bookMetadata); + } else if (file.getName().endsWith(".yml")) { + return bookFromYml(file, bookMetadata); + } else { + throw new IllegalArgumentException(StaticMessage.EXCEPTION_BOOK_UNKNOWN_EXTENSION.toString()); + } + } + /** * Saves a book's contents to a .yml file * @@ -81,53 +100,6 @@ public final class BookToFromTextHelper { bookYml.save(path + fileName + ".yml"); } - /** - * Gets a file configuration containing a book's information - * - * @param bookMetadata

Metadata about the book to save

- */ - private static FileConfiguration getBookConfiguration(@NotNull BookMeta bookMetadata) { - FileConfiguration bookYml = YamlConfiguration.loadConfiguration(new File("", "blank")); - - if (bookMetadata.hasTitle()) { - bookYml.set("Title", bookMetadata.getTitle()); - } - if (bookMetadata.hasAuthor()) { - bookYml.set("Author", bookMetadata.getAuthor()); - } - BookMeta.Generation generation = bookMetadata.getGeneration(); - if (generation == null) { - generation = BookMeta.Generation.ORIGINAL; - } - bookYml.set("Generation", generation.name()); - if (bookMetadata.hasPages()) { - bookYml.set("Pages", bookMetadata.getPages()); - } - if (bookMetadata.hasLore()) { - bookYml.set("Lore", bookMetadata.getLore()); - } - - return bookYml; - } - - /** - * Loads a book from a file - * - * @param file

The file to load

- * @param bookMetadata

The book metadata to use for saving the book

- * @return

The book metadata of the loaded book

- */ - @Nullable - public static BookMeta bookFromFile(@NotNull File file, @NotNull BookMeta bookMetadata) { - if (file.getName().endsWith(".txt")) { - return bookFromTXT(file.getName(), file, bookMetadata); - } else if (file.getName().endsWith(".yml")) { - return bookFromYml(file, bookMetadata); - } else { - throw new IllegalArgumentException("Trying to load a book file with an unrecognized extension"); - } - } - /** * Loads a book from a .yml file * @@ -192,6 +164,38 @@ public final class BookToFromTextHelper { return meta; } + /** + * Gets a file configuration containing a book's information + * + * @param bookMetadata

Metadata about the book to save

+ */ + private static FileConfiguration getBookConfiguration(@NotNull BookMeta bookMetadata) { + FileConfiguration bookYml = YamlConfiguration.loadConfiguration(new File("", "blank")); + + if (bookMetadata.hasTitle()) { + bookYml.set("Title", bookMetadata.getTitle()); + } + if (bookMetadata.hasAuthor()) { + bookYml.set("Author", bookMetadata.getAuthor()); + } + BookMeta.Generation generation = bookMetadata.getGeneration(); + if (generation == null) { + generation = BookMeta.Generation.ORIGINAL; + } + bookYml.set("Generation", generation.name()); + if (bookMetadata.hasPages()) { + bookYml.set("Pages", bookMetadata.getPages()); + } + if (bookMetadata.hasLore()) { + bookYml.set("Lore", bookMetadata.getLore()); + } + if (bookMetadata.hasDisplayName()) { + bookYml.set("DisplayName", bookMetadata.getDisplayName()); + } + + return bookYml; + } + /** * Loads a book from a .yml file * @@ -212,7 +216,8 @@ public final class BookToFromTextHelper { stringFormatter.getUnFormattedColoredMessage(Formatting.NEUTRAL_UNKNOWN_AUTHOR)))); bookMetadata.setPages(bookYml.getStringList("Pages")); bookMetadata.setLore(bookYml.getStringList("Lore")); - } catch (IllegalArgumentException e) { + bookMetadata.setDisplayName(bookYml.getString("DisplayName")); + } catch (IllegalArgumentException exception) { return null; } return bookMetadata; @@ -221,27 +226,26 @@ public final class BookToFromTextHelper { /** * Loads a book from a text file * - * @param fileName

The name of the file to load. Used to create author and title

* @param file

The file to load

* @param bookMetadata

Metadata which will be altered with the book's contents

* @return

Metadata for the loaded book

*/ @Nullable - private static BookMeta bookFromTXT(@NotNull String fileName, @NotNull File file, @NotNull BookMeta bookMetadata) { + private static BookMeta bookFromTXT(@NotNull File file, @NotNull BookMeta bookMetadata) { //Get title and author from the file name - String title = BookFileHelper.getBookTitleFromPath(fileName); - String author = BookFileHelper.getBookAuthorFromPath(fileName); + String title = BookFileHelper.getBookTitleFromPath(file.getName()); + String author = BookFileHelper.getBookAuthorFromPath(file.getName()); //Read the .txt file List rawPages; try { rawPages = readTextFile(file); if (rawPages == null) { - BooksWithoutBorders.log(Level.SEVERE, "Text file's first line was null"); + BooksWithoutBorders.log(Level.SEVERE, StaticMessage.EXCEPTION_BOOK_LOAD_NULL.toString()); return null; } } catch (IOException exception) { - BooksWithoutBorders.log(Level.SEVERE, "Unable to read text file"); + BooksWithoutBorders.log(Level.SEVERE, StaticMessage.EXCEPTION_BOOK_LOAD_FAILED.toString()); return null; }