Cleans a lot of code
This commit is contained in:
parent
07ff93e7bf
commit
9209e3f52d
@ -10,10 +10,10 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.utility.BookFormatter;
|
||||
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
|
||||
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
@ -549,94 +549,11 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("give")) {
|
||||
if (!sender.hasPermission("bookswithoutborders.give")) {
|
||||
sendErrorMessage(sender, " You don't have permission to use this command!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (args.length == 2 || args.length > 5) {
|
||||
sendErrorMessage(sender, "Incorrect number of arguments for this command!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (args.length == 1) {
|
||||
loadList.put(sender.getName(), listFiles(sender, false, 0, 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(sender.getName(), listFiles(sender, false, 0, true));
|
||||
}
|
||||
|
||||
ItemStack newBook;
|
||||
Player receivingPlayer = this.getServer().getPlayer(args[2]);
|
||||
if (receivingPlayer == null) {
|
||||
sendErrorMessage(sender, "Player not found!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (receivingPlayer.getInventory().firstEmpty() == -1) {
|
||||
sendErrorMessage(sender, "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(sender, 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(sender, cleanString(args[1]), args[3], "player");
|
||||
else
|
||||
newBook = loadBook(sender, cleanString(args[1]), "true", "player", Integer.parseInt(args[3]));
|
||||
} else
|
||||
newBook = loadBook(sender, cleanString(args[1]), "true", "player");
|
||||
|
||||
if (newBook != null) {
|
||||
receivingPlayer.getInventory().addItem(newBook);
|
||||
sendSuccessMessage(sender, "Book sent!");
|
||||
sendSuccessMessage(receivingPlayer, "Book received!");
|
||||
return true;
|
||||
} else {
|
||||
sendErrorMessage(sender, "Book failed to load!");
|
||||
return false;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
sendErrorMessage(sender, "Invalid number of book copies specified!");
|
||||
return false;
|
||||
}
|
||||
return commandGive(sender, args);
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("delete")) {
|
||||
if (!sender.hasPermission("bookswithoutborders.delete")) {
|
||||
sendErrorMessage(sender, " You don't have permission to use this command!");
|
||||
return false;
|
||||
}
|
||||
|
||||
//lists delete-able files
|
||||
if (args.length == 1) {
|
||||
loadList.put(sender.getName(), listFiles(sender, false, 0, false));
|
||||
return true;
|
||||
}
|
||||
//actual deletion
|
||||
if (args.length == 2) {
|
||||
if (!loadList.containsKey(sender.getName())) {
|
||||
sendErrorMessage(sender, "You must first use /bwb delete to create a list of delete-able files!");
|
||||
return false;
|
||||
} else {
|
||||
if (!loadList.get(sender.getName()).isEmpty()) {
|
||||
deleteBook(sender, args[1], false);
|
||||
return true;
|
||||
} else {
|
||||
sendErrorMessage(sender, "No files available to delete!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return commandDelete(sender, args);
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("unsign")) {
|
||||
@ -1122,6 +1039,111 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the delete command
|
||||
* @param sender <p>The command sender which sent the command</p>
|
||||
* @param args <p>The arguments given</p>
|
||||
* @return <p>True if the command was executed successfully</p>
|
||||
*/
|
||||
protected boolean commandDelete(CommandSender sender, String[] args) {
|
||||
if (!sender.hasPermission("bookswithoutborders.delete")) {
|
||||
sendErrorMessage(sender, " You don't have permission to use this command!");
|
||||
return false;
|
||||
}
|
||||
|
||||
//List deletable files
|
||||
if (args.length == 1) {
|
||||
loadList.put(sender.getName(), listFiles(sender, false, 0, false));
|
||||
return true;
|
||||
}
|
||||
//Delete the file
|
||||
if (args.length == 2) {
|
||||
if (!loadList.containsKey(sender.getName())) {
|
||||
sendErrorMessage(sender, "You must first use /bwb delete to create a list of delete-able files!");
|
||||
return false;
|
||||
} else {
|
||||
if (!loadList.get(sender.getName()).isEmpty()) {
|
||||
deleteBook(sender, args[1], false);
|
||||
return true;
|
||||
} else {
|
||||
sendErrorMessage(sender, "No files available to delete!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
sendErrorMessage(sender, "Incorrect number of arguments for this command!");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the give command
|
||||
* @param sender <p>The command sender which sent the command</p>
|
||||
* @param args <p>The arguments given</p>
|
||||
* @return <p>True if the command was executed successfully</p>
|
||||
*/
|
||||
protected boolean commandGive(CommandSender sender, String[] args) {
|
||||
if (!sender.hasPermission("bookswithoutborders.give")) {
|
||||
sendErrorMessage(sender, " You don't have permission to use this command!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (args.length == 2 || args.length > 5) {
|
||||
sendErrorMessage(sender, "Incorrect number of arguments for this command!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (args.length == 1) {
|
||||
loadList.put(sender.getName(), listFiles(sender, false, 0, 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(sender.getName(), listFiles(sender, false, 0, true));
|
||||
}
|
||||
|
||||
ItemStack newBook;
|
||||
Player receivingPlayer = this.getServer().getPlayer(args[2]);
|
||||
if (receivingPlayer == null) {
|
||||
sendErrorMessage(sender, "Player not found!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (receivingPlayer.getInventory().firstEmpty() == -1) {
|
||||
sendErrorMessage(sender, "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(sender, 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(sender, cleanString(args[1]), args[3], "player");
|
||||
else
|
||||
newBook = loadBook(sender, cleanString(args[1]), "true", "player", Integer.parseInt(args[3]));
|
||||
} else
|
||||
newBook = loadBook(sender, cleanString(args[1]), "true", "player");
|
||||
|
||||
if (newBook != null) {
|
||||
receivingPlayer.getInventory().addItem(newBook);
|
||||
sendSuccessMessage(sender, "Book sent!");
|
||||
sendSuccessMessage(receivingPlayer, "Book received!");
|
||||
return true;
|
||||
} else {
|
||||
sendErrorMessage(sender, "Book failed to load!");
|
||||
return false;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
sendErrorMessage(sender, "Invalid number of book copies specified!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected String cleanString(String fname) {
|
||||
//removes illegal characters
|
||||
if (fname.contains("/")) {
|
||||
@ -1227,32 +1249,32 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
String nullcheck;
|
||||
|
||||
try {
|
||||
FileReader fr = new FileReader(file);
|
||||
BufferedReader br = new BufferedReader(fr);
|
||||
FileReader fileReader = new FileReader(file);
|
||||
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
||||
|
||||
rawPages.add(br.readLine());
|
||||
rawPages.add(bufferedReader.readLine());
|
||||
|
||||
if (rawPages.get(0) == null) {
|
||||
br.close();
|
||||
bufferedReader.close();
|
||||
return null;
|
||||
}
|
||||
|
||||
//if a book is being loaded its content need not be adjusted
|
||||
if (rawPages.get(0).equalsIgnoreCase("[Book]")) {
|
||||
rawPages.remove(0);
|
||||
nullcheck = br.readLine();
|
||||
nullcheck = bufferedReader.readLine();
|
||||
while (nullcheck != null) {
|
||||
rawPages.add(nullcheck);
|
||||
nullcheck = br.readLine();
|
||||
nullcheck = bufferedReader.readLine();
|
||||
}
|
||||
} else//adjusts content to page length
|
||||
{
|
||||
} else {
|
||||
//adjusts content to page length
|
||||
while (rawPages.get(rawPages.size() - 1) != null) {
|
||||
rawPages = pageFormat(rawPages);
|
||||
rawPages.add(br.readLine());
|
||||
BookFormatter.formatLastPage(rawPages);
|
||||
rawPages.add(bufferedReader.readLine());
|
||||
}
|
||||
}
|
||||
br.close();
|
||||
bufferedReader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
@ -1590,50 +1612,6 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
return fileList;
|
||||
}
|
||||
|
||||
protected List<String> pageFormat(List<String> rawPages) {
|
||||
//adds newline if content does not exceed page limit
|
||||
if (rawPages.get(rawPages.size() - 1).length() <= 254 && !rawPages.get(rawPages.size() - 1).isEmpty()) {
|
||||
rawPages.set(rawPages.size() - 1, (rawPages.get(rawPages.size() - 1)) + "\n");
|
||||
}
|
||||
|
||||
//combines lines until page limit is reached
|
||||
if (rawPages.size() > 1) {
|
||||
if (rawPages.get(rawPages.size() - 2).length() + rawPages.get(rawPages.size() - 1).length() <= 256) {
|
||||
rawPages.set(rawPages.size() - 2, (rawPages.get(rawPages.size() - 2)) + (rawPages.get(rawPages.size() - 1)));
|
||||
rawPages.remove(rawPages.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//splits page if it is too long
|
||||
if (rawPages.get(rawPages.size() - 1).length() > 256) {
|
||||
int lS;//the last space before a pagebreak
|
||||
while (rawPages.get(rawPages.size() - 1).length() > 256) {
|
||||
//avoiding word split requires spaces
|
||||
if (rawPages.get(rawPages.size() - 1).substring(0, 256).contains(" ")) {
|
||||
lS = rawPages.get(rawPages.size() - 1).substring(0, 256).lastIndexOf(" ");
|
||||
rawPages.add(rawPages.get(rawPages.size() - 1).substring(lS + 1));
|
||||
//adds newline if content does not exceed page limit
|
||||
if (rawPages.get(rawPages.size() - 1).length() <= 254) {
|
||||
rawPages.set(rawPages.size() - 1, (rawPages.get(rawPages.size() - 1)) + "\n");
|
||||
}
|
||||
//removes excess page content
|
||||
rawPages.set(rawPages.size() - 2, rawPages.get(rawPages.size() - 2).substring(0, lS));
|
||||
} else//if solid wall of text
|
||||
{
|
||||
rawPages.add(rawPages.get(rawPages.size() - 1).substring(256));
|
||||
//adds newline if content does not exceed page limit
|
||||
if (rawPages.get(rawPages.size() - 1).length() <= 254) {
|
||||
rawPages.set(rawPages.size() - 1, (rawPages.get(rawPages.size() - 1)) + "\n");
|
||||
}
|
||||
//removes excess page content
|
||||
rawPages.set(rawPages.size() - 2, rawPages.get(rawPages.size() - 2).substring(0, 256));
|
||||
}
|
||||
}
|
||||
}
|
||||
return rawPages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes null and empty items from a list
|
||||
*
|
||||
@ -1678,46 +1656,41 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
return null;
|
||||
}
|
||||
|
||||
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) {
|
||||
BookMeta newMetadata = saveBookPlaintext(groupName, player, book, integerKey);
|
||||
if (newMetadata == null) {
|
||||
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!");
|
||||
//Get the encrypted pages
|
||||
List<String> encryptedPages = EncryptionHelper.encryptBookPages(book, style, integerKey, player);
|
||||
if (encryptedPages == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//Change the book text to the encrypted text
|
||||
encryptedPages = pageFormat(encryptedPages);
|
||||
newPages = cleanList(encryptedPages);
|
||||
//Format the last page just in case
|
||||
BookFormatter.formatLastPage(encryptedPages);
|
||||
//Remove empty pages
|
||||
List<String> newPages = cleanList(encryptedPages);
|
||||
|
||||
ItemStack encryptedBook = createEncryptedBook(book, newPages, mainHand, player, newMetadata);
|
||||
|
||||
sendSuccessMessage(player, "Book encrypted!");
|
||||
encryptedBook = new ItemStack(Material.WRITTEN_BOOK);
|
||||
return encryptedBook;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new encrypted book
|
||||
* @param book <p>The book to encrypt</p>
|
||||
* @param newPages <p>The new encrypted pages</p>
|
||||
* @param mainHand <p>Whether the book is held in the player's main hand or off hand</p>
|
||||
* @param player <p>The player encrypting the book</p>
|
||||
* @param newMetadata <p>The new metadata of the book</p>
|
||||
* @return <p>An encrypted version of the book</p>
|
||||
*/
|
||||
private ItemStack createEncryptedBook(BookMeta book, List<String> newPages, boolean mainHand, Player player, BookMeta newMetadata) {
|
||||
//Create the encrypted book
|
||||
ItemStack encryptedBook = new ItemStack(Material.WRITTEN_BOOK);
|
||||
book.setPages(newPages);
|
||||
encryptedBook.setItemMeta(book);
|
||||
|
||||
@ -1727,14 +1700,36 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
} else {
|
||||
encryptedBook.setAmount(player.getInventory().getItemInOffHand().getAmount());
|
||||
}
|
||||
//Set new item metadata if using group encryption
|
||||
if (newMetadata != null) {
|
||||
//Set new item metadata
|
||||
encryptedBook.setItemMeta(newMetadata);
|
||||
}
|
||||
|
||||
return encryptedBook;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a book's plain text to a file
|
||||
* @param groupName <p>The group who's allowed to decrypt the book, or ""</p>
|
||||
* @param player <p>The player trying to encrypt the book</p>
|
||||
* @param book <p>The book to encrypt</p>
|
||||
* @param integerKey <p>The key used to encrypt the book</p>
|
||||
* @return <p>The new metadata for the book, or null if it could not be saved</p>
|
||||
*/
|
||||
private BookMeta saveBookPlaintext(String groupName, Player player, BookMeta book, String integerKey) {
|
||||
BookMeta newMetadata = book;
|
||||
boolean wasSaved;
|
||||
if (groupName.trim().isEmpty()) {
|
||||
wasSaved = eSave(player, book, integerKey);
|
||||
} else {
|
||||
newMetadata = groupESave(player, book, integerKey, groupName);
|
||||
wasSaved = newMetadata != null;
|
||||
}
|
||||
if (wasSaved) {
|
||||
return newMetadata;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a success message to a command sender (player or a console)
|
||||
* @param sender <p>The sender to send the message to</p>
|
||||
@ -1750,7 +1745,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
* @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) {
|
||||
public static void sendErrorMessage(CommandSender sender, String message) {
|
||||
sender.sendMessage(errorColor + message);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user