diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index 169bfd6..0b74ede 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -418,11 +418,12 @@ public class BooksWithoutBorders extends JavaPlugin { return false; } - if (player.getItemInHand().getType() == Material.WRITTEN_BOOK || player.getItemInHand().getType() == Material.WRITABLE_BOOK) { + if (InventoryHelper.isHoldingBook(player, true) || + InventoryHelper.isHoldingBook(player, false)) { if (args.length == 2) { saveBook(player, args[1], true); } else { - saveBook((Player) sender, "false", true); + saveBook(player, "false", true); } return true; } else { @@ -462,7 +463,7 @@ public class BooksWithoutBorders extends JavaPlugin { } if (args.length == 1) { - loadList.put(sender.getName(), listFiles(sender, true, 0, false)); + loadList.put(sender.getName(), listFiles(sender, true, false)); return true; } @@ -470,7 +471,7 @@ public class BooksWithoutBorders extends JavaPlugin { if (!Character.isDigit(args[1].charAt(x))) break; if (x == args[1].length() - 1) - loadList.put(sender.getName(), listFiles(sender, true, 0, true)); + loadList.put(sender.getName(), listFiles(sender, true, true)); } ItemStack newBook; @@ -511,7 +512,7 @@ public class BooksWithoutBorders extends JavaPlugin { } if (args.length == 1) { - loadList.put(sender.getName(), listFiles(sender, false, 0, false)); + loadList.put(sender.getName(), listFiles(sender, false, false)); return true; } @@ -519,7 +520,7 @@ public class BooksWithoutBorders extends JavaPlugin { if (!Character.isDigit(args[1].charAt(x))) break; if (x == args[1].length() - 1) - loadList.put(sender.getName(), listFiles(sender, false, 0, true)); + loadList.put(sender.getName(), listFiles(sender, false, true)); } ItemStack newBook; @@ -783,7 +784,7 @@ public class BooksWithoutBorders extends JavaPlugin { } if (args.length == 1) { - loadList.put(sender.getName(), listFiles(sender, true, 0, false)); + loadList.put(sender.getName(), listFiles(sender, true, false)); return true; } @@ -791,7 +792,7 @@ public class BooksWithoutBorders extends JavaPlugin { if (!Character.isDigit(args[1].charAt(x))) break; if (x == args[1].length() - 1) - loadList.put(sender.getName(), listFiles(sender, true, 0, true)); + loadList.put(sender.getName(), listFiles(sender, true, true)); } ItemStack newBook; @@ -843,7 +844,7 @@ public class BooksWithoutBorders extends JavaPlugin { //lists delete-able files if (args.length == 1) { - loadList.put(sender.getName(), listFiles(sender, true, 0, false)); + loadList.put(sender.getName(), listFiles(sender, true, false)); return true; } //actual deletion @@ -980,8 +981,9 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Executes the copy command + * * @param player
The player which sent the command
- * @param argsThe arguments given
+ * @param argsThe arguments given
* @returnTrue if the command was executed successfully
*/ protected boolean commandCopy(Player player, String[] args) { @@ -1032,6 +1034,7 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Executes the un-sign command + * * @param playerThe player which executed the command
* @returnTrue if the command was executed successfully
*/ @@ -1054,8 +1057,9 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Executes the delete command + * * @param playerThe player which sent the command
- * @param argsThe arguments given
+ * @param argsThe arguments given
* @returnTrue if the command was executed successfully
*/ protected boolean commandDelete(Player player, String[] args) { @@ -1066,7 +1070,7 @@ public class BooksWithoutBorders extends JavaPlugin { //List deletable files if (args.length == 1) { - loadList.put(player.getName(), listFiles(player, false, 0, false)); + loadList.put(player.getName(), listFiles(player, false, false)); return true; } //Delete the file @@ -1090,8 +1094,9 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Executes the give command + * * @param playerThe player which sent the command
- * @param argsThe arguments given
+ * @param argsThe arguments given
* @returnTrue if the command was executed successfully
*/ protected boolean commandGive(Player player, String[] args) { @@ -1106,7 +1111,7 @@ public class BooksWithoutBorders extends JavaPlugin { } if (args.length == 1) { - loadList.put(player.getName(), listFiles(player, false, 0, false)); + loadList.put(player.getName(), listFiles(player, false, false)); return true; } @@ -1114,7 +1119,7 @@ public class BooksWithoutBorders extends JavaPlugin { if (!Character.isDigit(args[1].charAt(x))) break; if (x == args[1].length() - 1) - loadList.put(player.getName(), listFiles(player, false, 0, true)); + loadList.put(player.getName(), listFiles(player, false, true)); } ItemStack newBook; @@ -1303,6 +1308,7 @@ public class BooksWithoutBorders extends JavaPlugin { } protected void saveBook(Player player, String dupe, Boolean pub) { + //Notice: Could be both a signed or unsigned book BookMeta book = (BookMeta) player.getItemInHand().getItemMeta(); String path; if (pub) { @@ -1589,40 +1595,28 @@ public class BooksWithoutBorders extends JavaPlugin { } } - ItemStack nubook = new ItemStack(Material.WRITTEN_BOOK);//Book(book.getAuthor(), book.getTitle(), pages, 1, 387); - nubook.setItemMeta(book); - nubook.setAmount(player.getItemInHand().getAmount()); - return nubook; + ItemStack newBook = new ItemStack(Material.WRITTEN_BOOK);//Book(book.getAuthor(), book.getTitle(), pages, 1, 387); + newBook.setItemMeta(book); + newBook.setAmount(InventoryHelper.getHeldBook(player).getAmount()); + return newBook; } - protected ListThe command sender looking for files
+ * @param listPublicWhether to list public or personal files
+ * @param silentWhether to just return the list without printing it
+ * @returnA list of available files
+ */ + protected ListThe player encrypting the book
+ * + * @param playerThe player encrypting the book
* @param mainHandWhether the player is holding the book in its main hand
- * @param keyThe key/password to use for encryption
- * @param styleThe encryption style to use
+ * @param keyThe key/password to use for encryption
+ * @param styleThe encryption style to use
* @returnAn encrypted version of the book
*/ protected ItemStack encryptBook(Player player, boolean mainHand, String key, EncryptionStyle style) { @@ -1651,10 +1646,11 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Encrypts a book - * @param playerThe player encrypting the book
- * @param mainHandWhether the player is holding the book in its main hand
- * @param keyThe key/password to use for encryption
- * @param styleThe encryption style to use
+ * + * @param playerThe player encrypting the book
+ * @param mainHandWhether the player is holding the book in its main hand
+ * @param keyThe key/password to use for encryption
+ * @param styleThe encryption style to use
* @param groupNameThe name of the group to encrypt for, or "" otherwise
* @returnAn encrypted version of the book
*/ @@ -1694,10 +1690,11 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Creates a new encrypted book - * @param bookThe 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 bookThe 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
*/ @@ -1717,9 +1714,10 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Saves a book's plain text to a file - * @param groupNameThe group who's allowed to decrypt the book, or ""
- * @param playerThe player trying to encrypt the book
- * @param bookThe book to encrypt
+ * + * @param groupNameThe 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
*/ @@ -1741,10 +1739,11 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Sends a success message to a command sender (player or a console) - * @param senderThe sender to send the message to
+ * + * @param senderThe sender to send the message to
* @param messageThe message to send
*/ - private void sendSuccessMessage(CommandSender sender, String message) { + public static void sendSuccessMessage(CommandSender sender, String message) { sender.sendMessage(successColor + message); } @@ -1816,7 +1815,7 @@ public class BooksWithoutBorders extends JavaPlugin { ItemStack newBook = new ItemStack(Material.WRITABLE_BOOK); newBook.setItemMeta(oldBook); - InventoryHelper.replaceHeldBook(player, newBook, mainHand); + InventoryHelper.replaceHeldItem(player, newBook, mainHand); } /** diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBordersListener.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBordersListener.java index 0d64df2..1a6b6c1 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBordersListener.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBordersListener.java @@ -232,7 +232,7 @@ public class BooksWithoutBordersListener implements Listener { markGiveSignValidity(event, true); } else { if (isBookListIndex(lines[2])) { - ListThe string which might be a book index
* @returnTrue if the number is a book index
*/ @@ -255,14 +256,16 @@ public class BooksWithoutBordersListener implements Listener { File[] foundFiles = bookDirectory.listFiles(File::isFile); return parsedLine >= 0 && parsedLine <= Objects.requireNonNull(foundFiles).length; - } catch (NumberFormatException ignored) {} + } catch (NumberFormatException ignored) { + } return false; } /** * Marks a give sign as valid or invalid - * @param eventThe event causing the creation of the give sign
+ * + * @param eventThe event causing the creation of the give sign
* @param isValidWhether the created sign is valid
*/ private void markGiveSignValidity(SignChangeEvent event, boolean isValid) { @@ -289,7 +292,7 @@ public class BooksWithoutBordersListener implements Listener { //Handles loadList numbers if (isBookListIndex(bookName)) { - ListThe path to a book
* @returnTrue if the file exists and points to a book file
*/ @@ -15,6 +22,14 @@ public class FileHelper { new File(bookFile + ".yml").isFile()) && !bookFile.contains("../") && !bookFile.contains("..\\"); } + /** + * Gets a book file given its path + * + *This function adds extensions to the path as necessary, returning the first match.
+ * + * @param bookPathThe path of the book to get
+ * @returnThe file the path points to, or null otherwise
+ */ public static File getBookFile(String bookPath) { if (!bookFileExists(bookPath)) { return null; @@ -38,4 +53,36 @@ public class FileHelper { return null; } + /** + * Lists available files + * + * @param senderThe command sender looking for files
+ * @param searchDirectoryThe directory to search for files
+ * @param silentWhether to just return the list without printing it
+ * @returnA list of available files
+ */ + public static ListThe player holding the book
* @returnThe book the player is holding
*/ public static ItemStack getHeldBook(Player player) { - BookHoldingState holdingState = getHoldingBookState(player); + return getHeldBook(player, true); + } + + /** + * Gets the book the holder is playing + * + * @param playerThe player holding the book
+ * @param signedBookWhether to check for signed or unsigned books
+ * @returnThe book the player is holding
+ */ + public static ItemStack getHeldBook(Player player, boolean signedBook) { + BookHoldingState holdingState; + if (signedBook) { + holdingState = getHoldingBookState(player); + } else { + holdingState = getHoldingUnsignedBookState(player); + } ItemStack heldBook; if (holdingState == BookHoldingState.MAIN_HAND) { - heldBook = getHeldBook(player, true); + heldBook = getHeldItem(player, true); } else if (holdingState == BookHoldingState.OFF_HAND) { - heldBook = getHeldBook(player, false); + heldBook = getHeldItem(player, false); } else { throw new IllegalArgumentException("The player is not holding exactly one book."); } @@ -29,18 +46,20 @@ public class InventoryHelper { /** * Replaces the book the player is holding - * @param playerThe player holding the book
+ * + * @param playerThe player holding the book
* @param newBookThe new book the player should hold
*/ public static void setHeldBook(Player player, ItemStack newBook) { BookHoldingState holdingState = getHoldingBookState(player); - replaceHeldBook(player, newBook, holdingState == BookHoldingState.MAIN_HAND); + replaceHeldItem(player, newBook, holdingState == BookHoldingState.MAIN_HAND); } /** * Performs checks to validate that a player contains exactly one book - * @param playerThe player to validate
- * @param noBookMessageThe message to display if the player is not holding a book
+ * + * @param playerThe player to validate
+ * @param noBookMessageThe message to display if the player is not holding a book
* @param twoBooksMessageThe message to display if the player is holding one book in each hand
* @returnFalse if the player is holding exactly one book
*/ @@ -60,14 +79,54 @@ public class InventoryHelper { return false; } + /** + * Check whether the player is holding one book + * + * @param playerThe player possibly holding a book
+ * @param signedBookWhether to look for signed or unsigned books
+ * @returnTrue if the player is holding one book
+ */ + public static boolean isHoldingBook(Player player, boolean signedBook) { + BookHoldingState holdingState; + if (signedBook) { + holdingState = getHoldingBookState(player); + } else { + holdingState = getHoldingUnsignedBookState(player); + } + + return holdingState == BookHoldingState.MAIN_HAND || holdingState == BookHoldingState.OFF_HAND; + } + /** * Gets the state of which hands of the player contains a book + * * @param playerThe player possibly holding a book
* @returnThe state of the player's book holding
*/ public static BookHoldingState getHoldingBookState(Player player) { - boolean hasBookInMainHand = player.getInventory().getItemInMainHand().getType() == Material.WRITTEN_BOOK; - boolean hasBookInOffHand = player.getInventory().getItemInOffHand().getType() == Material.WRITTEN_BOOK; + return getHoldingItemState(player, Material.WRITTEN_BOOK); + } + + /** + * Gets the state of which hands of the player contains a writable book + * + * @param playerThe player possibly holding a book
+ * @returnThe state of the player's book holding
+ */ + public static BookHoldingState getHoldingUnsignedBookState(Player player) { + return getHoldingItemState(player, Material.WRITABLE_BOOK); + } + + /** + * Gets the state of which hands of the player contains an item type + * + * @param playerThe player possibly holding the item
+ * @param typeThe type of material to look for
+ * @returnThe state of the player's item holding
+ */ + private static BookHoldingState getHoldingItemState(Player player, Material type) { + boolean hasBookInMainHand = player.getInventory().getItemInMainHand().getType() == type; + boolean hasBookInOffHand = player.getInventory().getItemInOffHand().getType() == type; if (hasBookInMainHand && hasBookInOffHand) { return BookHoldingState.BOTH_HANDS; @@ -88,16 +147,17 @@ public class InventoryHelper { * @returnInformation about the held book
*/ public static BookMeta getHeldBookMetadata(Player player, boolean mainHand) { - return (BookMeta) getHeldBook(player, mainHand).getItemMeta(); + return (BookMeta) getHeldItem(player, mainHand).getItemMeta(); } /** * Gets the item the player is holding - * @param playerThe player to get from
+ * + * @param playerThe player to get from
* @param mainHandWhether to get the item in the player's main hand or off hand
* @returnThe item the player is holding in the given hand
*/ - public static ItemStack getHeldBook(Player player, boolean mainHand) { + public static ItemStack getHeldItem(Player player, boolean mainHand) { if (mainHand) { return player.getInventory().getItemInMainHand(); } else { @@ -112,7 +172,7 @@ public class InventoryHelper { * @param newBookThe new book the player should hold
* @param mainHandWhether to replace the item in the player's main hand or off hand
*/ - public static void replaceHeldBook(Player player, ItemStack newBook, boolean mainHand) { + public static void replaceHeldItem(Player player, ItemStack newBook, boolean mainHand) { if (mainHand) { player.getInventory().setItemInMainHand(newBook); } else {