From 9209e3f52ddeec29bcc6a4ab9787700f895ba954 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sun, 29 Aug 2021 19:18:31 +0200 Subject: [PATCH] Cleans a lot of code --- .../BooksWithoutBorders.java | 351 +++++++++--------- 1 file changed, 173 insertions(+), 178 deletions(-) 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 args

The arguments given

+ * @return

True 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 sender

The command sender which sent the command

+ * @param args

The arguments given

+ * @return

True 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 List pageFormat(List rawPages) { - //adds newline if content does not exceed page limit - if (rawPages.get(rawPages.size() - 1).length() <= 254 && !rawPages.get(rawPages.size() - 1).isEmpty()) { - rawPages.set(rawPages.size() - 1, (rawPages.get(rawPages.size() - 1)) + "\n"); - } - - //combines lines until page limit is reached - if (rawPages.size() > 1) { - if (rawPages.get(rawPages.size() - 2).length() + rawPages.get(rawPages.size() - 1).length() <= 256) { - rawPages.set(rawPages.size() - 2, (rawPages.get(rawPages.size() - 2)) + (rawPages.get(rawPages.size() - 1))); - rawPages.remove(rawPages.size() - 1); - } - } - - - //splits page if it is too long - if (rawPages.get(rawPages.size() - 1).length() > 256) { - int lS;//the last space before a pagebreak - while (rawPages.get(rawPages.size() - 1).length() > 256) { - //avoiding word split requires spaces - if (rawPages.get(rawPages.size() - 1).substring(0, 256).contains(" ")) { - lS = rawPages.get(rawPages.size() - 1).substring(0, 256).lastIndexOf(" "); - rawPages.add(rawPages.get(rawPages.size() - 1).substring(lS + 1)); - //adds newline if content does not exceed page limit - if (rawPages.get(rawPages.size() - 1).length() <= 254) { - rawPages.set(rawPages.size() - 1, (rawPages.get(rawPages.size() - 1)) + "\n"); - } - //removes excess page content - rawPages.set(rawPages.size() - 2, rawPages.get(rawPages.size() - 2).substring(0, lS)); - } else//if solid wall of text - { - rawPages.add(rawPages.get(rawPages.size() - 1).substring(256)); - //adds newline if content does not exceed page limit - if (rawPages.get(rawPages.size() - 1).length() <= 254) { - rawPages.set(rawPages.size() - 1, (rawPages.get(rawPages.size() - 1)) + "\n"); - } - //removes excess page content - rawPages.set(rawPages.size() - 2, rawPages.get(rawPages.size() - 2).substring(0, 256)); - } - } - } - return rawPages; - } - /** * Removes null and empty items from a list * @@ -1678,46 +1656,41 @@ public class BooksWithoutBorders extends JavaPlugin { return null; } - BookMeta newMetadata = null; - //Save the book's un-encrypted contents to a file - boolean wasSaved; - if (groupName.trim().isEmpty()) { - wasSaved = eSave(player, book, integerKey); - } else { - wasSaved = (newMetadata = groupESave(player, book, integerKey, groupName)) != null; - } - if (!wasSaved) { + BookMeta newMetadata = saveBookPlaintext(groupName, player, book, integerKey); + if (newMetadata == null) { return null; } - ItemStack encryptedBook; - List encryptedPages = new ArrayList<>(); - List newPages; - - //Scramble the book's contents - if (style == EncryptionStyle.DNA) { - //Encrypt the pages using gene-based encryption - GenenCrypt gc = new GenenCrypt(integerKey); - for (int x = 0; x < book.getPages().size(); x++) { - encryptedPages.add(gc.encrypt(book.getPage(x + 1))); - } - } else if (style == EncryptionStyle.SUBSTITUTION) { - //Encrypt the pages using a substitution cipher - SubstitutionCipher sc = new SubstitutionCipher(); - for (int x = 0; x < book.getPages().size(); x++) { - encryptedPages.add(sc.encrypt(book.getPage(x + 1), integerKey)); - } - } else { - sendErrorMessage(player, "Invalid encryption style encountered!"); + //Get the encrypted pages + List encryptedPages = EncryptionHelper.encryptBookPages(book, style, integerKey, player); + if (encryptedPages == null) { return null; } - //Change the book text to the encrypted text - encryptedPages = pageFormat(encryptedPages); - newPages = cleanList(encryptedPages); + //Format the last page just in case + BookFormatter.formatLastPage(encryptedPages); + //Remove empty pages + List newPages = cleanList(encryptedPages); + + ItemStack encryptedBook = createEncryptedBook(book, newPages, mainHand, player, newMetadata); + sendSuccessMessage(player, "Book encrypted!"); - encryptedBook = new ItemStack(Material.WRITTEN_BOOK); + return encryptedBook; + } + + /** + * Creates a new encrypted book + * @param book

The book to encrypt

+ * @param newPages

The new encrypted pages

+ * @param mainHand

Whether the book is held in the player's main hand or off hand

+ * @param player

The player encrypting the book

+ * @param newMetadata

The new metadata of the book

+ * @return

An encrypted version of the book

+ */ + private ItemStack createEncryptedBook(BookMeta book, List newPages, boolean mainHand, Player player, BookMeta newMetadata) { + //Create the encrypted book + ItemStack encryptedBook = new ItemStack(Material.WRITTEN_BOOK); book.setPages(newPages); encryptedBook.setItemMeta(book); @@ -1727,14 +1700,36 @@ public class BooksWithoutBorders extends JavaPlugin { } else { encryptedBook.setAmount(player.getInventory().getItemInOffHand().getAmount()); } - //Set new item metadata if using group encryption - if (newMetadata != null) { - encryptedBook.setItemMeta(newMetadata); - } + //Set new item metadata + encryptedBook.setItemMeta(newMetadata); return encryptedBook; } + /** + * Saves a book's plain text to a file + * @param groupName

The group who's allowed to decrypt the book, or ""

+ * @param player

The player trying to encrypt the book

+ * @param book

The book to encrypt

+ * @param integerKey

The key used to encrypt the book

+ * @return

The 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 sender

The sender to send the message to

@@ -1750,7 +1745,7 @@ public class BooksWithoutBorders extends JavaPlugin { * @param sender

The sender to send the message to

* @param message

The message to send

*/ - private void sendErrorMessage(CommandSender sender, String message) { + public static void sendErrorMessage(CommandSender sender, String message) { sender.sendMessage(errorColor + message); }