From 6efbd377eebe2408aa57d32811e10f251602f9dd Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 30 Aug 2021 00:35:54 +0200 Subject: [PATCH] Adds more consideration to the existence of unsigned books --- .../BooksWithoutBorders.java | 125 +++++++++--------- .../BooksWithoutBordersListener.java | 13 +- .../utility/FileHelper.java | 47 +++++++ .../utility/InventoryHelper.java | 86 ++++++++++-- 4 files changed, 190 insertions(+), 81 deletions(-) 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 args

The arguments given

+ * @param args

The arguments given

* @return

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

The player which executed the command

* @return

True if the command was executed successfully

*/ @@ -1054,8 +1057,9 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Executes the delete command + * * @param player

The player which sent the command

- * @param args

The arguments given

+ * @param args

The arguments given

* @return

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

The player which sent the command

- * @param args

The arguments given

+ * @param args

The arguments given

* @return

True 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 List listFiles(CommandSender sender, Boolean listPublic, int offset, boolean silent) { - List fileList = new ArrayList<>(); - File file = (listPublic) ? new File(bookFolder) : - new File(bookFolder + cleanString(sender.getName()) + SLASH); - File[] fl = file.listFiles(); - - if (!file.exists()) { - sendErrorMessage(sender, "No books have been saved!"); - return null; + /** + * Lists available files + * + * @param sender

The command sender looking for files

+ * @param listPublic

Whether to list public or personal files

+ * @param silent

Whether to just return the list without printing it

+ * @return

A list of available files

+ */ + protected List listFiles(CommandSender sender, Boolean listPublic, boolean silent) { + File file; + if (listPublic) { + file = new File(bookFolder); + } else { + file = new File(bookFolder + cleanString(sender.getName()) + SLASH); } - - if (fl.length == 0) { - sendErrorMessage(sender, "No books have been saved!"); - return null; - } - - if (!silent) { - sendSuccessMessage(sender, "Available Books:"); - } - for (File value : fl) { - if (value.isFile()) { - fileList.add(value.getName()); - if (!silent) - sender.sendMessage(ChatColor.GRAY + "[" + (fileList.size() + offset) + "] " + value.getName()); - } - } - - return fileList; + return FileHelper.listFiles(sender, file, silent); } /** @@ -1639,10 +1633,11 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Encrypts a book - * @param player

The player encrypting the book

+ * + * @param player

The player encrypting the book

* @param mainHand

Whether the player is holding the book in its main hand

- * @param key

The key/password to use for encryption

- * @param style

The encryption style to use

+ * @param key

The key/password to use for encryption

+ * @param style

The encryption style to use

* @return

An 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 player

The player encrypting the book

- * @param mainHand

Whether the player is holding the book in its main hand

- * @param key

The key/password to use for encryption

- * @param style

The encryption style to use

+ * + * @param player

The player encrypting the book

+ * @param mainHand

Whether the player is holding the book in its main hand

+ * @param key

The key/password to use for encryption

+ * @param style

The encryption style to use

* @param groupName

The name of the group to encrypt for, or "" otherwise

* @return

An encrypted version of the book

*/ @@ -1694,10 +1690,11 @@ public class BooksWithoutBorders extends JavaPlugin { /** * 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 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

*/ @@ -1717,9 +1714,10 @@ public class BooksWithoutBorders extends JavaPlugin { /** * 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 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

*/ @@ -1741,10 +1739,11 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Sends a success message to a command sender (player or a console) - * @param sender

The sender to send the message to

+ * + * @param sender

The sender to send the message to

* @param message

The 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])) { - List availableFiles = BooksWithoutBorders.bwb.listFiles(player, true, 0, true); + List availableFiles = BooksWithoutBorders.bwb.listFiles(player, true, true); BooksWithoutBorders.loadList.put(player.getName(), availableFiles); markGiveSignValidity(event, true); return; @@ -243,6 +243,7 @@ public class BooksWithoutBordersListener implements Listener { /** * Check whether an integer points to a public book + * * @param possibleIndex

The string which might be a book index

* @return

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

The event causing the creation of the give sign

+ * + * @param event

The event causing the creation of the give sign

* @param isValid

Whether 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)) { - List availableFiles = BooksWithoutBorders.bwb.listFiles(player, true, 0, true); + List availableFiles = BooksWithoutBorders.bwb.listFiles(player, true, true); BooksWithoutBorders.loadList.put(player.getName(), availableFiles); } @@ -406,7 +409,7 @@ public class BooksWithoutBordersListener implements Listener { if (!Character.isDigit(fileName.charAt(x))) break; if (x == fileName.length() - 1) { - BooksWithoutBorders.loadList.put(player.getName(), BooksWithoutBorders.bwb.listFiles(player, true, 0, true)); + BooksWithoutBorders.loadList.put(player.getName(), BooksWithoutBorders.bwb.listFiles(player, true, true)); isLoadListNumber = true; } } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java index e75bea7..b944f6a 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java @@ -1,11 +1,18 @@ package net.knarcraft.bookswithoutborders.utility; +import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; + import java.io.File; +import java.util.ArrayList; +import java.util.List; public class FileHelper { /** * Checks if a file path points to an actual book + * * @param bookFile

The path to a book

* @return

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

The path of the book to get

+ * @return

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

The command sender looking for files

+ * @param searchDirectory

The directory to search for files

+ * @param silent

Whether to just return the list without printing it

+ * @return

A list of available files

+ */ + public static List listFiles(CommandSender sender, File searchDirectory, boolean silent) { + List fileList = new ArrayList<>(); + File[] existingFiles = searchDirectory.listFiles(); + + if (!searchDirectory.exists() || existingFiles == null || existingFiles.length == 0) { + BooksWithoutBorders.sendErrorMessage(sender, "No books have been saved!"); + return null; + } + + if (!silent) { + BooksWithoutBorders.sendSuccessMessage(sender, "Available Books:"); + } + for (File foundFile : existingFiles) { + if (foundFile.isFile()) { + fileList.add(foundFile.getName()); + if (!silent) { + sender.sendMessage(ChatColor.GRAY + "[" + fileList.size() + "] " + foundFile.getName()); + } + } + } + + return fileList; + } + } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryHelper.java index 526535f..789cb50 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryHelper.java @@ -11,16 +11,33 @@ public class InventoryHelper { /** * Gets the book the holder is playing + * * @param player

The player holding the book

* @return

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

The player holding the book

+ * @param signedBook

Whether to check for signed or unsigned books

+ * @return

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

The player holding the book

+ * + * @param player

The player holding the book

* @param newBook

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

The player to validate

- * @param noBookMessage

The message to display if the player is not holding a 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

* @return

False 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 player

The player possibly holding a book

+ * @param signedBook

Whether to look for signed or unsigned books

+ * @return

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

The player possibly holding a book

* @return

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

The player possibly holding a book

+ * @return

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

The player possibly holding the item

+ * @param type

The type of material to look for

+ * @return

The 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 { * @return

Information 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 player

The player to get from

+ * + * @param player

The player to get from

* @param mainHand

Whether to get the item in the player's main hand or off hand

* @return

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

The new book the player should hold

* @param mainHand

Whether 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 {