Adds more consideration to the existence of unsigned books
This commit is contained in:
parent
57cc06bdb8
commit
6efbd377ee
@ -418,11 +418,12 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
return false;
|
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) {
|
if (args.length == 2) {
|
||||||
saveBook(player, args[1], true);
|
saveBook(player, args[1], true);
|
||||||
} else {
|
} else {
|
||||||
saveBook((Player) sender, "false", true);
|
saveBook(player, "false", true);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
@ -462,7 +463,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
loadList.put(sender.getName(), listFiles(sender, true, 0, false));
|
loadList.put(sender.getName(), listFiles(sender, true, false));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -470,7 +471,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
if (!Character.isDigit(args[1].charAt(x)))
|
if (!Character.isDigit(args[1].charAt(x)))
|
||||||
break;
|
break;
|
||||||
if (x == args[1].length() - 1)
|
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;
|
ItemStack newBook;
|
||||||
@ -511,7 +512,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
loadList.put(sender.getName(), listFiles(sender, false, 0, false));
|
loadList.put(sender.getName(), listFiles(sender, false, false));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -519,7 +520,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
if (!Character.isDigit(args[1].charAt(x)))
|
if (!Character.isDigit(args[1].charAt(x)))
|
||||||
break;
|
break;
|
||||||
if (x == args[1].length() - 1)
|
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;
|
ItemStack newBook;
|
||||||
@ -783,7 +784,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
loadList.put(sender.getName(), listFiles(sender, true, 0, false));
|
loadList.put(sender.getName(), listFiles(sender, true, false));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -791,7 +792,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
if (!Character.isDigit(args[1].charAt(x)))
|
if (!Character.isDigit(args[1].charAt(x)))
|
||||||
break;
|
break;
|
||||||
if (x == args[1].length() - 1)
|
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;
|
ItemStack newBook;
|
||||||
@ -843,7 +844,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
|
|
||||||
//lists delete-able files
|
//lists delete-able files
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
loadList.put(sender.getName(), listFiles(sender, true, 0, false));
|
loadList.put(sender.getName(), listFiles(sender, true, false));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//actual deletion
|
//actual deletion
|
||||||
@ -980,6 +981,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the copy command
|
* Executes the copy command
|
||||||
|
*
|
||||||
* @param player <p>The player which sent the command</p>
|
* @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>
|
* @return <p>True if the command was executed successfully</p>
|
||||||
@ -1032,6 +1034,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the un-sign command
|
* Executes the un-sign command
|
||||||
|
*
|
||||||
* @param player <p>The player which executed the command</p>
|
* @param player <p>The player which executed the command</p>
|
||||||
* @return <p>True if the command was executed successfully</p>
|
* @return <p>True if the command was executed successfully</p>
|
||||||
*/
|
*/
|
||||||
@ -1054,6 +1057,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the delete command
|
* Executes the delete command
|
||||||
|
*
|
||||||
* @param player <p>The player which sent the command</p>
|
* @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>
|
* @return <p>True if the command was executed successfully</p>
|
||||||
@ -1066,7 +1070,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
|
|
||||||
//List deletable files
|
//List deletable files
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
loadList.put(player.getName(), listFiles(player, false, 0, false));
|
loadList.put(player.getName(), listFiles(player, false, false));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//Delete the file
|
//Delete the file
|
||||||
@ -1090,6 +1094,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the give command
|
* Executes the give command
|
||||||
|
*
|
||||||
* @param player <p>The player which sent the command</p>
|
* @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>
|
* @return <p>True if the command was executed successfully</p>
|
||||||
@ -1106,7 +1111,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
loadList.put(player.getName(), listFiles(player, false, 0, false));
|
loadList.put(player.getName(), listFiles(player, false, false));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1114,7 +1119,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
if (!Character.isDigit(args[1].charAt(x)))
|
if (!Character.isDigit(args[1].charAt(x)))
|
||||||
break;
|
break;
|
||||||
if (x == args[1].length() - 1)
|
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;
|
ItemStack newBook;
|
||||||
@ -1303,6 +1308,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void saveBook(Player player, String dupe, Boolean pub) {
|
protected void saveBook(Player player, String dupe, Boolean pub) {
|
||||||
|
//Notice: Could be both a signed or unsigned book
|
||||||
BookMeta book = (BookMeta) player.getItemInHand().getItemMeta();
|
BookMeta book = (BookMeta) player.getItemInHand().getItemMeta();
|
||||||
String path;
|
String path;
|
||||||
if (pub) {
|
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);
|
ItemStack newBook = new ItemStack(Material.WRITTEN_BOOK);//Book(book.getAuthor(), book.getTitle(), pages, 1, 387);
|
||||||
nubook.setItemMeta(book);
|
newBook.setItemMeta(book);
|
||||||
nubook.setAmount(player.getItemInHand().getAmount());
|
newBook.setAmount(InventoryHelper.getHeldBook(player).getAmount());
|
||||||
return nubook;
|
return newBook;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<String> listFiles(CommandSender sender, Boolean listPublic, int offset, boolean silent) {
|
/**
|
||||||
List<String> fileList = new ArrayList<>();
|
* Lists available files
|
||||||
File file = (listPublic) ? new File(bookFolder) :
|
*
|
||||||
new File(bookFolder + cleanString(sender.getName()) + SLASH);
|
* @param sender <p>The command sender looking for files</p>
|
||||||
File[] fl = file.listFiles();
|
* @param listPublic <p>Whether to list public or personal files</p>
|
||||||
|
* @param silent <p>Whether to just return the list without printing it</p>
|
||||||
if (!file.exists()) {
|
* @return <p>A list of available files</p>
|
||||||
sendErrorMessage(sender, "No books have been saved!");
|
*/
|
||||||
return null;
|
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);
|
||||||
}
|
}
|
||||||
|
return FileHelper.listFiles(sender, file, silent);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1639,6 +1633,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypts a book
|
* 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 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 key <p>The key/password to use for encryption</p>
|
||||||
@ -1651,6 +1646,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypts a book
|
* 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 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 key <p>The key/password to use for encryption</p>
|
||||||
@ -1694,6 +1690,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new encrypted book
|
* Creates a new encrypted book
|
||||||
|
*
|
||||||
* @param book <p>The book to encrypt</p>
|
* @param book <p>The book to encrypt</p>
|
||||||
* @param newPages <p>The new encrypted pages</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 mainHand <p>Whether the book is held in the player's main hand or off hand</p>
|
||||||
@ -1717,6 +1714,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves a book's plain text to a file
|
* Saves a book's plain text to a file
|
||||||
|
*
|
||||||
* @param groupName <p>The group who's allowed to decrypt the book, or ""</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 player <p>The player trying to encrypt the book</p>
|
||||||
* @param book <p>The book to encrypt</p>
|
* @param book <p>The book to encrypt</p>
|
||||||
@ -1741,10 +1739,11 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a success message to a command sender (player or a console)
|
* 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>
|
* @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);
|
sender.sendMessage(successColor + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1816,7 +1815,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
ItemStack newBook = new ItemStack(Material.WRITABLE_BOOK);
|
ItemStack newBook = new ItemStack(Material.WRITABLE_BOOK);
|
||||||
newBook.setItemMeta(oldBook);
|
newBook.setItemMeta(oldBook);
|
||||||
|
|
||||||
InventoryHelper.replaceHeldBook(player, newBook, mainHand);
|
InventoryHelper.replaceHeldItem(player, newBook, mainHand);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -232,7 +232,7 @@ public class BooksWithoutBordersListener implements Listener {
|
|||||||
markGiveSignValidity(event, true);
|
markGiveSignValidity(event, true);
|
||||||
} else {
|
} else {
|
||||||
if (isBookListIndex(lines[2])) {
|
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);
|
BooksWithoutBorders.loadList.put(player.getName(), availableFiles);
|
||||||
markGiveSignValidity(event, true);
|
markGiveSignValidity(event, true);
|
||||||
return;
|
return;
|
||||||
@ -243,6 +243,7 @@ public class BooksWithoutBordersListener implements Listener {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether an integer points to a public book
|
* Check whether an integer points to a public book
|
||||||
|
*
|
||||||
* @param possibleIndex <p>The string which might be a book index</p>
|
* @param possibleIndex <p>The string which might be a book index</p>
|
||||||
* @return <p>True if the number is a book index</p>
|
* @return <p>True if the number is a book index</p>
|
||||||
*/
|
*/
|
||||||
@ -255,13 +256,15 @@ public class BooksWithoutBordersListener implements Listener {
|
|||||||
File[] foundFiles = bookDirectory.listFiles(File::isFile);
|
File[] foundFiles = bookDirectory.listFiles(File::isFile);
|
||||||
|
|
||||||
return parsedLine >= 0 && parsedLine <= Objects.requireNonNull(foundFiles).length;
|
return parsedLine >= 0 && parsedLine <= Objects.requireNonNull(foundFiles).length;
|
||||||
} catch (NumberFormatException ignored) {}
|
} catch (NumberFormatException ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Marks a give sign as valid or invalid
|
* 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>
|
* @param isValid <p>Whether the created sign is valid</p>
|
||||||
*/
|
*/
|
||||||
@ -289,7 +292,7 @@ public class BooksWithoutBordersListener implements Listener {
|
|||||||
|
|
||||||
//Handles loadList numbers
|
//Handles loadList numbers
|
||||||
if (isBookListIndex(bookName)) {
|
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);
|
BooksWithoutBorders.loadList.put(player.getName(), availableFiles);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -406,7 +409,7 @@ public class BooksWithoutBordersListener implements Listener {
|
|||||||
if (!Character.isDigit(fileName.charAt(x)))
|
if (!Character.isDigit(fileName.charAt(x)))
|
||||||
break;
|
break;
|
||||||
if (x == fileName.length() - 1) {
|
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;
|
isLoadListNumber = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,18 @@
|
|||||||
package net.knarcraft.bookswithoutborders.utility;
|
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.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class FileHelper {
|
public class FileHelper {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if a file path points to an actual book
|
* Checks if a file path points to an actual book
|
||||||
|
*
|
||||||
* @param bookFile <p>The path to a book</p>
|
* @param bookFile <p>The path to a book</p>
|
||||||
* @return <p>True if the file exists and points to a book file</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("..\\");
|
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) {
|
public static File getBookFile(String bookPath) {
|
||||||
if (!bookFileExists(bookPath)) {
|
if (!bookFileExists(bookPath)) {
|
||||||
return null;
|
return null;
|
||||||
@ -38,4 +53,36 @@ public class FileHelper {
|
|||||||
return null;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,16 +11,33 @@ public class InventoryHelper {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the book the holder is playing
|
* Gets the book the holder is playing
|
||||||
|
*
|
||||||
* @param player <p>The player holding the book</p>
|
* @param player <p>The player holding the book</p>
|
||||||
* @return <p>The book the player is holding</p>
|
* @return <p>The book the player is holding</p>
|
||||||
*/
|
*/
|
||||||
public static ItemStack getHeldBook(Player player) {
|
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;
|
ItemStack heldBook;
|
||||||
if (holdingState == BookHoldingState.MAIN_HAND) {
|
if (holdingState == BookHoldingState.MAIN_HAND) {
|
||||||
heldBook = getHeldBook(player, true);
|
heldBook = getHeldItem(player, true);
|
||||||
} else if (holdingState == BookHoldingState.OFF_HAND) {
|
} else if (holdingState == BookHoldingState.OFF_HAND) {
|
||||||
heldBook = getHeldBook(player, false);
|
heldBook = getHeldItem(player, false);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("The player is not holding exactly one book.");
|
throw new IllegalArgumentException("The player is not holding exactly one book.");
|
||||||
}
|
}
|
||||||
@ -29,16 +46,18 @@ public class InventoryHelper {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces the book the player is holding
|
* 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>
|
* @param newBook <p>The new book the player should hold</p>
|
||||||
*/
|
*/
|
||||||
public static void setHeldBook(Player player, ItemStack newBook) {
|
public static void setHeldBook(Player player, ItemStack newBook) {
|
||||||
BookHoldingState holdingState = getHoldingBookState(player);
|
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
|
* Performs checks to validate that a player contains exactly one book
|
||||||
|
*
|
||||||
* @param player <p>The player to validate</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 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>
|
* @param twoBooksMessage <p>The message to display if the player is holding one book in each hand</p>
|
||||||
@ -60,14 +79,54 @@ public class InventoryHelper {
|
|||||||
return false;
|
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
|
* Gets the state of which hands of the player contains a book
|
||||||
|
*
|
||||||
* @param player <p>The player possibly holding a book</p>
|
* @param player <p>The player possibly holding a book</p>
|
||||||
* @return <p>The state of the player's book holding</p>
|
* @return <p>The state of the player's book holding</p>
|
||||||
*/
|
*/
|
||||||
public static BookHoldingState getHoldingBookState(Player player) {
|
public static BookHoldingState getHoldingBookState(Player player) {
|
||||||
boolean hasBookInMainHand = player.getInventory().getItemInMainHand().getType() == Material.WRITTEN_BOOK;
|
return getHoldingItemState(player, Material.WRITTEN_BOOK);
|
||||||
boolean hasBookInOffHand = player.getInventory().getItemInOffHand().getType() == 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) {
|
if (hasBookInMainHand && hasBookInOffHand) {
|
||||||
return BookHoldingState.BOTH_HANDS;
|
return BookHoldingState.BOTH_HANDS;
|
||||||
@ -88,16 +147,17 @@ public class InventoryHelper {
|
|||||||
* @return <p>Information about the held book</p>
|
* @return <p>Information about the held book</p>
|
||||||
*/
|
*/
|
||||||
public static BookMeta getHeldBookMetadata(Player player, boolean mainHand) {
|
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
|
* 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>
|
* @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>
|
* @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) {
|
if (mainHand) {
|
||||||
return player.getInventory().getItemInMainHand();
|
return player.getInventory().getItemInMainHand();
|
||||||
} else {
|
} else {
|
||||||
@ -112,7 +172,7 @@ public class InventoryHelper {
|
|||||||
* @param newBook <p>The new book the player should hold</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>
|
* @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) {
|
if (mainHand) {
|
||||||
player.getInventory().setItemInMainHand(newBook);
|
player.getInventory().setItemInMainHand(newBook);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user