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; return false;
} }
InventoryHelper.notHoldingOneWritableBookCheck(player, "You must be holding a writable book to copy it!", if (InventoryHelper.notHoldingOneWritableBookCheck(player, "You must be holding a writable book to " +
"You cannot copy two books at once!"); "clear it!", "You cannot clear two books at once!")) {
return false;
}
//Clear the player's held book //Clear the player's held book
ItemStack heldBook = InventoryHelper.getHeldBook(player, true); ItemStack heldBook = InventoryHelper.getHeldBook(player, false);
BookMeta bookMeta = (BookMeta) heldBook; BookMeta bookMeta = (BookMeta) heldBook.getItemMeta();
if (bookMeta == null) {
BooksWithoutBorders.sendErrorMessage(sender, "Unable to get metadata for the held book!");
return false;
}
bookMeta.setPages(""); bookMeta.setPages("");
heldBook.setItemMeta(bookMeta); heldBook.setItemMeta(bookMeta);
BooksWithoutBorders.sendSuccessMessage(sender, "Book cleared!");
return true; return true;
} }

View File

@ -110,14 +110,15 @@ public final class EconomyHelper {
int clearedAmount = 0; int clearedAmount = 0;
for (ItemStack itemStack : books) { for (ItemStack itemStack : books) {
if (itemStack.getAmount() > itemCost) { if (itemStack.getAmount() > itemCost) {
//If encountering an item stack with more than enough books, remove the necessary books
itemStack.setAmount(itemStack.getAmount() - itemCost); itemStack.setAmount(itemStack.getAmount() - itemCost);
break; return true;
} else { } else {
clearedAmount += itemStack.getAmount(); clearedAmount += itemStack.getAmount();
player.getInventory().remove(itemStack); player.getInventory().removeItem(itemStack);
} }
if (clearedAmount >= itemCost) { if (clearedAmount >= itemCost) {
break; return true;
} }
} }
return true; return true;
@ -147,12 +148,12 @@ public final class EconomyHelper {
private static List<ItemStack> getPlayersEmptyBooks(Player player) { private static List<ItemStack> getPlayersEmptyBooks(Player player) {
List<ItemStack> validBooks = new ArrayList<>(); List<ItemStack> validBooks = new ArrayList<>();
for (ItemStack itemStack : player.getInventory().getContents()) { for (ItemStack itemStack : player.getInventory().getContents()) {
if (itemStack.getType() != Material.WRITABLE_BOOK) { if (itemStack == null || itemStack.getType() != Material.WRITABLE_BOOK) {
continue; continue;
} }
BookMeta book = (BookMeta) itemStack; BookMeta book = (BookMeta) itemStack.getItemMeta();
//Only accept empty books //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); validBooks.add(itemStack);
} }
} }
@ -170,8 +171,10 @@ public final class EconomyHelper {
private static boolean payForBookPrintingEconomy(Player player, double cost, int numCopies) { private static boolean payForBookPrintingEconomy(Player player, double cost, int numCopies) {
if ((economy.getBalance(player) - cost) >= 0) { if ((economy.getBalance(player) - cost) >= 0) {
economy.withdrawPlayer(player, cost); economy.withdrawPlayer(player, cost);
BooksWithoutBorders.sendSuccessMessage(player, economy.format(cost) + " withdrawn to create " + numCopies + " book(s)"); BooksWithoutBorders.sendSuccessMessage(player, economy.format(cost) + " withdrawn to create " +
BooksWithoutBorders.sendSuccessMessage(player, "New balance: " + economy.format(economy.getBalance(player))); numCopies + " book(s)");
BooksWithoutBorders.sendSuccessMessage(player, "New balance: " +
economy.format(economy.getBalance(player)));
return true; return true;
} else { } else {
BooksWithoutBorders.sendErrorMessage(player, economy.format(cost) + " is required for this command!"); BooksWithoutBorders.sendErrorMessage(player, economy.format(cost) + " is required for this command!");