Renames FileHelper to BookFileHelper

Makes sure books are saved and read as UTF-8
This commit is contained in:
Kristian Knarvik 2022-11-07 09:23:25 +01:00
parent 04d4bc8303
commit 2383dfd2ef
10 changed files with 32 additions and 30 deletions

View File

@ -26,7 +26,7 @@ import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
import net.knarcraft.bookswithoutborders.listener.BookEventListener; import net.knarcraft.bookswithoutborders.listener.BookEventListener;
import net.knarcraft.bookswithoutborders.listener.PlayerEventListener; import net.knarcraft.bookswithoutborders.listener.PlayerEventListener;
import net.knarcraft.bookswithoutborders.listener.SignEventListener; 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.KnarLib;
import net.knarcraft.knarlib.util.UpdateChecker; import net.knarcraft.knarlib.util.UpdateChecker;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -85,7 +85,7 @@ public class BooksWithoutBorders extends JavaPlugin {
} else if (sender instanceof Player player) { } else if (sender instanceof Player player) {
UUID playerUUID = player.getUniqueId(); UUID playerUUID = player.getUniqueId();
if (!playerBooksList.containsKey(playerUUID)) { if (!playerBooksList.containsKey(playerUUID)) {
List<String> newFiles = FileHelper.listFiles(sender, false); List<String> newFiles = BookFileHelper.listFiles(sender, false);
playerBooksList.put(playerUUID, newFiles); playerBooksList.put(playerUUID, newFiles);
} }
return playerBooksList.get(playerUUID); return playerBooksList.get(playerUUID);
@ -101,7 +101,7 @@ public class BooksWithoutBorders extends JavaPlugin {
* @param updatePublic <p>Whether to update public books</p> * @param updatePublic <p>Whether to update public books</p>
*/ */
public static void updateBooks(CommandSender sender, boolean updatePublic) { public static void updateBooks(CommandSender sender, boolean updatePublic) {
List<String> newFiles = FileHelper.listFiles(sender, updatePublic); List<String> newFiles = BookFileHelper.listFiles(sender, updatePublic);
if (updatePublic) { if (updatePublic) {
publicBooksList = newFiles; publicBooksList = newFiles;
} else if (sender instanceof Player player) { } else if (sender instanceof Player player) {
@ -125,7 +125,7 @@ public class BooksWithoutBorders extends JavaPlugin {
consoleSender = this.getServer().getConsoleSender(); consoleSender = this.getServer().getConsoleSender();
playerBooksList = new HashMap<>(); playerBooksList = new HashMap<>();
BooksWithoutBordersConfig.initialize(this); BooksWithoutBordersConfig.initialize(this);
publicBooksList = FileHelper.listFiles(consoleSender, true); publicBooksList = BookFileHelper.listFiles(consoleSender, true);
PluginManager pluginManager = this.getServer().getPluginManager(); PluginManager pluginManager = this.getServer().getPluginManager();

View File

@ -2,8 +2,8 @@ package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.state.BookDirectory; import net.knarcraft.bookswithoutborders.state.BookDirectory;
import net.knarcraft.bookswithoutborders.utility.BookFileHelper;
import net.knarcraft.bookswithoutborders.utility.BookHelper; import net.knarcraft.bookswithoutborders.utility.BookHelper;
import net.knarcraft.bookswithoutborders.utility.FileHelper;
import net.knarcraft.knarlib.util.TabCompletionHelper; import net.knarcraft.knarlib.util.TabCompletionHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -41,7 +41,7 @@ public class CommandDelete implements TabExecutor {
boolean deleteBook(CommandSender sender, String[] args, boolean deletePublic) { boolean deleteBook(CommandSender sender, String[] args, boolean deletePublic) {
//List deletable files //List deletable files
if (args.length == 0) { if (args.length == 0) {
FileHelper.printBooks(sender, deletePublic); BookFileHelper.printBooks(sender, deletePublic);
return true; return true;
} }
//Delete the file //Delete the file
@ -82,7 +82,7 @@ public class CommandDelete implements TabExecutor {
//Get the file to be deleted //Get the file to be deleted
String bookDirectory = BookHelper.getBookDirectoryPathString( String bookDirectory = BookHelper.getBookDirectoryPathString(
isPublic ? BookDirectory.PUBLIC : BookDirectory.PLAYER, sender); 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 //Send message if no such file could be found
if (file == null) { if (file == null) {

View File

@ -1,8 +1,8 @@
package net.knarcraft.bookswithoutborders.command; package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.utility.BookFileHelper;
import net.knarcraft.bookswithoutborders.utility.BookLoader; import net.knarcraft.bookswithoutborders.utility.BookLoader;
import net.knarcraft.bookswithoutborders.utility.FileHelper;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper; import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper;
import net.knarcraft.knarlib.util.TabCompletionHelper; import net.knarcraft.knarlib.util.TabCompletionHelper;
@ -50,7 +50,7 @@ public class CommandGive implements TabExecutor {
} }
if (args.length == 0) { if (args.length == 0) {
FileHelper.printBooks(sender, givePublic); BookFileHelper.printBooks(sender, givePublic);
return true; return true;
} }

View File

@ -1,8 +1,8 @@
package net.knarcraft.bookswithoutborders.command; package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.utility.BookFileHelper;
import net.knarcraft.bookswithoutborders.utility.BookLoader; import net.knarcraft.bookswithoutborders.utility.BookLoader;
import net.knarcraft.bookswithoutborders.utility.FileHelper;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper; import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper;
import net.knarcraft.knarlib.util.TabCompletionHelper; import net.knarcraft.knarlib.util.TabCompletionHelper;
@ -50,7 +50,7 @@ public class CommandLoad implements TabExecutor {
//Show books available to the player //Show books available to the player
if (argumentCount == 0) { if (argumentCount == 0) {
FileHelper.printBooks(sender, loadPublic); BookFileHelper.printBooks(sender, loadPublic);
return true; return true;
} }

View File

@ -4,9 +4,9 @@ import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
import net.knarcraft.bookswithoutborders.state.BookDirectory; import net.knarcraft.bookswithoutborders.state.BookDirectory;
import net.knarcraft.bookswithoutborders.state.ItemSlot; import net.knarcraft.bookswithoutborders.state.ItemSlot;
import net.knarcraft.bookswithoutborders.utility.BookFileHelper;
import net.knarcraft.bookswithoutborders.utility.BookHelper; import net.knarcraft.bookswithoutborders.utility.BookHelper;
import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper; import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper;
import net.knarcraft.bookswithoutborders.utility.FileHelper;
import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -104,7 +104,7 @@ public class CommandSave implements TabExecutor {
} }
//Find any duplicates of the book //Find any duplicates of the book
int foundDuplicates = FileHelper.findDuplicates(foundFiles, fileName); int foundDuplicates = BookFileHelper.findDuplicates(foundFiles, fileName);
//Deal with duplicates //Deal with duplicates
if (foundDuplicates > 0) { if (foundDuplicates > 0) {

View File

@ -3,9 +3,9 @@ package net.knarcraft.bookswithoutborders.listener;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
import net.knarcraft.bookswithoutborders.state.EncryptionStyle; import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
import net.knarcraft.bookswithoutborders.utility.BookFileHelper;
import net.knarcraft.bookswithoutborders.utility.BookLoader; import net.knarcraft.bookswithoutborders.utility.BookLoader;
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper; import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
import net.knarcraft.bookswithoutborders.utility.FileHelper;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material; 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.getBookFolder;
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash; 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 * 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 //Tests if a full file name has been supplied and points to an actual file
String signFile = getBookFolder() + lines[2] + lines[3]; String signFile = getBookFolder() + lines[2] + lines[3];
if (FileHelper.bookFileExists(signFile)) { if (BookFileHelper.bookFileExists(signFile)) {
markGiveSignValidity(event, true); markGiveSignValidity(event, true);
return; return;
} else { } else {

View File

@ -17,9 +17,9 @@ import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig
/** /**
* Helper class for dealing with files * 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) { if (file == null) {
return new ArrayList<>(); 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) { public static void printBooks(CommandSender sender, boolean listPublic) {
List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, listPublic); List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, listPublic);
FileHelper.printFiles(sender, availableBooks); BookFileHelper.printFiles(sender, availableBooks);
} }
/** /**

View File

@ -120,9 +120,9 @@ public final class BookLoader {
String slash = BooksWithoutBordersConfig.getSlash(); String slash = BooksWithoutBordersConfig.getSlash();
String bookFolder = BooksWithoutBordersConfig.getBookFolder(); String bookFolder = BooksWithoutBordersConfig.getBookFolder();
if (bookDirectory == BookDirectory.ENCRYPTED) { if (bookDirectory == BookDirectory.ENCRYPTED) {
file = FileHelper.getBookFile(bookFolder + "Encrypted" + slash + directory + slash + fileName); file = BookFileHelper.getBookFile(bookFolder + "Encrypted" + slash + directory + slash + fileName);
} else { } else {
file = FileHelper.getBookFile(BookHelper.getBookDirectoryPathString(bookDirectory, sender) + fileName); file = BookFileHelper.getBookFile(BookHelper.getBookDirectoryPathString(bookDirectory, sender) + fileName);
} }
if (file == null || !file.isFile()) { if (file == null || !file.isFile()) {

View File

@ -1,16 +1,18 @@
package net.knarcraft.bookswithoutborders.utility; package net.knarcraft.bookswithoutborders.utility;
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
import net.knarcraft.knarlib.util.FileHelper;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.meta.BookMeta; import org.bukkit.inventory.meta.BookMeta;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -83,7 +85,7 @@ public final class BookToFromTextHelper {
* @throws IOException <p>If unable to save the book</p> * @throws IOException <p>If unable to save the book</p>
*/ */
public static void bookToTXT(String folderPath, String fileName, BookMeta bookMetadata) throws IOException { 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); PrintWriter printWriter = new PrintWriter(fileWriter);
List<String> pages = bookMetadata.getPages(); List<String> pages = bookMetadata.getPages();
@ -186,7 +188,7 @@ public final class BookToFromTextHelper {
*/ */
private static List<String> readTextFile(File file) throws IOException { private static List<String> readTextFile(File file) throws IOException {
List<String> rawPages = new ArrayList<>(); List<String> 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 //Use the first line to decide if we are loading a book, or creating a new book
String firstLine = bufferedReader.readLine(); String firstLine = bufferedReader.readLine();

View File

@ -1,13 +1,13 @@
package net.knarcraft.bookswithoutborders.util; package net.knarcraft.bookswithoutborders.util;
import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.BookFileHelper;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class FileHelperTest { public class BookFileHelperTest {
@Test @Test
public void findDuplicatesTest() { public void findDuplicatesTest() {
@ -17,9 +17,9 @@ public class FileHelperTest {
files[2] = new File("test/asd/(3)book+)Crab.yml"); files[2] = new File("test/asd/(3)book+)Crab.yml");
files[3] = new File("test/asd/(2)book+)Crab.yml"); files[3] = new File("test/asd/(2)book+)Crab.yml");
files[4] = new File("test/asd/(1)book+)Crab.yml"); files[4] = new File("test/asd/(1)book+)Crab.yml");
assertEquals(4, FileHelper.findDuplicates(files, "book+)Crab.yml")); assertEquals(4, BookFileHelper.findDuplicates(files, "book+)Crab.yml"));
assertEquals(1, FileHelper.findDuplicates(files, "book+)Fish.yml")); assertEquals(1, BookFileHelper.findDuplicates(files, "book+)Fish.yml"));
assertEquals(0, FileHelper.findDuplicates(files, "book+)Horse.yml")); assertEquals(0, BookFileHelper.findDuplicates(files, "book+)Horse.yml"));
} }
} }