Lots of cleanup and improvements
Makes a new enum for representing the three different folders Splits file listing from file printing Uses a separate list for public files Only updates the public files list when necessary Removes some confusion caused by bookFromYml calling bookFromTXT Increases readability for a lot of code
This commit is contained in:
parent
4ec46d3d9b
commit
48ac82f4d4
@ -22,6 +22,7 @@ import net.knarcraft.bookswithoutborders.command.CommandUnSign;
|
|||||||
import net.knarcraft.bookswithoutborders.command.GiveTabCompleter;
|
import net.knarcraft.bookswithoutborders.command.GiveTabCompleter;
|
||||||
import net.knarcraft.bookswithoutborders.listener.PlayerEventListener;
|
import net.knarcraft.bookswithoutborders.listener.PlayerEventListener;
|
||||||
import net.knarcraft.bookswithoutborders.listener.SignEventListener;
|
import net.knarcraft.bookswithoutborders.listener.SignEventListener;
|
||||||
|
import net.knarcraft.bookswithoutborders.state.BookDirectory;
|
||||||
import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper;
|
import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.EconomyHelper;
|
import net.knarcraft.bookswithoutborders.utility.EconomyHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
||||||
@ -67,15 +68,58 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
private static boolean adminDecrypt;
|
private static boolean adminDecrypt;
|
||||||
|
|
||||||
private static ItemFactory itemFactory;
|
private static ItemFactory itemFactory;
|
||||||
public static Map<String, List<String>> loadList;
|
private static Map<String, List<String>> playerBooksList;
|
||||||
|
private static List<String> publicBooksList;
|
||||||
private static BooksWithoutBorders booksWithoutBorders;
|
private static BooksWithoutBorders booksWithoutBorders;
|
||||||
public static ConsoleCommandSender consoleSender;
|
private static ConsoleCommandSender consoleSender;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the console sender for printing to the console
|
||||||
|
* @return <p>The console's console sender</p>
|
||||||
|
*/
|
||||||
|
public static ConsoleCommandSender getConsoleSender() {
|
||||||
|
return consoleSender;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets available books
|
||||||
|
* @param sender <p>The sender wanting to see available books</p>
|
||||||
|
* @param getPublic <p>Whether to get available public books</p>
|
||||||
|
* @return <p>A list of available books</p>
|
||||||
|
*/
|
||||||
|
public static List<String> getAvailableBooks(CommandSender sender, boolean getPublic) {
|
||||||
|
if (getPublic) {
|
||||||
|
return new ArrayList<>(publicBooksList);
|
||||||
|
} else {
|
||||||
|
String senderName = sender.getName();
|
||||||
|
if (!playerBooksList.containsKey(senderName)) {
|
||||||
|
List<String> newFiles = FileHelper.listFiles(sender, false);
|
||||||
|
playerBooksList.put(senderName, newFiles);
|
||||||
|
}
|
||||||
|
return playerBooksList.get(senderName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates available books
|
||||||
|
* @param sender <p>The sender to update books for</p>
|
||||||
|
* @param updatePublic <p>Whether to update public books</p>
|
||||||
|
*/
|
||||||
|
public static void updateBooks(CommandSender sender, boolean updatePublic) {
|
||||||
|
List<String> newFiles = FileHelper.listFiles(sender, updatePublic);
|
||||||
|
if (updatePublic) {
|
||||||
|
publicBooksList = newFiles;
|
||||||
|
} else {
|
||||||
|
playerBooksList.put(sender.getName(), newFiles);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
booksWithoutBorders = this;
|
booksWithoutBorders = this;
|
||||||
consoleSender = this.getServer().getConsoleSender();
|
consoleSender = this.getServer().getConsoleSender();
|
||||||
loadList = new HashMap<>();
|
playerBooksList = new HashMap<>();
|
||||||
|
publicBooksList = FileHelper.listFiles(consoleSender, true);
|
||||||
firstBooks = new ArrayList<>();
|
firstBooks = new ArrayList<>();
|
||||||
BooksWithoutBordersSettings.initialize(this);
|
BooksWithoutBordersSettings.initialize(this);
|
||||||
|
|
||||||
@ -394,38 +438,61 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStack loadBook(CommandSender sender, String fileName, String isSigned, String dir) {
|
/**
|
||||||
return loadBook(sender, fileName, isSigned, dir, 1);
|
* Loads the given book
|
||||||
|
* @param sender <p>The command sender trying to load the book</p>
|
||||||
|
* @param fileName <p>The index or file name of the book to load</p>
|
||||||
|
* @param isSigned <p>Whether to load the book as signed, and not unsigned</p>
|
||||||
|
* @param directory <p>The directory to save the book in</p>
|
||||||
|
* @return <p>The loaded book</p>
|
||||||
|
*/
|
||||||
|
public ItemStack loadBook(CommandSender sender, String fileName, String isSigned, String directory) {
|
||||||
|
return loadBook(sender, fileName, isSigned, directory, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStack loadBook(CommandSender sender, String fileName, String isSigned, String dir, int numCopies) {
|
/**
|
||||||
//checks if player is using list number to load
|
* Loads the given book
|
||||||
for (int x = 0; x < fileName.length(); x++) {
|
* @param sender <p>The command sender trying to load the book</p>
|
||||||
if (!Character.isDigit(fileName.charAt(x))) {
|
* @param fileName <p>The index or file name of the book to load</p>
|
||||||
break;
|
* @param isSigned <p>Whether to load the book as signed, and not unsigned</p>
|
||||||
}
|
* @param directory <p>The directory to save the book in</p>
|
||||||
if (x == fileName.length() - 1 && Character.isDigit(fileName.charAt(x)) && loadList.containsKey(sender.getName())) {
|
* @param numCopies <p>The number of copies to load</p>
|
||||||
if (Integer.parseInt(fileName) <= loadList.get(sender.getName()).size()) {
|
* @return <p>The loaded book</p>
|
||||||
fileName = loadList.get(sender.getName()).get(Integer.parseInt(fileName) - 1);
|
*/
|
||||||
}
|
public ItemStack loadBook(CommandSender sender, String fileName, String isSigned, String directory, int numCopies) {
|
||||||
}
|
BookDirectory bookDirectory = BookDirectory.getFromString(directory);
|
||||||
}
|
|
||||||
|
|
||||||
File file;
|
//Find the filename if a book index is given
|
||||||
if (dir.equalsIgnoreCase("public")) {
|
try {
|
||||||
|
int bookIndex = Integer.parseInt(fileName);
|
||||||
|
List<String> availableFiles = getAvailableBooks(sender, bookDirectory == BookDirectory.PUBLIC);
|
||||||
|
if (bookIndex <= availableFiles.size()) {
|
||||||
|
fileName = availableFiles.get(Integer.parseInt(fileName) - 1);
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException ignored) {}
|
||||||
|
|
||||||
|
//Get the full path of the book to load
|
||||||
|
File file = null;
|
||||||
|
if (bookDirectory == BookDirectory.PUBLIC) {
|
||||||
file = FileHelper.getBookFile(getBookFolder() + fileName);
|
file = FileHelper.getBookFile(getBookFolder() + fileName);
|
||||||
} else if (dir.equalsIgnoreCase("player")) {
|
} else if (bookDirectory == BookDirectory.PLAYER) {
|
||||||
file = FileHelper.getBookFile(getBookFolder() + cleanString(sender.getName()) + getSlash() + fileName);
|
file = FileHelper.getBookFile(getBookFolder() + cleanString(sender.getName()) + getSlash() + fileName);
|
||||||
} else if (!dir.trim().isEmpty()) {
|
} else if (bookDirectory == BookDirectory.ENCRYPTED) {
|
||||||
file = FileHelper.getBookFile(getBookFolder() + "Encrypted" + getSlash() + dir + getSlash() + fileName);
|
file = FileHelper.getBookFile(getBookFolder() + "Encrypted" + getSlash() + directory + getSlash() + fileName);
|
||||||
} else {
|
|
||||||
file = null;
|
|
||||||
}
|
}
|
||||||
if (file == null || !file.isFile()) {
|
if (file == null || !file.isFile()) {
|
||||||
sendErrorMessage(sender, "Incorrect file name!");
|
sendErrorMessage(sender, "Incorrect file name!");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Make sure the player can pay for the book
|
||||||
|
if (booksHavePrice() && !sender.hasPermission("bookswithoutborders.bypassBookPrice") &&
|
||||||
|
(bookDirectory == BookDirectory.PUBLIC || bookDirectory == BookDirectory.PLAYER) &&
|
||||||
|
cannotPayForBookPrinting((Player) sender, numCopies)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Generate a new empty book
|
||||||
ItemStack book;
|
ItemStack book;
|
||||||
BookMeta bookMetadata = (BookMeta) itemFactory.getItemMeta(Material.WRITTEN_BOOK);
|
BookMeta bookMetadata = (BookMeta) itemFactory.getItemMeta(Material.WRITTEN_BOOK);
|
||||||
if (isSigned.equalsIgnoreCase("true")) {
|
if (isSigned.equalsIgnoreCase("true")) {
|
||||||
@ -434,14 +501,15 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
book = new ItemStack(Material.WRITABLE_BOOK);
|
book = new ItemStack(Material.WRITABLE_BOOK);
|
||||||
}
|
}
|
||||||
|
|
||||||
BookToFromTextHelper.bookFromYml(file, bookMetadata);
|
//Load the book from the given file
|
||||||
|
BookToFromTextHelper.bookFromFile(file, bookMetadata);
|
||||||
if (bookMetadata == null) {
|
if (bookMetadata == null) {
|
||||||
sendErrorMessage(sender, "File was blank!!");
|
sendErrorMessage(sender, "File was blank!!");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dir.equalsIgnoreCase("public") && !dir.equalsIgnoreCase("player") &&
|
//Remove "encrypted" from the book lore
|
||||||
!dir.equalsIgnoreCase("") && bookMetadata.hasLore()) {
|
if (bookDirectory == BookDirectory.ENCRYPTED && bookMetadata.hasLore()) {
|
||||||
List<String> oldLore = bookMetadata.getLore();
|
List<String> oldLore = bookMetadata.getLore();
|
||||||
if (oldLore != null) {
|
if (oldLore != null) {
|
||||||
List<String> newLore = new ArrayList<>(oldLore);
|
List<String> newLore = new ArrayList<>(oldLore);
|
||||||
@ -450,16 +518,10 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Set the metadata and amount to the new book
|
||||||
book.setItemMeta(bookMetadata);
|
book.setItemMeta(bookMetadata);
|
||||||
book.setAmount(numCopies);
|
book.setAmount(numCopies);
|
||||||
|
|
||||||
if (booksHavePrice() && !sender.hasPermission("bookswithoutborders.bypassBookPrice") &&
|
|
||||||
(dir.equalsIgnoreCase("public") || dir.equalsIgnoreCase("player") ||
|
|
||||||
dir.equalsIgnoreCase("")) &&
|
|
||||||
cannotPayForBookPrinting((Player) sender, numCopies)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return book;
|
return book;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
||||||
@ -39,17 +40,20 @@ public class CommandDelete implements CommandExecutor {
|
|||||||
boolean deleteBook(CommandSender sender, String[] args, boolean deletePublic) {
|
boolean deleteBook(CommandSender sender, String[] args, boolean deletePublic) {
|
||||||
//List deletable files
|
//List deletable files
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, deletePublic, false));
|
FileHelper.printBooks(sender, deletePublic);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//Delete the file
|
//Delete the file
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
if (!BooksWithoutBorders.loadList.containsKey(sender.getName())) {
|
List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, deletePublic);
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "You must first use /bwb delete to create a list of delete-able files!");
|
if (availableBooks == null) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(sender, "You must first use /deletebook to create a list of delete-able files!");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
if (!BooksWithoutBorders.loadList.get(sender.getName()).isEmpty()) {
|
if (!availableBooks.isEmpty()) {
|
||||||
performBookDeletion(sender, args[0], deletePublic);
|
performBookDeletion(sender, args[0], deletePublic);
|
||||||
|
//Update the book list
|
||||||
|
BooksWithoutBorders.updateBooks(sender, deletePublic);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "No files available to delete!");
|
BooksWithoutBorders.sendErrorMessage(sender, "No files available to delete!");
|
||||||
@ -72,9 +76,9 @@ public class CommandDelete implements CommandExecutor {
|
|||||||
//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);
|
||||||
String senderName = sender.getName();
|
List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, isPublic);
|
||||||
if (BooksWithoutBorders.loadList.containsKey(senderName) && loadListIndex <= BooksWithoutBorders.loadList.get(senderName).size()) {
|
if (loadListIndex <= availableBooks.size()) {
|
||||||
fileName = BooksWithoutBorders.loadList.get(senderName).get(loadListIndex - 1);
|
fileName = availableBooks.get(loadListIndex - 1);
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException ignored) {
|
} catch (NumberFormatException ignored) {
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,14 @@ public class CommandGive implements CommandExecutor {
|
|||||||
return giveBook(sender, args, false, "player");
|
return giveBook(sender, args, false, "player");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gives a book to another player
|
||||||
|
* @param sender <p>The sender trying to give a book</p>
|
||||||
|
* @param args <p>The arguments given</p>
|
||||||
|
* @param givePublic <p>Whether to give a public book</p>
|
||||||
|
* @param folder <p>The folder containing the book to load</p>
|
||||||
|
* @return <p>True if the book was given successfully</p>
|
||||||
|
*/
|
||||||
boolean giveBook(CommandSender sender, String[] args, boolean givePublic, String folder) {
|
boolean giveBook(CommandSender sender, String[] args, boolean givePublic, String folder) {
|
||||||
if (args.length == 1 || args.length > 4) {
|
if (args.length == 1 || args.length > 4) {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "Incorrect number of arguments for this command!");
|
BooksWithoutBorders.sendErrorMessage(sender, "Incorrect number of arguments for this command!");
|
||||||
@ -33,7 +41,7 @@ public class CommandGive implements CommandExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, givePublic, false));
|
FileHelper.printBooks(sender, givePublic);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +64,7 @@ public class CommandGive implements CommandExecutor {
|
|||||||
//Load books available to the player
|
//Load books available to the player
|
||||||
try {
|
try {
|
||||||
Integer.parseInt(bookIdentifier);
|
Integer.parseInt(bookIdentifier);
|
||||||
BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, givePublic, true));
|
BooksWithoutBorders.updateBooks(sender, givePublic);
|
||||||
} catch (NumberFormatException ignored) {
|
} catch (NumberFormatException ignored) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ public class CommandLoad implements CommandExecutor {
|
|||||||
|
|
||||||
//Show books available to the player
|
//Show books available to the player
|
||||||
if (argumentCount == 0) {
|
if (argumentCount == 0) {
|
||||||
BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(player, loadPublic, false));
|
FileHelper.printBooks(sender, loadPublic);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,13 +67,13 @@ public class CommandLoad implements CommandExecutor {
|
|||||||
//Load books available to the player
|
//Load books available to the player
|
||||||
try {
|
try {
|
||||||
Integer.parseInt(bookIdentifier);
|
Integer.parseInt(bookIdentifier);
|
||||||
BooksWithoutBorders.loadList.put(player.getName(), FileHelper.listFiles(player, loadPublic, true));
|
BooksWithoutBorders.updateBooks(sender, loadPublic);
|
||||||
} catch (NumberFormatException ignored) {
|
} catch (NumberFormatException ignored) {
|
||||||
}
|
}
|
||||||
|
|
||||||
String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier);
|
String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier);
|
||||||
try {
|
try {
|
||||||
//Give the new book if it cannot be loaded
|
//Give the new book if it can be loaded
|
||||||
ItemStack newBook = booksWithoutBorders.loadBook(player, bookToLoad, isSigned, directory, Integer.parseInt(copies));
|
ItemStack newBook = booksWithoutBorders.loadBook(player, bookToLoad, isSigned, directory, Integer.parseInt(copies));
|
||||||
if (newBook != null) {
|
if (newBook != null) {
|
||||||
player.getInventory().addItem(newBook);
|
player.getInventory().addItem(newBook);
|
||||||
|
@ -136,6 +136,8 @@ public class CommandSave implements CommandExecutor {
|
|||||||
BookToFromTextHelper.bookToTXT(savePath, fileName, book);
|
BookToFromTextHelper.bookToTXT(savePath, fileName, book);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Update the relevant book list
|
||||||
|
BooksWithoutBorders.updateBooks(player, saveToPublicFolder);
|
||||||
BooksWithoutBorders.sendSuccessMessage(player, "Book Saved as \"" + fileName + "\"");
|
BooksWithoutBorders.sendSuccessMessage(player, "Book Saved as \"" + fileName + "\"");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -49,7 +49,7 @@ public class GiveTabCompleter implements TabCompleter {
|
|||||||
|
|
||||||
if (argumentCount == 1) {
|
if (argumentCount == 1) {
|
||||||
//Return list of books
|
//Return list of books
|
||||||
return BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, false, true));
|
return BooksWithoutBorders.getAvailableBooks(sender, false);
|
||||||
} else if (argumentCount == 2) {
|
} else if (argumentCount == 2) {
|
||||||
//Return online players
|
//Return online players
|
||||||
return playerNames;
|
return playerNames;
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package net.knarcraft.bookswithoutborders.listener;
|
package net.knarcraft.bookswithoutborders.listener;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
|
||||||
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
@ -16,11 +15,9 @@ import org.bukkit.inventory.meta.BookMeta;
|
|||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
||||||
import static net.knarcraft.bookswithoutborders.utility.FileHelper.isBookListIndex;
|
|
||||||
|
|
||||||
public class PlayerEventListener implements Listener {
|
public class PlayerEventListener implements Listener {
|
||||||
|
|
||||||
@ -90,12 +87,6 @@ public class PlayerEventListener implements Listener {
|
|||||||
private boolean giveBookToNewPlayer(String bookName, Player player, boolean sendMessage) {
|
private boolean giveBookToNewPlayer(String bookName, Player player, boolean sendMessage) {
|
||||||
if (!bookName.trim().isEmpty()) {
|
if (!bookName.trim().isEmpty()) {
|
||||||
|
|
||||||
//Handles loadList numbers
|
|
||||||
if (isBookListIndex(bookName)) {
|
|
||||||
List<String> availableFiles = FileHelper.listFiles(player, true, true);
|
|
||||||
BooksWithoutBorders.loadList.put(player.getName(), availableFiles);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Give the book to the player if it exists
|
//Give the book to the player if it exists
|
||||||
ItemStack newBook = booksWithoutBorders.loadBook(player, bookName, "true", "public");
|
ItemStack newBook = booksWithoutBorders.loadBook(player, bookName, "true", "public");
|
||||||
if (newBook != null) {
|
if (newBook != null) {
|
||||||
|
@ -21,7 +21,6 @@ import org.bukkit.inventory.PlayerInventory;
|
|||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
||||||
@ -191,8 +190,7 @@ public class SignEventListener implements Listener {
|
|||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
if (isBookListIndex(lines[2])) {
|
if (isBookListIndex(lines[2])) {
|
||||||
List<String> availableFiles = FileHelper.listFiles(player, true, true);
|
BooksWithoutBorders.updateBooks(player, true);
|
||||||
BooksWithoutBorders.loadList.put(player.getName(), availableFiles);
|
|
||||||
markGiveSignValidity(event, true);
|
markGiveSignValidity(event, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -295,17 +293,16 @@ public class SignEventListener implements Listener {
|
|||||||
String fileName = ChatColor.stripColor(sign.getLine(2));
|
String fileName = ChatColor.stripColor(sign.getLine(2));
|
||||||
boolean isLoadListNumber = false;
|
boolean isLoadListNumber = false;
|
||||||
|
|
||||||
for (int x = 0; x < fileName.length(); x++) {
|
try {
|
||||||
if (!Character.isDigit(fileName.charAt(x)))
|
Integer.parseInt(fileName);
|
||||||
break;
|
|
||||||
if (x == fileName.length() - 1) {
|
|
||||||
BooksWithoutBorders.loadList.put(player.getName(), FileHelper.listFiles(player, true, true));
|
|
||||||
isLoadListNumber = true;
|
isLoadListNumber = true;
|
||||||
}
|
} catch (NumberFormatException ignored){}
|
||||||
}
|
|
||||||
|
|
||||||
if (!isLoadListNumber && sign.getLine(3).length() >= 2)
|
//Add the third line to the second line for the full filename
|
||||||
fileName = ChatColor.stripColor(sign.getLine(2)) + ChatColor.stripColor(sign.getLine(3));
|
String thirdLine = sign.getLine(3);
|
||||||
|
if (!isLoadListNumber && thirdLine.length() >= 2) {
|
||||||
|
fileName += ChatColor.stripColor(thirdLine);
|
||||||
|
}
|
||||||
|
|
||||||
ItemStack newBook = BooksWithoutBorders.getInstance().loadBook(player, fileName, "true", "public");
|
ItemStack newBook = BooksWithoutBorders.getInstance().loadBook(player, fileName, "true", "public");
|
||||||
|
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package net.knarcraft.bookswithoutborders.state;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This enum represents the different directories books can be saved in
|
||||||
|
*/
|
||||||
|
public enum BookDirectory {
|
||||||
|
PUBLIC,
|
||||||
|
PLAYER,
|
||||||
|
ENCRYPTED;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the relevant book directory given a directory name
|
||||||
|
* @param directory <p>The directory to transform</p>
|
||||||
|
* @return <p>A book directory, or null if the given directory is empty</p>
|
||||||
|
*/
|
||||||
|
public static BookDirectory getFromString(String directory) {
|
||||||
|
if (directory.equalsIgnoreCase("public")) {
|
||||||
|
return BookDirectory.PUBLIC;
|
||||||
|
} else if (directory.equalsIgnoreCase("player")) {
|
||||||
|
return BookDirectory.PLAYER;
|
||||||
|
} else if (!directory.trim().isEmpty()) {
|
||||||
|
return BookDirectory.ENCRYPTED;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -52,32 +52,21 @@ public final class BookToFromTextHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a book from a .yml file
|
* Loads a book from a file
|
||||||
*
|
* @param file <p>The file to load</p>
|
||||||
* @param file <p>The path of the file to load</p>
|
* @param bookMetadata <p>The book metadata to use for saving the book</p>
|
||||||
* @param bookMetadata <p>Metadata which will be altered with the book's contents</p>
|
* @return <p>The book metadata of the loaded book</p>
|
||||||
* @return <p>Metadata for the loaded book</p>
|
|
||||||
*/
|
*/
|
||||||
public static BookMeta bookFromYml(File file, BookMeta bookMetadata) {
|
public static BookMeta bookFromFile(File file, BookMeta bookMetadata) {
|
||||||
//Handles old style loading
|
if (file.getName().endsWith(".txt")) {
|
||||||
if (file.getName().substring(file.getName().lastIndexOf(".")).equals(".txt"))
|
return bookFromTXT(file.getName(), file, bookMetadata);
|
||||||
bookMetadata = bookFromTXT(file.getName(), file, bookMetadata);
|
} else if (file.getName().endsWith(".yml")) {
|
||||||
else {
|
return bookFromYml(file, bookMetadata);
|
||||||
try {
|
} else {
|
||||||
FileConfiguration bookYml = YamlConfiguration.loadConfiguration(file);
|
throw new IllegalArgumentException("Trying to load a book file with an unrecognized extension");
|
||||||
|
|
||||||
bookMetadata.setTitle(bookYml.getString("Title", "Untitled"));
|
|
||||||
bookMetadata.setAuthor(bookYml.getString("Author", "Unknown"));
|
|
||||||
bookMetadata.setPages(bookYml.getStringList("Pages"));
|
|
||||||
bookMetadata.setLore(bookYml.getStringList("Lore"));
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return bookMetadata;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves a book's contents to a text file
|
* Saves a book's contents to a text file
|
||||||
*
|
*
|
||||||
@ -91,6 +80,7 @@ public final class BookToFromTextHelper {
|
|||||||
PrintWriter printWriter = new PrintWriter(fileWriter);
|
PrintWriter printWriter = new PrintWriter(fileWriter);
|
||||||
List<String> pages = bookMetadata.getPages();
|
List<String> pages = bookMetadata.getPages();
|
||||||
|
|
||||||
|
//Save each page of the book as a text line
|
||||||
printWriter.println("[Book]");
|
printWriter.println("[Book]");
|
||||||
for (String page : pages) {
|
for (String page : pages) {
|
||||||
printWriter.println(page);
|
printWriter.println(page);
|
||||||
@ -98,6 +88,27 @@ public final class BookToFromTextHelper {
|
|||||||
printWriter.close();
|
printWriter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a book from a .yml file
|
||||||
|
*
|
||||||
|
* @param file <p>The path of the file to load</p>
|
||||||
|
* @param bookMetadata <p>Metadata which will be altered with the book's contents</p>
|
||||||
|
* @return <p>Metadata for the loaded book</p>
|
||||||
|
*/
|
||||||
|
private static BookMeta bookFromYml(File file, BookMeta bookMetadata) {
|
||||||
|
try {
|
||||||
|
FileConfiguration bookYml = YamlConfiguration.loadConfiguration(file);
|
||||||
|
|
||||||
|
bookMetadata.setTitle(bookYml.getString("Title", "Untitled"));
|
||||||
|
bookMetadata.setAuthor(bookYml.getString("Author", "Unknown"));
|
||||||
|
bookMetadata.setPages(bookYml.getStringList("Pages"));
|
||||||
|
bookMetadata.setLore(bookYml.getStringList("Lore"));
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return bookMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a book from a text file
|
* Loads a book from a text file
|
||||||
*
|
*
|
||||||
@ -111,6 +122,7 @@ public final class BookToFromTextHelper {
|
|||||||
String title;
|
String title;
|
||||||
String titleAuthorSeparator = BooksWithoutBorders.getTitleAuthorSeparator();
|
String titleAuthorSeparator = BooksWithoutBorders.getTitleAuthorSeparator();
|
||||||
|
|
||||||
|
//Get title and author from the file name
|
||||||
if (fileName.contains(titleAuthorSeparator)) {
|
if (fileName.contains(titleAuthorSeparator)) {
|
||||||
author = fileName.substring(fileName.indexOf(titleAuthorSeparator) + 1, fileName.length() - 4);
|
author = fileName.substring(fileName.indexOf(titleAuthorSeparator) + 1, fileName.length() - 4);
|
||||||
title = fileName.substring(0, fileName.indexOf(titleAuthorSeparator));
|
title = fileName.substring(0, fileName.indexOf(titleAuthorSeparator));
|
||||||
@ -119,45 +131,22 @@ public final class BookToFromTextHelper {
|
|||||||
title = fileName.substring(0, fileName.length() - 4);
|
title = fileName.substring(0, fileName.length() - 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Replace underscores with spaces
|
||||||
title = fixName(title, true);
|
title = fixName(title, true);
|
||||||
|
|
||||||
List<String> rawPages = new ArrayList<>();
|
//Read the .txt file
|
||||||
String nullCheck;
|
List<String> rawPages;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FileReader fileReader = new FileReader(file);
|
rawPages = readTextFile(file);
|
||||||
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
|
||||||
|
|
||||||
rawPages.add(bufferedReader.readLine());
|
|
||||||
|
|
||||||
if (rawPages.get(0) == null) {
|
|
||||||
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 = bufferedReader.readLine();
|
|
||||||
while (nullCheck != null) {
|
|
||||||
rawPages.add(nullCheck);
|
|
||||||
nullCheck = bufferedReader.readLine();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//Adjusts content to page length
|
|
||||||
while (rawPages.get(rawPages.size() - 1) != null) {
|
|
||||||
BookFormatter.formatLastPage(rawPages);
|
|
||||||
rawPages.add(bufferedReader.readLine());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bufferedReader.close();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Remove any empty pages
|
||||||
List<String> pages = new ArrayList<>(InputCleaningHelper.cleanList(rawPages));
|
List<String> pages = new ArrayList<>(InputCleaningHelper.cleanList(rawPages));
|
||||||
|
|
||||||
|
//Update the metadata of the book with its new values
|
||||||
bookMetadata.setAuthor(author);
|
bookMetadata.setAuthor(author);
|
||||||
bookMetadata.setTitle(title);
|
bookMetadata.setTitle(title);
|
||||||
bookMetadata.setPages(pages);
|
bookMetadata.setPages(pages);
|
||||||
@ -165,4 +154,43 @@ public final class BookToFromTextHelper {
|
|||||||
return bookMetadata;
|
return bookMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads text from a .txt file
|
||||||
|
* @param file <p>The file to read</p>
|
||||||
|
* @return <p>A string list where each string is the text on one page</p>
|
||||||
|
* @throws IOException <p>If unable to read from the file</p>
|
||||||
|
*/
|
||||||
|
private static List<String> readTextFile(File file) throws IOException {
|
||||||
|
List<String> rawPages = new ArrayList<>();
|
||||||
|
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
|
||||||
|
|
||||||
|
//Use the first line to decide if we are loading a book, or creating a new book
|
||||||
|
String firstLine = bufferedReader.readLine();
|
||||||
|
if (firstLine == null) {
|
||||||
|
bufferedReader.close();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (firstLine.equalsIgnoreCase("[Book]")) {
|
||||||
|
//Read every line directly as a page, as this is a saved book
|
||||||
|
String readLine;
|
||||||
|
do {
|
||||||
|
readLine = bufferedReader.readLine();
|
||||||
|
if (readLine != null) {
|
||||||
|
rawPages.add(readLine);
|
||||||
|
}
|
||||||
|
} while (readLine != null);
|
||||||
|
} else {
|
||||||
|
//Adjust content to page length for each added line to make the text fit the book
|
||||||
|
rawPages.add(firstLine);
|
||||||
|
while (rawPages.get(rawPages.size() - 1) != null) {
|
||||||
|
BookFormatter.formatLastPage(rawPages);
|
||||||
|
rawPages.add(bufferedReader.readLine());
|
||||||
|
}
|
||||||
|
BookFormatter.formatLastPage(rawPages);
|
||||||
|
}
|
||||||
|
bufferedReader.close();
|
||||||
|
|
||||||
|
return rawPages;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import net.knarcraft.bookswithoutborders.encryption.SubstitutionCipher;
|
|||||||
import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
|
import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
@ -213,7 +214,7 @@ public final class EncryptionHelper {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
bookMetadata = BookToFromTextHelper.bookFromYml(file, bookMetadata);
|
bookMetadata = BookToFromTextHelper.bookFromFile(file, bookMetadata);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Decryption failed!");
|
BooksWithoutBorders.sendErrorMessage(player, "Decryption failed!");
|
||||||
return null;
|
return null;
|
||||||
@ -221,14 +222,15 @@ public final class EncryptionHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (deleteEncryptedFile) {
|
if (deleteEncryptedFile) {
|
||||||
|
ConsoleCommandSender consoleSender = BooksWithoutBorders.getConsoleSender();
|
||||||
try {
|
try {
|
||||||
if (!file.delete()) {
|
if (!file.delete()) {
|
||||||
BooksWithoutBorders.sendErrorMessage(BooksWithoutBorders.consoleSender, "Book encryption data failed to delete upon decryption!");
|
BooksWithoutBorders.sendErrorMessage(consoleSender, "Book encryption data failed to delete upon decryption!");
|
||||||
BooksWithoutBorders.sendErrorMessage(BooksWithoutBorders.consoleSender, "File location:" + file.getPath());
|
BooksWithoutBorders.sendErrorMessage(consoleSender, "File location:" + file.getPath());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
BooksWithoutBorders.sendErrorMessage(BooksWithoutBorders.consoleSender, "Book encryption data failed to delete upon decryption!");
|
BooksWithoutBorders.sendErrorMessage(consoleSender, "Book encryption data failed to delete upon decryption!");
|
||||||
BooksWithoutBorders.sendErrorMessage(BooksWithoutBorders.consoleSender, "File location:" + file.getPath());
|
BooksWithoutBorders.sendErrorMessage(consoleSender, "File location:" + file.getPath());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -253,7 +255,7 @@ public final class EncryptionHelper {
|
|||||||
if (!dirTest.exists()) {
|
if (!dirTest.exists()) {
|
||||||
try {
|
try {
|
||||||
if (!dirTest.mkdir()) {
|
if (!dirTest.mkdir()) {
|
||||||
BooksWithoutBorders.sendErrorMessage(BooksWithoutBorders.consoleSender, "Unable to create encryption group folder!");
|
BooksWithoutBorders.sendErrorMessage(BooksWithoutBorders.getConsoleSender(), "Unable to create encryption group folder!");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -92,17 +92,39 @@ public final class FileHelper {
|
|||||||
*
|
*
|
||||||
* @param sender <p>The command sender looking for files</p>
|
* @param sender <p>The command sender looking for files</p>
|
||||||
* @param listPublic <p>Whether to list public or personal files</p>
|
* @param listPublic <p>Whether to list public or personal files</p>
|
||||||
* @param silent <p>Whether to just return the list without printing it</p>
|
|
||||||
* @return <p>A list of available files</p>
|
* @return <p>A list of available files</p>
|
||||||
*/
|
*/
|
||||||
public static List<String> listFiles(CommandSender sender, Boolean listPublic, boolean silent) {
|
public static List<String> listFiles(CommandSender sender, Boolean listPublic) {
|
||||||
File file;
|
File file;
|
||||||
if (listPublic) {
|
if (listPublic) {
|
||||||
file = new File(getBookFolder());
|
file = new File(getBookFolder());
|
||||||
} else {
|
} else {
|
||||||
file = new File(getBookFolder() + cleanString(sender.getName()) + getSlash());
|
file = new File(getBookFolder() + cleanString(sender.getName()) + getSlash());
|
||||||
}
|
}
|
||||||
return FileHelper.listFiles(sender, file, silent);
|
return FileHelper.listFiles(sender, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints the available books
|
||||||
|
* @param sender <p>The sender to display the books to</p>
|
||||||
|
* @param listPublic <p>Whether to display public books</p>
|
||||||
|
*/
|
||||||
|
public static void printBooks(CommandSender sender, boolean listPublic) {
|
||||||
|
List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, listPublic);
|
||||||
|
FileHelper.printFiles(sender, availableBooks);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints a list of files
|
||||||
|
* @param sender <p>The command sender to show the list to</p>
|
||||||
|
* @param fileList <p>The files to list</p>
|
||||||
|
*/
|
||||||
|
public static void printFiles(CommandSender sender, List<String> fileList) {
|
||||||
|
BooksWithoutBorders.sendSuccessMessage(sender, "Available Books:");
|
||||||
|
int listSize = fileList.size();
|
||||||
|
for (int fileIndex = 0; fileIndex < listSize; fileIndex++) {
|
||||||
|
sender.sendMessage(ChatColor.GRAY + "[" + (fileIndex + 1) + "] " + fileList.get(fileIndex));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -110,10 +132,9 @@ public final class FileHelper {
|
|||||||
*
|
*
|
||||||
* @param sender <p>The command sender looking for files</p>
|
* @param sender <p>The command sender looking for files</p>
|
||||||
* @param searchDirectory <p>The directory to search for files</p>
|
* @param searchDirectory <p>The directory to search for files</p>
|
||||||
* @param silent <p>Whether to just return the list without printing it</p>
|
|
||||||
* @return <p>A list of available files</p>
|
* @return <p>A list of available files</p>
|
||||||
*/
|
*/
|
||||||
public static List<String> listFiles(CommandSender sender, File searchDirectory, boolean silent) {
|
private static List<String> listFiles(CommandSender sender, File searchDirectory) {
|
||||||
List<String> fileList = new ArrayList<>();
|
List<String> fileList = new ArrayList<>();
|
||||||
File[] existingFiles = searchDirectory.listFiles();
|
File[] existingFiles = searchDirectory.listFiles();
|
||||||
|
|
||||||
@ -122,15 +143,9 @@ public final class FileHelper {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!silent) {
|
|
||||||
BooksWithoutBorders.sendSuccessMessage(sender, "Available Books:");
|
|
||||||
}
|
|
||||||
for (File foundFile : existingFiles) {
|
for (File foundFile : existingFiles) {
|
||||||
if (foundFile.isFile()) {
|
if (foundFile.isFile()) {
|
||||||
fileList.add(foundFile.getName());
|
fileList.add(foundFile.getName());
|
||||||
if (!silent) {
|
|
||||||
sender.sendMessage(ChatColor.GRAY + "[" + fileList.size() + "] " + foundFile.getName());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user