Moves more commands to their appropriate classes

This commit is contained in:
Kristian Knarvik 2021-08-30 18:06:19 +02:00
parent 504ef09192
commit fbee4a90b0
9 changed files with 304 additions and 339 deletions

View File

@ -13,8 +13,13 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import net.knarcraft.bookswithoutborders.command.CommandCopy;
import net.knarcraft.bookswithoutborders.command.CommandDecrypt; 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.CommandGive;
import net.knarcraft.bookswithoutborders.command.CommandGroupEncrypt;
import net.knarcraft.bookswithoutborders.command.CommandUnSign;
import net.knarcraft.bookswithoutborders.command.GiveTabCompleter; import net.knarcraft.bookswithoutborders.command.GiveTabCompleter;
import net.knarcraft.bookswithoutborders.utility.BookFormatter; import net.knarcraft.bookswithoutborders.utility.BookFormatter;
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper; import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
@ -52,7 +57,7 @@ public class BooksWithoutBorders extends JavaPlugin {
protected static Economy eco; protected static Economy eco;
protected static Material bookPriceType = null; protected static Material bookPriceType = null;
protected static double bookPriceQuantity; protected static double bookPriceQuantity;
protected static boolean authorOnlyCopy; public static boolean authorOnlyCopy;
protected static boolean useYml; protected static boolean useYml;
public static boolean adminDecrypt; public static boolean adminDecrypt;
protected static ItemFactory iF; protected static ItemFactory iF;
@ -86,6 +91,11 @@ public class BooksWithoutBorders extends JavaPlugin {
giveCommand.setTabCompleter(new GiveTabCompleter(this)); giveCommand.setTabCompleter(new GiveTabCompleter(this));
} }
this.getCommand("decrypt").setExecutor(new CommandDecrypt(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() { 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 (args[0].equalsIgnoreCase("setTitle")) {
if (!sender.hasPermission("bookswithoutborders.settitle")) { if (!sender.hasPermission("bookswithoutborders.settitle")) {
sendErrorMessage(sender, " You don't have permission to use this command!"); sendErrorMessage(sender, " You don't have permission to use this command!");
@ -853,301 +847,6 @@ public class BooksWithoutBorders extends JavaPlugin {
return false; return false;
} }
/**
* Executes the group encrypt command
*
* @param player <p>The player which sent the command</p>
* @param args <p>The arguments given</p>
* @return <p>True if the command was executed successfully</p>
*/
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<String> 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 <p>The player which sent the command</p>
* @param args <p>The arguments given</p>
* @return <p>True if the command was executed successfully</p>
*/
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 <p>The player which sent the command</p>
* @param args <p>The arguments given</p>
* @return <p>True if the command was executed successfully</p>
*/
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 <p>The player which executed the command</p>
* @return <p>True if the command was executed successfully</p>
*/
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 <p>The player which sent the command</p>
* @param args <p>The arguments given</p>
* @return <p>True if the command was executed successfully</p>
*/
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 <p>The player which sent the command</p>
* @param args <p>The arguments given</p>
* @return <p>True if the command was executed successfully</p>
*/
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) { public String cleanString(String fname) {
//removes illegal characters //removes illegal characters
if (fname.contains("/")) { if (fname.contains("/")) {
@ -1643,7 +1342,7 @@ public class BooksWithoutBorders extends JavaPlugin {
* @param groupName <p>The name of the group to encrypt for, or "" otherwise</p> * @param groupName <p>The name of the group to encrypt for, or "" otherwise</p>
* @return <p>An encrypted version of the book</p> * @return <p>An encrypted version of the book</p>
*/ */
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 //converts user supplied key into integer form
String integerKey = EncryptionHelper.getNumberKeyFromStringKey(key); String integerKey = EncryptionHelper.getNumberKeyFromStringKey(key);
@ -1752,7 +1451,7 @@ public class BooksWithoutBorders extends JavaPlugin {
* @param fileName <p>The file name of the book</p> * @param fileName <p>The file name of the book</p>
* @param isPublic <p>Whether the book to delete is public or not</p> * @param isPublic <p>Whether the book to delete is public or not</p>
*/ */
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 //If the file name is an index of the load list, load the book
try { try {
int loadListIndex = Integer.parseInt(fileName); int loadListIndex = Integer.parseInt(fileName);
@ -1795,7 +1494,7 @@ public class BooksWithoutBorders extends JavaPlugin {
* @param player <p>The player holding the book</p> * @param player <p>The player holding the book</p>
* @param mainHand <p>Whether the player is holding the book in its main hand or its off hand</p> * @param mainHand <p>Whether the player is holding the book in its main hand or its off hand</p>
*/ */
protected void unSignHeldBook(Player player, boolean mainHand) { public void unSignHeldBook(Player player, boolean mainHand) {
//Get the old book //Get the old book
BookMeta oldBook = InventoryHelper.getHeldBookMetadata(player, mainHand); BookMeta oldBook = InventoryHelper.getHeldBookMetadata(player, mainHand);
@ -1811,7 +1510,7 @@ public class BooksWithoutBorders extends JavaPlugin {
* *
* @return <p>True if players need to pay for printing books</p> * @return <p>True if players need to pay for printing books</p>
*/ */
protected boolean booksHavePrice() { public boolean booksHavePrice() {
return (bookPriceType != null && bookPriceQuantity > 0); return (bookPriceType != null && bookPriceQuantity > 0);
} }
@ -1822,7 +1521,7 @@ public class BooksWithoutBorders extends JavaPlugin {
* @param numCopies <p>The number of copies the player is trying to print</p> * @param numCopies <p>The number of copies the player is trying to print</p>
* @return <p>True if the player cannot pay for the printing of the books</p> * @return <p>True if the player cannot pay for the printing of the books</p>
*/ */
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 //BookPriceQuantity: How many items are required to pay for each book
//BookPriceType: Which item is used to pay for the books. AIR = use economy //BookPriceType: Which item is used to pay for the books. AIR = use economy
double cost = bookPriceQuantity * numCopies; double cost = bookPriceQuantity * numCopies;
@ -1894,7 +1593,7 @@ public class BooksWithoutBorders extends JavaPlugin {
* @param book <p>The book to check</p> * @param book <p>The book to check</p>
* @return <p>True if the player is the book's author</p> * @return <p>True if the player is the book's author</p>
*/ */
protected boolean isAuthor(Player player, BookMeta book) { public boolean isAuthor(Player player, BookMeta book) {
String author = book.getAuthor(); String author = book.getAuthor();
if (author != null && cleanString(player.getName()).equalsIgnoreCase(cleanString(author))) { if (author != null && cleanString(player.getName()).equalsIgnoreCase(cleanString(author))) {
return true; return true;

View File

@ -1,12 +1,70 @@
package net.knarcraft.bookswithoutborders.command; 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.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.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import java.util.Objects;
public class CommandCopy implements CommandExecutor { public class CommandCopy implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders;
public CommandCopy(BooksWithoutBorders booksWithoutBorders) {
this.booksWithoutBorders = 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)) {
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
return false; 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;
}
} }

View File

@ -1,12 +1,48 @@
package net.knarcraft.bookswithoutborders.command; package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
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;
public class CommandDelete implements CommandExecutor { public class CommandDelete implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders;
public CommandDelete(BooksWithoutBorders booksWithoutBorders) {
this.booksWithoutBorders = 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)) {
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
return false; 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;
}
} }

View File

@ -1,12 +1,66 @@
package net.knarcraft.bookswithoutborders.command; 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.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.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import java.util.Objects;
public class CommandEncrypt implements CommandExecutor { public class CommandEncrypt implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders;
public CommandEncrypt(BooksWithoutBorders booksWithoutBorders) {
this.booksWithoutBorders = 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)) {
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
return false; 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;
}
}
} }

View File

@ -7,6 +7,9 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
/**
* Command executor for the give command
*/
public class CommandGive implements CommandExecutor { public class CommandGive implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders; private final BooksWithoutBorders booksWithoutBorders;

View File

@ -1,12 +1,83 @@
package net.knarcraft.bookswithoutborders.command; 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.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.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import java.util.List;
/**
* Command executor for the group encrypt command
*/
public class CommandGroupEncrypt implements CommandExecutor { public class CommandGroupEncrypt implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders;
public CommandGroupEncrypt(BooksWithoutBorders booksWithoutBorders) {
this.booksWithoutBorders = 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)) {
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
return false; 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<String> 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;
}
}
} }

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -15,12 +15,32 @@ commands:
usage: /<command> usage: /<command>
give: give:
description: Gives the held book to another player description: Gives the held book to another player
usage: /<command> [file name or number] <playername> [# of copies (num)] [signed (true/false)] usage: /<command> <file name or number> <playername> [# of copies (num)] [signed (true/false)]
permission: bookswithoutborders.give permission: bookswithoutborders.give
decrypt: decrypt:
description: Encrypts the held book description: Encrypts the held book
usage: /<command> <password> usage: /<command> <key>
permission: bookswithoutborders.decrypt permission: bookswithoutborders.decrypt
groupencrypt:
description: Encrypts the held book for the given group
usage: /<command> <group name> <key> [encryption style]
permission: bookswithoutborders.groupencrypt
delete:
description: Deletes a book saved by the executing user
usage: /<command> <file name or number>
permission: bookswithoutborders.delete
copy:
description: Copies the held book
usage: /<command> <copies>
permission: bookswithoutborders.copy
unsign:
description: Un-signs the held book, making it writable
usage: /<command>
permission: bookswithoutborders.unsign
encrypt:
description: Encrypts the held book
usage: /<command> <key> [encryption style]
permission: bookswithoutborders.encrypt
permissions: permissions:
bookswithoutborders.admin: bookswithoutborders.admin:
description: Grants all permissions description: Grants all permissions