From 2383dfd2ef79affc2b6bdfe61ec9d71e6959811f Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 7 Nov 2022 09:23:25 +0100 Subject: [PATCH] Renames FileHelper to BookFileHelper Makes sure books are saved and read as UTF-8 --- .../bookswithoutborders/BooksWithoutBorders.java | 8 ++++---- .../bookswithoutborders/command/CommandDelete.java | 6 +++--- .../bookswithoutborders/command/CommandGive.java | 4 ++-- .../bookswithoutborders/command/CommandLoad.java | 4 ++-- .../bookswithoutborders/command/CommandSave.java | 4 ++-- .../listener/SignEventListener.java | 6 +++--- .../utility/{FileHelper.java => BookFileHelper.java} | 8 ++++---- .../bookswithoutborders/utility/BookLoader.java | 4 ++-- .../utility/BookToFromTextHelper.java | 8 +++++--- .../{FileHelperTest.java => BookFileHelperTest.java} | 10 +++++----- 10 files changed, 32 insertions(+), 30 deletions(-) rename src/main/java/net/knarcraft/bookswithoutborders/utility/{FileHelper.java => BookFileHelper.java} (97%) rename src/test/java/net/knarcraft/bookswithoutborders/util/{FileHelperTest.java => BookFileHelperTest.java} (60%) diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index d8c6243..3ee89d9 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -26,7 +26,7 @@ import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.knarcraft.bookswithoutborders.listener.BookEventListener; import net.knarcraft.bookswithoutborders.listener.PlayerEventListener; import net.knarcraft.bookswithoutborders.listener.SignEventListener; -import net.knarcraft.bookswithoutborders.utility.FileHelper; +import net.knarcraft.bookswithoutborders.utility.BookFileHelper; import net.knarcraft.knarlib.KnarLib; import net.knarcraft.knarlib.util.UpdateChecker; import org.bukkit.command.CommandExecutor; @@ -85,7 +85,7 @@ public class BooksWithoutBorders extends JavaPlugin { } else if (sender instanceof Player player) { UUID playerUUID = player.getUniqueId(); if (!playerBooksList.containsKey(playerUUID)) { - List newFiles = FileHelper.listFiles(sender, false); + List newFiles = BookFileHelper.listFiles(sender, false); playerBooksList.put(playerUUID, newFiles); } return playerBooksList.get(playerUUID); @@ -101,7 +101,7 @@ public class BooksWithoutBorders extends JavaPlugin { * @param updatePublic

Whether to update public books

*/ public static void updateBooks(CommandSender sender, boolean updatePublic) { - List newFiles = FileHelper.listFiles(sender, updatePublic); + List newFiles = BookFileHelper.listFiles(sender, updatePublic); if (updatePublic) { publicBooksList = newFiles; } else if (sender instanceof Player player) { @@ -125,7 +125,7 @@ public class BooksWithoutBorders extends JavaPlugin { consoleSender = this.getServer().getConsoleSender(); playerBooksList = new HashMap<>(); BooksWithoutBordersConfig.initialize(this); - publicBooksList = FileHelper.listFiles(consoleSender, true); + publicBooksList = BookFileHelper.listFiles(consoleSender, true); PluginManager pluginManager = this.getServer().getPluginManager(); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java index 371e402..f95afbb 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java @@ -2,8 +2,8 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.state.BookDirectory; +import net.knarcraft.bookswithoutborders.utility.BookFileHelper; import net.knarcraft.bookswithoutborders.utility.BookHelper; -import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.knarlib.util.TabCompletionHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -41,7 +41,7 @@ public class CommandDelete implements TabExecutor { boolean deleteBook(CommandSender sender, String[] args, boolean deletePublic) { //List deletable files if (args.length == 0) { - FileHelper.printBooks(sender, deletePublic); + BookFileHelper.printBooks(sender, deletePublic); return true; } //Delete the file @@ -82,7 +82,7 @@ public class CommandDelete implements TabExecutor { //Get the file to be deleted String bookDirectory = BookHelper.getBookDirectoryPathString( isPublic ? BookDirectory.PUBLIC : BookDirectory.PLAYER, sender); - File file = FileHelper.getBookFile(bookDirectory + fileName); + File file = BookFileHelper.getBookFile(bookDirectory + fileName); //Send message if no such file could be found if (file == null) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java index bf02400..3a2062c 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java @@ -1,8 +1,8 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.utility.BookFileHelper; import net.knarcraft.bookswithoutborders.utility.BookLoader; -import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper; import net.knarcraft.knarlib.util.TabCompletionHelper; @@ -50,7 +50,7 @@ public class CommandGive implements TabExecutor { } if (args.length == 0) { - FileHelper.printBooks(sender, givePublic); + BookFileHelper.printBooks(sender, givePublic); return true; } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java index fb6919b..04be5ee 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java @@ -1,8 +1,8 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.utility.BookFileHelper; import net.knarcraft.bookswithoutborders.utility.BookLoader; -import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper; import net.knarcraft.knarlib.util.TabCompletionHelper; @@ -50,7 +50,7 @@ public class CommandLoad implements TabExecutor { //Show books available to the player if (argumentCount == 0) { - FileHelper.printBooks(sender, loadPublic); + BookFileHelper.printBooks(sender, loadPublic); return true; } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java index 7a527d3..9205d8f 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java @@ -4,9 +4,9 @@ import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.knarcraft.bookswithoutborders.state.BookDirectory; import net.knarcraft.bookswithoutborders.state.ItemSlot; +import net.knarcraft.bookswithoutborders.utility.BookFileHelper; import net.knarcraft.bookswithoutborders.utility.BookHelper; import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper; -import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -104,7 +104,7 @@ public class CommandSave implements TabExecutor { } //Find any duplicates of the book - int foundDuplicates = FileHelper.findDuplicates(foundFiles, fileName); + int foundDuplicates = BookFileHelper.findDuplicates(foundFiles, fileName); //Deal with duplicates if (foundDuplicates > 0) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/listener/SignEventListener.java b/src/main/java/net/knarcraft/bookswithoutborders/listener/SignEventListener.java index 2f30cf9..c2385f6 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/listener/SignEventListener.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/listener/SignEventListener.java @@ -3,9 +3,9 @@ package net.knarcraft.bookswithoutborders.listener; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.knarcraft.bookswithoutborders.state.EncryptionStyle; +import net.knarcraft.bookswithoutborders.utility.BookFileHelper; import net.knarcraft.bookswithoutborders.utility.BookLoader; import net.knarcraft.bookswithoutborders.utility.EncryptionHelper; -import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import net.md_5.bungee.api.ChatColor; import org.bukkit.Material; @@ -27,7 +27,7 @@ import java.io.File; import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder; import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash; -import static net.knarcraft.bookswithoutborders.utility.FileHelper.isBookListIndex; +import static net.knarcraft.bookswithoutborders.utility.BookFileHelper.isBookListIndex; /** * A listener for relevant sign events such as clicking a decryption sign @@ -195,7 +195,7 @@ public class SignEventListener implements Listener { //Tests if a full file name has been supplied and points to an actual file String signFile = getBookFolder() + lines[2] + lines[3]; - if (FileHelper.bookFileExists(signFile)) { + if (BookFileHelper.bookFileExists(signFile)) { markGiveSignValidity(event, true); return; } else { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFileHelper.java similarity index 97% rename from src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java rename to src/main/java/net/knarcraft/bookswithoutborders/utility/BookFileHelper.java index 8dd0f70..79a7254 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFileHelper.java @@ -17,9 +17,9 @@ import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig /** * Helper class for dealing with files */ -public final class FileHelper { +public final class BookFileHelper { - private FileHelper() { + private BookFileHelper() { } /** @@ -98,7 +98,7 @@ public final class FileHelper { if (file == null) { return new ArrayList<>(); } - return FileHelper.listFiles(sender, file); + return BookFileHelper.listFiles(sender, file); } /** @@ -109,7 +109,7 @@ public final class FileHelper { */ public static void printBooks(CommandSender sender, boolean listPublic) { List availableBooks = BooksWithoutBorders.getAvailableBooks(sender, listPublic); - FileHelper.printFiles(sender, availableBooks); + BookFileHelper.printFiles(sender, availableBooks); } /** diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoader.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoader.java index 93f76ff..4a17c56 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoader.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoader.java @@ -120,9 +120,9 @@ public final class BookLoader { String slash = BooksWithoutBordersConfig.getSlash(); String bookFolder = BooksWithoutBordersConfig.getBookFolder(); if (bookDirectory == BookDirectory.ENCRYPTED) { - file = FileHelper.getBookFile(bookFolder + "Encrypted" + slash + directory + slash + fileName); + file = BookFileHelper.getBookFile(bookFolder + "Encrypted" + slash + directory + slash + fileName); } else { - file = FileHelper.getBookFile(BookHelper.getBookDirectoryPathString(bookDirectory, sender) + fileName); + file = BookFileHelper.getBookFile(BookHelper.getBookDirectoryPathString(bookDirectory, sender) + fileName); } if (file == null || !file.isFile()) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java index 5eb3069..f0dcc01 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java @@ -1,16 +1,18 @@ package net.knarcraft.bookswithoutborders.utility; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; +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 java.io.BufferedReader; import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -83,7 +85,7 @@ public final class BookToFromTextHelper { * @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"); + FileWriter fileWriter = new FileWriter(folderPath + fileName + ".txt", StandardCharsets.UTF_8); PrintWriter printWriter = new PrintWriter(fileWriter); List pages = bookMetadata.getPages(); @@ -186,7 +188,7 @@ public final class BookToFromTextHelper { */ private static List readTextFile(File file) throws IOException { List rawPages = new ArrayList<>(); - BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); + BufferedReader bufferedReader = FileHelper.getBufferedReaderFromInputStream(new FileInputStream(file)); //Use the first line to decide if we are loading a book, or creating a new book String firstLine = bufferedReader.readLine(); diff --git a/src/test/java/net/knarcraft/bookswithoutborders/util/FileHelperTest.java b/src/test/java/net/knarcraft/bookswithoutborders/util/BookFileHelperTest.java similarity index 60% rename from src/test/java/net/knarcraft/bookswithoutborders/util/FileHelperTest.java rename to src/test/java/net/knarcraft/bookswithoutborders/util/BookFileHelperTest.java index decd05f..6a30cd6 100644 --- a/src/test/java/net/knarcraft/bookswithoutborders/util/FileHelperTest.java +++ b/src/test/java/net/knarcraft/bookswithoutborders/util/BookFileHelperTest.java @@ -1,13 +1,13 @@ package net.knarcraft.bookswithoutborders.util; -import net.knarcraft.bookswithoutborders.utility.FileHelper; +import net.knarcraft.bookswithoutborders.utility.BookFileHelper; import org.junit.Test; import java.io.File; import static org.junit.Assert.assertEquals; -public class FileHelperTest { +public class BookFileHelperTest { @Test public void findDuplicatesTest() { @@ -17,9 +17,9 @@ public class FileHelperTest { files[2] = new File("test/asd/(3)book+)Crab.yml"); files[3] = new File("test/asd/(2)book+)Crab.yml"); files[4] = new File("test/asd/(1)book+)Crab.yml"); - assertEquals(4, FileHelper.findDuplicates(files, "book+)Crab.yml")); - assertEquals(1, FileHelper.findDuplicates(files, "book+)Fish.yml")); - assertEquals(0, FileHelper.findDuplicates(files, "book+)Horse.yml")); + assertEquals(4, BookFileHelper.findDuplicates(files, "book+)Crab.yml")); + assertEquals(1, BookFileHelper.findDuplicates(files, "book+)Fish.yml")); + assertEquals(0, BookFileHelper.findDuplicates(files, "book+)Horse.yml")); } }