Adds an enum to easier keep track of which hands of a player contain a book
This commit is contained in:
parent
9209e3f52d
commit
d50af8a12b
@ -0,0 +1,25 @@
|
|||||||
|
package net.knarcraft.bookswithoutborders;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This enum represents the possible states for players holding books
|
||||||
|
*/
|
||||||
|
public enum BookHoldingState {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The player is holding one book in each hand
|
||||||
|
*/
|
||||||
|
BOTH_HANDS,
|
||||||
|
/**
|
||||||
|
* The player is holding one book in their main hand
|
||||||
|
*/
|
||||||
|
MAIN_HAND,
|
||||||
|
/**
|
||||||
|
* The player is holding one book in their off hand
|
||||||
|
*/
|
||||||
|
OFF_HAND,
|
||||||
|
/**
|
||||||
|
* The player is not holding any books
|
||||||
|
*/
|
||||||
|
NONE
|
||||||
|
|
||||||
|
}
|
@ -548,82 +548,16 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args[0].equalsIgnoreCase("give")) {
|
String pluginCommand = args[0].toLowerCase();
|
||||||
return commandGive(sender, args);
|
switch (pluginCommand) {
|
||||||
}
|
case "give":
|
||||||
|
return commandGive(player, args);
|
||||||
if (args[0].equalsIgnoreCase("delete")) {
|
case "delete":
|
||||||
return commandDelete(sender, args);
|
return commandDelete(player, args);
|
||||||
}
|
case "unsign":
|
||||||
|
return commandUnSign(player);
|
||||||
if (args[0].equalsIgnoreCase("unsign")) {
|
case "copy":
|
||||||
if (!sender.hasPermission("bookswithoutborders.unsign")) {
|
return commandCopy(player, args);
|
||||||
sendErrorMessage(sender, " You don't have permission to use this command!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(((Player) sender).getItemInHand().getType() == Material.WRITTEN_BOOK)) {
|
|
||||||
sendErrorMessage(sender, "You must be holding a written book to unsign it!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean hasBookInMainHand = player.getInventory().getItemInMainHand().getType() == Material.WRITTEN_BOOK;
|
|
||||||
boolean hasBookInOffHand = player.getInventory().getItemInOffHand().getType() == Material.WRITTEN_BOOK;
|
|
||||||
|
|
||||||
//Find which hand the player is using to hold the book. If holding one in each, throw an error
|
|
||||||
if (hasBookInMainHand && hasBookInOffHand) {
|
|
||||||
sendErrorMessage(sender, "You cannot un-sign two books at once. Please un-equip one " +
|
|
||||||
"of the books you're holding!");
|
|
||||||
} else if (hasBookInMainHand) {
|
|
||||||
unSignHeldBook(player, true);
|
|
||||||
} else if (hasBookInOffHand) {
|
|
||||||
unSignHeldBook(player, false);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (args[0].equalsIgnoreCase("copy")) {
|
|
||||||
if (!sender.hasPermission("bookswithoutborders.copy")) {
|
|
||||||
sendErrorMessage(sender, " You don't have permission to use this command!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(((Player) sender).getItemInHand().getType() == Material.WRITTEN_BOOK)) {
|
|
||||||
sendErrorMessage(sender, "You must be holding a written book to copy it!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (args.length < 2) {
|
|
||||||
sendErrorMessage(sender, "You must specifiy the number of copies to be made!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (Integer.parseInt(args[1]) > 0) {
|
|
||||||
if (authorOnlyCopy && !sender.hasPermission("bookswithoutborders.bypassauthoronlycopy")) {
|
|
||||||
if (!isAuthor((Player) sender, (BookMeta) ((Player) sender).getItemInHand().getItemMeta()))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (booksHavePrice() &&
|
|
||||||
!sender.hasPermission("bookswithoutborders.bypassbookprice") &&
|
|
||||||
cannotPayForBookPrinting((Player) sender, Integer.parseInt(args[1]))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
((Player) sender).getItemInHand().setAmount(((Player) sender).getItemInHand().getAmount() + Integer.parseInt(args[1]));
|
|
||||||
sendSuccessMessage(sender, "Book copied!");
|
|
||||||
} else {
|
|
||||||
sendErrorMessage(sender, "Book not copied!");
|
|
||||||
sendErrorMessage(sender, "Number specified was invalid!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
sendErrorMessage(sender, "Book not copied!");
|
|
||||||
sendErrorMessage(sender, "Number specified was invalid!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args[0].equalsIgnoreCase("encrypt")) {
|
if (args[0].equalsIgnoreCase("encrypt")) {
|
||||||
@ -1040,60 +974,172 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the delete command
|
* Executes the copy command
|
||||||
* @param sender <p>The command sender 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>
|
||||||
*/
|
*/
|
||||||
protected boolean commandDelete(CommandSender sender, String[] args) {
|
protected boolean commandCopy(Player player, String[] args) {
|
||||||
if (!sender.hasPermission("bookswithoutborders.delete")) {
|
if (!player.hasPermission("bookswithoutborders.copy")) {
|
||||||
sendErrorMessage(sender, " You don't have permission to use this command!");
|
sendErrorMessage(player, " You don't have permission to use this command!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
BookHoldingState holdingState = holdingBook(player);
|
||||||
|
|
||||||
|
if (holdingState == BookHoldingState.NONE) {
|
||||||
|
sendErrorMessage(player, "You must be holding a written book to copy it!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (holdingState == BookHoldingState.BOTH_HANDS) {
|
||||||
|
sendErrorMessage(player, "You cannot copy two books at once!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.length < 2) {
|
||||||
|
sendErrorMessage(player, "You must specifiy the number of copies to be made!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ItemStack heldBook;
|
||||||
|
if (holdingState == BookHoldingState.MAIN_HAND) {
|
||||||
|
heldBook = player.getInventory().getItemInMainHand();
|
||||||
|
} else {
|
||||||
|
heldBook = player.getInventory().getItemInOffHand();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (Integer.parseInt(args[1]) > 0) {
|
||||||
|
if (authorOnlyCopy && !player.hasPermission("bookswithoutborders.bypassauthoronlycopy")) {
|
||||||
|
if (!isAuthor(player, (BookMeta) Objects.requireNonNull(heldBook.getItemMeta())))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (booksHavePrice() &&
|
||||||
|
!player.hasPermission("bookswithoutborders.bypassbookprice") &&
|
||||||
|
cannotPayForBookPrinting(player, Integer.parseInt(args[1]))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
heldBook.setAmount(heldBook.getAmount() + Integer.parseInt(args[1]));
|
||||||
|
sendSuccessMessage(player, "Book copied!");
|
||||||
|
} else {
|
||||||
|
sendErrorMessage(player, "Book not copied!");
|
||||||
|
sendErrorMessage(player, "Number specified was invalid!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
sendErrorMessage(player, "Book not copied!");
|
||||||
|
sendErrorMessage(player, "Number specified was invalid!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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>
|
||||||
|
*/
|
||||||
|
protected boolean commandUnSign(Player player) {
|
||||||
|
if (!player.hasPermission("bookswithoutborders.unsign")) {
|
||||||
|
sendErrorMessage(player, " You don't have permission to use this command!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
BookHoldingState holdingState = holdingBook(player);
|
||||||
|
|
||||||
|
if (holdingState == BookHoldingState.NONE) {
|
||||||
|
sendErrorMessage(player, "You must be holding a written book to un-sign it!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Find which hand the player is using to hold the book. If holding one in each, throw an error
|
||||||
|
if (holdingState == BookHoldingState.BOTH_HANDS) {
|
||||||
|
sendErrorMessage(player, "You cannot un-sign two books at once. Please un-equip one " +
|
||||||
|
"of the books you're holding!");
|
||||||
|
} else {
|
||||||
|
unSignHeldBook(player, holdingState == BookHoldingState.MAIN_HAND);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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>
|
||||||
|
*/
|
||||||
|
protected BookHoldingState holdingBook(Player player) {
|
||||||
|
boolean hasBookInMainHand = player.getInventory().getItemInMainHand().getType() == Material.WRITTEN_BOOK;
|
||||||
|
boolean hasBookInOffHand = player.getInventory().getItemInOffHand().getType() == Material.WRITTEN_BOOK;
|
||||||
|
|
||||||
|
if (hasBookInMainHand && hasBookInOffHand) {
|
||||||
|
return BookHoldingState.BOTH_HANDS;
|
||||||
|
} else if (hasBookInMainHand) {
|
||||||
|
return BookHoldingState.MAIN_HAND;
|
||||||
|
} else if (hasBookInOffHand) {
|
||||||
|
return BookHoldingState.OFF_HAND;
|
||||||
|
} else {
|
||||||
|
return BookHoldingState.NONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the delete command
|
||||||
|
* @param player <p>The player which sent the command</p>
|
||||||
|
* @param args <p>The arguments given</p>
|
||||||
|
* @return <p>True if the command was executed successfully</p>
|
||||||
|
*/
|
||||||
|
protected boolean commandDelete(Player player, String[] args) {
|
||||||
|
if (!player.hasPermission("bookswithoutborders.delete")) {
|
||||||
|
sendErrorMessage(player, " You don't have permission to use this command!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//List deletable files
|
//List deletable files
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
loadList.put(sender.getName(), listFiles(sender, false, 0, false));
|
loadList.put(player.getName(), listFiles(player, false, 0, false));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//Delete the file
|
//Delete the file
|
||||||
if (args.length == 2) {
|
if (args.length == 2) {
|
||||||
if (!loadList.containsKey(sender.getName())) {
|
if (!loadList.containsKey(player.getName())) {
|
||||||
sendErrorMessage(sender, "You must first use /bwb delete to create a list of delete-able files!");
|
sendErrorMessage(player, "You must first use /bwb delete to create a list of delete-able files!");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
if (!loadList.get(sender.getName()).isEmpty()) {
|
if (!loadList.get(player.getName()).isEmpty()) {
|
||||||
deleteBook(sender, args[1], false);
|
deleteBook(player, args[1], false);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
sendErrorMessage(sender, "No files available to delete!");
|
sendErrorMessage(player, "No files available to delete!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sendErrorMessage(sender, "Incorrect number of arguments for this command!");
|
sendErrorMessage(player, "Incorrect number of arguments for this command!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the give command
|
* Executes the give command
|
||||||
* @param sender <p>The command sender 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>
|
||||||
*/
|
*/
|
||||||
protected boolean commandGive(CommandSender sender, String[] args) {
|
protected boolean commandGive(Player player, String[] args) {
|
||||||
if (!sender.hasPermission("bookswithoutborders.give")) {
|
if (!player.hasPermission("bookswithoutborders.give")) {
|
||||||
sendErrorMessage(sender, " You don't have permission to use this command!");
|
sendErrorMessage(player, " You don't have permission to use this command!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 2 || args.length > 5) {
|
if (args.length == 2 || args.length > 5) {
|
||||||
sendErrorMessage(sender, "Incorrect number of arguments for this command!");
|
sendErrorMessage(player, "Incorrect number of arguments for this command!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
loadList.put(sender.getName(), listFiles(sender, false, 0, false));
|
loadList.put(player.getName(), listFiles(player, false, 0, false));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1101,18 +1147,18 @@ 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(player.getName(), listFiles(player, false, 0, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemStack newBook;
|
ItemStack newBook;
|
||||||
Player receivingPlayer = this.getServer().getPlayer(args[2]);
|
Player receivingPlayer = this.getServer().getPlayer(args[2]);
|
||||||
if (receivingPlayer == null) {
|
if (receivingPlayer == null) {
|
||||||
sendErrorMessage(sender, "Player not found!");
|
sendErrorMessage(player, "Player not found!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (receivingPlayer.getInventory().firstEmpty() == -1) {
|
if (receivingPlayer.getInventory().firstEmpty() == -1) {
|
||||||
sendErrorMessage(sender, "Receiving player must have space in their inventory to receive books!");
|
sendErrorMessage(player, "Receiving player must have space in their inventory to receive books!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1120,26 +1166,26 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
if (args.length == 5)
|
if (args.length == 5)
|
||||||
newBook = loadBook(sender, cleanString(args[1]), args[4], "player", Integer.parseInt(args[3]));
|
newBook = loadBook(player, cleanString(args[1]), args[4], "player", Integer.parseInt(args[3]));
|
||||||
else if (args.length == 4) {
|
else if (args.length == 4) {
|
||||||
if (args[3].equalsIgnoreCase("true") || args[3].equalsIgnoreCase("false"))
|
if (args[3].equalsIgnoreCase("true") || args[3].equalsIgnoreCase("false"))
|
||||||
newBook = loadBook(sender, cleanString(args[1]), args[3], "player");
|
newBook = loadBook(player, cleanString(args[1]), args[3], "player");
|
||||||
else
|
else
|
||||||
newBook = loadBook(sender, cleanString(args[1]), "true", "player", Integer.parseInt(args[3]));
|
newBook = loadBook(player, cleanString(args[1]), "true", "player", Integer.parseInt(args[3]));
|
||||||
} else
|
} else
|
||||||
newBook = loadBook(sender, cleanString(args[1]), "true", "player");
|
newBook = loadBook(player, cleanString(args[1]), "true", "player");
|
||||||
|
|
||||||
if (newBook != null) {
|
if (newBook != null) {
|
||||||
receivingPlayer.getInventory().addItem(newBook);
|
receivingPlayer.getInventory().addItem(newBook);
|
||||||
sendSuccessMessage(sender, "Book sent!");
|
sendSuccessMessage(player, "Book sent!");
|
||||||
sendSuccessMessage(receivingPlayer, "Book received!");
|
sendSuccessMessage(receivingPlayer, "Book received!");
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
sendErrorMessage(sender, "Book failed to load!");
|
sendErrorMessage(player, "Book failed to load!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
sendErrorMessage(sender, "Invalid number of book copies specified!");
|
sendErrorMessage(player, "Invalid number of book copies specified!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1259,7 +1305,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//if a book is being loaded its content need not be adjusted
|
//If a book is being loaded its content need not be adjusted
|
||||||
if (rawPages.get(0).equalsIgnoreCase("[Book]")) {
|
if (rawPages.get(0).equalsIgnoreCase("[Book]")) {
|
||||||
rawPages.remove(0);
|
rawPages.remove(0);
|
||||||
nullcheck = bufferedReader.readLine();
|
nullcheck = bufferedReader.readLine();
|
||||||
@ -1268,7 +1314,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
nullcheck = bufferedReader.readLine();
|
nullcheck = bufferedReader.readLine();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//adjusts content to page length
|
//Adjusts content to page length
|
||||||
while (rawPages.get(rawPages.size() - 1) != null) {
|
while (rawPages.get(rawPages.size() - 1) != null) {
|
||||||
BookFormatter.formatLastPage(rawPages);
|
BookFormatter.formatLastPage(rawPages);
|
||||||
rawPages.add(bufferedReader.readLine());
|
rawPages.add(bufferedReader.readLine());
|
||||||
@ -1582,9 +1628,9 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
return nubook;
|
return nubook;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<String> listFiles(CommandSender sender, Boolean pub, int offset, boolean silent) {
|
protected List<String> listFiles(CommandSender sender, Boolean listPublic, int offset, boolean silent) {
|
||||||
List<String> fileList = new ArrayList<>();
|
List<String> fileList = new ArrayList<>();
|
||||||
File file = (pub) ? new File(bookFolder) :
|
File file = (listPublic) ? new File(bookFolder) :
|
||||||
new File(bookFolder + cleanString(sender.getName()) + SLASH);
|
new File(bookFolder + cleanString(sender.getName()) + SLASH);
|
||||||
File[] fl = file.listFiles();
|
File[] fl = file.listFiles();
|
||||||
|
|
||||||
@ -1605,7 +1651,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
if (value.isFile()) {
|
if (value.isFile()) {
|
||||||
fileList.add(value.getName());
|
fileList.add(value.getName());
|
||||||
if (!silent)
|
if (!silent)
|
||||||
sender.sendMessage(ChatColor.GRAY + "[" + (fileList.size() + offset) + "] " + value.getName()/*.substring(0, fl[x].getName().length()-4)*/);
|
sender.sendMessage(ChatColor.GRAY + "[" + (fileList.size() + offset) + "] " + value.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1937,4 +1983,5 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
sendErrorMessage(player, "You must be the author of this book to use this command!");
|
sendErrorMessage(player, "You must be the author of this book to use this command!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user