diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index 1cdcb52..9d3e7e6 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -13,8 +13,13 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import net.knarcraft.bookswithoutborders.command.CommandCopy; import net.knarcraft.bookswithoutborders.command.CommandDecrypt; +import net.knarcraft.bookswithoutborders.command.CommandDelete; +import net.knarcraft.bookswithoutborders.command.CommandEncrypt; import net.knarcraft.bookswithoutborders.command.CommandGive; +import net.knarcraft.bookswithoutborders.command.CommandGroupEncrypt; +import net.knarcraft.bookswithoutborders.command.CommandUnSign; import net.knarcraft.bookswithoutborders.command.GiveTabCompleter; import net.knarcraft.bookswithoutborders.utility.BookFormatter; import net.knarcraft.bookswithoutborders.utility.EncryptionHelper; @@ -52,7 +57,7 @@ public class BooksWithoutBorders extends JavaPlugin { protected static Economy eco; protected static Material bookPriceType = null; protected static double bookPriceQuantity; - protected static boolean authorOnlyCopy; + public static boolean authorOnlyCopy; protected static boolean useYml; public static boolean adminDecrypt; protected static ItemFactory iF; @@ -86,6 +91,11 @@ public class BooksWithoutBorders extends JavaPlugin { giveCommand.setTabCompleter(new GiveTabCompleter(this)); } this.getCommand("decrypt").setExecutor(new CommandDecrypt(this)); + this.getCommand("groupencrypt").setExecutor(new CommandGroupEncrypt(this)); + this.getCommand("delete").setExecutor(new CommandDelete(this)); + this.getCommand("copy").setExecutor(new CommandCopy(this)); + this.getCommand("unsign").setExecutor(new CommandUnSign(this)); + this.getCommand("encrypt").setExecutor(new CommandEncrypt(this)); } protected boolean init() { @@ -563,22 +573,6 @@ public class BooksWithoutBorders extends JavaPlugin { } } - 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); - case "encrypt": - return commandEncrypt(player, args); - case "groupEncrypt": - return commandGroupEncrypt(player, args); - } - if (args[0].equalsIgnoreCase("setTitle")) { if (!sender.hasPermission("bookswithoutborders.settitle")) { sendErrorMessage(sender, " You don't have permission to use this command!"); @@ -853,301 +847,6 @@ public class BooksWithoutBorders extends JavaPlugin { return false; } - /** - * Executes the group encrypt command - * - * @param player

The player which sent the command

- * @param args

The arguments given

- * @return

True if the command was executed successfully

- */ - private boolean commandGroupEncrypt(Player player, String[] args) { - if (!player.hasPermission("bookswithoutborders.groupencrypt")) { - sendErrorMessage(player, " You don't have permission to use this command!"); - 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 < 3) { - sendErrorMessage(player, "You must specify a key to encrypt a book!"); - return false; - } - if (args.length > 4) { - sendErrorMessage(player, "Too many command options specified!"); - return false; - } - - ItemStack heldBook = InventoryHelper.getHeldBook(player); - BookMeta bookMetadata = (BookMeta) heldBook.getItemMeta(); - - if (bookMetadata == null) { - sendErrorMessage(player, "Your book seems to be corrupt!"); - return false; - } - - if (!bookMetadata.hasPages()) { - sendErrorMessage(player, "Book must have contents to encrypt!"); - return false; - } - - List lore = bookMetadata.getLore(); - if (bookMetadata.hasLore() && lore != null && lore.get(0).contains(" encrypted]")) { - sendErrorMessage(player, "Book is already group encrypted!"); - return false; - } - - ItemStack eBook; - - if (args.length == 4) { - eBook = encryptBook(player, true, args[2], EncryptionStyle.getFromString(args[3]), args[1]); - } else { - eBook = encryptBook(player, true, args[2], EncryptionStyle.SUBSTITUTION, args[1]); - } - - if (eBook != null) { - InventoryHelper.setHeldBook(player, eBook); - return true; - } else { - return false; - } - } - - /** - * Executes the encrypt command - * - * @param player

The player which sent the command

- * @param args

The arguments given

- * @return

True if the command was executed successfully

- */ - private 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.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) { - 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

- * @param args

The arguments given

- * @return

True if the command was executed successfully

- */ - private boolean commandCopy(Player player, String[] args) { - if (!player.hasPermission("bookswithoutborders.copy")) { - sendErrorMessage(player, " You don't have permission to use this command!"); - return false; - } - - if (InventoryHelper.notHoldingOneWrittenBookCheck(player, "You must be holding a written book to copy it!", - "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 = InventoryHelper.getHeldBook(player); - - 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

- */ - private boolean commandUnSign(Player player) { - if (!player.hasPermission("bookswithoutborders.unsign")) { - sendErrorMessage(player, " You don't have permission to use this command!"); - return false; - } - - if (InventoryHelper.notHoldingOneWrittenBookCheck(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 - BookHoldingState holdingState = InventoryHelper.getHoldingSignedBookState(player); - unSignHeldBook(player, holdingState == BookHoldingState.MAIN_HAND); - return true; - } - - /** - * 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

- */ - private 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(player.getName(), listFiles(player, false, false)); - return true; - } - //Delete the file - if (args.length == 2) { - 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(player.getName()).isEmpty()) { - deleteBook(player, args[1], false); - return true; - } else { - sendErrorMessage(player, "No files available to delete!"); - return false; - } - } - } - sendErrorMessage(player, "Incorrect number of arguments for this command!"); - return false; - } - - /** - * Executes the give command - * - * @param player

The player which sent the command

- * @param args

The arguments given

- * @return

True if the command was executed successfully

- */ - private 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(player, "Incorrect number of arguments for this command!"); - return false; - } - - if (args.length == 1) { - loadList.put(player.getName(), listFiles(player, false, false)); - return true; - } - - for (int x = 0; x < args[1].length(); x++) { - if (!Character.isDigit(args[1].charAt(x))) - break; - if (x == args[1].length() - 1) - loadList.put(player.getName(), listFiles(player, false, true)); - } - - ItemStack newBook; - Player receivingPlayer = this.getServer().getPlayer(args[2]); - if (receivingPlayer == null) { - sendErrorMessage(player, "Player not found!"); - return false; - } - - if (receivingPlayer.getInventory().firstEmpty() == -1) { - sendErrorMessage(player, "Receiving player must have space in their inventory to receive books!"); - return false; - } - - //bwb give [bookname] [player] [numCopies] [issigned] - try { - - if (args.length == 5) - 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(player, cleanString(args[1]), args[3], "player"); - else - newBook = loadBook(player, cleanString(args[1]), "true", "player", Integer.parseInt(args[3])); - } else - newBook = loadBook(player, cleanString(args[1]), "true", "player"); - - if (newBook != null) { - receivingPlayer.getInventory().addItem(newBook); - sendSuccessMessage(player, "Book sent!"); - sendSuccessMessage(receivingPlayer, "Book received!"); - return true; - } else { - sendErrorMessage(player, "Book failed to load!"); - return false; - } - } catch (NumberFormatException e) { - sendErrorMessage(player, "Invalid number of book copies specified!"); - return false; - } - } - public String cleanString(String fname) { //removes illegal characters if (fname.contains("/")) { @@ -1643,7 +1342,7 @@ public class BooksWithoutBorders extends JavaPlugin { * @param groupName

The name of the group to encrypt for, or "" otherwise

* @return

An encrypted version of the book

*/ - protected ItemStack encryptBook(Player player, boolean mainHand, String key, EncryptionStyle style, String groupName) { + public ItemStack encryptBook(Player player, boolean mainHand, String key, EncryptionStyle style, String groupName) { //converts user supplied key into integer form String integerKey = EncryptionHelper.getNumberKeyFromStringKey(key); @@ -1752,7 +1451,7 @@ public class BooksWithoutBorders extends JavaPlugin { * @param fileName

The file name of the book

* @param isPublic

Whether the book to delete is public or not

*/ - protected void deleteBook(CommandSender sender, String fileName, Boolean isPublic) { + public void deleteBook(CommandSender sender, String fileName, Boolean isPublic) { //If the file name is an index of the load list, load the book try { int loadListIndex = Integer.parseInt(fileName); @@ -1795,7 +1494,7 @@ public class BooksWithoutBorders extends JavaPlugin { * @param player

The player holding the book

* @param mainHand

Whether the player is holding the book in its main hand or its off hand

*/ - protected void unSignHeldBook(Player player, boolean mainHand) { + public void unSignHeldBook(Player player, boolean mainHand) { //Get the old book BookMeta oldBook = InventoryHelper.getHeldBookMetadata(player, mainHand); @@ -1811,7 +1510,7 @@ public class BooksWithoutBorders extends JavaPlugin { * * @return

True if players need to pay for printing books

*/ - protected boolean booksHavePrice() { + public boolean booksHavePrice() { return (bookPriceType != null && bookPriceQuantity > 0); } @@ -1822,7 +1521,7 @@ public class BooksWithoutBorders extends JavaPlugin { * @param numCopies

The number of copies the player is trying to print

* @return

True if the player cannot pay for the printing of the books

*/ - protected boolean cannotPayForBookPrinting(Player player, int numCopies) { + public boolean cannotPayForBookPrinting(Player player, int numCopies) { //BookPriceQuantity: How many items are required to pay for each book //BookPriceType: Which item is used to pay for the books. AIR = use economy double cost = bookPriceQuantity * numCopies; @@ -1894,7 +1593,7 @@ public class BooksWithoutBorders extends JavaPlugin { * @param book

The book to check

* @return

True if the player is the book's author

*/ - protected boolean isAuthor(Player player, BookMeta book) { + public boolean isAuthor(Player player, BookMeta book) { String author = book.getAuthor(); if (author != null && cleanString(player.getName()).equalsIgnoreCase(cleanString(author))) { return true; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java index 4541598..7179538 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java @@ -1,12 +1,70 @@ package net.knarcraft.bookswithoutborders.command; +import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +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.Objects; public class CommandCopy implements CommandExecutor { + + private final BooksWithoutBorders booksWithoutBorders; + + public CommandCopy(BooksWithoutBorders booksWithoutBorders) { + this.booksWithoutBorders = booksWithoutBorders; + } + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - return false; + 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 copy it!", + "You cannot copy two books at once!")) { + return false; + } + + if (args.length < 1) { + BooksWithoutBorders.sendErrorMessage(player, "You must specifiy the number of copies to be made!"); + return false; + } + + ItemStack heldBook = InventoryHelper.getHeldBook(player); + + try { + int copies = Integer.parseInt(args[0]); + if (copies > 0) { + if (BooksWithoutBorders.authorOnlyCopy && !player.hasPermission("bookswithoutborders.bypassauthoronlycopy")) { + if (!booksWithoutBorders.isAuthor(player, (BookMeta) Objects.requireNonNull(heldBook.getItemMeta()))) + return false; + } + + if (booksWithoutBorders.booksHavePrice() && + !player.hasPermission("bookswithoutborders.bypassbookprice") && + booksWithoutBorders.cannotPayForBookPrinting(player, copies)) { + return false; + } + + heldBook.setAmount(heldBook.getAmount() + copies); + BooksWithoutBorders.sendSuccessMessage(player, "Book copied!"); + } else { + BooksWithoutBorders.sendErrorMessage(player, "Book not copied!"); + BooksWithoutBorders.sendErrorMessage(player, "Number specified was invalid!"); + return false; + } + } catch (NumberFormatException e) { + BooksWithoutBorders.sendErrorMessage(player, "Book not copied!"); + BooksWithoutBorders.sendErrorMessage(player, "Number specified was invalid!"); + return false; + } + return true; } + } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java index ca09485..18a3883 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java @@ -1,12 +1,48 @@ package net.knarcraft.bookswithoutborders.command; +import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; public class CommandDelete implements CommandExecutor { + + private final BooksWithoutBorders booksWithoutBorders; + + public CommandDelete(BooksWithoutBorders booksWithoutBorders) { + this.booksWithoutBorders = 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; + } + + //List deletable files + if (args.length == 0) { + BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, false, false)); + return true; + } + //Delete the file + if (args.length == 1) { + if (!BooksWithoutBorders.loadList.containsKey(player.getName())) { + BooksWithoutBorders.sendErrorMessage(player, "You must first use /bwb delete to create a list of delete-able files!"); + return false; + } else { + if (!BooksWithoutBorders.loadList.get(player.getName()).isEmpty()) { + booksWithoutBorders.deleteBook(player, args[0], false); + return true; + } else { + BooksWithoutBorders.sendErrorMessage(player, "No files available to delete!"); + return false; + } + } + } + BooksWithoutBorders.sendErrorMessage(player, "Incorrect number of arguments for this command!"); return false; } + } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java index 1b8fb99..b3067e4 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java @@ -1,12 +1,66 @@ package net.knarcraft.bookswithoutborders.command; +import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.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.Objects; public class CommandEncrypt implements CommandExecutor { + + private final BooksWithoutBorders booksWithoutBorders; + + public CommandEncrypt(BooksWithoutBorders booksWithoutBorders) { + this.booksWithoutBorders = booksWithoutBorders; + } + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - return false; + 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 < 1) { + BooksWithoutBorders.sendErrorMessage(player, "You must specify a key to encrypt a book!"); + return false; + } + if (args.length > 2) { + BooksWithoutBorders.sendErrorMessage(player, "Too many command options specified!"); + return false; + } + + ItemStack heldBook = InventoryHelper.getHeldBook(player); + + if (!((BookMeta) Objects.requireNonNull(heldBook.getItemMeta())).hasPages()) { + BooksWithoutBorders.sendErrorMessage(player, "Book must have contents to encrypt!"); + return false; + } + + 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, ""); + } + + if (encryptedBook != null) { + InventoryHelper.setHeldBook(player, encryptedBook); + return true; + } else { + return false; + } } } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java index d158156..df1d44c 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java @@ -7,6 +7,9 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +/** + * Command executor for the give command + */ public class CommandGive implements CommandExecutor { private final BooksWithoutBorders booksWithoutBorders; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGroupEncrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGroupEncrypt.java index b0d37f6..0774424 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGroupEncrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGroupEncrypt.java @@ -1,12 +1,83 @@ package net.knarcraft.bookswithoutborders.command; +import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.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; + +/** + * Command executor for the group encrypt command + */ public class CommandGroupEncrypt implements CommandExecutor { + + private final BooksWithoutBorders booksWithoutBorders; + + public CommandGroupEncrypt(BooksWithoutBorders booksWithoutBorders) { + this.booksWithoutBorders = booksWithoutBorders; + } + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - return false; + 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); + BookMeta bookMetadata = (BookMeta) heldBook.getItemMeta(); + + 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; + } + + List lore = bookMetadata.getLore(); + if (bookMetadata.hasLore() && lore != null && lore.get(0).contains(" encrypted]")) { + BooksWithoutBorders.sendErrorMessage(player, "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.setHeldBook(player, eBook); + return true; + } else { + return false; + } } + } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java new file mode 100644 index 0000000..d77b3ee --- /dev/null +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java @@ -0,0 +1,36 @@ +package net.knarcraft.bookswithoutborders.command; + +import net.knarcraft.bookswithoutborders.BookHoldingState; +import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +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; + +public class CommandUnSign implements CommandExecutor { + + private final BooksWithoutBorders booksWithoutBorders; + + public CommandUnSign(BooksWithoutBorders booksWithoutBorders) { + this.booksWithoutBorders = 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 signed 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 + BookHoldingState holdingState = InventoryHelper.getHoldingSignedBookState(player); + booksWithoutBorders.unSignHeldBook(player, holdingState == BookHoldingState.MAIN_HAND); + return true; + } +} diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnsign.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnsign.java deleted file mode 100644 index 983a67e..0000000 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnsign.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.knarcraft.bookswithoutborders.command; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; - -public class CommandUnsign implements CommandExecutor { - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - return false; - } -} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0e35061..104052e 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -14,13 +14,33 @@ commands: aliases: [bwb] usage: / give: - description: Gives the held book to another player - usage: / [file name or number] [# of copies (num)] [signed (true/false)] - permission: bookswithoutborders.give + description: Gives the held book to another player + usage: / [# of copies (num)] [signed (true/false)] + permission: bookswithoutborders.give decrypt: - description: Encrypts the held book - usage: / - permission: bookswithoutborders.decrypt + description: Encrypts the held book + usage: / + permission: bookswithoutborders.decrypt + groupencrypt: + description: Encrypts the held book for the given group + usage: / [encryption style] + permission: bookswithoutborders.groupencrypt + delete: + description: Deletes a book saved by the executing user + usage: / + permission: bookswithoutborders.delete + copy: + description: Copies the held book + usage: / + permission: bookswithoutborders.copy + unsign: + description: Un-signs the held book, making it writable + usage: / + permission: bookswithoutborders.unsign + encrypt: + description: Encrypts the held book + usage: / [encryption style] + permission: bookswithoutborders.encrypt permissions: bookswithoutborders.admin: description: Grants all permissions