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:
Kristian Knarvik 2022-11-04 16:06:27 +01:00
parent f005b8f8e5
commit 90bab4a148
2 changed files with 22 additions and 12 deletions

View File

@ -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;
}

View File

@ -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!");