diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index 6f6e0d4..169bfd6 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -16,6 +16,7 @@ import java.util.Objects; import net.knarcraft.bookswithoutborders.utility.BookFormatter; import net.knarcraft.bookswithoutborders.utility.EncryptionHelper; import net.knarcraft.bookswithoutborders.utility.FileHelper; +import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import net.milkbowl.vault.economy.Economy; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -558,46 +559,8 @@ public class BooksWithoutBorders extends JavaPlugin { return commandUnSign(player); case "copy": return commandCopy(player, args); - } - - if (args[0].equalsIgnoreCase("encrypt")) { - if (!sender.hasPermission("bookswithoutborders.encrypt")) { - sendErrorMessage(sender, " You don't have permission to use this command!"); - return false; - } - - if (!(((Player) sender).getItemInHand().getType() == Material.WRITTEN_BOOK)) { - sendErrorMessage(sender, "You must be holding a written book to encrypt it!"); - return false; - } - - if (args.length < 2) { - sendErrorMessage(sender, "You must specify a key to encrypt a book!"); - return false; - } - if (args.length > 3) { - sendErrorMessage(sender, "Too many command options specified!"); - return false; - } - - if (!((BookMeta) ((Player) sender).getItemInHand().getItemMeta()).hasPages()) { - sendErrorMessage(sender, "Book must have contents to encrypt!"); - return false; - } - - ItemStack eBook; - - if (args.length == 3) { - eBook = encryptBook((Player) sender, true, args[1], EncryptionStyle.getFromString(args[2]), ""); - } else { - eBook = encryptBook((Player) sender, true, args[1], EncryptionStyle.SUBSTITUTION, ""); - } - - if (eBook != null) { - ((Player) sender).setItemInHand(eBook); - return true; - } else - return false; + case "encrypt": + return commandEncrypt(player, args); } if (args[0].equalsIgnoreCase("groupEncrypt")) { @@ -973,6 +936,48 @@ public class BooksWithoutBorders extends JavaPlugin { return false; } + protected boolean commandEncrypt(Player player, String[] args) { + if (!player.hasPermission("bookswithoutborders.encrypt")) { + sendErrorMessage(player, " You don't have permission to use this command!"); + return false; + } + + if (InventoryHelper.notHoldingOneBookCheck(player, "You must be holding a written book to encrypt it!", + "You cannot encrypt two books at once!")) { + return false; + } + + if (args.length < 2) { + sendErrorMessage(player, "You must specify a key to encrypt a book!"); + return false; + } + if (args.length > 3) { + sendErrorMessage(player, "Too many command options specified!"); + return false; + } + + ItemStack heldBook = InventoryHelper.getHeldBook(player); + + if (!((BookMeta) Objects.requireNonNull(heldBook.getItemMeta())).hasPages()) { + sendErrorMessage(player, "Book must have contents to encrypt!"); + return false; + } + + ItemStack encryptedBook; + + if (args.length == 3) { + encryptedBook = encryptBook(player, true, args[1], EncryptionStyle.getFromString(args[2]), ""); + } else { + encryptedBook = encryptBook(player, true, args[1], EncryptionStyle.SUBSTITUTION, ""); + } + + if (encryptedBook != null) { + InventoryHelper.setHeldBook(player, encryptedBook); + return true; + } else + return false; + } + /** * Executes the copy command * @param player

The player which sent the command

@@ -985,15 +990,8 @@ public class BooksWithoutBorders extends JavaPlugin { return false; } - BookHoldingState holdingState = holdingBook(player); - - if (holdingState == BookHoldingState.NONE) { - sendErrorMessage(player, "You must be holding a written book to copy it!"); - return false; - } - - if (holdingState == BookHoldingState.BOTH_HANDS) { - sendErrorMessage(player, "You cannot copy two books at once!"); + if (InventoryHelper.notHoldingOneBookCheck(player, "You must be holding a written book to copy it!", + "You cannot copy two books at once!")) { return false; } @@ -1002,12 +1000,7 @@ public class BooksWithoutBorders extends JavaPlugin { return false; } - ItemStack heldBook; - if (holdingState == BookHoldingState.MAIN_HAND) { - heldBook = player.getInventory().getItemInMainHand(); - } else { - heldBook = player.getInventory().getItemInOffHand(); - } + ItemStack heldBook = InventoryHelper.getHeldBook(player); try { if (Integer.parseInt(args[1]) > 0) { @@ -1048,43 +1041,17 @@ public class BooksWithoutBorders extends JavaPlugin { return false; } - BookHoldingState holdingState = holdingBook(player); - - if (holdingState == BookHoldingState.NONE) { - sendErrorMessage(player, "You must be holding a written book to un-sign it!"); + if (InventoryHelper.notHoldingOneBookCheck(player, "You must be holding a written book to un-sign it!", + "You cannot un-sign two books at once. Please un-equip one of the books you're holding!")) { return false; } //Find which hand the player is using to hold the book. If holding one in each, throw an error - if (holdingState == BookHoldingState.BOTH_HANDS) { - sendErrorMessage(player, "You cannot un-sign two books at once. Please un-equip one " + - "of the books you're holding!"); - } else { - unSignHeldBook(player, holdingState == BookHoldingState.MAIN_HAND); - } + BookHoldingState holdingState = InventoryHelper.getHoldingBookState(player); + unSignHeldBook(player, holdingState == BookHoldingState.MAIN_HAND); return true; } - /** - * 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

- */ - protected BookHoldingState holdingBook(Player player) { - boolean hasBookInMainHand = player.getInventory().getItemInMainHand().getType() == Material.WRITTEN_BOOK; - boolean hasBookInOffHand = player.getInventory().getItemInOffHand().getType() == Material.WRITTEN_BOOK; - - if (hasBookInMainHand && hasBookInOffHand) { - return BookHoldingState.BOTH_HANDS; - } else if (hasBookInMainHand) { - return BookHoldingState.MAIN_HAND; - } else if (hasBookInOffHand) { - return BookHoldingState.OFF_HAND; - } else { - return BookHoldingState.NONE; - } - } - /** * Executes the delete command * @param player

The player which sent the command

@@ -1695,7 +1662,7 @@ public class BooksWithoutBorders extends JavaPlugin { //converts user supplied key into integer form String integerKey = EncryptionHelper.getNumberKeyFromStringKey(key); - BookMeta book = getHeldBookMetadata(player, mainHand); + BookMeta book = InventoryHelper.getHeldBookMetadata(player, mainHand); if (!book.hasPages()) { sendErrorMessage(player, "Book is empty!"); @@ -1741,11 +1708,7 @@ public class BooksWithoutBorders extends JavaPlugin { encryptedBook.setItemMeta(book); //Update item amount - if (mainHand) { - encryptedBook.setAmount(player.getInventory().getItemInMainHand().getAmount()); - } else { - encryptedBook.setAmount(player.getInventory().getItemInOffHand().getAmount()); - } + encryptedBook.setAmount(InventoryHelper.getHeldBook(player).getAmount()); //Set new item metadata encryptedBook.setItemMeta(newMetadata); @@ -1847,43 +1810,13 @@ public class BooksWithoutBorders extends JavaPlugin { */ protected void unSignHeldBook(Player player, boolean mainHand) { //Get the old book - BookMeta oldBook = getHeldBookMetadata(player, mainHand); + BookMeta oldBook = InventoryHelper.getHeldBookMetadata(player, mainHand); //UnSign the book ItemStack newBook = new ItemStack(Material.WRITABLE_BOOK); newBook.setItemMeta(oldBook); - replaceHeldBook(player, newBook, mainHand); - } - - /** - * Gets metadata about the player's held book - * - * @param player

The player holding the book

- * @param mainHand

Whether to get information about a book in the player's main hand or off hand

- * @return

Information about the held book

- */ - private BookMeta getHeldBookMetadata(Player player, boolean mainHand) { - if (mainHand) { - return (BookMeta) player.getInventory().getItemInMainHand().getItemMeta(); - } else { - return (BookMeta) player.getInventory().getItemInOffHand().getItemMeta(); - } - } - - /** - * Replaces the player's held item - * - * @param player

The player to replace the item for

- * @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

- */ - private void replaceHeldBook(Player player, ItemStack newBook, boolean mainHand) { - if (mainHand) { - player.getInventory().setItemInMainHand(newBook); - } else { - player.getInventory().setItemInOffHand(newBook); - } + InventoryHelper.replaceHeldBook(player, newBook, mainHand); } /** diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryHelper.java new file mode 100644 index 0000000..526535f --- /dev/null +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryHelper.java @@ -0,0 +1,123 @@ +package net.knarcraft.bookswithoutborders.utility; + +import net.knarcraft.bookswithoutborders.BookHoldingState; +import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; + +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); + ItemStack heldBook; + if (holdingState == BookHoldingState.MAIN_HAND) { + heldBook = getHeldBook(player, true); + } else if (holdingState == BookHoldingState.OFF_HAND) { + heldBook = getHeldBook(player, false); + } else { + throw new IllegalArgumentException("The player is not holding exactly one book."); + } + return heldBook; + } + + /** + * Replaces the book the player is holding + * @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); + } + + /** + * 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 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

+ */ + public static boolean notHoldingOneBookCheck(Player player, String noBookMessage, String twoBooksMessage) { + BookHoldingState holdingState = getHoldingBookState(player); + + if (holdingState == BookHoldingState.NONE) { + BooksWithoutBorders.sendErrorMessage(player, noBookMessage); + return true; + } + + if (holdingState == BookHoldingState.BOTH_HANDS) { + BooksWithoutBorders.sendErrorMessage(player, twoBooksMessage); + return true; + } + + return false; + } + + /** + * 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; + + if (hasBookInMainHand && hasBookInOffHand) { + return BookHoldingState.BOTH_HANDS; + } else if (hasBookInMainHand) { + return BookHoldingState.MAIN_HAND; + } else if (hasBookInOffHand) { + return BookHoldingState.OFF_HAND; + } else { + return BookHoldingState.NONE; + } + } + + /** + * Gets metadata about the player's held book + * + * @param player

The player holding the book

+ * @param mainHand

Whether to get information about a book in the player's main hand or off hand

+ * @return

Information about the held book

+ */ + public static BookMeta getHeldBookMetadata(Player player, boolean mainHand) { + return (BookMeta) getHeldBook(player, mainHand).getItemMeta(); + } + + /** + * Gets the item the player is holding + * @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) { + if (mainHand) { + return player.getInventory().getItemInMainHand(); + } else { + return player.getInventory().getItemInOffHand(); + } + } + + /** + * Replaces the player's held item + * + * @param player

The player to replace the item for

+ * @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) { + if (mainHand) { + player.getInventory().setItemInMainHand(newBook); + } else { + player.getInventory().setItemInOffHand(newBook); + } + } + +}