Adds more consideration to the existence of unsigned books

This commit is contained in:
Kristian Knarvik 2021-08-30 00:35:54 +02:00
parent 57cc06bdb8
commit 6efbd377ee
4 changed files with 190 additions and 81 deletions

View File

@ -418,11 +418,12 @@ public class BooksWithoutBorders extends JavaPlugin {
return false;
}
if (player.getItemInHand().getType() == Material.WRITTEN_BOOK || player.getItemInHand().getType() == Material.WRITABLE_BOOK) {
if (InventoryHelper.isHoldingBook(player, true) ||
InventoryHelper.isHoldingBook(player, false)) {
if (args.length == 2) {
saveBook(player, args[1], true);
} else {
saveBook((Player) sender, "false", true);
saveBook(player, "false", true);
}
return true;
} else {
@ -462,7 +463,7 @@ public class BooksWithoutBorders extends JavaPlugin {
}
if (args.length == 1) {
loadList.put(sender.getName(), listFiles(sender, true, 0, false));
loadList.put(sender.getName(), listFiles(sender, true, false));
return true;
}
@ -470,7 +471,7 @@ public class BooksWithoutBorders extends JavaPlugin {
if (!Character.isDigit(args[1].charAt(x)))
break;
if (x == args[1].length() - 1)
loadList.put(sender.getName(), listFiles(sender, true, 0, true));
loadList.put(sender.getName(), listFiles(sender, true, true));
}
ItemStack newBook;
@ -511,7 +512,7 @@ public class BooksWithoutBorders extends JavaPlugin {
}
if (args.length == 1) {
loadList.put(sender.getName(), listFiles(sender, false, 0, false));
loadList.put(sender.getName(), listFiles(sender, false, false));
return true;
}
@ -519,7 +520,7 @@ public class BooksWithoutBorders extends JavaPlugin {
if (!Character.isDigit(args[1].charAt(x)))
break;
if (x == args[1].length() - 1)
loadList.put(sender.getName(), listFiles(sender, false, 0, true));
loadList.put(sender.getName(), listFiles(sender, false, true));
}
ItemStack newBook;
@ -783,7 +784,7 @@ public class BooksWithoutBorders extends JavaPlugin {
}
if (args.length == 1) {
loadList.put(sender.getName(), listFiles(sender, true, 0, false));
loadList.put(sender.getName(), listFiles(sender, true, false));
return true;
}
@ -791,7 +792,7 @@ public class BooksWithoutBorders extends JavaPlugin {
if (!Character.isDigit(args[1].charAt(x)))
break;
if (x == args[1].length() - 1)
loadList.put(sender.getName(), listFiles(sender, true, 0, true));
loadList.put(sender.getName(), listFiles(sender, true, true));
}
ItemStack newBook;
@ -843,7 +844,7 @@ public class BooksWithoutBorders extends JavaPlugin {
//lists delete-able files
if (args.length == 1) {
loadList.put(sender.getName(), listFiles(sender, true, 0, false));
loadList.put(sender.getName(), listFiles(sender, true, false));
return true;
}
//actual deletion
@ -980,8 +981,9 @@ public class BooksWithoutBorders extends JavaPlugin {
/**
* Executes the copy command
*
* @param player <p>The player which sent the command</p>
* @param args <p>The arguments given</p>
* @param args <p>The arguments given</p>
* @return <p>True if the command was executed successfully</p>
*/
protected boolean commandCopy(Player player, String[] args) {
@ -1032,6 +1034,7 @@ public class BooksWithoutBorders extends JavaPlugin {
/**
* Executes the un-sign command
*
* @param player <p>The player which executed the command</p>
* @return <p>True if the command was executed successfully</p>
*/
@ -1054,8 +1057,9 @@ public class BooksWithoutBorders extends JavaPlugin {
/**
* Executes the delete command
*
* @param player <p>The player which sent the command</p>
* @param args <p>The arguments given</p>
* @param args <p>The arguments given</p>
* @return <p>True if the command was executed successfully</p>
*/
protected boolean commandDelete(Player player, String[] args) {
@ -1066,7 +1070,7 @@ public class BooksWithoutBorders extends JavaPlugin {
//List deletable files
if (args.length == 1) {
loadList.put(player.getName(), listFiles(player, false, 0, false));
loadList.put(player.getName(), listFiles(player, false, false));
return true;
}
//Delete the file
@ -1090,8 +1094,9 @@ public class BooksWithoutBorders extends JavaPlugin {
/**
* Executes the give command
*
* @param player <p>The player which sent the command</p>
* @param args <p>The arguments given</p>
* @param args <p>The arguments given</p>
* @return <p>True if the command was executed successfully</p>
*/
protected boolean commandGive(Player player, String[] args) {
@ -1106,7 +1111,7 @@ public class BooksWithoutBorders extends JavaPlugin {
}
if (args.length == 1) {
loadList.put(player.getName(), listFiles(player, false, 0, false));
loadList.put(player.getName(), listFiles(player, false, false));
return true;
}
@ -1114,7 +1119,7 @@ public class BooksWithoutBorders extends JavaPlugin {
if (!Character.isDigit(args[1].charAt(x)))
break;
if (x == args[1].length() - 1)
loadList.put(player.getName(), listFiles(player, false, 0, true));
loadList.put(player.getName(), listFiles(player, false, true));
}
ItemStack newBook;
@ -1303,6 +1308,7 @@ public class BooksWithoutBorders extends JavaPlugin {
}
protected void saveBook(Player player, String dupe, Boolean pub) {
//Notice: Could be both a signed or unsigned book
BookMeta book = (BookMeta) player.getItemInHand().getItemMeta();
String path;
if (pub) {
@ -1589,40 +1595,28 @@ public class BooksWithoutBorders extends JavaPlugin {
}
}
ItemStack nubook = new ItemStack(Material.WRITTEN_BOOK);//Book(book.getAuthor(), book.getTitle(), pages, 1, 387);
nubook.setItemMeta(book);
nubook.setAmount(player.getItemInHand().getAmount());
return nubook;
ItemStack newBook = new ItemStack(Material.WRITTEN_BOOK);//Book(book.getAuthor(), book.getTitle(), pages, 1, 387);
newBook.setItemMeta(book);
newBook.setAmount(InventoryHelper.getHeldBook(player).getAmount());
return newBook;
}
protected List<String> listFiles(CommandSender sender, Boolean listPublic, int offset, boolean silent) {
List<String> fileList = new ArrayList<>();
File file = (listPublic) ? new File(bookFolder) :
new File(bookFolder + cleanString(sender.getName()) + SLASH);
File[] fl = file.listFiles();
if (!file.exists()) {
sendErrorMessage(sender, "No books have been saved!");
return null;
/**
* Lists available files
*
* @param sender <p>The command sender looking for 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>
*/
protected List<String> listFiles(CommandSender sender, Boolean listPublic, boolean silent) {
File file;
if (listPublic) {
file = new File(bookFolder);
} else {
file = new File(bookFolder + cleanString(sender.getName()) + SLASH);
}
if (fl.length == 0) {
sendErrorMessage(sender, "No books have been saved!");
return null;
}
if (!silent) {
sendSuccessMessage(sender, "Available Books:");
}
for (File value : fl) {
if (value.isFile()) {
fileList.add(value.getName());
if (!silent)
sender.sendMessage(ChatColor.GRAY + "[" + (fileList.size() + offset) + "] " + value.getName());
}
}
return fileList;
return FileHelper.listFiles(sender, file, silent);
}
/**
@ -1639,10 +1633,11 @@ public class BooksWithoutBorders extends JavaPlugin {
/**
* Encrypts a book
* @param player <p>The player encrypting the book</p>
*
* @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 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) {
@ -1651,10 +1646,11 @@ public class BooksWithoutBorders extends JavaPlugin {
/**
* 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 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>
*/
@ -1694,10 +1690,11 @@ public class BooksWithoutBorders extends JavaPlugin {
/**
* 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 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>
*/
@ -1717,9 +1714,10 @@ public class BooksWithoutBorders extends JavaPlugin {
/**
* 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 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>
*/
@ -1741,10 +1739,11 @@ public class BooksWithoutBorders extends JavaPlugin {
/**
* 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>
*/
private void sendSuccessMessage(CommandSender sender, String message) {
public static void sendSuccessMessage(CommandSender sender, String message) {
sender.sendMessage(successColor + message);
}
@ -1816,7 +1815,7 @@ public class BooksWithoutBorders extends JavaPlugin {
ItemStack newBook = new ItemStack(Material.WRITABLE_BOOK);
newBook.setItemMeta(oldBook);
InventoryHelper.replaceHeldBook(player, newBook, mainHand);
InventoryHelper.replaceHeldItem(player, newBook, mainHand);
}
/**

View File

@ -232,7 +232,7 @@ public class BooksWithoutBordersListener implements Listener {
markGiveSignValidity(event, true);
} else {
if (isBookListIndex(lines[2])) {
List<String> availableFiles = BooksWithoutBorders.bwb.listFiles(player, true, 0, true);
List<String> availableFiles = BooksWithoutBorders.bwb.listFiles(player, true, true);
BooksWithoutBorders.loadList.put(player.getName(), availableFiles);
markGiveSignValidity(event, true);
return;
@ -243,6 +243,7 @@ public class BooksWithoutBordersListener implements Listener {
/**
* Check whether an integer points to a public book
*
* @param possibleIndex <p>The string which might be a book index</p>
* @return <p>True if the number is a book index</p>
*/
@ -255,14 +256,16 @@ public class BooksWithoutBordersListener implements Listener {
File[] foundFiles = bookDirectory.listFiles(File::isFile);
return parsedLine >= 0 && parsedLine <= Objects.requireNonNull(foundFiles).length;
} catch (NumberFormatException ignored) {}
} catch (NumberFormatException ignored) {
}
return false;
}
/**
* Marks a give sign as valid or invalid
* @param event <p>The event causing the creation of the give sign</p>
*
* @param event <p>The event causing the creation of the give sign</p>
* @param isValid <p>Whether the created sign is valid</p>
*/
private void markGiveSignValidity(SignChangeEvent event, boolean isValid) {
@ -289,7 +292,7 @@ public class BooksWithoutBordersListener implements Listener {
//Handles loadList numbers
if (isBookListIndex(bookName)) {
List<String> availableFiles = BooksWithoutBorders.bwb.listFiles(player, true, 0, true);
List<String> availableFiles = BooksWithoutBorders.bwb.listFiles(player, true, true);
BooksWithoutBorders.loadList.put(player.getName(), availableFiles);
}
@ -406,7 +409,7 @@ public class BooksWithoutBordersListener implements Listener {
if (!Character.isDigit(fileName.charAt(x)))
break;
if (x == fileName.length() - 1) {
BooksWithoutBorders.loadList.put(player.getName(), BooksWithoutBorders.bwb.listFiles(player, true, 0, true));
BooksWithoutBorders.loadList.put(player.getName(), BooksWithoutBorders.bwb.listFiles(player, true, true));
isLoadListNumber = true;
}
}

View File

@ -1,11 +1,18 @@
package net.knarcraft.bookswithoutborders.utility;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class FileHelper {
/**
* Checks if a file path points to an actual book
*
* @param bookFile <p>The path to a book</p>
* @return <p>True if the file exists and points to a book file</p>
*/
@ -15,6 +22,14 @@ public class FileHelper {
new File(bookFile + ".yml").isFile()) && !bookFile.contains("../") && !bookFile.contains("..\\");
}
/**
* Gets a book file given its path
*
* <p>This function adds extensions to the path as necessary, returning the first match.</p>
*
* @param bookPath <p>The path of the book to get</p>
* @return <p>The file the path points to, or null otherwise</p>
*/
public static File getBookFile(String bookPath) {
if (!bookFileExists(bookPath)) {
return null;
@ -38,4 +53,36 @@ public class FileHelper {
return null;
}
/**
* Lists available files
*
* @param sender <p>The command sender looking 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>
*/
public static List<String> listFiles(CommandSender sender, File searchDirectory, boolean silent) {
List<String> fileList = new ArrayList<>();
File[] existingFiles = searchDirectory.listFiles();
if (!searchDirectory.exists() || existingFiles == null || existingFiles.length == 0) {
BooksWithoutBorders.sendErrorMessage(sender, "No books have been saved!");
return null;
}
if (!silent) {
BooksWithoutBorders.sendSuccessMessage(sender, "Available Books:");
}
for (File foundFile : existingFiles) {
if (foundFile.isFile()) {
fileList.add(foundFile.getName());
if (!silent) {
sender.sendMessage(ChatColor.GRAY + "[" + fileList.size() + "] " + foundFile.getName());
}
}
}
return fileList;
}
}

View File

@ -11,16 +11,33 @@ public class InventoryHelper {
/**
* Gets the book the holder is playing
*
* @param player <p>The player holding the book</p>
* @return <p>The book the player is holding</p>
*/
public static ItemStack getHeldBook(Player player) {
BookHoldingState holdingState = getHoldingBookState(player);
return getHeldBook(player, true);
}
/**
* Gets the book the holder is playing
*
* @param player <p>The player holding the book</p>
* @param signedBook <p>Whether to check for signed or unsigned books</p>
* @return <p>The book the player is holding</p>
*/
public static ItemStack getHeldBook(Player player, boolean signedBook) {
BookHoldingState holdingState;
if (signedBook) {
holdingState = getHoldingBookState(player);
} else {
holdingState = getHoldingUnsignedBookState(player);
}
ItemStack heldBook;
if (holdingState == BookHoldingState.MAIN_HAND) {
heldBook = getHeldBook(player, true);
heldBook = getHeldItem(player, true);
} else if (holdingState == BookHoldingState.OFF_HAND) {
heldBook = getHeldBook(player, false);
heldBook = getHeldItem(player, false);
} else {
throw new IllegalArgumentException("The player is not holding exactly one book.");
}
@ -29,18 +46,20 @@ public class InventoryHelper {
/**
* Replaces the book the player is holding
* @param player <p>The player holding the book</p>
*
* @param player <p>The player holding the book</p>
* @param newBook <p>The new book the player should hold</p>
*/
public static void setHeldBook(Player player, ItemStack newBook) {
BookHoldingState holdingState = getHoldingBookState(player);
replaceHeldBook(player, newBook, holdingState == BookHoldingState.MAIN_HAND);
replaceHeldItem(player, newBook, holdingState == BookHoldingState.MAIN_HAND);
}
/**
* Performs checks to validate that a player contains exactly one book
* @param player <p>The player to validate</p>
* @param noBookMessage <p>The message to display if the player is not holding a book</p>
*
* @param player <p>The player to validate</p>
* @param noBookMessage <p>The message to display if the player is not holding a book</p>
* @param twoBooksMessage <p>The message to display if the player is holding one book in each hand</p>
* @return <p>False if the player is holding exactly one book</p>
*/
@ -60,14 +79,54 @@ public class InventoryHelper {
return false;
}
/**
* Check whether the player is holding one book
*
* @param player <p>The player possibly holding a book</p>
* @param signedBook <p>Whether to look for signed or unsigned books</p>
* @return <p>True if the player is holding one book</p>
*/
public static boolean isHoldingBook(Player player, boolean signedBook) {
BookHoldingState holdingState;
if (signedBook) {
holdingState = getHoldingBookState(player);
} else {
holdingState = getHoldingUnsignedBookState(player);
}
return holdingState == BookHoldingState.MAIN_HAND || holdingState == BookHoldingState.OFF_HAND;
}
/**
* Gets the state of which hands of the player contains a book
*
* @param player <p>The player possibly holding a book</p>
* @return <p>The state of the player's book holding</p>
*/
public static BookHoldingState getHoldingBookState(Player player) {
boolean hasBookInMainHand = player.getInventory().getItemInMainHand().getType() == Material.WRITTEN_BOOK;
boolean hasBookInOffHand = player.getInventory().getItemInOffHand().getType() == Material.WRITTEN_BOOK;
return getHoldingItemState(player, Material.WRITTEN_BOOK);
}
/**
* Gets the state of which hands of the player contains a writable book
*
* @param player <p>The player possibly holding a book</p>
* @return <p>The state of the player's book holding</p>
*/
public static BookHoldingState getHoldingUnsignedBookState(Player player) {
return getHoldingItemState(player, Material.WRITABLE_BOOK);
}
/**
* Gets the state of which hands of the player contains an item type
*
* @param player <p>The player possibly holding the item</p>
* @param type <p>The type of material to look for</p>
* @return <p>The state of the player's item holding</p>
*/
private static BookHoldingState getHoldingItemState(Player player, Material type) {
boolean hasBookInMainHand = player.getInventory().getItemInMainHand().getType() == type;
boolean hasBookInOffHand = player.getInventory().getItemInOffHand().getType() == type;
if (hasBookInMainHand && hasBookInOffHand) {
return BookHoldingState.BOTH_HANDS;
@ -88,16 +147,17 @@ public class InventoryHelper {
* @return <p>Information about the held book</p>
*/
public static BookMeta getHeldBookMetadata(Player player, boolean mainHand) {
return (BookMeta) getHeldBook(player, mainHand).getItemMeta();
return (BookMeta) getHeldItem(player, mainHand).getItemMeta();
}
/**
* Gets the item the player is holding
* @param player <p>The player to get from</p>
*
* @param player <p>The player to get from</p>
* @param mainHand <p>Whether to get the item in the player's main hand or off hand</p>
* @return <p>The item the player is holding in the given hand</p>
*/
public static ItemStack getHeldBook(Player player, boolean mainHand) {
public static ItemStack getHeldItem(Player player, boolean mainHand) {
if (mainHand) {
return player.getInventory().getItemInMainHand();
} else {
@ -112,7 +172,7 @@ public class InventoryHelper {
* @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>
*/
public static void replaceHeldBook(Player player, ItemStack newBook, boolean mainHand) {
public static void replaceHeldItem(Player player, ItemStack newBook, boolean mainHand) {
if (mainHand) {
player.getInventory().setItemInMainHand(newBook);
} else {