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:
@@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
||||
@@ -39,17 +40,20 @@ public class CommandDelete implements CommandExecutor {
|
||||
boolean deleteBook(CommandSender sender, String[] args, boolean deletePublic) {
|
||||
//List deletable files
|
||||
if (args.length == 0) {
|
||||
BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, deletePublic, false));
|
||||
FileHelper.printBooks(sender, deletePublic);
|
||||
return true;
|
||||
}
|
||||
//Delete the file
|
||||
if (args.length == 1) {
|
||||
if (!BooksWithoutBorders.loadList.containsKey(sender.getName())) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "You must first use /bwb delete to create a list of delete-able files!");
|
||||
List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, deletePublic);
|
||||
if (availableBooks == null) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "You must first use /deletebook to create a list of delete-able files!");
|
||||
return false;
|
||||
} else {
|
||||
if (!BooksWithoutBorders.loadList.get(sender.getName()).isEmpty()) {
|
||||
if (!availableBooks.isEmpty()) {
|
||||
performBookDeletion(sender, args[0], deletePublic);
|
||||
//Update the book list
|
||||
BooksWithoutBorders.updateBooks(sender, deletePublic);
|
||||
return true;
|
||||
} else {
|
||||
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
|
||||
try {
|
||||
int loadListIndex = Integer.parseInt(fileName);
|
||||
String senderName = sender.getName();
|
||||
if (BooksWithoutBorders.loadList.containsKey(senderName) && loadListIndex <= BooksWithoutBorders.loadList.get(senderName).size()) {
|
||||
fileName = BooksWithoutBorders.loadList.get(senderName).get(loadListIndex - 1);
|
||||
List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, isPublic);
|
||||
if (loadListIndex <= availableBooks.size()) {
|
||||
fileName = availableBooks.get(loadListIndex - 1);
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
|
@@ -26,6 +26,14 @@ public class CommandGive implements CommandExecutor {
|
||||
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) {
|
||||
if (args.length == 1 || args.length > 4) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "Incorrect number of arguments for this command!");
|
||||
@@ -33,7 +41,7 @@ public class CommandGive implements CommandExecutor {
|
||||
}
|
||||
|
||||
if (args.length == 0) {
|
||||
BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, givePublic, false));
|
||||
FileHelper.printBooks(sender, givePublic);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -56,7 +64,7 @@ public class CommandGive implements CommandExecutor {
|
||||
//Load books available to the player
|
||||
try {
|
||||
Integer.parseInt(bookIdentifier);
|
||||
BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, givePublic, true));
|
||||
BooksWithoutBorders.updateBooks(sender, givePublic);
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
|
||||
|
@@ -45,7 +45,7 @@ public class CommandLoad implements CommandExecutor {
|
||||
|
||||
//Show books available to the player
|
||||
if (argumentCount == 0) {
|
||||
BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(player, loadPublic, false));
|
||||
FileHelper.printBooks(sender, loadPublic);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -67,13 +67,13 @@ public class CommandLoad implements CommandExecutor {
|
||||
//Load books available to the player
|
||||
try {
|
||||
Integer.parseInt(bookIdentifier);
|
||||
BooksWithoutBorders.loadList.put(player.getName(), FileHelper.listFiles(player, loadPublic, true));
|
||||
BooksWithoutBorders.updateBooks(sender, loadPublic);
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
|
||||
String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier);
|
||||
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));
|
||||
if (newBook != null) {
|
||||
player.getInventory().addItem(newBook);
|
||||
|
@@ -136,6 +136,8 @@ public class CommandSave implements CommandExecutor {
|
||||
BookToFromTextHelper.bookToTXT(savePath, fileName, book);
|
||||
}
|
||||
|
||||
//Update the relevant book list
|
||||
BooksWithoutBorders.updateBooks(player, saveToPublicFolder);
|
||||
BooksWithoutBorders.sendSuccessMessage(player, "Book Saved as \"" + fileName + "\"");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
@@ -49,7 +49,7 @@ public class GiveTabCompleter implements TabCompleter {
|
||||
|
||||
if (argumentCount == 1) {
|
||||
//Return list of books
|
||||
return BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, false, true));
|
||||
return BooksWithoutBorders.getAvailableBooks(sender, false);
|
||||
} else if (argumentCount == 2) {
|
||||
//Return online players
|
||||
return playerNames;
|
||||
|
Reference in New Issue
Block a user