diff --git a/README.md b/README.md index 5653b99..ac03e37 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ # Books Without Borders -This is an attempt at a rewrite of the Books Without Borders plugin. This rewrite uses the -source code given at [the original bukkit page](https://dev.bukkit.org/projects/books-without-borders). -I'm not planning any new features at this time. The only goal is to make it 1.17.1 compliant, but I'll make the code -more maintainable along the way. -While the original version still works, it's using a lot of depreciated function calls which will -most likely break in the future. +This is an attempt at a rewrite of the Books Without Borders plugin. This rewrite uses the source code given +at [the original bukkit page](https://dev.bukkit.org/projects/books-without-borders). I'm not planning any new features +at this time. The only goal is to make it 1.17.1 compliant, but I'll make the code more maintainable along the way. +While the original version still works, it's using a lot of depreciated function calls which will most likely break in +the future. ## Books without Borders! -Ever wanted to export your book to a text file? Ever want to import it back in? Accidentally sign it too soon? If so, Books without Borders has got your back! +Ever wanted to export your book to a text file? Ever want to import it back in? Accidentally sign it too soon? If so, +Books without Borders has got your back! ### Features diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index ae6258c..d66748a 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -24,7 +24,6 @@ import net.knarcraft.bookswithoutborders.listener.BooksWithoutBordersListener; import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper; import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.milkbowl.vault.economy.Economy; -import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandExecutor; @@ -54,6 +53,11 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getCommandColor; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getErrorColor; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSuccessColor; import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString; import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName; @@ -62,7 +66,6 @@ public class BooksWithoutBorders extends JavaPlugin { //The separating string between the book title and the book author public static String titleAuthorSeparator; public static String loreSeparator; - public static final String SLASH = System.getProperty("file.separator"); public static List firstBooks; public static String welcomeMessage; protected static List existingPlayers; @@ -77,21 +80,17 @@ public class BooksWithoutBorders extends JavaPlugin { public static BooksWithoutBorders bwb; protected static BooksWithoutBordersListener bL; public static ConsoleCommandSender consoleSender; - public static String bookFolder; - public static final ChatColor errorColor = ChatColor.RED; - public static final ChatColor successColor = ChatColor.GREEN; - public static final ChatColor commandColor = ChatColor.YELLOW; @Override public void onEnable() { bwb = this; consoleSender = this.getServer().getConsoleSender(); - bookFolder = this.getDataFolder().getAbsolutePath() + SLASH + "Books" + SLASH; bL = new BooksWithoutBordersListener(); loadList = new HashMap<>(); firstBooks = new ArrayList<>(); + BooksWithoutBordersSettings.initialize(this); - if (SLASH != null && init()) { + if (getSlash() != null && init()) { this.getServer().getPluginManager().registerEvents(bL, this); } else { this.getPluginLoader().disablePlugin(this); @@ -188,8 +187,8 @@ public class BooksWithoutBorders extends JavaPlugin { this.saveConfig(); - File fTest = new File(bookFolder); - File efTest = new File(bookFolder + "Encrypted" + SLASH); + File fTest = new File(getBookFolder()); + File efTest = new File(getBookFolder() + "Encrypted" + getSlash()); if (!fTest.exists()) { try { @@ -283,7 +282,7 @@ public class BooksWithoutBorders extends JavaPlugin { } public boolean loadExistingPlayers() { - File fTest = new File(this.getDataFolder().getAbsolutePath() + SLASH + "Existing Players.txt"); + File fTest = new File(this.getDataFolder().getAbsolutePath() + getSlash() + "Existing Players.txt"); existingPlayers = new ArrayList<>(); if (!fTest.exists()) { @@ -329,7 +328,7 @@ public class BooksWithoutBorders extends JavaPlugin { existingPlayers.add(playerName); try { - PrintWriter pw = new PrintWriter(new FileWriter(this.getDataFolder().getAbsolutePath() + SLASH + "Existing Players.txt", true)); + PrintWriter pw = new PrintWriter(new FileWriter(this.getDataFolder().getAbsolutePath() + getSlash() + "Existing Players.txt", true)); pw.println(playerName); pw.close(); } catch (IOException e) { @@ -363,9 +362,9 @@ public class BooksWithoutBorders extends JavaPlugin { String savePath; if (saveToPublicFolder) { - savePath = bookFolder; + savePath = getBookFolder(); } else { - savePath = bookFolder + cleanString(player.getName()) + SLASH; + savePath = getBookFolder() + cleanString(player.getName()) + getSlash(); } //Generate book filename @@ -403,14 +402,14 @@ public class BooksWithoutBorders extends JavaPlugin { //Skip duplicate book if (!fileName.contains("Untitled") && !overwrite) { sendErrorMessage(player, "Book is already saved!"); - sendErrorMessage(player, "Use " + commandColor + "/bwb Save true " + errorColor + "to overwrite!"); + sendErrorMessage(player, "Use " + getCommandColor() + "/bwb Save true " + getErrorColor() + "to overwrite!"); return; } //Skip if duplicate limit is reached if (foundDuplicates > bookDuplicateLimit) { sendErrorMessage(player, "Maximum amount of " + fileName + " duplicates reached!"); - sendErrorMessage(player, "Use " + commandColor + "/bwb Save true " + errorColor + "to overwrite!"); + sendErrorMessage(player, "Use " + getCommandColor() + "/bwb Save true " + getErrorColor() + "to overwrite!"); return; } @@ -453,11 +452,11 @@ public class BooksWithoutBorders extends JavaPlugin { File file; if (dir.equalsIgnoreCase("public")) { - file = FileHelper.getBookFile(bookFolder + fileName); + file = FileHelper.getBookFile(getBookFolder() + fileName); } else if (dir.equalsIgnoreCase("player")) { - file = FileHelper.getBookFile(bookFolder + cleanString(sender.getName()) + SLASH + fileName); + file = FileHelper.getBookFile(getBookFolder() + cleanString(sender.getName()) + getSlash() + fileName); } else if (dir.trim().isEmpty()) { - file = FileHelper.getBookFile(bookFolder + "Encrypted" + SLASH + dir + SLASH + fileName); + file = FileHelper.getBookFile(getBookFolder() + "Encrypted" + getSlash() + dir + getSlash() + fileName); } else { file = null; } @@ -586,9 +585,9 @@ public class BooksWithoutBorders extends JavaPlugin { public List listFiles(CommandSender sender, Boolean listPublic, boolean silent) { File file; if (listPublic) { - file = new File(bookFolder); + file = new File(getBookFolder()); } else { - file = new File(bookFolder + cleanString(sender.getName()) + SLASH); + file = new File(getBookFolder() + cleanString(sender.getName()) + getSlash()); } return FileHelper.listFiles(sender, file, silent); } @@ -600,7 +599,7 @@ public class BooksWithoutBorders extends JavaPlugin { * @param message

The message to send

*/ public static void sendSuccessMessage(CommandSender sender, String message) { - sender.sendMessage(successColor + message); + sender.sendMessage(getSuccessColor() + message); } /** @@ -610,7 +609,7 @@ public class BooksWithoutBorders extends JavaPlugin { * @param message

The message to send

*/ public static void sendErrorMessage(CommandSender sender, String message) { - sender.sendMessage(errorColor + message); + sender.sendMessage(getErrorColor() + message); } /** diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBordersSettings.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBordersSettings.java new file mode 100644 index 0000000..0cd35c6 --- /dev/null +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBordersSettings.java @@ -0,0 +1,77 @@ +package net.knarcraft.bookswithoutborders; + +import org.bukkit.ChatColor; + +/** + * Class for getting various settings + */ +public class BooksWithoutBordersSettings { + + //Static settings + private static final ChatColor errorColor = ChatColor.RED; + private static final ChatColor successColor = ChatColor.GREEN; + private static final ChatColor commandColor = ChatColor.YELLOW; + private static final String SLASH = System.getProperty("file.separator"); + private static boolean isInitialized; + public static String bookFolder; + + /** + * Initializes the books without borders settings class + * + * @param booksWithoutBorders

The books without borders object used for getting required data

+ */ + public static void initialize(BooksWithoutBorders booksWithoutBorders) { + if (isInitialized) { + throw new IllegalArgumentException("Settings class initialized twice. This should not happen!"); + } + isInitialized = true; + bookFolder = booksWithoutBorders.getDataFolder().getAbsolutePath() + getSlash() + "Books" + getSlash(); + } + + /** + * Gets the folder used for storing books + * + * @return

The folder used for storing books

+ */ + public static String getBookFolder() { + return bookFolder; + } + + /** + * Gets the color to use for error messages + * + * @return

The color to use for error messages

+ */ + public static ChatColor getErrorColor() { + return errorColor; + } + + /** + * Gets the color to use for success messages + * + * @return

The color to use for success messages

+ */ + public static ChatColor getSuccessColor() { + return successColor; + } + + /** + * Gets the color used to color commands + * + * @return

The color used to color commands

+ */ + public static ChatColor getCommandColor() { + return commandColor; + } + + /** + * Gets the correct slash to use for the used OS + * + * @return

The slash to use for file separators

+ */ + public static String getSlash() { + return SLASH; + } + + +} diff --git a/src/main/java/net/knarcraft/bookswithoutborders/GenenCrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/GenenCrypt.java index 0a14a6f..716e84f 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/GenenCrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/GenenCrypt.java @@ -1,8 +1,8 @@ package net.knarcraft.bookswithoutborders; -import java.util.Random; import java.util.ArrayList; import java.util.HashMap; +import java.util.Random; /** * Case-insensitive gene-based encryption @@ -23,6 +23,7 @@ public class GenenCrypt { /** * Instantiates a new GenenCrypt + * * @param key

The key used to generate the codon table

*/ public GenenCrypt(String key) { @@ -126,6 +127,7 @@ public class GenenCrypt { /** * Encrypts some input + * * @param input

The input to encrypt

* @return

The encrypted input

*/ @@ -158,6 +160,7 @@ public class GenenCrypt { /** * Decrypts some input + * * @param input

The input to decrypt

* @return

The decrypted input

*/ diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandBooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandBooksWithoutBorders.java index c92fd50..b80a2a5 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandBooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandBooksWithoutBorders.java @@ -9,10 +9,10 @@ import org.bukkit.entity.Player; import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.bookPriceQuantity; import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.bookPriceType; -import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.commandColor; import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.sendErrorMessage; import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.sendSuccessMessage; -import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.successColor; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getCommandColor; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSuccessColor; /** * Command executor for the books without borders (bwb) command @@ -37,29 +37,31 @@ public class CommandBooksWithoutBorders implements CommandExecutor { /** * Shows all commands available to the console + * * @param sender

The console which sent the command

*/ private void showConsoleCommands(CommandSender sender) { - sender.sendMessage(commandColor + "Use: /bwb [Command]"); - sender.sendMessage(commandColor + "[] denote parameters"); - sender.sendMessage(commandColor + "Commands:"); - sender.sendMessage(commandColor + "\nReload:" + successColor + " Reloads BwB's config file"); - sender.sendMessage(commandColor + "givePublic [file name or number] [player name] [true/false]: " + successColor); + sender.sendMessage(getCommandColor() + "Use: /bwb [Command]"); + sender.sendMessage(getCommandColor() + "[] denote parameters"); + sender.sendMessage(getCommandColor() + "Commands:"); + sender.sendMessage(getCommandColor() + "\nReload:" + getSuccessColor() + " Reloads BwB's config file"); + sender.sendMessage(getCommandColor() + "givePublic [file name or number] [player name] [true/false]: " + getSuccessColor()); sendSuccessMessage(sender, "Gives the selected player a book from the public directory"); sendSuccessMessage(sender, "If no file is specified, a list of available files is returned"); - sender.sendMessage(commandColor + "deletePublic [file name or number]: " + successColor + "Deletes the specified"); + sender.sendMessage(getCommandColor() + "deletePublic [file name or number]: " + getSuccessColor() + "Deletes the specified"); sendSuccessMessage(sender, "file in the public directory"); sendSuccessMessage(sender, "If no file is specified, a list of available files is returned"); } /** * Shows all commands available to the sending player + * * @param sender

The player which sent the command

*/ private void showPlayerCommands(CommandSender sender) { //Lists all commands - sender.sendMessage(commandColor + "Use: /bwb [Command]"); - sender.sendMessage(commandColor + "[] denote parameters"); + sender.sendMessage(getCommandColor() + "Use: /bwb [Command]"); + sender.sendMessage(getCommandColor() + "[] denote parameters"); if (booksWithoutBorders.booksHavePrice()) { if (bookPriceType != Material.AIR) { @@ -68,80 +70,80 @@ public class CommandBooksWithoutBorders implements CommandExecutor { sendErrorMessage(sender, "[" + BooksWithoutBorders.eco.format(bookPriceQuantity) + " is required to create a book]"); } } - sender.sendMessage(commandColor + "Commands:"); + sender.sendMessage(getCommandColor() + "Commands:"); if (sender.hasPermission("bookswithoutborders.load")) { - sender.sendMessage(commandColor + "\nLoad [file name or number] [# of copies] [true/false]:"); + sender.sendMessage(getCommandColor() + "\nLoad [file name or number] [# of copies] [true/false]:"); sendSuccessMessage(sender, "Creates a book from the specified file and gives it to the player"); sendSuccessMessage(sender, "If no file is specified, a list of available files is returned"); sendSuccessMessage(sender, "If true is specified the book will be signed, if false it will be"); sendSuccessMessage(sender, "unsigned"); } if (sender.hasPermission("bookswithoutborders.loadPublic")) { - sender.sendMessage(commandColor + "loadPublic [file name or number] [# of copies] [true/false]:"); + sender.sendMessage(getCommandColor() + "loadPublic [file name or number] [# of copies] [true/false]:"); sendSuccessMessage(sender, "Same as Load, but views files in the public directory"); } if (sender.hasPermission("bookswithoutborders.save")) { - sender.sendMessage("\n" + commandColor + "Save [true/false]: " + successColor + "Saves the book the player is"); + sender.sendMessage("\n" + getCommandColor() + "Save [true/false]: " + getSuccessColor() + "Saves the book the player is"); sendSuccessMessage(sender, "holding to a text file in a private directory"); sendSuccessMessage(sender, "If true is specified, a book of the same name by the same"); sendSuccessMessage(sender, "author will be overwritten by the new book"); } if (sender.hasPermission("bookswithoutborders.savePublic")) { - sender.sendMessage(commandColor + "savePublic [true/false]: " + successColor + "Same as Save,"); + sender.sendMessage(getCommandColor() + "savePublic [true/false]: " + getSuccessColor() + "Same as Save,"); sendSuccessMessage(sender, "but saves files in the public directory"); } if (sender.hasPermission("bookswithoutborders.give")) { - sender.sendMessage("\n" + commandColor + "Give [file name or number] [player name] [# of copies] [true/false]:"); + sender.sendMessage("\n" + getCommandColor() + "Give [file name or number] [player name] [# of copies] [true/false]:"); sendSuccessMessage(sender, "Gives the selected player a book from your personal directory"); } if (sender.hasPermission("bookswithoutborders.givePublic")) { - sender.sendMessage(commandColor + "givePublic [file name or number] [player name] [# of copies] [true/false]:"); + sender.sendMessage(getCommandColor() + "givePublic [file name or number] [player name] [# of copies] [true/false]:"); sendSuccessMessage(sender, "Same as give, but uses books from the public directory"); } if (sender.hasPermission("bookswithoutborders.delete")) { - sender.sendMessage(commandColor + "\nDelete [file name or number]: " + successColor + "Deletes the specified"); + sender.sendMessage(getCommandColor() + "\nDelete [file name or number]: " + getSuccessColor() + "Deletes the specified"); sendSuccessMessage(sender, "file in the player's directory"); sendSuccessMessage(sender, "If no file is specified, a list of available files is returned"); } if (sender.hasPermission("bookswithoutborders.admin")) { - sender.sendMessage(commandColor + "deletePublic [file name or number]: " + successColor + "Same as Delete,"); + sender.sendMessage(getCommandColor() + "deletePublic [file name or number]: " + getSuccessColor() + "Same as Delete,"); sendSuccessMessage(sender, "but deletes files in the public directory"); - sender.sendMessage(commandColor + "\nReload:" + successColor + " Reloads BwB's configuration file"); + sender.sendMessage(getCommandColor() + "\nReload:" + getSuccessColor() + " Reloads BwB's configuration file"); } if (sender.hasPermission("bookswithoutborders.unsign")) { - sender.sendMessage("\n" + commandColor + "Unsign: " + successColor + "Un-signs the book the player is holding"); + sender.sendMessage("\n" + getCommandColor() + "Unsign: " + getSuccessColor() + "Un-signs the book the player is holding"); } if (sender.hasPermission("bookswithoutborders.copy")) { - sender.sendMessage("\n" + commandColor + "Copy [number of copies]: " + successColor + "Copies the book the player is holding"); + sender.sendMessage("\n" + getCommandColor() + "Copy [number of copies]: " + getSuccessColor() + "Copies the book the player is holding"); } if (sender.hasPermission("bookswithoutborders.encrypt")) { - sender.sendMessage("\n" + commandColor + "Encrypt [key] [style]: " + successColor + "Encrypts the book the player is holding"); - sender.sendMessage(commandColor + "[key]" + successColor + " is required and can be any phrase or number excluding spaces"); - sender.sendMessage(commandColor + "[style]" + successColor + " is not required. Possible values are \"DNA\" or \"Magic\""); + sender.sendMessage("\n" + getCommandColor() + "Encrypt [key] [style]: " + getSuccessColor() + "Encrypts the book the player is holding"); + sender.sendMessage(getCommandColor() + "[key]" + getSuccessColor() + " is required and can be any phrase or number excluding spaces"); + sender.sendMessage(getCommandColor() + "[style]" + getSuccessColor() + " is not required. Possible values are \"DNA\" or \"Magic\""); } if (sender.hasPermission("bookswithoutborders.groupEncrypt")) { - sender.sendMessage("\n" + commandColor + "groupEncrypt [group name] [key] [style]: " + successColor + "Encrypts book so that only players with the" + - "\n bookswithoutborders.decrypt." + commandColor + "[group name]" + successColor + " permission may decrypt the book by holding and left clicking the book"); + sender.sendMessage("\n" + getCommandColor() + "groupEncrypt [group name] [key] [style]: " + getSuccessColor() + "Encrypts book so that only players with the" + + "\n bookswithoutborders.decrypt." + getCommandColor() + "[group name]" + getSuccessColor() + " permission may decrypt the book by holding and left clicking the book"); } if (sender.hasPermission("bookswithoutborders.decrypt")) { - sender.sendMessage("\n" + commandColor + "Decrypt [key]: " + successColor + "Decrypts the book the player is holding"); - sender.sendMessage(commandColor + "[key]" + successColor + " is required and MUST be IDENTICAL to the key used to encrypt held book"); + sender.sendMessage("\n" + getCommandColor() + "Decrypt [key]: " + getSuccessColor() + "Decrypts the book the player is holding"); + sender.sendMessage(getCommandColor() + "[key]" + getSuccessColor() + " is required and MUST be IDENTICAL to the key used to encrypt held book"); } if (sender.hasPermission("bookswithoutborders.setTitle")) { - sender.sendMessage("\n" + commandColor + "setTitle [title]: " + successColor + "Sets the title of the book/item the player is holding"); + sender.sendMessage("\n" + getCommandColor() + "setTitle [title]: " + getSuccessColor() + "Sets the title of the book/item the player is holding"); } if (sender.hasPermission("bookswithoutborders.setAuthor")) { - sender.sendMessage("\n" + commandColor + "setAuthor [author]: " + successColor + "Sets the author of the book the player is holding"); + sender.sendMessage("\n" + getCommandColor() + "setAuthor [author]: " + getSuccessColor() + "Sets the author of the book the player is holding"); } if (sender.hasPermission("bookswithoutborders.setLore")) { - sender.sendMessage("\n" + commandColor + "setLore [lore]: " + successColor + "Sets the lore of the item the player is holding"); + sender.sendMessage("\n" + getCommandColor() + "setLore [lore]: " + getSuccessColor() + "Sets the lore of the item the player is holding"); sendSuccessMessage(sender, "Insert the lore_line_separator character to force a new line\n[\"~\" by default]"); } if (sender.hasPermission("bookswithoutborders.setBookPrice")) { - sender.sendMessage("\n" + commandColor + "setBookPrice [Item/Eco] [quantity]: " + successColor + "Sets the per-book-price to create a book via commands." + + sender.sendMessage("\n" + getCommandColor() + "setBookPrice [Item/Eco] [quantity]: " + getSuccessColor() + "Sets the per-book-price to create a book via commands." + "\nIf [Item], the item in the player's hand in the amount of [quantity] will be the price.\nIf [Eco], a Vault based economy will be used for price." + "\nIf neither [Item/Eco] or [quantity] are specified the current price to create books will be removed."); } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDecrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDecrypt.java index 187669e..804d3c6 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDecrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDecrypt.java @@ -12,6 +12,9 @@ import org.bukkit.inventory.meta.BookMeta; import java.io.File; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash; + /** * Command executor for the decrypt command */ @@ -39,7 +42,7 @@ public class CommandDecrypt implements CommandExecutor { //Warning: admin decrypt only allows decrypting files created by the same player. Not sure if intended if (args.length == 0 && BooksWithoutBorders.adminDecrypt && player.hasPermission("bookswithoutborders.admin")) { - String path = BooksWithoutBorders.bookFolder + "Encrypted" + BooksWithoutBorders.SLASH; + String path = getBookFolder() + "Encrypted" + getSlash(); String fileName; if (bookMetadata.hasTitle()) { fileName = bookMetadata.getTitle() + BooksWithoutBorders.titleAuthorSeparator + bookMetadata.getAuthor(); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java index 92a2eb7..55c26f1 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java @@ -10,6 +10,9 @@ import org.bukkit.entity.Player; import java.io.File; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash; + /** * Command executor for the delete command */ @@ -33,8 +36,9 @@ public class CommandDelete implements CommandExecutor { /** * Deletes a book - * @param sender

The sender trying to delete the book

- * @param args

The arguments given

+ * + * @param sender

The sender trying to delete the book

+ * @param args

The arguments given

* @param deletePublic

Whether to delete a public book

* @return

True if the book was deleted successfully

*/ @@ -84,10 +88,10 @@ public class CommandDelete implements CommandExecutor { //Get the file to be deleted File file; if (isPublic) { - file = FileHelper.getBookFile(BooksWithoutBorders.bookFolder + fileName); + file = FileHelper.getBookFile(getBookFolder() + fileName); } else { - file = FileHelper.getBookFile(BooksWithoutBorders.bookFolder + - InputCleaningHelper.cleanString(sender.getName()) + BooksWithoutBorders.SLASH + fileName); + file = FileHelper.getBookFile(getBookFolder() + + InputCleaningHelper.cleanString(sender.getName()) + getSlash() + fileName); } //Send message if no such file could be found diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java index 9f1942d..d346aa5 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java @@ -28,9 +28,10 @@ public class CommandEncrypt implements CommandExecutor { /** * Performs necessary pre-checks before going through with the encryption - * @param sender

The sender trying to encrypt a book

- * @param args

The arguments given

- * @param necessaryArguments

How many arguments is the minimum requirement

+ * + * @param sender

The sender trying to encrypt a book

+ * @param args

The arguments given

+ * @param necessaryArguments

How many arguments is the minimum requirement

* @param missingArgumentsError

The error to show if a required argument is missing

* @return

The metadata of the book to encrypt, or null if any checks fail

*/ @@ -71,10 +72,11 @@ public class CommandEncrypt implements CommandExecutor { /** * Encrypts the given book + * * @param encryptionStyle

The encryption style to use

- * @param player

The player encrypting the book

- * @param key

The encryption key to use

- * @param group

The group to encrypt for

+ * @param player

The player encrypting the book

+ * @param key

The encryption key to use

+ * @param group

The group to encrypt for

* @return

True if the book was encrypted successfully

*/ boolean encryptBook(EncryptionStyle encryptionStyle, Player player, String key, String group) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java index 3b50b14..556d71c 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java @@ -60,7 +60,8 @@ public class CommandGive implements CommandExecutor { try { Integer.parseInt(bookIdentifier); BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, givePublic, true)); - } catch (NumberFormatException ignored) {} + } catch (NumberFormatException ignored) { + } Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(receivingPlayerName); if (receivingPlayer == null) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java index 2d96e07..d4880d3 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java @@ -26,9 +26,10 @@ public class CommandLoad implements CommandExecutor { /** * Loads a stored book - * @param sender

The sender asking to load the book

- * @param args

The arguments given

- * @param directory

The directory to load from (public/player)

+ * + * @param sender

The sender asking to load the book

+ * @param args

The arguments given

+ * @param directory

The directory to load from (public/player)

* @param loadPublic

Whether to list public files as loadable

* @return

True if the book was loaded successfully

*/ @@ -70,7 +71,8 @@ public class CommandLoad implements CommandExecutor { try { Integer.parseInt(bookIdentifier); BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, loadPublic, true)); - } catch (NumberFormatException ignored) {} + } catch (NumberFormatException ignored) { + } String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier); try { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java index 7cf0c14..2af27f2 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java @@ -27,8 +27,9 @@ public class CommandSave implements CommandExecutor { /** * Saves the player's held book if it exists - * @param sender

The sender of the command

- * @param args

The arguments given

+ * + * @param sender

The sender of the command

+ * @param args

The arguments given

* @param savePublic

Whether to save the book in the public directory or the player directory

* @return

True if a book was saved successfully

*/ diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java index fb486a7..96bc897 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java @@ -36,7 +36,7 @@ public class CommandSetBookPrice implements CommandExecutor { //Warn about invalid argument if (!args[0].equalsIgnoreCase("Item") && !args[0].equalsIgnoreCase("Eco")) { - BooksWithoutBorders. sendErrorMessage(sender, "Price type must be \"Item\" or \"Eco\"!"); + BooksWithoutBorders.sendErrorMessage(sender, "Price type must be \"Item\" or \"Eco\"!"); return false; } @@ -60,6 +60,7 @@ public class CommandSetBookPrice implements CommandExecutor { /** * Removes the book price + * * @param sender

The sender of the command

*/ private void clearItemPrice(CommandSender sender) { @@ -74,8 +75,9 @@ public class CommandSetBookPrice implements CommandExecutor { /** * Sets the book price to use items, and updates book price + * * @param sender

The sender of the command

- * @param price

The new price

+ * @param price

The new price

* @return

True if the price was changed successfully

*/ private boolean setItemPrice(CommandSender sender, double price) { @@ -105,8 +107,9 @@ public class CommandSetBookPrice implements CommandExecutor { /** * Sets the book price to use economy, and updates book price + * * @param sender

The sender of the command

- * @param price

The new price

+ * @param price

The new price

* @return

True if the price was changed successfully

*/ private boolean setEconomyPrice(CommandSender sender, double price) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/listener/BooksWithoutBordersListener.java b/src/main/java/net/knarcraft/bookswithoutborders/listener/BooksWithoutBordersListener.java index 20c77d6..8ef9994 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/listener/BooksWithoutBordersListener.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/listener/BooksWithoutBordersListener.java @@ -1,9 +1,5 @@ package net.knarcraft.bookswithoutborders.listener; -import java.io.File; -import java.util.List; -import java.util.Objects; - import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.state.EncryptionStyle; import net.knarcraft.bookswithoutborders.utility.EncryptionHelper; @@ -28,14 +24,19 @@ import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.BookMeta; import org.bukkit.inventory.meta.ItemMeta; +import java.io.File; +import java.util.List; +import java.util.Objects; + +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash; + /** * A listener for relevant events */ public class BooksWithoutBordersListener implements Listener { - private final String slash = BooksWithoutBorders.SLASH; - private final String bookFolderPath = BooksWithoutBorders.bwb.getDataFolder().getAbsolutePath() + slash + "Books"; - private final String bookFolder = bookFolderPath + slash; + private final String slash = getSlash(); /** * Updates old books to a newer format @@ -63,10 +64,10 @@ public class BooksWithoutBordersListener implements Listener { String cleanPlayerName = InputCleaningHelper.cleanString(player.getName()); String[] possiblePaths = new String[]{ - bookFolder + fileName + ".yml", - bookFolder + fileName + ".txt", - bookFolder + cleanPlayerName + slash + fileName + ".yml", - bookFolder + cleanPlayerName + slash + fileName + ".txt" + getBookFolder() + fileName + ".yml", + getBookFolder() + fileName + ".txt", + getBookFolder() + cleanPlayerName + slash + fileName + ".yml", + getBookFolder() + cleanPlayerName + slash + fileName + ".txt" }; for (String path : possiblePaths) { @@ -231,7 +232,7 @@ public class BooksWithoutBordersListener implements Listener { } //Tests if a full file name has been supplied and points to an actual file - String signFile = bookFolder + lines[2] + lines[3]; + String signFile = getBookFolder() + lines[2] + lines[3]; if (FileHelper.bookFileExists(signFile)) { markGiveSignValidity(event, true); } else { @@ -252,7 +253,7 @@ public class BooksWithoutBordersListener implements Listener { * @return

True if the number is a book index

*/ private boolean isBookListIndex(String possibleIndex) { - File bookDirectory = new File(bookFolderPath); + File bookDirectory = new File(getBookFolder().replaceAll("[\\\\/]$", "")); try { //Tests if a load list number has been supplied @@ -363,9 +364,9 @@ public class BooksWithoutBordersListener implements Listener { String encryptionFile = InputCleaningHelper.cleanString(groupName) + slash + fileName + ".yml"; - File file = new File(bookFolder + "Encrypted" + slash + encryptionFile); + File file = new File(getBookFolder() + "Encrypted" + slash + encryptionFile); if (!file.isFile()) { - file = new File(bookFolder + fileName + ".txt"); + file = new File(getBookFolder() + fileName + ".txt"); if (!file.isFile()) { return; } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/state/EncryptionStyle.java b/src/main/java/net/knarcraft/bookswithoutborders/state/EncryptionStyle.java index ff406c5..ea9f87b 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/state/EncryptionStyle.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/state/EncryptionStyle.java @@ -15,6 +15,7 @@ public enum EncryptionStyle { /** * Gets an encryption style given its name + * * @param name

The name of the encryption style

* @return

An encryption style or null if no match is found

*/ diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFormatter.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFormatter.java index 50f0f9b..c3a7ecd 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFormatter.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFormatter.java @@ -37,7 +37,8 @@ public class BookFormatter { /** * Splits the last page if it overflows - * @param rawPages

The raw pages to format

+ * + * @param rawPages

The raw pages to format

* @param maxPageText

The max number of characters which fit on a page

* @param fitsNewline

The max number of characters on a page which still fits a newline character

*/ @@ -65,7 +66,8 @@ public class BookFormatter { /** * Combines the two last pages if they can fit on the same page - * @param rawPages

The raw pages to format

+ * + * @param rawPages

The raw pages to format

* @param maxPageText

The max number of characters which fit on a page

*/ public static void formatLastPageCombinePages(List rawPages, int maxPageText) { @@ -79,7 +81,8 @@ public class BookFormatter { /** * Adds newline if the contents of the last page does not exceed page limit and is non-empty - * @param rawPages

The raw pages to format

+ * + * @param rawPages

The raw pages to format

* @param fitsNewline

The max number of characters on a page which still fits a newline character

*/ public static void formatLastPageAddNewline(List rawPages, int fitsNewline) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java index dc952ba..2bd263c 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java @@ -21,12 +21,14 @@ import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixN */ public final class BookToFromTextHelper { - private BookToFromTextHelper() {} + private BookToFromTextHelper() { + } /** * Saves a book's contents to a .yml file - * @param path

The path of the folder to save to. Must end with a slash

- * @param fileName

The name of the file to load to

+ * + * @param path

The path of the folder to save to. Must end with a slash

+ * @param fileName

The name of the file to load to

* @param bookMetadata

Metadata about the book to save

* @throws IOException

If unable to save the book

*/ @@ -51,7 +53,8 @@ public final class BookToFromTextHelper { /** * Loads a book from a .yml file - * @param file

The path of the file to load

+ * + * @param file

The path of the file to load

* @param bookMetadata

Metadata which will be altered with the book's contents

* @return

Metadata for the loaded book

*/ @@ -77,8 +80,9 @@ public final class BookToFromTextHelper { /** * Saves a book's contents to a text file - * @param folderPath

The folder path to save to. Must end with a slash

- * @param fileName

The name of the file to save to

+ * + * @param folderPath

The folder path to save to. Must end with a slash

+ * @param fileName

The name of the file to save to

* @param bookMetadata

Metadata about the book to save

* @throws IOException

If unable to save the book

*/ @@ -96,8 +100,9 @@ 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 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

*/ diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptionHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptionHelper.java index d59db4e..73d1bbd 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptionHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptionHelper.java @@ -1,9 +1,9 @@ package net.knarcraft.bookswithoutborders.utility; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; -import net.knarcraft.bookswithoutborders.state.EncryptionStyle; import net.knarcraft.bookswithoutborders.GenenCrypt; import net.knarcraft.bookswithoutborders.SubstitutionCipher; +import net.knarcraft.bookswithoutborders.state.EncryptionStyle; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -15,6 +15,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash; import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString; import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName; @@ -22,6 +24,7 @@ public class EncryptionHelper { /** * Transforms a string key/password into its numerical values + * * @param key

The key to transform

* @return

The numbers representing the key's characters

*/ @@ -35,10 +38,11 @@ public class EncryptionHelper { /** * Encrypts the pages of a book - * @param book

The book to encrypt

- * @param style

The encryption style to use

+ * + * @param book

The book to encrypt

+ * @param style

The encryption style to use

* @param integerKey

The encryption key to use

- * @param player

The player trying to encrypt a book

+ * @param player

The player trying to encrypt a book

* @return

The pages of the book in encrypted form

*/ public static List encryptBookPages(BookMeta book, EncryptionStyle style, String integerKey, Player player) { @@ -180,7 +184,7 @@ public class EncryptionHelper { public static ItemStack loadEncryptedBook(Player player, String key, boolean deleteEncryptedFile) { ItemStack heldBook = InventoryHelper.getHeldBook(player, true); BookMeta bookMetadata = (BookMeta) heldBook.getItemMeta(); - String path = BooksWithoutBorders.bookFolder + "Encrypted" + BooksWithoutBorders.SLASH; + String path = getBookFolder() + "Encrypted" + getSlash(); if (bookMetadata == null) { return null; @@ -236,7 +240,7 @@ public class EncryptionHelper { * @return

The new encrypted metadata for the book, or null if encryption failed

*/ protected static BookMeta saveEncryptedBookForGroup(Player player, BookMeta bookMetadata, String groupName) { - String path = BooksWithoutBorders.bookFolder + "Encrypted" + BooksWithoutBorders.SLASH + cleanString(groupName) + BooksWithoutBorders.SLASH; + String path = getBookFolder() + "Encrypted" + getSlash() + cleanString(groupName) + getSlash(); File dirTest = new File(path); //Creates group dir if (!dirTest.exists()) { @@ -291,7 +295,7 @@ public class EncryptionHelper { * @return

The new encrypted metadata for the book, or null if encryption failed

*/ protected static Boolean saveEncryptedBook(Player player, BookMeta bookMetaData, String key) { - String path = BooksWithoutBorders.bookFolder + "Encrypted" + BooksWithoutBorders.SLASH; + String path = getBookFolder() + "Encrypted" + getSlash(); String fileName = (!bookMetaData.hasTitle()) ? "Untitled," + player.getName() : bookMetaData.getTitle() + BooksWithoutBorders.titleAuthorSeparator + bookMetaData.getAuthor(); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryHelper.java index 8338a0a..1c78e06 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryHelper.java @@ -1,7 +1,7 @@ package net.knarcraft.bookswithoutborders.utility; -import net.knarcraft.bookswithoutborders.state.BookHoldingState; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.state.BookHoldingState; import net.knarcraft.bookswithoutborders.state.ItemSlot; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -50,6 +50,7 @@ public class InventoryHelper { /** * Performs checks to validate that a player contains exactly one written book + * * @param player

The player to validate

* @param noBookMessage

The message to display if the player is not holding a book

* @param twoBooksMessage

The message to display if the player is holding one book in each hand

@@ -74,15 +75,16 @@ public class InventoryHelper { /** * Gets the slot of the player's held book - * @param player

The player holding the book

+ * + * @param player

The player holding the book

* @param handMatters

Whether the differentiation between the main hand and the off hand is relevant

- * @param mainHand

Whether to search the player's main hand or off hand, if it's relevant

+ * @param mainHand

Whether to search the player's main hand or off hand, if it's relevant

* @param typeMatters

Whether the differentiation between signed and unsigned books is relevant

* @param writtenBook

Whether to search for written or unwritten books, if it's relevant

* @return

The slot of the player's held book

*/ public static ItemSlot getHeldSlotBook(Player player, boolean handMatters, boolean mainHand, - boolean typeMatters, boolean writtenBook) { + boolean typeMatters, boolean writtenBook) { BookHoldingState state = getBookHoldingState(player); ItemStack mainHandItem = getHeldItem(player, true); ItemStack offHandItem = getHeldItem(player, false); @@ -118,7 +120,7 @@ public class InventoryHelper { return ItemSlot.MAIN_HAND; } else if ((writtenBook && (state == BookHoldingState.SIGNED_OFF_HAND || state == BookHoldingState.UNSIGNED_MAIN_HAND_SIGNED_OFF_HAND)) || (!writtenBook && ( - state == BookHoldingState.UNSIGNED_OFF_HAND) || + state == BookHoldingState.UNSIGNED_OFF_HAND) || state == BookHoldingState.SIGNED_MAIN_HAND_UNSIGNED_OFF_HAND)) { return ItemSlot.OFF_HAND; } @@ -134,6 +136,7 @@ public class InventoryHelper { /** * Gets the state of the player's book holding from all possible states + * * @param player

The player to check

* @return

The state of the player's book holding

*/ diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 7223c8e..1268247 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,15 +3,15 @@ version: '${project.version}' main: net.knarcraft.bookswithoutborders.BooksWithoutBorders api-version: 1.17 prefix: Books Without Borders -authors: [EpicKnarvik97, AkiraAkiba] +authors: [ EpicKnarvik97, AkiraAkiba ] description: A continuation of the original Books Without Borders -softdepend: [Vault] +softdepend: [ Vault ] website: ???? dev-url: https://git.knarcraft.net/EpicKnarvik97/Books-Without-Borders commands: bookswithoutborders: description: Lists Books Without Borders's commands and uses. - aliases: [bwb] + aliases: [ bwb ] usage: / give: description: Gives the held book to another player