Removes redundancy between the encrypt command and the group encrypt command
This commit is contained in:
parent
b021cc3471
commit
8defb301bc
@ -10,8 +10,6 @@ import org.bukkit.entity.Player;
|
|||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command executor for the encrypt command
|
* Command executor for the encrypt command
|
||||||
*/
|
*/
|
||||||
@ -25,39 +23,67 @@ public class CommandEncrypt implements CommandExecutor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
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)) {
|
if (!(sender instanceof Player player)) {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a 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!")) {
|
"You cannot encrypt two books at once!")) {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length < 1) {
|
int argumentCount = args.length;
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "You must specify a key to encrypt a book!");
|
if (argumentCount < necessaryArguments) {
|
||||||
return false;
|
BooksWithoutBorders.sendErrorMessage(player, missingArgumentsError);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
if (args.length > 2) {
|
if (argumentCount > necessaryArguments + 1) {
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Too many command options specified!");
|
BooksWithoutBorders.sendErrorMessage(player, "Too many command options specified!");
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
|
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
|
||||||
|
BookMeta bookMetadata = (BookMeta) heldBook.getItemMeta();
|
||||||
if (!((BookMeta) Objects.requireNonNull(heldBook.getItemMeta())).hasPages()) {
|
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!");
|
BooksWithoutBorders.sendErrorMessage(player, "Book must have contents to encrypt!");
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
return bookMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
ItemStack encryptedBook;
|
/**
|
||||||
|
* Encrypts the given book
|
||||||
if (args.length == 2) {
|
* @param encryptionStyle <p>The encryption style to use</p>
|
||||||
encryptedBook = booksWithoutBorders.encryptBook(player, true, args[0], EncryptionStyle.getFromString(args[1]), "");
|
* @param player <p>The player encrypting the book</p>
|
||||||
} else {
|
* @param key <p>The encryption key to use</p>
|
||||||
encryptedBook = booksWithoutBorders.encryptBook(player, true, args[0], EncryptionStyle.SUBSTITUTION, "");
|
* @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) {
|
if (encryptedBook != null) {
|
||||||
InventoryHelper.setHeldWrittenBook(player, encryptedBook);
|
InventoryHelper.setHeldWrittenBook(player, encryptedBook);
|
||||||
@ -66,4 +92,5 @@ public class CommandEncrypt implements CommandExecutor {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,10 @@ package net.knarcraft.bookswithoutborders.command;
|
|||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
|
import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -15,69 +13,30 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
* Command executor for the group encrypt command
|
* Command executor for the group encrypt command
|
||||||
*/
|
*/
|
||||||
public class CommandGroupEncrypt implements CommandExecutor {
|
public class CommandGroupEncrypt extends CommandEncrypt implements CommandExecutor {
|
||||||
|
|
||||||
private final BooksWithoutBorders booksWithoutBorders;
|
|
||||||
|
|
||||||
public CommandGroupEncrypt(BooksWithoutBorders booksWithoutBorders) {
|
public CommandGroupEncrypt(BooksWithoutBorders booksWithoutBorders) {
|
||||||
this.booksWithoutBorders = booksWithoutBorders;
|
super(booksWithoutBorders);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||||
if (!(sender instanceof Player player)) {
|
BookMeta bookMetadata = performPreChecks(sender, args, 2,
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
"You must specify a group name and key to encrypt a book!");
|
||||||
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();
|
|
||||||
|
|
||||||
if (bookMetadata == null) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Check if book is already group encrypted
|
||||||
List<String> lore = bookMetadata.getLore();
|
List<String> lore = bookMetadata.getLore();
|
||||||
if (bookMetadata.hasLore() && lore != null && lore.get(0).contains(" encrypted]")) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemStack eBook;
|
EncryptionStyle encryptionStyle = args.length == 3 ? EncryptionStyle.getFromString(args[2]) : EncryptionStyle.SUBSTITUTION;
|
||||||
|
return encryptBook(encryptionStyle, (Player) sender, args[1], args[0]);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user