Removes redundancy between the encrypt command and the group encrypt command

This commit is contained in:
Kristian Knarvik 2021-09-01 22:32:33 +02:00
parent b021cc3471
commit 8defb301bc
2 changed files with 55 additions and 69 deletions

View File

@ -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 <p>The sender trying to encrypt a book</p>
* @param args <p>The arguments given</p>
* @param necessaryArguments <p>How many arguments is the minimum requirement</p>
* @param missingArgumentsError <p>The error to show if a required argument is missing</p>
* @return <p>The metadata of the book to encrypt, or null if any checks fail</p>
*/
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 <p>The encryption style to use</p>
* @param player <p>The player encrypting the book</p>
* @param key <p>The encryption key to use</p>
* @param group <p>The group to encrypt for</p>
* @return <p>True if the book was encrypted successfully</p>
*/
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;
}
}
}

View File

@ -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<String> 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]);
}
}