From 90bab4a148bb02cfd17ac0543362b91c57346647 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 4 Nov 2022 16:06:27 +0100 Subject: [PATCH] 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. --- .../command/CommandClear.java | 15 +++++++++++---- .../utility/EconomyHelper.java | 19 +++++++++++-------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandClear.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandClear.java index 52d6902..2419353 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandClear.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandClear.java @@ -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; } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/EconomyHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/EconomyHelper.java index e31ab3d..8af3c9c 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/EconomyHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/EconomyHelper.java @@ -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 getPlayersEmptyBooks(Player player) { List 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!");