Retains display name of saved books
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good
This commit is contained in:
@@ -26,7 +26,9 @@ public enum StaticMessage {
|
|||||||
DEBUG_AES_INVALID_PADDING_CIPHER("Invalid AES padding during Cipher generation"),
|
DEBUG_AES_INVALID_PADDING_CIPHER("Invalid AES padding during Cipher generation"),
|
||||||
DEBUG_AES_INVALID_KEY_SPECIFICATION("Invalid AES key specification"),
|
DEBUG_AES_INVALID_KEY_SPECIFICATION("Invalid AES key specification"),
|
||||||
WARNING_USER_BOOK_MIGRATION_IMPOSSIBLE("Unable to migrate player book directory for player {player}"),
|
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;
|
private final @NotNull String messageString;
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package net.knarcraft.bookswithoutborders.utility;
|
package net.knarcraft.bookswithoutborders.utility;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.StaticMessage;
|
||||||
import net.knarcraft.bookswithoutborders.config.translation.Formatting;
|
import net.knarcraft.bookswithoutborders.config.translation.Formatting;
|
||||||
import net.knarcraft.bookswithoutborders.encryption.AESConfiguration;
|
import net.knarcraft.bookswithoutborders.encryption.AESConfiguration;
|
||||||
import net.knarcraft.bookswithoutborders.encryption.EncryptionStyle;
|
import net.knarcraft.bookswithoutborders.encryption.EncryptionStyle;
|
||||||
@@ -30,6 +31,24 @@ public final class BookToFromTextHelper {
|
|||||||
private BookToFromTextHelper() {
|
private BookToFromTextHelper() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a book from a file
|
||||||
|
*
|
||||||
|
* @param file <p>The file to load</p>
|
||||||
|
* @param bookMetadata <p>The book metadata to use for saving the book</p>
|
||||||
|
* @return <p>The book metadata of the loaded book</p>
|
||||||
|
*/
|
||||||
|
@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
|
* Saves a book's contents to a .yml file
|
||||||
*
|
*
|
||||||
@@ -81,53 +100,6 @@ public final class BookToFromTextHelper {
|
|||||||
bookYml.save(path + fileName + ".yml");
|
bookYml.save(path + fileName + ".yml");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a file configuration containing a book's information
|
|
||||||
*
|
|
||||||
* @param bookMetadata <p>Metadata about the book to save</p>
|
|
||||||
*/
|
|
||||||
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 <p>The file to load</p>
|
|
||||||
* @param bookMetadata <p>The book metadata to use for saving the book</p>
|
|
||||||
* @return <p>The book metadata of the loaded book</p>
|
|
||||||
*/
|
|
||||||
@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
|
* Loads a book from a .yml file
|
||||||
*
|
*
|
||||||
@@ -192,6 +164,38 @@ public final class BookToFromTextHelper {
|
|||||||
return meta;
|
return meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a file configuration containing a book's information
|
||||||
|
*
|
||||||
|
* @param bookMetadata <p>Metadata about the book to save</p>
|
||||||
|
*/
|
||||||
|
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
|
* Loads a book from a .yml file
|
||||||
*
|
*
|
||||||
@@ -212,7 +216,8 @@ public final class BookToFromTextHelper {
|
|||||||
stringFormatter.getUnFormattedColoredMessage(Formatting.NEUTRAL_UNKNOWN_AUTHOR))));
|
stringFormatter.getUnFormattedColoredMessage(Formatting.NEUTRAL_UNKNOWN_AUTHOR))));
|
||||||
bookMetadata.setPages(bookYml.getStringList("Pages"));
|
bookMetadata.setPages(bookYml.getStringList("Pages"));
|
||||||
bookMetadata.setLore(bookYml.getStringList("Lore"));
|
bookMetadata.setLore(bookYml.getStringList("Lore"));
|
||||||
} catch (IllegalArgumentException e) {
|
bookMetadata.setDisplayName(bookYml.getString("DisplayName"));
|
||||||
|
} catch (IllegalArgumentException exception) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return bookMetadata;
|
return bookMetadata;
|
||||||
@@ -221,27 +226,26 @@ public final class BookToFromTextHelper {
|
|||||||
/**
|
/**
|
||||||
* Loads a book from a text file
|
* Loads a book from a text file
|
||||||
*
|
*
|
||||||
* @param fileName <p>The name of the file to load. Used to create author and title</p>
|
|
||||||
* @param file <p>The file to load</p>
|
* @param file <p>The file to load</p>
|
||||||
* @param bookMetadata <p>Metadata which will be altered with the book's contents</p>
|
* @param bookMetadata <p>Metadata which will be altered with the book's contents</p>
|
||||||
* @return <p>Metadata for the loaded book</p>
|
* @return <p>Metadata for the loaded book</p>
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@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
|
//Get title and author from the file name
|
||||||
String title = BookFileHelper.getBookTitleFromPath(fileName);
|
String title = BookFileHelper.getBookTitleFromPath(file.getName());
|
||||||
String author = BookFileHelper.getBookAuthorFromPath(fileName);
|
String author = BookFileHelper.getBookAuthorFromPath(file.getName());
|
||||||
|
|
||||||
//Read the .txt file
|
//Read the .txt file
|
||||||
List<String> rawPages;
|
List<String> rawPages;
|
||||||
try {
|
try {
|
||||||
rawPages = readTextFile(file);
|
rawPages = readTextFile(file);
|
||||||
if (rawPages == null) {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
} catch (IOException exception) {
|
} catch (IOException exception) {
|
||||||
BooksWithoutBorders.log(Level.SEVERE, "Unable to read text file");
|
BooksWithoutBorders.log(Level.SEVERE, StaticMessage.EXCEPTION_BOOK_LOAD_FAILED.toString());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user