diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java index 34fb317..ba2ea7a 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java @@ -12,11 +12,13 @@ import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; +import org.bukkit.inventory.meta.WritableBookMeta; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.logging.Level; /** * Command executor for the unsign command @@ -49,21 +51,36 @@ public class CommandUnSign implements TabExecutor { */ public void unSignHeldBook(Player player, boolean mainHand) { //Get the old book - BookMeta oldBook = InventoryHelper.getHeldBookMetadata(player, mainHand); + BookMeta oldMetadata = InventoryHelper.getHeldBookMetadata(player, mainHand); ItemStack heldBook = InventoryHelper.getHeldBook(player, mainHand); //Only allow the owner to un-sign the book if (BooksWithoutBordersConfig.getAuthorOnlyUnsign() && !player.hasPermission("bookswithoutborders.bypassAuthorOnlyUnsign")) { - if (BookHelper.isNotAuthor(player, Objects.requireNonNull(oldBook))) { + if (BookHelper.isNotAuthor(player, Objects.requireNonNull(oldMetadata))) { return; } } - //UnSign the book - ItemStack newBook = new ItemStack(Material.WRITABLE_BOOK); - newBook.setAmount(heldBook.getAmount()); - newBook.setItemMeta(oldBook); + WritableBookMeta newMetadata = (BookMeta) BooksWithoutBorders.getItemFactory().getItemMeta(Material.WRITABLE_BOOK); + if (newMetadata == null) { + BooksWithoutBorders.getInstance().getLogger().log(Level.SEVERE, "Unable to create writable book metadata"); + return; + } + //Create a new unsigned book with the same data + ItemStack newBook = new ItemStack(Material.WRITABLE_BOOK, heldBook.getAmount()); + if (oldMetadata.hasLore()) { + List oldLore = oldMetadata.getLore(); + if (oldLore != null) { + List newLore = new ArrayList<>(oldLore); + newLore.remove(0); + newMetadata.setLore(newLore); + } + } + newMetadata.setPages(oldMetadata.getPages()); + newBook.setItemMeta(newMetadata); + + // Give the player the new book InventoryHelper.replaceHeldItem(player, newBook, mainHand); }