From d50af8a12b41116bad9d4853f56dbaa9000894b7 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sun, 29 Aug 2021 22:37:11 +0200 Subject: [PATCH] Adds an enum to easier keep track of which hands of a player contain a book --- .../bookswithoutborders/BookHoldingState.java | 25 ++ .../BooksWithoutBorders.java | 265 +++++++++++------- 2 files changed, 181 insertions(+), 109 deletions(-) create mode 100644 src/main/java/net/knarcraft/bookswithoutborders/BookHoldingState.java diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BookHoldingState.java b/src/main/java/net/knarcraft/bookswithoutborders/BookHoldingState.java new file mode 100644 index 0000000..0bdc4aa --- /dev/null +++ b/src/main/java/net/knarcraft/bookswithoutborders/BookHoldingState.java @@ -0,0 +1,25 @@ +package net.knarcraft.bookswithoutborders; + +/** + * This enum represents the possible states for players holding books + */ +public enum BookHoldingState { + + /** + * The player is holding one book in each hand + */ + BOTH_HANDS, + /** + * The player is holding one book in their main hand + */ + MAIN_HAND, + /** + * The player is holding one book in their off hand + */ + OFF_HAND, + /** + * The player is not holding any books + */ + NONE + +} diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index a472764..6f6e0d4 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -548,82 +548,16 @@ public class BooksWithoutBorders extends JavaPlugin { } } - if (args[0].equalsIgnoreCase("give")) { - return commandGive(sender, args); - } - - if (args[0].equalsIgnoreCase("delete")) { - return commandDelete(sender, args); - } - - if (args[0].equalsIgnoreCase("unsign")) { - if (!sender.hasPermission("bookswithoutborders.unsign")) { - 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 unsign it!"); - return false; - } - - boolean hasBookInMainHand = player.getInventory().getItemInMainHand().getType() == Material.WRITTEN_BOOK; - boolean hasBookInOffHand = player.getInventory().getItemInOffHand().getType() == Material.WRITTEN_BOOK; - - //Find which hand the player is using to hold the book. If holding one in each, throw an error - if (hasBookInMainHand && hasBookInOffHand) { - sendErrorMessage(sender, "You cannot un-sign two books at once. Please un-equip one " + - "of the books you're holding!"); - } else if (hasBookInMainHand) { - unSignHeldBook(player, true); - } else if (hasBookInOffHand) { - unSignHeldBook(player, false); - } - return true; - } - - if (args[0].equalsIgnoreCase("copy")) { - if (!sender.hasPermission("bookswithoutborders.copy")) { - 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 copy it!"); - return false; - } - - if (args.length < 2) { - sendErrorMessage(sender, "You must specifiy the number of copies to be made!"); - return false; - } - - try { - if (Integer.parseInt(args[1]) > 0) { - if (authorOnlyCopy && !sender.hasPermission("bookswithoutborders.bypassauthoronlycopy")) { - if (!isAuthor((Player) sender, (BookMeta) ((Player) sender).getItemInHand().getItemMeta())) - return false; - } - - if (booksHavePrice() && - !sender.hasPermission("bookswithoutborders.bypassbookprice") && - cannotPayForBookPrinting((Player) sender, Integer.parseInt(args[1]))) { - return false; - } - - ((Player) sender).getItemInHand().setAmount(((Player) sender).getItemInHand().getAmount() + Integer.parseInt(args[1])); - sendSuccessMessage(sender, "Book copied!"); - } else { - sendErrorMessage(sender, "Book not copied!"); - sendErrorMessage(sender, "Number specified was invalid!"); - return false; - } - } catch (NumberFormatException e) { - sendErrorMessage(sender, "Book not copied!"); - sendErrorMessage(sender, "Number specified was invalid!"); - return false; - } - return true; + String pluginCommand = args[0].toLowerCase(); + switch (pluginCommand) { + case "give": + return commandGive(player, args); + case "delete": + return commandDelete(player, args); + case "unsign": + return commandUnSign(player); + case "copy": + return commandCopy(player, args); } if (args[0].equalsIgnoreCase("encrypt")) { @@ -1040,60 +974,172 @@ public class BooksWithoutBorders extends JavaPlugin { } /** - * Executes the delete command - * @param sender

The command sender which sent the command

+ * Executes the copy command + * @param player

The player 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!"); + protected boolean commandCopy(Player player, String[] args) { + if (!player.hasPermission("bookswithoutborders.copy")) { + sendErrorMessage(player, " You don't have permission to use this command!"); + 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!"); + return false; + } + + if (args.length < 2) { + sendErrorMessage(player, "You must specifiy the number of copies to be made!"); + return false; + } + + ItemStack heldBook; + if (holdingState == BookHoldingState.MAIN_HAND) { + heldBook = player.getInventory().getItemInMainHand(); + } else { + heldBook = player.getInventory().getItemInOffHand(); + } + + try { + if (Integer.parseInt(args[1]) > 0) { + if (authorOnlyCopy && !player.hasPermission("bookswithoutborders.bypassauthoronlycopy")) { + if (!isAuthor(player, (BookMeta) Objects.requireNonNull(heldBook.getItemMeta()))) + return false; + } + + if (booksHavePrice() && + !player.hasPermission("bookswithoutborders.bypassbookprice") && + cannotPayForBookPrinting(player, Integer.parseInt(args[1]))) { + return false; + } + + heldBook.setAmount(heldBook.getAmount() + Integer.parseInt(args[1])); + sendSuccessMessage(player, "Book copied!"); + } else { + sendErrorMessage(player, "Book not copied!"); + sendErrorMessage(player, "Number specified was invalid!"); + return false; + } + } catch (NumberFormatException e) { + sendErrorMessage(player, "Book not copied!"); + sendErrorMessage(player, "Number specified was invalid!"); + return false; + } + return true; + } + + /** + * Executes the un-sign command + * @param player

The player which executed the command

+ * @return

True if the command was executed successfully

+ */ + protected boolean commandUnSign(Player player) { + if (!player.hasPermission("bookswithoutborders.unsign")) { + sendErrorMessage(player, " You don't have permission to use this command!"); + return false; + } + + BookHoldingState holdingState = holdingBook(player); + + if (holdingState == BookHoldingState.NONE) { + sendErrorMessage(player, "You must be holding a written book to un-sign it!"); + 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); + } + 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

+ * @param args

The arguments given

+ * @return

True if the command was executed successfully

+ */ + protected boolean commandDelete(Player player, String[] args) { + if (!player.hasPermission("bookswithoutborders.delete")) { + sendErrorMessage(player, " 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)); + loadList.put(player.getName(), listFiles(player, 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!"); + if (!loadList.containsKey(player.getName())) { + sendErrorMessage(player, "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); + if (!loadList.get(player.getName()).isEmpty()) { + deleteBook(player, args[1], false); return true; } else { - sendErrorMessage(sender, "No files available to delete!"); + sendErrorMessage(player, "No files available to delete!"); return false; } } } - sendErrorMessage(sender, "Incorrect number of arguments for this command!"); + sendErrorMessage(player, "Incorrect number of arguments for this command!"); return false; } /** * Executes the give command - * @param sender

The command sender which sent the command

+ * @param player

The player 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!"); + protected boolean commandGive(Player player, String[] args) { + if (!player.hasPermission("bookswithoutborders.give")) { + sendErrorMessage(player, " 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!"); + sendErrorMessage(player, "Incorrect number of arguments for this command!"); return false; } if (args.length == 1) { - loadList.put(sender.getName(), listFiles(sender, false, 0, false)); + loadList.put(player.getName(), listFiles(player, false, 0, false)); return true; } @@ -1101,18 +1147,18 @@ 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(player.getName(), listFiles(player, false, 0, true)); } ItemStack newBook; Player receivingPlayer = this.getServer().getPlayer(args[2]); if (receivingPlayer == null) { - sendErrorMessage(sender, "Player not found!"); + sendErrorMessage(player, "Player not found!"); return false; } if (receivingPlayer.getInventory().firstEmpty() == -1) { - sendErrorMessage(sender, "Receiving player must have space in their inventory to receive books!"); + sendErrorMessage(player, "Receiving player must have space in their inventory to receive books!"); return false; } @@ -1120,26 +1166,26 @@ public class BooksWithoutBorders extends JavaPlugin { try { if (args.length == 5) - newBook = loadBook(sender, cleanString(args[1]), args[4], "player", Integer.parseInt(args[3])); + newBook = loadBook(player, 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"); + newBook = loadBook(player, cleanString(args[1]), args[3], "player"); else - newBook = loadBook(sender, cleanString(args[1]), "true", "player", Integer.parseInt(args[3])); + newBook = loadBook(player, cleanString(args[1]), "true", "player", Integer.parseInt(args[3])); } else - newBook = loadBook(sender, cleanString(args[1]), "true", "player"); + newBook = loadBook(player, cleanString(args[1]), "true", "player"); if (newBook != null) { receivingPlayer.getInventory().addItem(newBook); - sendSuccessMessage(sender, "Book sent!"); + sendSuccessMessage(player, "Book sent!"); sendSuccessMessage(receivingPlayer, "Book received!"); return true; } else { - sendErrorMessage(sender, "Book failed to load!"); + sendErrorMessage(player, "Book failed to load!"); return false; } } catch (NumberFormatException e) { - sendErrorMessage(sender, "Invalid number of book copies specified!"); + sendErrorMessage(player, "Invalid number of book copies specified!"); return false; } } @@ -1259,7 +1305,7 @@ public class BooksWithoutBorders extends JavaPlugin { return null; } - //if a book is being loaded its content need not be adjusted + //If a book is being loaded its content need not be adjusted if (rawPages.get(0).equalsIgnoreCase("[Book]")) { rawPages.remove(0); nullcheck = bufferedReader.readLine(); @@ -1268,7 +1314,7 @@ public class BooksWithoutBorders extends JavaPlugin { nullcheck = bufferedReader.readLine(); } } else { - //adjusts content to page length + //Adjusts content to page length while (rawPages.get(rawPages.size() - 1) != null) { BookFormatter.formatLastPage(rawPages); rawPages.add(bufferedReader.readLine()); @@ -1582,9 +1628,9 @@ public class BooksWithoutBorders extends JavaPlugin { return nubook; } - protected List listFiles(CommandSender sender, Boolean pub, int offset, boolean silent) { + protected List listFiles(CommandSender sender, Boolean listPublic, int offset, boolean silent) { List fileList = new ArrayList<>(); - File file = (pub) ? new File(bookFolder) : + File file = (listPublic) ? new File(bookFolder) : new File(bookFolder + cleanString(sender.getName()) + SLASH); File[] fl = file.listFiles(); @@ -1605,7 +1651,7 @@ public class BooksWithoutBorders extends JavaPlugin { if (value.isFile()) { fileList.add(value.getName()); if (!silent) - sender.sendMessage(ChatColor.GRAY + "[" + (fileList.size() + offset) + "] " + value.getName()/*.substring(0, fl[x].getName().length()-4)*/); + sender.sendMessage(ChatColor.GRAY + "[" + (fileList.size() + offset) + "] " + value.getName()); } } @@ -1937,4 +1983,5 @@ public class BooksWithoutBorders extends JavaPlugin { sendErrorMessage(player, "You must be the author of this book to use this command!"); return false; } + }