Fixes various bugs in the new features
Fixes the clearBook command not being aborted when no book is held. Adds missing clearBook success message Fixes a bug where too many empty books would be taken. Adds some missing null checks when looking for empty books.
This commit is contained in:
parent
f005b8f8e5
commit
90bab4a148
@ -27,14 +27,21 @@ public class CommandClear implements TabExecutor {
|
||||
return false;
|
||||
}
|
||||
|
||||
InventoryHelper.notHoldingOneWritableBookCheck(player, "You must be holding a writable book to copy it!",
|
||||
"You cannot copy two books at once!");
|
||||
if (InventoryHelper.notHoldingOneWritableBookCheck(player, "You must be holding a writable book to " +
|
||||
"clear it!", "You cannot clear two books at once!")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//Clear the player's held book
|
||||
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
|
||||
BookMeta bookMeta = (BookMeta) heldBook;
|
||||
ItemStack heldBook = InventoryHelper.getHeldBook(player, false);
|
||||
BookMeta bookMeta = (BookMeta) heldBook.getItemMeta();
|
||||
if (bookMeta == null) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "Unable to get metadata for the held book!");
|
||||
return false;
|
||||
}
|
||||
bookMeta.setPages("");
|
||||
heldBook.setItemMeta(bookMeta);
|
||||
BooksWithoutBorders.sendSuccessMessage(sender, "Book cleared!");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -110,14 +110,15 @@ public final class EconomyHelper {
|
||||
int clearedAmount = 0;
|
||||
for (ItemStack itemStack : books) {
|
||||
if (itemStack.getAmount() > itemCost) {
|
||||
//If encountering an item stack with more than enough books, remove the necessary books
|
||||
itemStack.setAmount(itemStack.getAmount() - itemCost);
|
||||
break;
|
||||
return true;
|
||||
} else {
|
||||
clearedAmount += itemStack.getAmount();
|
||||
player.getInventory().remove(itemStack);
|
||||
player.getInventory().removeItem(itemStack);
|
||||
}
|
||||
if (clearedAmount >= itemCost) {
|
||||
break;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@ -147,12 +148,12 @@ public final class EconomyHelper {
|
||||
private static List<ItemStack> getPlayersEmptyBooks(Player player) {
|
||||
List<ItemStack> validBooks = new ArrayList<>();
|
||||
for (ItemStack itemStack : player.getInventory().getContents()) {
|
||||
if (itemStack.getType() != Material.WRITABLE_BOOK) {
|
||||
if (itemStack == null || itemStack.getType() != Material.WRITABLE_BOOK) {
|
||||
continue;
|
||||
}
|
||||
BookMeta book = (BookMeta) itemStack;
|
||||
BookMeta book = (BookMeta) itemStack.getItemMeta();
|
||||
//Only accept empty books
|
||||
if (!book.hasPages() || (book.getPageCount() == 1 && book.getPage(1).trim().isEmpty())) {
|
||||
if (book != null && (!book.hasPages() || (book.getPageCount() == 1 && book.getPage(1).trim().isEmpty()))) {
|
||||
validBooks.add(itemStack);
|
||||
}
|
||||
}
|
||||
@ -170,8 +171,10 @@ public final class EconomyHelper {
|
||||
private static boolean payForBookPrintingEconomy(Player player, double cost, int numCopies) {
|
||||
if ((economy.getBalance(player) - cost) >= 0) {
|
||||
economy.withdrawPlayer(player, cost);
|
||||
BooksWithoutBorders.sendSuccessMessage(player, economy.format(cost) + " withdrawn to create " + numCopies + " book(s)");
|
||||
BooksWithoutBorders.sendSuccessMessage(player, "New balance: " + economy.format(economy.getBalance(player)));
|
||||
BooksWithoutBorders.sendSuccessMessage(player, economy.format(cost) + " withdrawn to create " +
|
||||
numCopies + " book(s)");
|
||||
BooksWithoutBorders.sendSuccessMessage(player, "New balance: " +
|
||||
economy.format(economy.getBalance(player)));
|
||||
return true;
|
||||
} else {
|
||||
BooksWithoutBorders.sendErrorMessage(player, economy.format(cost) + " is required for this command!");
|
||||
|
Loading…
Reference in New Issue
Block a user