diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index 0e97642..a472764 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -10,10 +10,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; -import java.util.ListIterator; import java.util.Map; import java.util.Objects; +import net.knarcraft.bookswithoutborders.utility.BookFormatter; import net.knarcraft.bookswithoutborders.utility.EncryptionHelper; import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.milkbowl.vault.economy.Economy; @@ -549,94 +549,11 @@ public class BooksWithoutBorders extends JavaPlugin { } if (args[0].equalsIgnoreCase("give")) { - if (!sender.hasPermission("bookswithoutborders.give")) { - sendErrorMessage(sender, " You don't have permission to use this command!"); - return false; - } - - if (args.length == 2 || args.length > 5) { - sendErrorMessage(sender, "Incorrect number of arguments for this command!"); - return false; - } - - if (args.length == 1) { - loadList.put(sender.getName(), listFiles(sender, false, 0, false)); - return true; - } - - for (int x = 0; x < args[1].length(); x++) { - if (!Character.isDigit(args[1].charAt(x))) - break; - if (x == args[1].length() - 1) - loadList.put(sender.getName(), listFiles(sender, false, 0, true)); - } - - ItemStack newBook; - Player receivingPlayer = this.getServer().getPlayer(args[2]); - if (receivingPlayer == null) { - sendErrorMessage(sender, "Player not found!"); - return false; - } - - if (receivingPlayer.getInventory().firstEmpty() == -1) { - sendErrorMessage(sender, "Receiving player must have space in their inventory to receive books!"); - return false; - } - - //bwb give [bookname] [player] [numCopies] [issigned] - try { - - if (args.length == 5) - newBook = loadBook(sender, cleanString(args[1]), args[4], "player", Integer.parseInt(args[3])); - else if (args.length == 4) { - if (args[3].equalsIgnoreCase("true") || args[3].equalsIgnoreCase("false")) - newBook = loadBook(sender, cleanString(args[1]), args[3], "player"); - else - newBook = loadBook(sender, cleanString(args[1]), "true", "player", Integer.parseInt(args[3])); - } else - newBook = loadBook(sender, cleanString(args[1]), "true", "player"); - - if (newBook != null) { - receivingPlayer.getInventory().addItem(newBook); - sendSuccessMessage(sender, "Book sent!"); - sendSuccessMessage(receivingPlayer, "Book received!"); - return true; - } else { - sendErrorMessage(sender, "Book failed to load!"); - return false; - } - } catch (NumberFormatException e) { - sendErrorMessage(sender, "Invalid number of book copies specified!"); - return false; - } + return commandGive(sender, args); } if (args[0].equalsIgnoreCase("delete")) { - if (!sender.hasPermission("bookswithoutborders.delete")) { - sendErrorMessage(sender, " You don't have permission to use this command!"); - return false; - } - - //lists delete-able files - if (args.length == 1) { - loadList.put(sender.getName(), listFiles(sender, false, 0, false)); - return true; - } - //actual deletion - if (args.length == 2) { - if (!loadList.containsKey(sender.getName())) { - sendErrorMessage(sender, "You must first use /bwb delete to create a list of delete-able files!"); - return false; - } else { - if (!loadList.get(sender.getName()).isEmpty()) { - deleteBook(sender, args[1], false); - return true; - } else { - sendErrorMessage(sender, "No files available to delete!"); - return false; - } - } - } + return commandDelete(sender, args); } if (args[0].equalsIgnoreCase("unsign")) { @@ -1122,6 +1039,111 @@ public class BooksWithoutBorders extends JavaPlugin { return false; } + /** + * Executes the delete command + * @param sender
The command sender which sent the command
+ * @param argsThe arguments given
+ * @returnTrue if the command was executed successfully
+ */ + protected boolean commandDelete(CommandSender sender, String[] args) { + if (!sender.hasPermission("bookswithoutborders.delete")) { + sendErrorMessage(sender, " You don't have permission to use this command!"); + return false; + } + + //List deletable files + if (args.length == 1) { + loadList.put(sender.getName(), listFiles(sender, false, 0, false)); + return true; + } + //Delete the file + if (args.length == 2) { + if (!loadList.containsKey(sender.getName())) { + sendErrorMessage(sender, "You must first use /bwb delete to create a list of delete-able files!"); + return false; + } else { + if (!loadList.get(sender.getName()).isEmpty()) { + deleteBook(sender, args[1], false); + return true; + } else { + sendErrorMessage(sender, "No files available to delete!"); + return false; + } + } + } + sendErrorMessage(sender, "Incorrect number of arguments for this command!"); + return false; + } + + /** + * Executes the give command + * @param senderThe command sender which sent the command
+ * @param argsThe arguments given
+ * @returnTrue if the command was executed successfully
+ */ + protected boolean commandGive(CommandSender sender, String[] args) { + if (!sender.hasPermission("bookswithoutborders.give")) { + sendErrorMessage(sender, " You don't have permission to use this command!"); + return false; + } + + if (args.length == 2 || args.length > 5) { + sendErrorMessage(sender, "Incorrect number of arguments for this command!"); + return false; + } + + if (args.length == 1) { + loadList.put(sender.getName(), listFiles(sender, false, 0, false)); + return true; + } + + for (int x = 0; x < args[1].length(); x++) { + if (!Character.isDigit(args[1].charAt(x))) + break; + if (x == args[1].length() - 1) + loadList.put(sender.getName(), listFiles(sender, false, 0, true)); + } + + ItemStack newBook; + Player receivingPlayer = this.getServer().getPlayer(args[2]); + if (receivingPlayer == null) { + sendErrorMessage(sender, "Player not found!"); + return false; + } + + if (receivingPlayer.getInventory().firstEmpty() == -1) { + sendErrorMessage(sender, "Receiving player must have space in their inventory to receive books!"); + return false; + } + + //bwb give [bookname] [player] [numCopies] [issigned] + try { + + if (args.length == 5) + newBook = loadBook(sender, cleanString(args[1]), args[4], "player", Integer.parseInt(args[3])); + else if (args.length == 4) { + if (args[3].equalsIgnoreCase("true") || args[3].equalsIgnoreCase("false")) + newBook = loadBook(sender, cleanString(args[1]), args[3], "player"); + else + newBook = loadBook(sender, cleanString(args[1]), "true", "player", Integer.parseInt(args[3])); + } else + newBook = loadBook(sender, cleanString(args[1]), "true", "player"); + + if (newBook != null) { + receivingPlayer.getInventory().addItem(newBook); + sendSuccessMessage(sender, "Book sent!"); + sendSuccessMessage(receivingPlayer, "Book received!"); + return true; + } else { + sendErrorMessage(sender, "Book failed to load!"); + return false; + } + } catch (NumberFormatException e) { + sendErrorMessage(sender, "Invalid number of book copies specified!"); + return false; + } + } + protected String cleanString(String fname) { //removes illegal characters if (fname.contains("/")) { @@ -1227,32 +1249,32 @@ public class BooksWithoutBorders extends JavaPlugin { String nullcheck; try { - FileReader fr = new FileReader(file); - BufferedReader br = new BufferedReader(fr); + FileReader fileReader = new FileReader(file); + BufferedReader bufferedReader = new BufferedReader(fileReader); - rawPages.add(br.readLine()); + rawPages.add(bufferedReader.readLine()); if (rawPages.get(0) == null) { - br.close(); + bufferedReader.close(); return null; } //if a book is being loaded its content need not be adjusted if (rawPages.get(0).equalsIgnoreCase("[Book]")) { rawPages.remove(0); - nullcheck = br.readLine(); + nullcheck = bufferedReader.readLine(); while (nullcheck != null) { rawPages.add(nullcheck); - nullcheck = br.readLine(); + nullcheck = bufferedReader.readLine(); } - } else//adjusts content to page length - { + } else { + //adjusts content to page length while (rawPages.get(rawPages.size() - 1) != null) { - rawPages = pageFormat(rawPages); - rawPages.add(br.readLine()); + BookFormatter.formatLastPage(rawPages); + rawPages.add(bufferedReader.readLine()); } } - br.close(); + bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); return null; @@ -1590,50 +1612,6 @@ public class BooksWithoutBorders extends JavaPlugin { return fileList; } - protected ListThe book to encrypt
+ * @param newPagesThe new encrypted pages
+ * @param mainHandWhether the book is held in the player's main hand or off hand
+ * @param playerThe player encrypting the book
+ * @param newMetadataThe new metadata of the book
+ * @returnAn encrypted version of the book
+ */ + private ItemStack createEncryptedBook(BookMeta book, ListThe group who's allowed to decrypt the book, or ""
+ * @param playerThe player trying to encrypt the book
+ * @param bookThe book to encrypt
+ * @param integerKeyThe key used to encrypt the book
+ * @returnThe new metadata for the book, or null if it could not be saved
+ */ + private BookMeta saveBookPlaintext(String groupName, Player player, BookMeta book, String integerKey) { + BookMeta newMetadata = book; + boolean wasSaved; + if (groupName.trim().isEmpty()) { + wasSaved = eSave(player, book, integerKey); + } else { + newMetadata = groupESave(player, book, integerKey, groupName); + wasSaved = newMetadata != null; + } + if (wasSaved) { + return newMetadata; + } else { + return null; + } + } + /** * Sends a success message to a command sender (player or a console) * @param senderThe sender to send the message to
@@ -1750,7 +1745,7 @@ public class BooksWithoutBorders extends JavaPlugin { * @param senderThe sender to send the message to
* @param messageThe message to send
*/ - private void sendErrorMessage(CommandSender sender, String message) { + public static void sendErrorMessage(CommandSender sender, String message) { sender.sendMessage(errorColor + message); }