diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java index 3b63887..2005730 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java @@ -8,6 +8,7 @@ import net.knarcraft.bookswithoutborders.utility.BookFileHelper; import net.knarcraft.bookswithoutborders.utility.BookHelper; import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; +import net.md_5.bungee.api.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -87,6 +88,11 @@ public class CommandSave implements TabExecutor { String savePath = BookHelper.getBookDirectoryPathString( saveToPublicFolder ? BookDirectory.PUBLIC : BookDirectory.PLAYER, player); + if (savePath == null) { + BooksWithoutBorders.sendErrorMessage(player, "Saving Failed! Unable to find the save path!"); + return; + } + //Generate book filename String fileName = BookHelper.getBookFile(book, player, saveToPublicFolder); @@ -142,7 +148,7 @@ public class CommandSave implements TabExecutor { //Update the relevant book list BooksWithoutBorders.updateBooks(player, saveToPublicFolder); - BooksWithoutBorders.sendSuccessMessage(player, "Book Saved as \"" + fileName + "\""); + BooksWithoutBorders.sendSuccessMessage(player, "Book Saved as \"" + fileName + ChatColor.RESET + "\""); } catch (IOException exception) { BooksWithoutBorders.getInstance().getLogger().log(Level.SEVERE, "Unable to save book"); } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFileHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFileHelper.java index be19e1c..dc15f50 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFileHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFileHelper.java @@ -7,7 +7,6 @@ import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.HoverEvent; -import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.hover.content.Text; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -189,18 +188,16 @@ public final class BookFileHelper { int startIndex = (page - 1) * booksPerPage; for (int bookIndex = startIndex; bookIndex < Math.min(startIndex + booksPerPage, availableBooks.size()); bookIndex++) { ComponentBuilder bookComponent = new ComponentBuilder(); - TextComponent indexComponent = new TextComponent("[" + (bookIndex + 1) + "]"); - indexComponent.setColor(ChatColor.GOLD); - bookComponent.append(indexComponent).event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, - "/" + command + " " + (bookIndex + 1))).event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, + bookComponent.append("[" + (bookIndex + 1) + "]").color(ChatColor.GOLD).event( + new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + command + " " + + (bookIndex + 1))).event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("Select book by index"))); bookComponent.append(" ", ComponentBuilder.FormatRetention.NONE); - TextComponent bookNameComponent = new TextComponent(getNiceName(availableBooks.get(bookIndex))); - bookNameComponent.setColor(ChatColor.WHITE); - bookComponent.append(bookNameComponent).event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, - "/" + command + " " + availableBooks.get(bookIndex))).event( + bookComponent.append(getNiceName(availableBooks.get(bookIndex))).color(ChatColor.WHITE).event( + new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + command + " " + + availableBooks.get(bookIndex))).event( new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("Select book by path"))); sender.spigot().sendMessage(bookComponent.create()); } @@ -230,7 +227,10 @@ public final class BookFileHelper { */ @NotNull private static String getNiceName(@NotNull String bookPath) { - return bookPath.substring(0, bookPath.length() - 4).replace(",", " by ").replace("_", " "); + bookPath = ChatColor.translateAlternateColorCodes('&', bookPath.substring(0, bookPath.length() - 4)) + .replace("_", ""); + String[] parts = bookPath.split(","); + return parts[0] + ChatColor.RESET + " by " + parts[1] + ChatColor.RESET; } /** diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java index af7cf1f..2635608 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java @@ -8,6 +8,8 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.util.UUID; @@ -31,7 +33,8 @@ public final class BookHelper { * @param author

The author string

* @return

The author string, converted if it was a UUID

*/ - public static String authorFromUUID(String author) { + @NotNull + public static String authorFromUUID(@NotNull String author) { try { UUID authorID = UUID.fromString(author); Player player = Bukkit.getPlayer(authorID); @@ -50,7 +53,7 @@ public final class BookHelper { * @param sender

The command sender trying to get the directory

* @return

The path of the directory, or null if not possible to get

*/ - public static File getBookDirectoryPath(BookDirectory bookDirectory, CommandSender sender) { + public static File getBookDirectoryPath(@NotNull BookDirectory bookDirectory, @NotNull CommandSender sender) { String bookFolderString = getBookDirectoryPathString(bookDirectory, sender); if (bookFolderString == null) { return null; @@ -65,7 +68,8 @@ public final class BookHelper { * @param sender

The command sender trying to get the directory

* @return

The path of the directory, or null if not possible to get

*/ - public static String getBookDirectoryPathString(BookDirectory bookDirectory, CommandSender sender) { + @Nullable + public static String getBookDirectoryPathString(@NotNull BookDirectory bookDirectory, @NotNull CommandSender sender) { String folder = null; String bookFolder = BooksWithoutBordersConfig.getBookFolder(); if (bookDirectory == BookDirectory.PUBLIC) { @@ -81,7 +85,7 @@ public final class BookHelper { * * @param bookItem

The book item to increase the generation of

*/ - public static void increaseGeneration(ItemStack bookItem) { + public static void increaseGeneration(@NotNull ItemStack bookItem) { BookMeta bookMeta = (BookMeta) bookItem.getItemMeta(); if (BooksWithoutBordersConfig.changeGenerationOnCopy() && bookMeta != null) { bookMeta.setGeneration(BookHelper.getNextGeneration(bookMeta.getGeneration())); @@ -98,7 +102,8 @@ public final class BookHelper { * @param currentGeneration

The current generation of the book

* @return

The next generation of the book

*/ - public static BookMeta.Generation getNextGeneration(BookMeta.Generation currentGeneration) { + @NotNull + public static BookMeta.Generation getNextGeneration(@Nullable BookMeta.Generation currentGeneration) { if (currentGeneration == null) { return BookMeta.Generation.COPY_OF_ORIGINAL; } @@ -116,7 +121,8 @@ public final class BookHelper { * @param player

The player trying to do something with the book

* @return

The book file

*/ - public static String getBookFile(BookMeta book, Player player, boolean isPublic) { + @NotNull + public static String getBookFile(@NotNull BookMeta book, @NotNull Player player, boolean isPublic) { String titleAuthorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator(); String bookName; if (book.hasTitle()) { @@ -145,7 +151,7 @@ public final class BookHelper { * @param book

The book to check

* @return

True if the player is not the book's author

*/ - public static boolean isNotAuthor(Player player, BookMeta book) { + public static boolean isNotAuthor(@NotNull Player player, @NotNull BookMeta book) { if (isAuthor(player.getName(), book.getAuthor())) { return false; } else { @@ -162,7 +168,7 @@ public final class BookHelper { * @param author

The author to check

* @return

True if the player is the author

*/ - private static boolean isAuthor(String playerName, String author) { + private static boolean isAuthor(@NotNull String playerName, @Nullable String author) { playerName = InputCleaningHelper.cleanString(playerName); return author != null && playerName.equalsIgnoreCase(InputCleaningHelper.cleanString(author)); } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoader.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoader.java index 9c4c16d..f50cafb 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoader.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoader.java @@ -9,6 +9,8 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.util.ArrayList; @@ -31,7 +33,9 @@ public final class BookLoader { * @param directory

The directory to save the book in

* @return

The loaded book

*/ - public static ItemStack loadBook(CommandSender sender, String fileName, String isSigned, String directory) { + @Nullable + public static ItemStack loadBook(@NotNull CommandSender sender, @NotNull String fileName, @NotNull String isSigned, + @NotNull String directory) { return loadBook(sender, fileName, isSigned, directory, 1); } @@ -45,7 +49,9 @@ public final class BookLoader { * @param numCopies

The number of copies to load

* @return

The loaded book

*/ - public static ItemStack loadBook(CommandSender sender, String fileName, String isSigned, String directory, int numCopies) { + @Nullable + public static ItemStack loadBook(@NotNull CommandSender sender, @NotNull String fileName, @NotNull String isSigned, + @NotNull String directory, int numCopies) { BookDirectory bookDirectory = BookDirectory.getFromString(directory); //Find the filename if a book index is given @@ -96,8 +102,13 @@ public final class BookLoader { book = new ItemStack(Material.WRITABLE_BOOK); } + if (bookMetadata == null) { + BooksWithoutBorders.sendErrorMessage(sender, "Unable to create blank book metadata!"); + return null; + } + //Load the book from the given file - BookToFromTextHelper.bookFromFile(file, bookMetadata); + bookMetadata = BookToFromTextHelper.bookFromFile(file, bookMetadata); if (bookMetadata == null) { BooksWithoutBorders.sendErrorMessage(sender, "File was blank!!"); return null; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java index 3f98c0c..ff7150f 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java @@ -6,6 +6,8 @@ import net.knarcraft.knarlib.util.FileHelper; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.inventory.meta.BookMeta; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.BufferedReader; import java.io.File; @@ -37,7 +39,7 @@ public final class BookToFromTextHelper { * @param bookMetadata

Metadata about the book to save

* @throws IOException

If unable to save the book

*/ - public static void bookToYml(String path, String fileName, BookMeta bookMetadata) throws IOException { + public static void bookToYml(@NotNull String path, @NotNull String fileName, @NotNull BookMeta bookMetadata) throws IOException { FileConfiguration bookYml = YamlConfiguration.loadConfiguration(new File(path, "blank")); if (bookMetadata.hasTitle()) { @@ -58,7 +60,7 @@ public final class BookToFromTextHelper { bookYml.set("Lore", bookMetadata.getLore()); } - bookYml.save(path + fileName + ".yml"); + bookYml.save(path + fileName.replace("§", "&") + ".yml"); } /** @@ -68,7 +70,8 @@ public final class BookToFromTextHelper { * @param bookMetadata

The book metadata to use for saving the book

* @return

The book metadata of the loaded book

*/ - public static BookMeta bookFromFile(File file, BookMeta bookMetadata) { + @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")) { @@ -86,8 +89,8 @@ public final class BookToFromTextHelper { * @param bookMetadata

Metadata about the book to save

* @throws IOException

If unable to save the book

*/ - public static void bookToTXT(String folderPath, String fileName, BookMeta bookMetadata) throws IOException { - FileWriter fileWriter = new FileWriter(folderPath + fileName + ".txt", StandardCharsets.UTF_8); + public static void bookToTXT(@NotNull String folderPath, @NotNull String fileName, @NotNull BookMeta bookMetadata) throws IOException { + FileWriter fileWriter = new FileWriter(folderPath + fileName.replace("§", "&") + ".txt", StandardCharsets.UTF_8); PrintWriter printWriter = new PrintWriter(fileWriter); List pages = bookMetadata.getPages(); @@ -112,7 +115,8 @@ public final class BookToFromTextHelper { * @param bookMetadata

Metadata which will be altered with the book's contents

* @return

Metadata for the loaded book

*/ - private static BookMeta bookFromYml(File file, BookMeta bookMetadata) { + @Nullable + private static BookMeta bookFromYml(@NotNull File file, @NotNull BookMeta bookMetadata) { try { FileConfiguration bookYml = YamlConfiguration.loadConfiguration(file); @@ -135,7 +139,8 @@ public final class BookToFromTextHelper { * @param bookMetadata

Metadata which will be altered with the book's contents

* @return

Metadata for the loaded book

*/ - private static BookMeta bookFromTXT(String fileName, File file, BookMeta bookMetadata) { + @Nullable + private static BookMeta bookFromTXT(@NotNull String fileName, @NotNull File file, @NotNull BookMeta bookMetadata) { String author; String title; String titleAuthorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator(); @@ -188,7 +193,8 @@ public final class BookToFromTextHelper { * @return

A string list where each string is the text on one page

* @throws IOException

If unable to read from the file

*/ - private static List readTextFile(File file) throws IOException { + @Nullable + private static List readTextFile(@NotNull File file) throws IOException { List rawPages = new ArrayList<>(); BufferedReader bufferedReader = FileHelper.getBufferedReaderFromInputStream(new FileInputStream(file));