Makes success message formatting more customizable, and cleans more code

This commit is contained in:
Kristian Knarvik 2021-08-29 05:45:27 +02:00
parent 2223d41a9c
commit 491462c132
2 changed files with 150 additions and 113 deletions

View File

@ -109,8 +109,8 @@ public class BooksWithoutBorders extends JavaPlugin {
//Handles old book and quill settings //Handles old book and quill settings
if (this.getConfig().contains("Options.Require_book_and_quill_to_create_book")) { if (this.getConfig().contains("Options.Require_book_and_quill_to_create_book")) {
consoleSender.sendMessage(successColor + "[BooksWithoutBorders] Found old config setting \"Require_book_and_quill_to_create_book\""); sendSuccessMessage(consoleSender, "[BooksWithoutBorders] Found old config setting \"Require_book_and_quill_to_create_book\"");
consoleSender.sendMessage(successColor + "Updating to \"Price_to_create_book\" settings"); sendSuccessMessage(consoleSender, "Updating to \"Price_to_create_book\" settings");
if (this.getConfig().getBoolean("Options.Require_book_and_quill_to_create_book")) { if (this.getConfig().getBoolean("Options.Require_book_and_quill_to_create_book")) {
bookPriceType = Material.WRITABLE_BOOK; bookPriceType = Material.WRITABLE_BOOK;
@ -302,41 +302,41 @@ public class BooksWithoutBorders extends JavaPlugin {
if (sender.hasPermission("bookswithoutborders.load")) { if (sender.hasPermission("bookswithoutborders.load")) {
sender.sendMessage(commandColor + "\nLoad [file name or number] [# of copies] [true/false]:"); sender.sendMessage(commandColor + "\nLoad [file name or number] [# of copies] [true/false]:");
sender.sendMessage(successColor + "Creates a book from the specified file and gives it to the player"); sendSuccessMessage(sender, "Creates a book from the specified file and gives it to the player");
sender.sendMessage(successColor + "If no file is specified, a list of available files is returned"); sendSuccessMessage(sender, "If no file is specified, a list of available files is returned");
sender.sendMessage(successColor + "If true is specified the book will be signed, if false it will be"); sendSuccessMessage(sender, "If true is specified the book will be signed, if false it will be");
sender.sendMessage(successColor + "unsigned"); sendSuccessMessage(sender, "unsigned");
} }
if (sender.hasPermission("bookswithoutborders.loadpublic")) { if (sender.hasPermission("bookswithoutborders.loadpublic")) {
sender.sendMessage(commandColor + "loadPublic [file name or number] [# of copies] [true/false]:"); sender.sendMessage(commandColor + "loadPublic [file name or number] [# of copies] [true/false]:");
sender.sendMessage(successColor + "Same as Load, but views files in the public directory"); sendSuccessMessage(sender, "Same as Load, but views files in the public directory");
} }
if (sender.hasPermission("bookswithoutborders.save")) { if (sender.hasPermission("bookswithoutborders.save")) {
sender.sendMessage("\n" + commandColor + "Save [true/false]: " + successColor + "Saves the book the player is"); sender.sendMessage("\n" + commandColor + "Save [true/false]: " + successColor + "Saves the book the player is");
sender.sendMessage(successColor + "holding to a text file in a private directory"); sendSuccessMessage(sender, "holding to a text file in a private directory");
sender.sendMessage(successColor + "If true is specified, a book of the same name by the same"); sendSuccessMessage(sender, "If true is specified, a book of the same name by the same");
sender.sendMessage(successColor + "author will be overwritten by the new book"); sendSuccessMessage(sender, "author will be overwritten by the new book");
} }
if (sender.hasPermission("bookswithoutborders.savepublic")) { if (sender.hasPermission("bookswithoutborders.savepublic")) {
sender.sendMessage(commandColor + "savePublic [true/false]: " + successColor + "Same as Save,"); sender.sendMessage(commandColor + "savePublic [true/false]: " + successColor + "Same as Save,");
sender.sendMessage(successColor + "but saves files in the public directory"); sendSuccessMessage(sender, "but saves files in the public directory");
} }
if (sender.hasPermission("bookswithoutborders.give")) { if (sender.hasPermission("bookswithoutborders.give")) {
sender.sendMessage("\n" + commandColor + "Give [file name or number] [playername] [# of copies] [true/false]:"); sender.sendMessage("\n" + commandColor + "Give [file name or number] [playername] [# of copies] [true/false]:");
sender.sendMessage(successColor + "Gives the selected player a book from your personal directory"); sendSuccessMessage(sender, "Gives the selected player a book from your personal directory");
} }
if (sender.hasPermission("bookswithoutborders.givepublic")) { if (sender.hasPermission("bookswithoutborders.givepublic")) {
sender.sendMessage(commandColor + "givePublic [file name or number] [playername] [# of copies] [true/false]:"); sender.sendMessage(commandColor + "givePublic [file name or number] [playername] [# of copies] [true/false]:");
sender.sendMessage(successColor + "Same as give, but uses books from the public directory"); sendSuccessMessage(sender, "Same as give, but uses books from the public directory");
} }
if (sender.hasPermission("bookswithoutborders.delete")) { if (sender.hasPermission("bookswithoutborders.delete")) {
sender.sendMessage(commandColor + "\nDelete [file name or number]: " + successColor + "Deletes the specified"); sender.sendMessage(commandColor + "\nDelete [file name or number]: " + successColor + "Deletes the specified");
sender.sendMessage(successColor + "file in the player's directory"); sendSuccessMessage(sender, "file in the player's directory");
sender.sendMessage(successColor + "If no file is specified, a list of available files is returned"); sendSuccessMessage(sender, "If no file is specified, a list of available files is returned");
} }
if (sender.hasPermission("bookswithoutborders.admin")) { if (sender.hasPermission("bookswithoutborders.admin")) {
sender.sendMessage(commandColor + "deletePublic [file name or number]: " + successColor + "Same as Delete,"); sender.sendMessage(commandColor + "deletePublic [file name or number]: " + successColor + "Same as Delete,");
sender.sendMessage(successColor + "but deletes files in the public directory"); sendSuccessMessage(sender, "but deletes files in the public directory");
sender.sendMessage(commandColor + "\nReload:" + successColor + " Reloads BwB's configuration file"); sender.sendMessage(commandColor + "\nReload:" + successColor + " Reloads BwB's configuration file");
} }
@ -358,13 +358,15 @@ public class BooksWithoutBorders extends JavaPlugin {
sender.sendMessage("\n" + commandColor + "Decrypt [key]: " + successColor + "Decrypts the book the player is holding"); sender.sendMessage("\n" + commandColor + "Decrypt [key]: " + successColor + "Decrypts the book the player is holding");
sender.sendMessage(commandColor + "[key]" + successColor + " is required and MUST be IDENTICAL to the key used to encrypt held book"); sender.sendMessage(commandColor + "[key]" + successColor + " is required and MUST be IDENTICAL to the key used to encrypt held book");
} }
if (sender.hasPermission("bookswithoutborders.settitle")) if (sender.hasPermission("bookswithoutborders.settitle")) {
sender.sendMessage("\n" + commandColor + "setTitle [title]: " + successColor + "Sets the title of the book/item the player is holding"); sender.sendMessage("\n" + commandColor + "setTitle [title]: " + successColor + "Sets the title of the book/item the player is holding");
if (sender.hasPermission("bookswithoutborders.setauthor")) }
if (sender.hasPermission("bookswithoutborders.setauthor")) {
sender.sendMessage("\n" + commandColor + "setAuthor [author]: " + successColor + "Sets the author of the book the player is holding"); sender.sendMessage("\n" + commandColor + "setAuthor [author]: " + successColor + "Sets the author of the book the player is holding");
}
if (sender.hasPermission("bookswithoutborders.setlore")) { if (sender.hasPermission("bookswithoutborders.setlore")) {
sender.sendMessage("\n" + commandColor + "setLore [lore]: " + successColor + "Sets the lore of the item the player is holding"); sender.sendMessage("\n" + commandColor + "setLore [lore]: " + successColor + "Sets the lore of the item the player is holding");
sender.sendMessage(successColor + "Insert the lore_line_separator character to force a new line\n[\"~\" by default]"); sendSuccessMessage(sender, "Insert the lore_line_separator character to force a new line\n[\"~\" by default]");
} }
if (sender.hasPermission("bookswithoutborders.setbookprice")) if (sender.hasPermission("bookswithoutborders.setbookprice"))
sender.sendMessage("\n" + commandColor + "setBookPrice [Item/Eco] [quantity]: " + successColor + "Sets the per-book-price to create a book via commands." + sender.sendMessage("\n" + commandColor + "setBookPrice [Item/Eco] [quantity]: " + successColor + "Sets the per-book-price to create a book via commands." +
@ -380,11 +382,11 @@ public class BooksWithoutBorders extends JavaPlugin {
sender.sendMessage(commandColor + "Commands:"); sender.sendMessage(commandColor + "Commands:");
sender.sendMessage(commandColor + "\nReload:" + successColor + " Reloads BwB's config file"); sender.sendMessage(commandColor + "\nReload:" + successColor + " Reloads BwB's config file");
sender.sendMessage(commandColor + "givePublic [file name or number] [playername] [true/false]: " + successColor); sender.sendMessage(commandColor + "givePublic [file name or number] [playername] [true/false]: " + successColor);
sender.sendMessage(successColor + "Gives the selected player a book from the public directory"); sendSuccessMessage(sender, "Gives the selected player a book from the public directory");
sender.sendMessage(successColor + "If no file is specified, a list of available files is returned"); sendSuccessMessage(sender, "If no file is specified, a list of available files is returned");
sender.sendMessage(commandColor + "deletePublic [file name or number]: " + successColor + "Deletes the specified"); sender.sendMessage(commandColor + "deletePublic [file name or number]: " + successColor + "Deletes the specified");
sender.sendMessage(successColor + "file in the public directory"); sendSuccessMessage(sender, "file in the public directory");
sender.sendMessage(successColor + "If no file is specified, a list of available files is returned"); sendSuccessMessage(sender, "If no file is specified, a list of available files is returned");
return true; return true;
} }
@ -397,9 +399,9 @@ public class BooksWithoutBorders extends JavaPlugin {
} }
} }
if (loadConfig() && loadExistingPlayers()) if (loadConfig() && loadExistingPlayers()) {
sender.sendMessage(successColor + "BooksWithoutBorders configuration reloaded!"); sendSuccessMessage(sender, "BooksWithoutBorders configuration reloaded!");
else { } else {
sendErrorMessage(sender, "Reload Failed!"); sendErrorMessage(sender, "Reload Failed!");
sendErrorMessage(sender, "See console for details"); sendErrorMessage(sender, "See console for details");
} }
@ -484,7 +486,7 @@ public class BooksWithoutBorders extends JavaPlugin {
if (newBook != null) { if (newBook != null) {
((Player) sender).getInventory().addItem(newBook); ((Player) sender).getInventory().addItem(newBook);
sender.sendMessage(successColor + "Book created!"); sendSuccessMessage(sender, "Book created!");
return true; return true;
} else { } else {
sendErrorMessage(sender, "Book failed to load!"); sendErrorMessage(sender, "Book failed to load!");
@ -534,7 +536,7 @@ public class BooksWithoutBorders extends JavaPlugin {
if (newBook != null) { if (newBook != null) {
((Player) sender).getInventory().addItem(newBook); ((Player) sender).getInventory().addItem(newBook);
sender.sendMessage(successColor + "Book created!"); sendSuccessMessage(sender, "Book created!");
return true; return true;
} else { } else {
sendErrorMessage(sender, "Book failed to load!"); sendErrorMessage(sender, "Book failed to load!");
@ -596,8 +598,8 @@ public class BooksWithoutBorders extends JavaPlugin {
if (newBook != null) { if (newBook != null) {
receivingPlayer.getInventory().addItem(newBook); receivingPlayer.getInventory().addItem(newBook);
sender.sendMessage(successColor + "Book sent!"); sendSuccessMessage(sender, "Book sent!");
receivingPlayer.sendMessage(successColor + "Book received!"); sendSuccessMessage(receivingPlayer, "Book received!");
return true; return true;
} else { } else {
sendErrorMessage(sender, "Book failed to load!"); sendErrorMessage(sender, "Book failed to load!");
@ -693,7 +695,7 @@ public class BooksWithoutBorders extends JavaPlugin {
} }
((Player) sender).getItemInHand().setAmount(((Player) sender).getItemInHand().getAmount() + Integer.parseInt(args[1])); ((Player) sender).getItemInHand().setAmount(((Player) sender).getItemInHand().getAmount() + Integer.parseInt(args[1]));
sender.sendMessage(successColor + "Book copied!"); sendSuccessMessage(sender, "Book copied!");
} else { } else {
sendErrorMessage(sender, "Book not copied!"); sendErrorMessage(sender, "Book not copied!");
sendErrorMessage(sender, "Number specified was invalid!"); sendErrorMessage(sender, "Number specified was invalid!");
@ -822,7 +824,7 @@ public class BooksWithoutBorders extends JavaPlugin {
ItemStack book = eLoad(((Player) sender), key, false); ItemStack book = eLoad(((Player) sender), key, false);
if (book != null) { if (book != null) {
((Player) sender).setItemInHand(book); ((Player) sender).setItemInHand(book);
sender.sendMessage(successColor + "Book auto-decrypted!"); sendSuccessMessage(sender, "Book auto-decrypted!");
return true; return true;
} else } else
return false; return false;
@ -830,6 +832,9 @@ public class BooksWithoutBorders extends JavaPlugin {
sendErrorMessage(sender, "No matching encrypted book found!"); sendErrorMessage(sender, "No matching encrypted book found!");
return false; return false;
} }
} else if (args.length == 1) {
sendErrorMessage(sender, "No decryption password given!");
return false;
} }
String key = EncryptionHelper.getNumberKeyFromStringKey(args[1]); String key = EncryptionHelper.getNumberKeyFromStringKey(args[1]);
@ -837,7 +842,7 @@ public class BooksWithoutBorders extends JavaPlugin {
ItemStack book = eLoad(((Player) sender), key, true); ItemStack book = eLoad(((Player) sender), key, true);
if (book != null) { if (book != null) {
((Player) sender).setItemInHand(book); ((Player) sender).setItemInHand(book);
sender.sendMessage(successColor + "Book decrypted!"); sendSuccessMessage(sender, "Book decrypted!");
return true; return true;
} else } else
return false; return false;
@ -880,7 +885,7 @@ public class BooksWithoutBorders extends JavaPlugin {
} }
((Player) sender).getItemInHand().setItemMeta(meta); ((Player) sender).getItemInHand().setItemMeta(meta);
sender.sendMessage(successColor + "Title set to " + title + "!"); sendSuccessMessage(sender, "Title set to " + title + "!");
return true; return true;
} }
@ -910,7 +915,7 @@ public class BooksWithoutBorders extends JavaPlugin {
} }
meta.setAuthor(author.toString()); meta.setAuthor(author.toString());
((Player) sender).getItemInHand().setItemMeta(meta); ((Player) sender).getItemInHand().setItemMeta(meta);
sender.sendMessage(successColor + "Book author set to " + author + "!"); sendSuccessMessage(sender, "Book author set to " + author + "!");
return true; return true;
} }
@ -945,7 +950,7 @@ public class BooksWithoutBorders extends JavaPlugin {
ItemMeta meta = ((Player) sender).getItemInHand().getItemMeta(); ItemMeta meta = ((Player) sender).getItemInHand().getItemMeta();
meta.setLore(nulore); meta.setLore(nulore);
((Player) sender).getItemInHand().setItemMeta(meta); ((Player) sender).getItemInHand().setItemMeta(meta);
sender.sendMessage(successColor + "Added lore to item!"); sendSuccessMessage(sender, "Added lore to item!");
return true; return true;
} }
} }
@ -1000,8 +1005,8 @@ public class BooksWithoutBorders extends JavaPlugin {
if (newBook != null) { if (newBook != null) {
receivingPlayer.getInventory().addItem(newBook); receivingPlayer.getInventory().addItem(newBook);
sender.sendMessage(successColor + "Book sent!"); sendSuccessMessage(sender, "Book sent!");
receivingPlayer.sendMessage(successColor + "Book received!"); sendSuccessMessage(receivingPlayer, "Book received!");
return true; return true;
} else { } else {
sendErrorMessage(sender, "Book failed to load!"); sendErrorMessage(sender, "Book failed to load!");
@ -1057,7 +1062,7 @@ public class BooksWithoutBorders extends JavaPlugin {
this.getConfig().set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity); this.getConfig().set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity);
this.saveConfig(); this.saveConfig();
sender.sendMessage(successColor + "Price to create books removed!"); sendSuccessMessage(sender, "Price to create books removed!");
return true; return true;
} }
@ -1089,7 +1094,7 @@ public class BooksWithoutBorders extends JavaPlugin {
this.getConfig().set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity); this.getConfig().set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity);
this.saveConfig(); this.saveConfig();
sender.sendMessage(successColor + "Book creation price set to " + (int) bookPriceQuantity + " " + bookPriceType.toString() + "(s)!"); sendSuccessMessage(sender, "Book creation price set to " + (int) bookPriceQuantity + " " + bookPriceType.toString() + "(s)!");
return true; return true;
} else if (args[1].equalsIgnoreCase("Eco")) { } else if (args[1].equalsIgnoreCase("Eco")) {
if (setupEconomy()) { if (setupEconomy()) {
@ -1099,7 +1104,7 @@ public class BooksWithoutBorders extends JavaPlugin {
this.getConfig().set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity); this.getConfig().set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity);
this.saveConfig(); this.saveConfig();
sender.sendMessage(successColor + "Book creation price set to " + eco.format(bookPriceQuantity) + "!"); sendSuccessMessage(sender, "Book creation price set to " + eco.format(bookPriceQuantity) + "!");
return true; return true;
} else { } else {
sendErrorMessage(sender, "BooksWithoutBorders failed to hook into Vault! Book price not set!"); sendErrorMessage(sender, "BooksWithoutBorders failed to hook into Vault! Book price not set!");
@ -1331,7 +1336,7 @@ public class BooksWithoutBorders extends JavaPlugin {
else else
bookToTXT(path, fname, book); bookToTXT(path, fname, book);
player.sendMessage(successColor + "Book Saved as \"" + fname + "\""); sendSuccessMessage(player, "Book Saved as \"" + fname + "\"");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -1465,7 +1470,7 @@ public class BooksWithoutBorders extends JavaPlugin {
bookToYml(path, fname, book); bookToYml(path, fname, book);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
sendErrorMessage(player, "Encrypted failed!"); sendErrorMessage(player, "Encryption failed!");
return false; return false;
} }
return true; return true;
@ -1571,8 +1576,9 @@ public class BooksWithoutBorders extends JavaPlugin {
return null; return null;
} }
if (!silent) if (!silent) {
sender.sendMessage(successColor + "Available Books:"); sendSuccessMessage(sender, "Available Books:");
}
for (File value : fl) { for (File value : fl) {
if (value.isFile()) { if (value.isFile()) {
fileList.add(value.getName()); fileList.add(value.getName());
@ -1628,32 +1634,39 @@ public class BooksWithoutBorders extends JavaPlugin {
return rawPages; return rawPages;
} }
protected List<String> cleanList(List<String> rawPages) { /**
String nullcheck; * Removes null and empty items from a list
//String[] pages; *
//clears rawpages of bad values * @param list <p>The list to clean</p>
ListIterator<String> li = rawPages.listIterator(); * @return <p>A clean list containing all relevant values</p>
while (li.hasNext()) { */
nullcheck = li.next(); protected List<String> cleanList(List<String> list) {
if (nullcheck == null) { List<String> resultList = new ArrayList<>(list);
li.remove(); resultList.removeIf((item) -> item == null || item.trim().isEmpty());
} else { return resultList;
if (nullcheck.replace(" ", "").isEmpty()) {
li.remove();
}
}
}
/*pages = new String[li.previousIndex()+1];
while(li.hasPrevious())
{pages[li.previousIndex()] = li.previous();}*/
return rawPages;
} }
/**
* Encrypts a book
* @param player <p>The player encrypting the book</p>
* @param mainHand <p>Whether the player is holding the book in its main hand</p>
* @param key <p>The key/password to use for encryption</p>
* @param style <p>The encryption style to use</p>
* @return <p>An encrypted version of the book</p>
*/
protected ItemStack encryptBook(Player player, boolean mainHand, String key, EncryptionStyle style) { protected ItemStack encryptBook(Player player, boolean mainHand, String key, EncryptionStyle style) {
return encryptBook(player, mainHand, key, style, ""); return encryptBook(player, mainHand, key, style, "");
} }
/**
* Encrypts a book
* @param player <p>The player encrypting the book</p>
* @param mainHand <p>Whether the player is holding the book in its main hand</p>
* @param key <p>The key/password to use for encryption</p>
* @param style <p>The encryption style to use</p>
* @param groupName <p>The name of the group to encrypt for, or "" otherwise</p>
* @return <p>An encrypted version of the book</p>
*/
protected ItemStack encryptBook(Player player, boolean mainHand, String key, EncryptionStyle style, String groupName) { protected 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);
@ -1665,55 +1678,78 @@ public class BooksWithoutBorders extends JavaPlugin {
return null; return null;
} }
BookMeta nuMeta = null; BookMeta newMetadata = null;
boolean wasSaved = (groupName.equalsIgnoreCase("")) ? eSave(player, book, integerKey) : (nuMeta = groupESave(player, book, integerKey, groupName)) != null;
if (wasSaved) {
ItemStack encryptedBook;
List<String> encryptedPages = new ArrayList<>();
List<String> nuPages;
if (style == EncryptionStyle.DNA) { //Save the book's un-encrypted contents to a file
GenenCrypt gc = new GenenCrypt(integerKey); boolean wasSaved;
for (int x = 0; x < book.getPages().size(); x++) { if (groupName.trim().isEmpty()) {
encryptedPages.add(gc.encrypt(book.getPage(x + 1))); wasSaved = eSave(player, book, integerKey);
} } else {
} else if (style == EncryptionStyle.MAGIC) { wasSaved = (newMetadata = groupESave(player, book, integerKey, groupName)) != null;
for (int i = 0; i < book.getPages().size(); i++) {
String page = book.getPage(i + 1);
page = "ァk" + page.replace("", "");
encryptedPages.add(page);
}
} else if (style == EncryptionStyle.SUBSTITUTION) {
SubstitutionCipher sc = new SubstitutionCipher();
for (int x = 0; x < book.getPages().size(); x++) {
encryptedPages.add(sc.encrypt(book.getPage(x + 1), integerKey));
}
} else {
sendErrorMessage(player, "Invalid encryption style encountered!");
}
encryptedPages = pageFormat(encryptedPages);
nuPages = cleanList(encryptedPages);
player.sendMessage(successColor + "Book encrypted!");
encryptedBook = new ItemStack(Material.WRITTEN_BOOK);
book.setPages(nuPages);
encryptedBook.setItemMeta(book);
encryptedBook.setAmount(player.getItemInHand().getAmount());
if (nuMeta != null)
encryptedBook.setItemMeta(nuMeta);
return encryptedBook;
} }
return null; if (!wasSaved) {
return null;
}
ItemStack encryptedBook;
List<String> encryptedPages = new ArrayList<>();
List<String> newPages;
//Scramble the book's contents
if (style == EncryptionStyle.DNA) {
//Encrypt the pages using gene-based encryption
GenenCrypt gc = new GenenCrypt(integerKey);
for (int x = 0; x < book.getPages().size(); x++) {
encryptedPages.add(gc.encrypt(book.getPage(x + 1)));
}
} else if (style == EncryptionStyle.SUBSTITUTION) {
//Encrypt the pages using a substitution cipher
SubstitutionCipher sc = new SubstitutionCipher();
for (int x = 0; x < book.getPages().size(); x++) {
encryptedPages.add(sc.encrypt(book.getPage(x + 1), integerKey));
}
} else {
sendErrorMessage(player, "Invalid encryption style encountered!");
return null;
}
//Change the book text to the encrypted text
encryptedPages = pageFormat(encryptedPages);
newPages = cleanList(encryptedPages);
sendSuccessMessage(player, "Book encrypted!");
encryptedBook = new ItemStack(Material.WRITTEN_BOOK);
book.setPages(newPages);
encryptedBook.setItemMeta(book);
//Update item amount
if (mainHand) {
encryptedBook.setAmount(player.getInventory().getItemInMainHand().getAmount());
} else {
encryptedBook.setAmount(player.getInventory().getItemInOffHand().getAmount());
}
//Set new item metadata if using group encryption
if (newMetadata != null) {
encryptedBook.setItemMeta(newMetadata);
}
return encryptedBook;
} }
/** /**
* Sends an error message to a player * Sends a success message to a command sender (player or a console)
* @param sender <p>The sender to send the message to</p> * @param sender <p>The sender to send the message to</p>
* @param message <p>The message to send</p> * @param message <p>The message to send</p>
*/ */
private void sendSuccessMessage(CommandSender sender, String message) {
sender.sendMessage(successColor + message);
}
/**
* Sends an error message to a command sender (player or a console)
*
* @param sender <p>The sender to send the message to</p>
* @param message <p>The message to send</p>
*/
private void sendErrorMessage(CommandSender sender, String message) { private void sendErrorMessage(CommandSender sender, String message) {
sender.sendMessage(errorColor + message); sender.sendMessage(errorColor + message);
} }
@ -1753,7 +1789,7 @@ public class BooksWithoutBorders extends JavaPlugin {
//Try to delete the file //Try to delete the file
try { try {
if (file.delete()) { if (file.delete()) {
sender.sendMessage(successColor + "\"" + fileName + "\" deleted successfully"); sendSuccessMessage(sender, "\"" + fileName + "\" deleted successfully");
} else { } else {
sendErrorMessage(sender, "Deletion failed without an exception!"); sendErrorMessage(sender, "Deletion failed without an exception!");
} }
@ -1781,7 +1817,8 @@ public class BooksWithoutBorders extends JavaPlugin {
/** /**
* Gets metadata about the player's held book * Gets metadata about the player's held book
* @param player <p>The player holding the book</p> *
* @param player <p>The player holding the book</p>
* @param mainHand <p>Whether to get information about a book in the player's main hand or off hand</p> * @param mainHand <p>Whether to get information about a book in the player's main hand or off hand</p>
* @return <p>Information about the held book</p> * @return <p>Information about the held book</p>
*/ */
@ -1795,8 +1832,9 @@ public class BooksWithoutBorders extends JavaPlugin {
/** /**
* Replaces the player's held item * Replaces the player's held item
* @param player <p>The player to replace the item for</p> *
* @param newBook <p>The new book the player should hold</p> * @param player <p>The player to replace the item for</p>
* @param newBook <p>The new book the player should hold</p>
* @param mainHand <p>Whether to replace the item in the player's main hand or off hand</p> * @param mainHand <p>Whether to replace the item in the player's main hand or off hand</p>
*/ */
private void replaceHeldBook(Player player, ItemStack newBook, boolean mainHand) { private void replaceHeldBook(Player player, ItemStack newBook, boolean mainHand) {
@ -1879,8 +1917,8 @@ public class BooksWithoutBorders extends JavaPlugin {
Economy economy = BooksWithoutBorders.eco; Economy economy = BooksWithoutBorders.eco;
if ((economy.getBalance(player) - cost) >= 0) { if ((economy.getBalance(player) - cost) >= 0) {
economy.withdrawPlayer(player, cost); economy.withdrawPlayer(player, cost);
player.sendMessage(successColor + economy.format(cost) + " withdrawn to create " + numCopies + " book(s)"); sendSuccessMessage(player, economy.format(cost) + " withdrawn to create " + numCopies + " book(s)");
player.sendMessage(successColor + "New balance: " + economy.format(economy.getBalance(player))); sendSuccessMessage(player, "New balance: " + economy.format(economy.getBalance(player)));
return true; return true;
} else { } else {
sendErrorMessage(player, economy.format(cost) + " is required for this command!"); sendErrorMessage(player, economy.format(cost) + " is required for this command!");

View File

@ -5,7 +5,6 @@ package net.knarcraft.bookswithoutborders;
*/ */
public enum EncryptionStyle { public enum EncryptionStyle {
DNA("dna"), DNA("dna"),
MAGIC("magic"),
SUBSTITUTION(""); SUBSTITUTION("");
private final String name; private final String name;