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
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\"");
consoleSender.sendMessage(successColor + "Updating to \"Price_to_create_book\" settings");
sendSuccessMessage(consoleSender, "[BooksWithoutBorders] Found old config setting \"Require_book_and_quill_to_create_book\"");
sendSuccessMessage(consoleSender, "Updating to \"Price_to_create_book\" settings");
if (this.getConfig().getBoolean("Options.Require_book_and_quill_to_create_book")) {
bookPriceType = Material.WRITABLE_BOOK;
@ -302,41 +302,41 @@ public class BooksWithoutBorders extends JavaPlugin {
if (sender.hasPermission("bookswithoutborders.load")) {
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");
sender.sendMessage(successColor + "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");
sender.sendMessage(successColor + "unsigned");
sendSuccessMessage(sender, "Creates a book from the specified file and gives it to the player");
sendSuccessMessage(sender, "If no file is specified, a list of available files is returned");
sendSuccessMessage(sender, "If true is specified the book will be signed, if false it will be");
sendSuccessMessage(sender, "unsigned");
}
if (sender.hasPermission("bookswithoutborders.loadpublic")) {
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")) {
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");
sender.sendMessage(successColor + "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, "holding to a text file in a private directory");
sendSuccessMessage(sender, "If true is specified, a book of the same name by the same");
sendSuccessMessage(sender, "author will be overwritten by the new book");
}
if (sender.hasPermission("bookswithoutborders.savepublic")) {
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")) {
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")) {
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")) {
sender.sendMessage(commandColor + "\nDelete [file name or number]: " + successColor + "Deletes the specified");
sender.sendMessage(successColor + "file in the player's directory");
sender.sendMessage(successColor + "If no file is specified, a list of available files is returned");
sendSuccessMessage(sender, "file in the player's directory");
sendSuccessMessage(sender, "If no file is specified, a list of available files is returned");
}
if (sender.hasPermission("bookswithoutborders.admin")) {
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");
}
@ -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(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");
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");
}
if (sender.hasPermission("bookswithoutborders.setlore")) {
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"))
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 + "\nReload:" + successColor + " Reloads BwB's config file");
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");
sender.sendMessage(successColor + "If no file is specified, a list of available files is returned");
sendSuccessMessage(sender, "Gives the selected player a book from the public directory");
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(successColor + "file in the public directory");
sender.sendMessage(successColor + "If no file is specified, a list of available files is returned");
sendSuccessMessage(sender, "file in the public directory");
sendSuccessMessage(sender, "If no file is specified, a list of available files is returned");
return true;
}
@ -397,9 +399,9 @@ public class BooksWithoutBorders extends JavaPlugin {
}
}
if (loadConfig() && loadExistingPlayers())
sender.sendMessage(successColor + "BooksWithoutBorders configuration reloaded!");
else {
if (loadConfig() && loadExistingPlayers()) {
sendSuccessMessage(sender, "BooksWithoutBorders configuration reloaded!");
} else {
sendErrorMessage(sender, "Reload Failed!");
sendErrorMessage(sender, "See console for details");
}
@ -484,7 +486,7 @@ public class BooksWithoutBorders extends JavaPlugin {
if (newBook != null) {
((Player) sender).getInventory().addItem(newBook);
sender.sendMessage(successColor + "Book created!");
sendSuccessMessage(sender, "Book created!");
return true;
} else {
sendErrorMessage(sender, "Book failed to load!");
@ -534,7 +536,7 @@ public class BooksWithoutBorders extends JavaPlugin {
if (newBook != null) {
((Player) sender).getInventory().addItem(newBook);
sender.sendMessage(successColor + "Book created!");
sendSuccessMessage(sender, "Book created!");
return true;
} else {
sendErrorMessage(sender, "Book failed to load!");
@ -596,8 +598,8 @@ public class BooksWithoutBorders extends JavaPlugin {
if (newBook != null) {
receivingPlayer.getInventory().addItem(newBook);
sender.sendMessage(successColor + "Book sent!");
receivingPlayer.sendMessage(successColor + "Book received!");
sendSuccessMessage(sender, "Book sent!");
sendSuccessMessage(receivingPlayer, "Book received!");
return true;
} else {
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]));
sender.sendMessage(successColor + "Book copied!");
sendSuccessMessage(sender, "Book copied!");
} else {
sendErrorMessage(sender, "Book not copied!");
sendErrorMessage(sender, "Number specified was invalid!");
@ -822,7 +824,7 @@ public class BooksWithoutBorders extends JavaPlugin {
ItemStack book = eLoad(((Player) sender), key, false);
if (book != null) {
((Player) sender).setItemInHand(book);
sender.sendMessage(successColor + "Book auto-decrypted!");
sendSuccessMessage(sender, "Book auto-decrypted!");
return true;
} else
return false;
@ -830,6 +832,9 @@ public class BooksWithoutBorders extends JavaPlugin {
sendErrorMessage(sender, "No matching encrypted book found!");
return false;
}
} else if (args.length == 1) {
sendErrorMessage(sender, "No decryption password given!");
return false;
}
String key = EncryptionHelper.getNumberKeyFromStringKey(args[1]);
@ -837,7 +842,7 @@ public class BooksWithoutBorders extends JavaPlugin {
ItemStack book = eLoad(((Player) sender), key, true);
if (book != null) {
((Player) sender).setItemInHand(book);
sender.sendMessage(successColor + "Book decrypted!");
sendSuccessMessage(sender, "Book decrypted!");
return true;
} else
return false;
@ -880,7 +885,7 @@ public class BooksWithoutBorders extends JavaPlugin {
}
((Player) sender).getItemInHand().setItemMeta(meta);
sender.sendMessage(successColor + "Title set to " + title + "!");
sendSuccessMessage(sender, "Title set to " + title + "!");
return true;
}
@ -910,7 +915,7 @@ public class BooksWithoutBorders extends JavaPlugin {
}
meta.setAuthor(author.toString());
((Player) sender).getItemInHand().setItemMeta(meta);
sender.sendMessage(successColor + "Book author set to " + author + "!");
sendSuccessMessage(sender, "Book author set to " + author + "!");
return true;
}
@ -945,7 +950,7 @@ public class BooksWithoutBorders extends JavaPlugin {
ItemMeta meta = ((Player) sender).getItemInHand().getItemMeta();
meta.setLore(nulore);
((Player) sender).getItemInHand().setItemMeta(meta);
sender.sendMessage(successColor + "Added lore to item!");
sendSuccessMessage(sender, "Added lore to item!");
return true;
}
}
@ -1000,8 +1005,8 @@ public class BooksWithoutBorders extends JavaPlugin {
if (newBook != null) {
receivingPlayer.getInventory().addItem(newBook);
sender.sendMessage(successColor + "Book sent!");
receivingPlayer.sendMessage(successColor + "Book received!");
sendSuccessMessage(sender, "Book sent!");
sendSuccessMessage(receivingPlayer, "Book received!");
return true;
} else {
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.saveConfig();
sender.sendMessage(successColor + "Price to create books removed!");
sendSuccessMessage(sender, "Price to create books removed!");
return true;
}
@ -1089,7 +1094,7 @@ public class BooksWithoutBorders extends JavaPlugin {
this.getConfig().set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity);
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;
} else if (args[1].equalsIgnoreCase("Eco")) {
if (setupEconomy()) {
@ -1099,7 +1104,7 @@ public class BooksWithoutBorders extends JavaPlugin {
this.getConfig().set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity);
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;
} else {
sendErrorMessage(sender, "BooksWithoutBorders failed to hook into Vault! Book price not set!");
@ -1331,7 +1336,7 @@ public class BooksWithoutBorders extends JavaPlugin {
else
bookToTXT(path, fname, book);
player.sendMessage(successColor + "Book Saved as \"" + fname + "\"");
sendSuccessMessage(player, "Book Saved as \"" + fname + "\"");
} catch (IOException e) {
e.printStackTrace();
}
@ -1465,7 +1470,7 @@ public class BooksWithoutBorders extends JavaPlugin {
bookToYml(path, fname, book);
} catch (IOException e) {
e.printStackTrace();
sendErrorMessage(player, "Encrypted failed!");
sendErrorMessage(player, "Encryption failed!");
return false;
}
return true;
@ -1571,8 +1576,9 @@ public class BooksWithoutBorders extends JavaPlugin {
return null;
}
if (!silent)
sender.sendMessage(successColor + "Available Books:");
if (!silent) {
sendSuccessMessage(sender, "Available Books:");
}
for (File value : fl) {
if (value.isFile()) {
fileList.add(value.getName());
@ -1628,32 +1634,39 @@ public class BooksWithoutBorders extends JavaPlugin {
return rawPages;
}
protected List<String> cleanList(List<String> rawPages) {
String nullcheck;
//String[] pages;
//clears rawpages of bad values
ListIterator<String> li = rawPages.listIterator();
while (li.hasNext()) {
nullcheck = li.next();
if (nullcheck == null) {
li.remove();
} else {
if (nullcheck.replace(" ", "").isEmpty()) {
li.remove();
}
}
}
/*pages = new String[li.previousIndex()+1];
while(li.hasPrevious())
{pages[li.previousIndex()] = li.previous();}*/
return rawPages;
/**
* Removes null and empty items from a list
*
* @param list <p>The list to clean</p>
* @return <p>A clean list containing all relevant values</p>
*/
protected List<String> cleanList(List<String> list) {
List<String> resultList = new ArrayList<>(list);
resultList.removeIf((item) -> item == null || item.trim().isEmpty());
return resultList;
}
/**
* 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) {
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) {
//converts user supplied key into integer form
String integerKey = EncryptionHelper.getNumberKeyFromStringKey(key);
@ -1665,52 +1678,75 @@ public class BooksWithoutBorders extends JavaPlugin {
return null;
}
BookMeta nuMeta = null;
boolean wasSaved = (groupName.equalsIgnoreCase("")) ? eSave(player, book, integerKey) : (nuMeta = groupESave(player, book, integerKey, groupName)) != null;
if (wasSaved) {
BookMeta newMetadata = null;
//Save the book's un-encrypted contents to a file
boolean wasSaved;
if (groupName.trim().isEmpty()) {
wasSaved = eSave(player, book, integerKey);
} else {
wasSaved = (newMetadata = groupESave(player, book, integerKey, groupName)) != null;
}
if (!wasSaved) {
return null;
}
ItemStack encryptedBook;
List<String> encryptedPages = new ArrayList<>();
List<String> nuPages;
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.MAGIC) {
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) {
//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!");
}
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;
}
//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 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>
*/
@ -1753,7 +1789,7 @@ public class BooksWithoutBorders extends JavaPlugin {
//Try to delete the file
try {
if (file.delete()) {
sender.sendMessage(successColor + "\"" + fileName + "\" deleted successfully");
sendSuccessMessage(sender, "\"" + fileName + "\" deleted successfully");
} else {
sendErrorMessage(sender, "Deletion failed without an exception!");
}
@ -1781,6 +1817,7 @@ public class BooksWithoutBorders extends JavaPlugin {
/**
* Gets metadata about the player's held book
*
* @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>
* @return <p>Information about the held book</p>
@ -1795,6 +1832,7 @@ public class BooksWithoutBorders extends JavaPlugin {
/**
* 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 mainHand <p>Whether to replace the item in the player's main hand or off hand</p>
@ -1879,8 +1917,8 @@ public class BooksWithoutBorders extends JavaPlugin {
Economy economy = BooksWithoutBorders.eco;
if ((economy.getBalance(player) - cost) >= 0) {
economy.withdrawPlayer(player, cost);
player.sendMessage(successColor + economy.format(cost) + " withdrawn to create " + numCopies + " book(s)");
player.sendMessage(successColor + "New balance: " + economy.format(economy.getBalance(player)));
sendSuccessMessage(player, economy.format(cost) + " withdrawn to create " + numCopies + " book(s)");
sendSuccessMessage(player, "New balance: " + economy.format(economy.getBalance(player)));
return true;
} else {
sendErrorMessage(player, economy.format(cost) + " is required for this command!");

View File

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