From 8defb301bcaaf7eadfdd1928ccdea4bf3da335ec Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 1 Sep 2021 22:32:33 +0200 Subject: [PATCH] Removes redundancy between the encrypt command and the group encrypt command --- .../command/CommandEncrypt.java | 67 +++++++++++++------ .../command/CommandGroupEncrypt.java | 57 +++------------- 2 files changed, 55 insertions(+), 69 deletions(-) diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java index 9652b74..ef69cbc 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java @@ -10,8 +10,6 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; -import java.util.Objects; - /** * Command executor for the encrypt command */ @@ -25,39 +23,67 @@ public class CommandEncrypt implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (performPreChecks(sender, args, 1, "You must specify a key to encrypt a book!") == null) { + return false; + } + + EncryptionStyle encryptionStyle = args.length == 2 ? EncryptionStyle.getFromString(args[1]) : EncryptionStyle.SUBSTITUTION; + return encryptBook(encryptionStyle, (Player) sender, args[0], ""); + } + + /** + * Performs necessary pre-checks before going through with the encryption + * @param sender

The sender trying to encrypt a book

+ * @param args

The arguments given

+ * @param necessaryArguments

How many arguments is the minimum requirement

+ * @param missingArgumentsError

The error to show if a required argument is missing

+ * @return

The metadata of the book to encrypt, or null if any checks fail

+ */ + BookMeta performPreChecks(CommandSender sender, String[] args, int necessaryArguments, String missingArgumentsError) { if (!(sender instanceof Player player)) { BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!"); - return false; + return null; } - if (InventoryHelper.notHoldingOneWrittenBookCheck(player, "You must be holding a written book to encrypt it!", + if (InventoryHelper.notHoldingOneWrittenBookCheck(player, + "You must be holding a written book to encrypt it!", "You cannot encrypt two books at once!")) { - return false; + return null; } - if (args.length < 1) { - BooksWithoutBorders.sendErrorMessage(player, "You must specify a key to encrypt a book!"); - return false; + int argumentCount = args.length; + if (argumentCount < necessaryArguments) { + BooksWithoutBorders.sendErrorMessage(player, missingArgumentsError); + return null; } - if (args.length > 2) { + if (argumentCount > necessaryArguments + 1) { BooksWithoutBorders.sendErrorMessage(player, "Too many command options specified!"); - return false; + return null; } ItemStack heldBook = InventoryHelper.getHeldBook(player, true); - - if (!((BookMeta) Objects.requireNonNull(heldBook.getItemMeta())).hasPages()) { + BookMeta bookMetadata = (BookMeta) heldBook.getItemMeta(); + if (bookMetadata == null) { + BooksWithoutBorders.sendErrorMessage(player, "Your book seems to be corrupt!"); + return null; + } + if (!bookMetadata.hasPages()) { BooksWithoutBorders.sendErrorMessage(player, "Book must have contents to encrypt!"); - return false; + return null; } + return bookMetadata; + } - ItemStack encryptedBook; - - if (args.length == 2) { - encryptedBook = booksWithoutBorders.encryptBook(player, true, args[0], EncryptionStyle.getFromString(args[1]), ""); - } else { - encryptedBook = booksWithoutBorders.encryptBook(player, true, args[0], EncryptionStyle.SUBSTITUTION, ""); - } + /** + * Encrypts the given book + * @param encryptionStyle

The encryption style to use

+ * @param player

The player encrypting the book

+ * @param key

The encryption key to use

+ * @param group

The group to encrypt for

+ * @return

True if the book was encrypted successfully

+ */ + boolean encryptBook(EncryptionStyle encryptionStyle, Player player, String key, String group) { + ItemStack encryptedBook = booksWithoutBorders.encryptBook(player, true, key, encryptionStyle, group); if (encryptedBook != null) { InventoryHelper.setHeldWrittenBook(player, encryptedBook); @@ -66,4 +92,5 @@ public class CommandEncrypt implements CommandExecutor { return false; } } + } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGroupEncrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGroupEncrypt.java index c1cb201..68f2059 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGroupEncrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGroupEncrypt.java @@ -2,12 +2,10 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.state.EncryptionStyle; -import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; import java.util.List; @@ -15,69 +13,30 @@ import java.util.List; /** * Command executor for the group encrypt command */ -public class CommandGroupEncrypt implements CommandExecutor { - - private final BooksWithoutBorders booksWithoutBorders; +public class CommandGroupEncrypt extends CommandEncrypt implements CommandExecutor { public CommandGroupEncrypt(BooksWithoutBorders booksWithoutBorders) { - this.booksWithoutBorders = booksWithoutBorders; + super(booksWithoutBorders); } @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (!(sender instanceof Player player)) { - BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!"); - return false; - } - - if (InventoryHelper.notHoldingOneWrittenBookCheck(player, - "You must be holding a written book to encrypt it!", - "You cannot encrypt two books at once!")) { - return false; - } - - if (args.length < 2) { - BooksWithoutBorders.sendErrorMessage(player, "You must specify a group name and key to encrypt a book!"); - return false; - } - if (args.length > 3) { - BooksWithoutBorders.sendErrorMessage(player, "Too many command options specified!"); - return false; - } - - ItemStack heldBook = InventoryHelper.getHeldBook(player, true); - BookMeta bookMetadata = (BookMeta) heldBook.getItemMeta(); + BookMeta bookMetadata = performPreChecks(sender, args, 2, + "You must specify a group name and key to encrypt a book!"); if (bookMetadata == null) { - BooksWithoutBorders.sendErrorMessage(player, "Your book seems to be corrupt!"); - return false; - } - - if (!bookMetadata.hasPages()) { - BooksWithoutBorders.sendErrorMessage(player, "Book must have contents to encrypt!"); return false; } + //Check if book is already group encrypted List lore = bookMetadata.getLore(); if (bookMetadata.hasLore() && lore != null && lore.get(0).contains(" encrypted]")) { - BooksWithoutBorders.sendErrorMessage(player, "Book is already group encrypted!"); + BooksWithoutBorders.sendErrorMessage(sender, "Book is already group encrypted!"); return false; } - ItemStack eBook; - - if (args.length == 3) { - eBook = booksWithoutBorders.encryptBook(player, true, args[1], EncryptionStyle.getFromString(args[2]), args[0]); - } else { - eBook = booksWithoutBorders.encryptBook(player, true, args[1], EncryptionStyle.SUBSTITUTION, args[0]); - } - - if (eBook != null) { - InventoryHelper.setHeldWrittenBook(player, eBook); - return true; - } else { - return false; - } + EncryptionStyle encryptionStyle = args.length == 3 ? EncryptionStyle.getFromString(args[2]) : EncryptionStyle.SUBSTITUTION; + return encryptBook(encryptionStyle, (Player) sender, args[1], args[0]); } }