From 94e7ea55678d11e1083de67e23ff352c35ddf65a Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 1 Sep 2025 19:40:53 +0200 Subject: [PATCH] Simplifies the awfully complex code for getting --- .../command/CommandEncrypt.java | 5 +- .../command/CommandSetAuthor.java | 8 +- .../command/CommandSetBookPrice.java | 3 +- .../command/CommandSetLore.java | 3 +- .../command/CommandSetTitle.java | 3 +- .../command/CommandUnSign.java | 15 +- .../config/StaticMessage.java | 2 + .../listener/PlayerEventListener.java | 2 +- .../listener/SignEventListener.java | 6 +- .../bookswithoutborders/state/ItemSlot.java | 21 +- .../utility/BookLoaderUtil.java | 17 +- .../utility/EncryptedBookUtil.java | 13 +- .../utility/InventoryUtil.java | 219 +++++++++--------- 13 files changed, 167 insertions(+), 150 deletions(-) diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java index 1eef03b..b8a237b 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java @@ -10,6 +10,7 @@ import net.knarcraft.bookswithoutborders.utility.EncryptedBookUtil; import net.knarcraft.bookswithoutborders.utility.InventoryUtil; import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.util.TabCompletionHelper; +import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -108,8 +109,8 @@ public class CommandEncrypt implements TabExecutor { */ protected boolean encryptBook(@NotNull EncryptionStyle encryptionStyle, @NotNull Player player, @NotNull String key, @NotNull String group, boolean preventAdminDecryption) { - ItemSlot heldSlot = InventoryUtil.getHeldSlotBook(player, false, false, true, true); - ItemStack encryptedBook = EncryptedBookUtil.encryptBook(player, heldSlot == ItemSlot.MAIN_HAND, key, + ItemSlot heldSlot = InventoryUtil.getHeldBookSlot(player, Material.WRITTEN_BOOK); + ItemStack encryptedBook = EncryptedBookUtil.encryptBook(player, heldSlot, key, encryptionStyle, group, preventAdminDecryption); if (encryptedBook != null) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetAuthor.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetAuthor.java index 6c3bb8b..ba83281 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetAuthor.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetAuthor.java @@ -6,6 +6,7 @@ import net.knarcraft.bookswithoutborders.utility.InventoryUtil; import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.property.ColorConversion; import net.knarcraft.knarlib.util.ColorHelper; +import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -42,10 +43,9 @@ public class CommandSetAuthor implements TabExecutor { return false; } - ItemSlot heldBookSlot = InventoryUtil.getHeldSlotBook(player, false, false, true, true); - boolean mainHand = heldBookSlot == ItemSlot.MAIN_HAND; - ItemStack heldBook = InventoryUtil.getHeldItem(player, mainHand); - BookMeta bookMetaData = InventoryUtil.getHeldBookMetadata(player, mainHand); + ItemSlot heldBookSlot = InventoryUtil.getHeldBookSlot(player, Material.WRITTEN_BOOK); + ItemStack heldBook = InventoryUtil.getHeldItem(player, heldBookSlot); + BookMeta bookMetaData = InventoryUtil.getHeldBookMetadata(player, heldBookSlot); if (bookMetaData == null) { new FormatBuilder(Translatable.ERROR_METADATA_MISSING).error(player); return false; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java index 733eb32..649bf46 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java @@ -7,6 +7,7 @@ import net.knarcraft.bookswithoutborders.config.StaticMessage; import net.knarcraft.bookswithoutborders.config.translation.CostMessage; import net.knarcraft.bookswithoutborders.config.translation.Translatable; import net.knarcraft.bookswithoutborders.manager.EconomyManager; +import net.knarcraft.bookswithoutborders.state.ItemSlot; import net.knarcraft.bookswithoutborders.utility.InventoryUtil; import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeUtil; import net.knarcraft.knarlib.formatting.FormatBuilder; @@ -96,7 +97,7 @@ public class CommandSetBookPrice implements TabExecutor { return false; } - ItemStack heldItem = InventoryUtil.getHeldItem(player, true); + ItemStack heldItem = InventoryUtil.getHeldItem(player, ItemSlot.MAIN_HAND); if (heldItem.getType() == Material.AIR) { new FormatBuilder(CostMessage.ERROR_COST_ITEM_MISSING).error(sender); return false; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetLore.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetLore.java index d54c36d..eed641e 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetLore.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetLore.java @@ -2,6 +2,7 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.config.translation.Translatable; +import net.knarcraft.bookswithoutborders.state.ItemSlot; import net.knarcraft.bookswithoutborders.utility.InventoryUtil; import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.property.ColorConversion; @@ -37,7 +38,7 @@ public class CommandSetLore implements TabExecutor { return false; } - ItemStack heldItem = InventoryUtil.getHeldItem(player, true); + ItemStack heldItem = InventoryUtil.getHeldItem(player, ItemSlot.MAIN_HAND); if (heldItem.getType() == Material.AIR) { new FormatBuilder(Translatable.ERROR_NO_ITEM).error(player); return false; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java index 8354ab6..82950c2 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java @@ -1,6 +1,7 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.config.translation.Translatable; +import net.knarcraft.bookswithoutborders.state.ItemSlot; import net.knarcraft.bookswithoutborders.utility.InputParsingUtil; import net.knarcraft.bookswithoutborders.utility.InventoryUtil; import net.knarcraft.knarlib.formatting.FormatBuilder; @@ -35,7 +36,7 @@ public class CommandSetTitle implements TabExecutor { return false; } - ItemStack heldItem = InventoryUtil.getHeldItem(player, true); + ItemStack heldItem = InventoryUtil.getHeldItem(player, ItemSlot.MAIN_HAND); if (heldItem.getType() == Material.AIR) { new FormatBuilder(Translatable.ERROR_NO_ITEM).error(player); return false; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java index 7e935c4..cb94598 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java @@ -7,6 +7,7 @@ import net.knarcraft.bookswithoutborders.state.ItemSlot; import net.knarcraft.bookswithoutborders.utility.BookMetaUtil; import net.knarcraft.bookswithoutborders.utility.InventoryUtil; import net.knarcraft.knarlib.formatting.FormatBuilder; +import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -39,8 +40,8 @@ public class CommandUnSign implements TabExecutor { } //Find which hand the player is using to hold the book. If holding one in each, throw an error - ItemSlot holdingSlot = InventoryUtil.getHeldSlotBook(player, false, false, true, true); - unSignHeldBook(player, holdingSlot == ItemSlot.MAIN_HAND); + ItemSlot holdingSlot = InventoryUtil.getHeldBookSlot(player, Material.WRITTEN_BOOK); + unSignHeldBook(player, holdingSlot); return true; } @@ -48,16 +49,16 @@ public class CommandUnSign implements TabExecutor { * Un-signs the player's currently held book * * @param player

The player holding the book

- * @param mainHand

Whether the player is holding the book in its main hand or its off hand

+ * @param itemSlot

The item slot of the held book

*/ - protected void unSignHeldBook(@NotNull Player player, boolean mainHand) { + protected void unSignHeldBook(@NotNull Player player, @NotNull ItemSlot itemSlot) { //Get the old book - BookMeta oldMetadata = InventoryUtil.getHeldBookMetadata(player, mainHand); + BookMeta oldMetadata = InventoryUtil.getHeldBookMetadata(player, itemSlot); if (oldMetadata == null) { new FormatBuilder(Translatable.ERROR_METADATA_MISSING).error(player); return; } - ItemStack heldBook = InventoryUtil.getHeldBook(player, mainHand); + ItemStack heldBook = InventoryUtil.getHeldBook(player, true); //Only allow the owner to un-sign the book if (BooksWithoutBorders.getConfiguration().getAuthorOnlyUnsign() && @@ -74,7 +75,7 @@ public class CommandUnSign implements TabExecutor { } reverseColorCodes(book); - InventoryUtil.replaceHeldItem(player, book, mainHand); + InventoryUtil.replaceHeldItem(player, book, itemSlot); new FormatBuilder(Translatable.SUCCESS_UNSIGNED).success(player); } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/config/StaticMessage.java b/src/main/java/net/knarcraft/bookswithoutborders/config/StaticMessage.java index d19994e..7a4bb2b 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/config/StaticMessage.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/config/StaticMessage.java @@ -47,6 +47,8 @@ public enum StaticMessage { EXCEPTION_MIGRATE_BOOK_LOAD_FAILED("Unable to load book: {path}"), EXCEPTION_META_HAS_SEPARATOR("The author; {author} or title; {title} contains the title author separator" + " ({separator}). Saving this book would lead to unexpected problems."), + EXCEPTION_GET_HELD_BOOK_INVALID_MATERIAL("Invalid required material specified for method"), + EXCEPTION_ITEM_SLOT_INVALID("Attempting to get item from an invalid item slot"), /** * The cost type used to specify economy diff --git a/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java b/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java index 813e55e..af8096d 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java @@ -61,7 +61,7 @@ public class PlayerEventListener implements Listener { if (!bookName.trim().isEmpty()) { //Give the book to the player if it exists - ItemStack newBook = BookLoaderUtil.loadBook(player, bookName, true, BookDirectory.PUBLIC); + ItemStack newBook = BookLoaderUtil.loadBook(player, bookName, true, BookDirectory.PUBLIC, 1); if (newBook != null) { player.getInventory().addItem(newBook); } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/listener/SignEventListener.java b/src/main/java/net/knarcraft/bookswithoutborders/listener/SignEventListener.java index 13d65f0..07d5e13 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/listener/SignEventListener.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/listener/SignEventListener.java @@ -7,6 +7,7 @@ import net.knarcraft.bookswithoutborders.config.translation.SignText; import net.knarcraft.bookswithoutborders.config.translation.Translatable; import net.knarcraft.bookswithoutborders.encryption.EncryptionStyle; import net.knarcraft.bookswithoutborders.state.BookDirectory; +import net.knarcraft.bookswithoutborders.state.ItemSlot; import net.knarcraft.bookswithoutborders.state.SignType; import net.knarcraft.bookswithoutborders.utility.BookFileUtil; import net.knarcraft.bookswithoutborders.utility.BookLoaderUtil; @@ -237,10 +238,9 @@ public class SignEventListener implements Listener { @NotNull EquipmentSlot hand) { ItemStack eBook; String[] lines = sign.getSide(Side.FRONT).getLines(); - boolean mainHand = hand == EquipmentSlot.HAND; if (heldItemType == Material.WRITTEN_BOOK) { player.closeInventory(); - eBook = EncryptedBookUtil.encryptBook(player, mainHand, InputCleaningUtil.stripColor(lines[2]), + eBook = EncryptedBookUtil.encryptBook(player, ItemSlot.fromEquipmentSlot(hand), InputCleaningUtil.stripColor(lines[2]), EncryptionStyle.getFromString(InputCleaningUtil.stripColor(lines[3])), false); if (eBook != null) { player.getInventory().setItem(hand, eBook); @@ -270,7 +270,7 @@ public class SignEventListener implements Listener { fileName += InputCleaningUtil.stripColor(thirdLine); } - ItemStack newBook = BookLoaderUtil.loadBook(player, fileName, true, BookDirectory.PUBLIC); + ItemStack newBook = BookLoaderUtil.loadBook(player, fileName, true, BookDirectory.PUBLIC, 1); if (newBook != null) { player.getInventory().addItem(newBook); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/state/ItemSlot.java b/src/main/java/net/knarcraft/bookswithoutborders/state/ItemSlot.java index 040603e..20bfda4 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/state/ItemSlot.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/state/ItemSlot.java @@ -1,5 +1,8 @@ package net.knarcraft.bookswithoutborders.state; +import org.bukkit.inventory.EquipmentSlot; +import org.jetbrains.annotations.NotNull; + /** * This enum represents the different relevant item slots a target item can exist in */ @@ -18,6 +21,22 @@ public enum ItemSlot { /** * The player does not have the item, or it is ambiguous */ - NONE + NONE, + ; + + /** + * Gets the corresponding item slot from the given equipment slot + * + * @param equipmentSlot

The equipment slot to convert

+ * @return

The corresponding equipment slot

+ */ + @NotNull + public static ItemSlot fromEquipmentSlot(@NotNull EquipmentSlot equipmentSlot) { + return switch (equipmentSlot) { + case HAND -> ItemSlot.MAIN_HAND; + case OFF_HAND -> ItemSlot.OFF_HAND; + default -> ItemSlot.NONE; + }; + } } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoaderUtil.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoaderUtil.java index c84340d..f8497b5 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoaderUtil.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoaderUtil.java @@ -26,21 +26,6 @@ public final class BookLoaderUtil { private BookLoaderUtil() { } - /** - * Loads the given book - * - * @param sender

The command sender trying to load the book

- * @param fileName

The index or file name of the book to load

- * @param isSigned

Whether to load the book as signed, and not unsigned

- * @param directory

The directory to save the book in

- * @return

The loaded book

- */ - @Nullable - public static ItemStack loadBook(@NotNull CommandSender sender, @NotNull String fileName, boolean isSigned, - @NotNull BookDirectory directory) { - return loadBook(sender, fileName, isSigned, directory, 1); - } - /** * Loads the given book * @@ -59,7 +44,7 @@ public final class BookLoaderUtil { int bookIndex = Integer.parseInt(fileName); List availableFiles = BooksWithoutBorders.getAvailableBooks(sender, bookDirectory == BookDirectory.PUBLIC); - if (bookIndex <= availableFiles.size()) { + if (bookIndex > 0 && bookIndex <= availableFiles.size()) { fileName = availableFiles.get(Integer.parseInt(fileName) - 1); } } catch (NumberFormatException ignored) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptedBookUtil.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptedBookUtil.java index 5914709..c2c3173 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptedBookUtil.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptedBookUtil.java @@ -14,6 +14,7 @@ import net.knarcraft.bookswithoutborders.encryption.Magic; import net.knarcraft.bookswithoutborders.encryption.OneTimePad; import net.knarcraft.bookswithoutborders.encryption.SubstitutionCipher; import net.knarcraft.bookswithoutborders.state.BookDirectory; +import net.knarcraft.bookswithoutborders.state.ItemSlot; import net.knarcraft.knarlib.formatting.FormatBuilder; import net.md_5.bungee.api.ChatColor; import org.bukkit.Material; @@ -88,23 +89,23 @@ public final class EncryptedBookUtil { * Encrypts a book * * @param player

The player encrypting the book

- * @param mainHand

Whether the player is holding the book in its main hand

+ * @param itemSlot

The item slot of the held book

* @param key

The key/password to use for encryption

* @param style

The encryption style to use

* @param preventAdminDecrypt

Whether to prevent storage of a key that can be used for admin decryption

* @return

An encrypted version of the book

*/ @Nullable - public static ItemStack encryptBook(@NotNull Player player, boolean mainHand, @NotNull String key, + public static ItemStack encryptBook(@NotNull Player player, @NotNull ItemSlot itemSlot, @NotNull String key, @NotNull EncryptionStyle style, boolean preventAdminDecrypt) { - return encryptBook(player, mainHand, key, style, "", preventAdminDecrypt); + return encryptBook(player, itemSlot, key, style, "", preventAdminDecrypt); } /** * Encrypts a book * * @param player

The player encrypting the book

- * @param mainHand

Whether the player is holding the book in its main hand

+ * @param itemSlot

The item slot of the held book

* @param key

The key/password to use for encryption

* @param style

The encryption style to use

* @param groupName

The name of the group to encrypt for, or "" otherwise

@@ -112,10 +113,10 @@ public final class EncryptedBookUtil { * @return

An encrypted version of the book

*/ @Nullable - public static ItemStack encryptBook(Player player, boolean mainHand, @NotNull String key, + public static ItemStack encryptBook(Player player, @NotNull ItemSlot itemSlot, @NotNull String key, @NotNull EncryptionStyle style, @NotNull String groupName, boolean preventAdminDecrypt) { - BookMeta book = InventoryUtil.getHeldBookMetadata(player, mainHand); + BookMeta book = InventoryUtil.getHeldBookMetadata(player, itemSlot); if (book == null) { new FormatBuilder(Translatable.ERROR_METADATA_MISSING).error(player); return null; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryUtil.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryUtil.java index 60d101e..fe57aab 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryUtil.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/InventoryUtil.java @@ -28,9 +28,9 @@ public final class InventoryUtil { */ @Nullable public static ItemStack getHeldBook(@NotNull Player player) { - ItemSlot holdingSlot = InventoryUtil.getHeldSlotBook(player, false, false, false, false); + ItemSlot holdingSlot = InventoryUtil.getHeldBookSlot(player, null); if (holdingSlot != ItemSlot.NONE) { - return InventoryUtil.getHeldItem(player, holdingSlot == ItemSlot.MAIN_HAND); + return InventoryUtil.getHeldItem(player, holdingSlot); } else { return null; } @@ -45,14 +45,11 @@ public final class InventoryUtil { */ @NotNull public static ItemStack getHeldBook(@NotNull Player player, boolean signedBook) { - ItemSlot heldSlot = getHeldSlotBook(player, false, false, true, signedBook); - if (heldSlot == ItemSlot.MAIN_HAND) { - return getHeldItem(player, true); - } else if (heldSlot == ItemSlot.OFF_HAND) { - return getHeldItem(player, false); - } else { + ItemSlot heldSlot = getHeldBookSlot(player, signedBook ? Material.WRITTEN_BOOK : Material.WRITABLE_BOOK); + if (heldSlot == ItemSlot.NONE) { throw new IllegalArgumentException(StaticMessage.EXCEPTION_NOT_HOLDING_ONE_BOOK.toString()); } + return getHeldItem(player, heldSlot); } /** @@ -62,11 +59,11 @@ public final class InventoryUtil { * @param newBook

The new book the player should hold

*/ public static void setHeldWrittenBook(@NotNull Player player, @NotNull ItemStack newBook) { - ItemSlot itemSlot = getHeldSlotBook(player, false, false, true, true); + ItemSlot itemSlot = getHeldBookSlot(player, Material.WRITTEN_BOOK); if (itemSlot == ItemSlot.MAIN_HAND) { - replaceHeldItem(player, newBook, true); + replaceHeldItem(player, newBook, itemSlot); } else if (itemSlot == ItemSlot.OFF_HAND) { - replaceHeldItem(player, newBook, false); + replaceHeldItem(player, newBook, itemSlot); } else { new FormatBuilder(Translatable.ERROR_BOOK_NOT_FOUND).error(player); } @@ -124,66 +121,117 @@ public final class InventoryUtil { return false; } + /** + * Gets metadata about the player's held book + * + * @param player

The player holding the book

+ * @param itemSlot

The item slot of the item to get metadata about

+ * @return

Information about the held book

+ */ + @Nullable + public static BookMeta getHeldBookMetadata(@NotNull Player player, @NotNull ItemSlot itemSlot) { + return (BookMeta) getHeldItem(player, itemSlot).getItemMeta(); + } + + /** + * Gets the item the player is holding + * + * @param player

The player to get from

+ * @param itemSlot

The item slot of the item to get

+ * @return

The item the player is holding in the given hand

+ */ + @NotNull + public static ItemStack getHeldItem(@NotNull Player player, @NotNull ItemSlot itemSlot) { + return switch (itemSlot) { + case MAIN_HAND -> player.getInventory().getItemInMainHand(); + case OFF_HAND -> player.getInventory().getItemInOffHand(); + case NONE -> throw new IllegalArgumentException(StaticMessage.EXCEPTION_ITEM_SLOT_INVALID.toString()); + }; + } + + /** + * Replaces the player's held item + * + * @param player

The player to replace the item for

+ * @param newBook

The new book the player should hold

+ * @param itemSlot

The item slot of the item to replace

+ */ + public static void replaceHeldItem(@NotNull Player player, @NotNull ItemStack newBook, @NotNull ItemSlot itemSlot) { + if (itemSlot == ItemSlot.MAIN_HAND) { + player.getInventory().setItemInMainHand(newBook); + } else if (itemSlot == ItemSlot.OFF_HAND) { + player.getInventory().setItemInOffHand(newBook); + } else { + throw new IllegalArgumentException(StaticMessage.EXCEPTION_ITEM_SLOT_INVALID.toString()); + } + } + /** * Gets the slot of the player's held book * - * @param player

The player holding the book

- * @param handMatters

Whether the differentiation between the main hand and the off hand is relevant

- * @param mainHand

Whether to search the player's main hand or off hand, if it's relevant

- * @param typeMatters

Whether the differentiation between signed and unsigned books is relevant

- * @param writtenBook

Whether to search for written or unwritten books, if it's relevant

+ * @param player

The player holding the book

+ * @param requiredMaterial

The required material of the book to look for. Set to null if it doesn't matter

* @return

The slot of the player's held book

*/ @NotNull - public static ItemSlot getHeldSlotBook(@NotNull Player player, boolean handMatters, boolean mainHand, - boolean typeMatters, boolean writtenBook) { + public static ItemSlot getHeldBookSlot(@NotNull Player player, @Nullable Material requiredMaterial) { BookHoldingState state = getBookHoldingState(player); - ItemStack mainHandItem = getHeldItem(player, true); - ItemStack offHandItem = getHeldItem(player, false); - Material requiredMaterial = writtenBook ? Material.WRITTEN_BOOK : Material.WRITABLE_BOOK; - //Ambiguous or empty - if (state == BookHoldingState.SIGNED_BOTH_HANDS || - state == BookHoldingState.UNSIGNED_BOTH_HANDS || - state == BookHoldingState.NONE) { - return ItemSlot.NONE; - } - if (handMatters && typeMatters) { - if (mainHand && mainHandItem.getType() == requiredMaterial) { - return ItemSlot.MAIN_HAND; - } else if (!mainHand && offHandItem.getType() == requiredMaterial) { - return ItemSlot.OFF_HAND; - } - } else if (handMatters) { - //Ambiguous - if (state == BookHoldingState.SIGNED_MAIN_HAND_UNSIGNED_OFF_HAND || - state == BookHoldingState.UNSIGNED_MAIN_HAND_SIGNED_OFF_HAND) { - return ItemSlot.NONE; - } - if (mainHand) { - return ItemSlot.MAIN_HAND; - } else { - return ItemSlot.OFF_HAND; - } - } else if (typeMatters) { - if ((writtenBook && (state == BookHoldingState.SIGNED_MAIN_HAND || - state == BookHoldingState.SIGNED_MAIN_HAND_UNSIGNED_OFF_HAND)) || (!writtenBook && - (state == BookHoldingState.UNSIGNED_MAIN_HAND || - state == BookHoldingState.UNSIGNED_MAIN_HAND_SIGNED_OFF_HAND))) { - return ItemSlot.MAIN_HAND; - } else if ((writtenBook && (state == BookHoldingState.SIGNED_OFF_HAND || - state == BookHoldingState.UNSIGNED_MAIN_HAND_SIGNED_OFF_HAND)) || (!writtenBook && ( - state == BookHoldingState.UNSIGNED_OFF_HAND) || - state == BookHoldingState.SIGNED_MAIN_HAND_UNSIGNED_OFF_HAND)) { - return ItemSlot.OFF_HAND; - } + + if (requiredMaterial != null) { + return switch (requiredMaterial) { + case WRITTEN_BOOK -> getSignedBookSlot(state); + case WRITABLE_BOOK -> getUnsignedBookSlot(state); + default -> + throw new IllegalArgumentException(StaticMessage.EXCEPTION_GET_HELD_BOOK_INVALID_MATERIAL.toString()); + }; } else { - if (state == BookHoldingState.SIGNED_MAIN_HAND || state == BookHoldingState.UNSIGNED_MAIN_HAND) { - return ItemSlot.MAIN_HAND; - } else if (state == BookHoldingState.SIGNED_OFF_HAND || state == BookHoldingState.UNSIGNED_OFF_HAND) { - return ItemSlot.OFF_HAND; - } + return getBookSlot(state); } - return ItemSlot.NONE; + } + + /** + * Gets the slot of the held book + * + * @param bookHoldingState

The book holding state to check

+ * @return

The item slot of the held book

+ */ + @NotNull + private static ItemSlot getBookSlot(@NotNull BookHoldingState bookHoldingState) { + return switch (bookHoldingState) { + case SIGNED_MAIN_HAND, UNSIGNED_MAIN_HAND -> ItemSlot.MAIN_HAND; + case SIGNED_OFF_HAND, UNSIGNED_OFF_HAND -> ItemSlot.OFF_HAND; + default -> ItemSlot.NONE; + }; + } + + /** + * Gets the slot of the held signed book + * + * @param bookHoldingState

The book holding state to check

+ * @return

The item slot of the held signed book

+ */ + @NotNull + private static ItemSlot getSignedBookSlot(@NotNull BookHoldingState bookHoldingState) { + return switch (bookHoldingState) { + case SIGNED_MAIN_HAND, SIGNED_MAIN_HAND_UNSIGNED_OFF_HAND -> ItemSlot.MAIN_HAND; + case SIGNED_OFF_HAND, UNSIGNED_MAIN_HAND_SIGNED_OFF_HAND -> ItemSlot.OFF_HAND; + default -> ItemSlot.NONE; + }; + } + + /** + * Gets the slot of the held unsigned book + * + * @param bookHoldingState

The book holding state to check

+ * @return

The item slot of the held unsigned book

+ */ + @NotNull + private static ItemSlot getUnsignedBookSlot(@NotNull BookHoldingState bookHoldingState) { + return switch (bookHoldingState) { + case UNSIGNED_MAIN_HAND, UNSIGNED_MAIN_HAND_SIGNED_OFF_HAND -> ItemSlot.MAIN_HAND; + case UNSIGNED_OFF_HAND, SIGNED_MAIN_HAND_UNSIGNED_OFF_HAND -> ItemSlot.OFF_HAND; + default -> ItemSlot.NONE; + }; } /** @@ -194,8 +242,8 @@ public final class InventoryUtil { */ @NotNull private static BookHoldingState getBookHoldingState(@NotNull Player player) { - ItemStack mainHandItem = getHeldItem(player, true); - ItemStack offHandItem = getHeldItem(player, false); + ItemStack mainHandItem = getHeldItem(player, ItemSlot.MAIN_HAND); + ItemStack offHandItem = getHeldItem(player, ItemSlot.OFF_HAND); boolean hasSignedBookInMainHand = mainHandItem.getType() == Material.WRITTEN_BOOK; boolean hasUnsignedBookInMainHand = mainHandItem.getType() == Material.WRITABLE_BOOK; @@ -223,47 +271,4 @@ public final class InventoryUtil { } } - /** - * Gets metadata about the player's held book - * - * @param player

The player holding the book

- * @param mainHand

Whether to get information about a book in the player's main hand or off hand

- * @return

Information about the held book

- */ - @Nullable - public static BookMeta getHeldBookMetadata(@NotNull Player player, boolean mainHand) { - return (BookMeta) getHeldItem(player, mainHand).getItemMeta(); - } - - /** - * Gets the item the player is holding - * - * @param player

The player to get from

- * @param mainHand

Whether to get the item in the player's main hand or off hand

- * @return

The item the player is holding in the given hand

- */ - @NotNull - public static ItemStack getHeldItem(@NotNull Player player, boolean mainHand) { - if (mainHand) { - return player.getInventory().getItemInMainHand(); - } else { - return player.getInventory().getItemInOffHand(); - } - } - - /** - * Replaces the player's held item - * - * @param player

The player to replace the item for

- * @param newBook

The new book the player should hold

- * @param mainHand

Whether to replace the item in the player's main hand or off hand

- */ - public static void replaceHeldItem(@NotNull Player player, @NotNull ItemStack newBook, boolean mainHand) { - if (mainHand) { - player.getInventory().setItemInMainHand(newBook); - } else { - player.getInventory().setItemInOffHand(newBook); - } - } - }