Simplifies the awfully complex code for getting
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good

This commit is contained in:
2025-09-01 19:40:53 +02:00
parent 3e45cad29a
commit 94e7ea5567
13 changed files with 167 additions and 150 deletions

View File

@@ -10,6 +10,7 @@ import net.knarcraft.bookswithoutborders.utility.EncryptedBookUtil;
import net.knarcraft.bookswithoutborders.utility.InventoryUtil; import net.knarcraft.bookswithoutborders.utility.InventoryUtil;
import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.formatting.FormatBuilder;
import net.knarcraft.knarlib.util.TabCompletionHelper; import net.knarcraft.knarlib.util.TabCompletionHelper;
import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor; 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, protected boolean encryptBook(@NotNull EncryptionStyle encryptionStyle, @NotNull Player player, @NotNull String key,
@NotNull String group, boolean preventAdminDecryption) { @NotNull String group, boolean preventAdminDecryption) {
ItemSlot heldSlot = InventoryUtil.getHeldSlotBook(player, false, false, true, true); ItemSlot heldSlot = InventoryUtil.getHeldBookSlot(player, Material.WRITTEN_BOOK);
ItemStack encryptedBook = EncryptedBookUtil.encryptBook(player, heldSlot == ItemSlot.MAIN_HAND, key, ItemStack encryptedBook = EncryptedBookUtil.encryptBook(player, heldSlot, key,
encryptionStyle, group, preventAdminDecryption); encryptionStyle, group, preventAdminDecryption);
if (encryptedBook != null) { if (encryptedBook != null) {

View File

@@ -6,6 +6,7 @@ import net.knarcraft.bookswithoutborders.utility.InventoryUtil;
import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.formatting.FormatBuilder;
import net.knarcraft.knarlib.property.ColorConversion; import net.knarcraft.knarlib.property.ColorConversion;
import net.knarcraft.knarlib.util.ColorHelper; import net.knarcraft.knarlib.util.ColorHelper;
import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor; import org.bukkit.command.TabExecutor;
@@ -42,10 +43,9 @@ public class CommandSetAuthor implements TabExecutor {
return false; return false;
} }
ItemSlot heldBookSlot = InventoryUtil.getHeldSlotBook(player, false, false, true, true); ItemSlot heldBookSlot = InventoryUtil.getHeldBookSlot(player, Material.WRITTEN_BOOK);
boolean mainHand = heldBookSlot == ItemSlot.MAIN_HAND; ItemStack heldBook = InventoryUtil.getHeldItem(player, heldBookSlot);
ItemStack heldBook = InventoryUtil.getHeldItem(player, mainHand); BookMeta bookMetaData = InventoryUtil.getHeldBookMetadata(player, heldBookSlot);
BookMeta bookMetaData = InventoryUtil.getHeldBookMetadata(player, mainHand);
if (bookMetaData == null) { if (bookMetaData == null) {
new FormatBuilder(Translatable.ERROR_METADATA_MISSING).error(player); new FormatBuilder(Translatable.ERROR_METADATA_MISSING).error(player);
return false; return false;

View File

@@ -7,6 +7,7 @@ import net.knarcraft.bookswithoutborders.config.StaticMessage;
import net.knarcraft.bookswithoutborders.config.translation.CostMessage; import net.knarcraft.bookswithoutborders.config.translation.CostMessage;
import net.knarcraft.bookswithoutborders.config.translation.Translatable; import net.knarcraft.bookswithoutborders.config.translation.Translatable;
import net.knarcraft.bookswithoutborders.manager.EconomyManager; import net.knarcraft.bookswithoutborders.manager.EconomyManager;
import net.knarcraft.bookswithoutborders.state.ItemSlot;
import net.knarcraft.bookswithoutborders.utility.InventoryUtil; import net.knarcraft.bookswithoutborders.utility.InventoryUtil;
import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeUtil; import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeUtil;
import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.formatting.FormatBuilder;
@@ -96,7 +97,7 @@ public class CommandSetBookPrice implements TabExecutor {
return false; return false;
} }
ItemStack heldItem = InventoryUtil.getHeldItem(player, true); ItemStack heldItem = InventoryUtil.getHeldItem(player, ItemSlot.MAIN_HAND);
if (heldItem.getType() == Material.AIR) { if (heldItem.getType() == Material.AIR) {
new FormatBuilder(CostMessage.ERROR_COST_ITEM_MISSING).error(sender); new FormatBuilder(CostMessage.ERROR_COST_ITEM_MISSING).error(sender);
return false; return false;

View File

@@ -2,6 +2,7 @@ package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.config.translation.Translatable; import net.knarcraft.bookswithoutborders.config.translation.Translatable;
import net.knarcraft.bookswithoutborders.state.ItemSlot;
import net.knarcraft.bookswithoutborders.utility.InventoryUtil; import net.knarcraft.bookswithoutborders.utility.InventoryUtil;
import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.formatting.FormatBuilder;
import net.knarcraft.knarlib.property.ColorConversion; import net.knarcraft.knarlib.property.ColorConversion;
@@ -37,7 +38,7 @@ public class CommandSetLore implements TabExecutor {
return false; return false;
} }
ItemStack heldItem = InventoryUtil.getHeldItem(player, true); ItemStack heldItem = InventoryUtil.getHeldItem(player, ItemSlot.MAIN_HAND);
if (heldItem.getType() == Material.AIR) { if (heldItem.getType() == Material.AIR) {
new FormatBuilder(Translatable.ERROR_NO_ITEM).error(player); new FormatBuilder(Translatable.ERROR_NO_ITEM).error(player);
return false; return false;

View File

@@ -1,6 +1,7 @@
package net.knarcraft.bookswithoutborders.command; package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.config.translation.Translatable; import net.knarcraft.bookswithoutborders.config.translation.Translatable;
import net.knarcraft.bookswithoutborders.state.ItemSlot;
import net.knarcraft.bookswithoutborders.utility.InputParsingUtil; import net.knarcraft.bookswithoutborders.utility.InputParsingUtil;
import net.knarcraft.bookswithoutborders.utility.InventoryUtil; import net.knarcraft.bookswithoutborders.utility.InventoryUtil;
import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.formatting.FormatBuilder;
@@ -35,7 +36,7 @@ public class CommandSetTitle implements TabExecutor {
return false; return false;
} }
ItemStack heldItem = InventoryUtil.getHeldItem(player, true); ItemStack heldItem = InventoryUtil.getHeldItem(player, ItemSlot.MAIN_HAND);
if (heldItem.getType() == Material.AIR) { if (heldItem.getType() == Material.AIR) {
new FormatBuilder(Translatable.ERROR_NO_ITEM).error(player); new FormatBuilder(Translatable.ERROR_NO_ITEM).error(player);
return false; return false;

View File

@@ -7,6 +7,7 @@ import net.knarcraft.bookswithoutborders.state.ItemSlot;
import net.knarcraft.bookswithoutborders.utility.BookMetaUtil; import net.knarcraft.bookswithoutborders.utility.BookMetaUtil;
import net.knarcraft.bookswithoutborders.utility.InventoryUtil; import net.knarcraft.bookswithoutborders.utility.InventoryUtil;
import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.formatting.FormatBuilder;
import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor; 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 //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); ItemSlot holdingSlot = InventoryUtil.getHeldBookSlot(player, Material.WRITTEN_BOOK);
unSignHeldBook(player, holdingSlot == ItemSlot.MAIN_HAND); unSignHeldBook(player, holdingSlot);
return true; return true;
} }
@@ -48,16 +49,16 @@ public class CommandUnSign implements TabExecutor {
* Un-signs the player's currently held book * Un-signs the player's currently held book
* *
* @param player <p>The player holding the book</p> * @param player <p>The player holding the book</p>
* @param mainHand <p>Whether the player is holding the book in its main hand or its off hand</p> * @param itemSlot <p>The item slot of the held book</p>
*/ */
protected void unSignHeldBook(@NotNull Player player, boolean mainHand) { protected void unSignHeldBook(@NotNull Player player, @NotNull ItemSlot itemSlot) {
//Get the old book //Get the old book
BookMeta oldMetadata = InventoryUtil.getHeldBookMetadata(player, mainHand); BookMeta oldMetadata = InventoryUtil.getHeldBookMetadata(player, itemSlot);
if (oldMetadata == null) { if (oldMetadata == null) {
new FormatBuilder(Translatable.ERROR_METADATA_MISSING).error(player); new FormatBuilder(Translatable.ERROR_METADATA_MISSING).error(player);
return; return;
} }
ItemStack heldBook = InventoryUtil.getHeldBook(player, mainHand); ItemStack heldBook = InventoryUtil.getHeldBook(player, true);
//Only allow the owner to un-sign the book //Only allow the owner to un-sign the book
if (BooksWithoutBorders.getConfiguration().getAuthorOnlyUnsign() && if (BooksWithoutBorders.getConfiguration().getAuthorOnlyUnsign() &&
@@ -74,7 +75,7 @@ public class CommandUnSign implements TabExecutor {
} }
reverseColorCodes(book); reverseColorCodes(book);
InventoryUtil.replaceHeldItem(player, book, mainHand); InventoryUtil.replaceHeldItem(player, book, itemSlot);
new FormatBuilder(Translatable.SUCCESS_UNSIGNED).success(player); new FormatBuilder(Translatable.SUCCESS_UNSIGNED).success(player);
} }

View File

@@ -47,6 +47,8 @@ public enum StaticMessage {
EXCEPTION_MIGRATE_BOOK_LOAD_FAILED("Unable to load book: {path}"), 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" + EXCEPTION_META_HAS_SEPARATOR("The author; {author} or title; {title} contains the title author separator" +
" ({separator}). Saving this book would lead to unexpected problems."), " ({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 * The cost type used to specify economy

View File

@@ -61,7 +61,7 @@ public class PlayerEventListener implements Listener {
if (!bookName.trim().isEmpty()) { if (!bookName.trim().isEmpty()) {
//Give the book to the player if it exists //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) { if (newBook != null) {
player.getInventory().addItem(newBook); player.getInventory().addItem(newBook);
} }

View File

@@ -7,6 +7,7 @@ import net.knarcraft.bookswithoutborders.config.translation.SignText;
import net.knarcraft.bookswithoutborders.config.translation.Translatable; import net.knarcraft.bookswithoutborders.config.translation.Translatable;
import net.knarcraft.bookswithoutborders.encryption.EncryptionStyle; import net.knarcraft.bookswithoutborders.encryption.EncryptionStyle;
import net.knarcraft.bookswithoutborders.state.BookDirectory; import net.knarcraft.bookswithoutborders.state.BookDirectory;
import net.knarcraft.bookswithoutborders.state.ItemSlot;
import net.knarcraft.bookswithoutborders.state.SignType; import net.knarcraft.bookswithoutborders.state.SignType;
import net.knarcraft.bookswithoutborders.utility.BookFileUtil; import net.knarcraft.bookswithoutborders.utility.BookFileUtil;
import net.knarcraft.bookswithoutborders.utility.BookLoaderUtil; import net.knarcraft.bookswithoutborders.utility.BookLoaderUtil;
@@ -237,10 +238,9 @@ public class SignEventListener implements Listener {
@NotNull EquipmentSlot hand) { @NotNull EquipmentSlot hand) {
ItemStack eBook; ItemStack eBook;
String[] lines = sign.getSide(Side.FRONT).getLines(); String[] lines = sign.getSide(Side.FRONT).getLines();
boolean mainHand = hand == EquipmentSlot.HAND;
if (heldItemType == Material.WRITTEN_BOOK) { if (heldItemType == Material.WRITTEN_BOOK) {
player.closeInventory(); 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); EncryptionStyle.getFromString(InputCleaningUtil.stripColor(lines[3])), false);
if (eBook != null) { if (eBook != null) {
player.getInventory().setItem(hand, eBook); player.getInventory().setItem(hand, eBook);
@@ -270,7 +270,7 @@ public class SignEventListener implements Listener {
fileName += InputCleaningUtil.stripColor(thirdLine); 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) { if (newBook != null) {
player.getInventory().addItem(newBook); player.getInventory().addItem(newBook);

View File

@@ -1,5 +1,8 @@
package net.knarcraft.bookswithoutborders.state; 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 * 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 * 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 <p>The equipment slot to convert</p>
* @return <p>The corresponding equipment slot</p>
*/
@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;
};
}
} }

View File

@@ -26,21 +26,6 @@ public final class BookLoaderUtil {
private BookLoaderUtil() { private BookLoaderUtil() {
} }
/**
* Loads the given book
*
* @param sender <p>The command sender trying to load the book</p>
* @param fileName <p>The index or file name of the book to load</p>
* @param isSigned <p>Whether to load the book as signed, and not unsigned</p>
* @param directory <p>The directory to save the book in</p>
* @return <p>The loaded book</p>
*/
@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 * Loads the given book
* *
@@ -59,7 +44,7 @@ public final class BookLoaderUtil {
int bookIndex = Integer.parseInt(fileName); int bookIndex = Integer.parseInt(fileName);
List<String> availableFiles = BooksWithoutBorders.getAvailableBooks(sender, List<String> availableFiles = BooksWithoutBorders.getAvailableBooks(sender,
bookDirectory == BookDirectory.PUBLIC); bookDirectory == BookDirectory.PUBLIC);
if (bookIndex <= availableFiles.size()) { if (bookIndex > 0 && bookIndex <= availableFiles.size()) {
fileName = availableFiles.get(Integer.parseInt(fileName) - 1); fileName = availableFiles.get(Integer.parseInt(fileName) - 1);
} }
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {

View File

@@ -14,6 +14,7 @@ import net.knarcraft.bookswithoutborders.encryption.Magic;
import net.knarcraft.bookswithoutborders.encryption.OneTimePad; import net.knarcraft.bookswithoutborders.encryption.OneTimePad;
import net.knarcraft.bookswithoutborders.encryption.SubstitutionCipher; import net.knarcraft.bookswithoutborders.encryption.SubstitutionCipher;
import net.knarcraft.bookswithoutborders.state.BookDirectory; import net.knarcraft.bookswithoutborders.state.BookDirectory;
import net.knarcraft.bookswithoutborders.state.ItemSlot;
import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.formatting.FormatBuilder;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
@@ -88,23 +89,23 @@ public final class EncryptedBookUtil {
* Encrypts a book * Encrypts a book
* *
* @param player <p>The player encrypting the book</p> * @param player <p>The player encrypting the book</p>
* @param mainHand <p>Whether the player is holding the book in its main hand</p> * @param itemSlot <p>The item slot of the held book</p>
* @param key <p>The key/password to use for encryption</p> * @param key <p>The key/password to use for encryption</p>
* @param style <p>The encryption style to use</p> * @param style <p>The encryption style to use</p>
* @param preventAdminDecrypt <p>Whether to prevent storage of a key that can be used for admin decryption</p> * @param preventAdminDecrypt <p>Whether to prevent storage of a key that can be used for admin decryption</p>
* @return <p>An encrypted version of the book</p> * @return <p>An encrypted version of the book</p>
*/ */
@Nullable @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) { @NotNull EncryptionStyle style, boolean preventAdminDecrypt) {
return encryptBook(player, mainHand, key, style, "", preventAdminDecrypt); return encryptBook(player, itemSlot, key, style, "", preventAdminDecrypt);
} }
/** /**
* Encrypts a book * Encrypts a book
* *
* @param player <p>The player encrypting the book</p> * @param player <p>The player encrypting the book</p>
* @param mainHand <p>Whether the player is holding the book in its main hand</p> * @param itemSlot <p>The item slot of the held book</p>
* @param key <p>The key/password to use for encryption</p> * @param key <p>The key/password to use for encryption</p>
* @param style <p>The encryption style to use</p> * @param style <p>The encryption style to use</p>
* @param groupName <p>The name of the group to encrypt for, or "" otherwise</p> * @param groupName <p>The name of the group to encrypt for, or "" otherwise</p>
@@ -112,10 +113,10 @@ public final class EncryptedBookUtil {
* @return <p>An encrypted version of the book</p> * @return <p>An encrypted version of the book</p>
*/ */
@Nullable @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, @NotNull EncryptionStyle style, @NotNull String groupName,
boolean preventAdminDecrypt) { boolean preventAdminDecrypt) {
BookMeta book = InventoryUtil.getHeldBookMetadata(player, mainHand); BookMeta book = InventoryUtil.getHeldBookMetadata(player, itemSlot);
if (book == null) { if (book == null) {
new FormatBuilder(Translatable.ERROR_METADATA_MISSING).error(player); new FormatBuilder(Translatable.ERROR_METADATA_MISSING).error(player);
return null; return null;

View File

@@ -28,9 +28,9 @@ public final class InventoryUtil {
*/ */
@Nullable @Nullable
public static ItemStack getHeldBook(@NotNull Player player) { 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) { if (holdingSlot != ItemSlot.NONE) {
return InventoryUtil.getHeldItem(player, holdingSlot == ItemSlot.MAIN_HAND); return InventoryUtil.getHeldItem(player, holdingSlot);
} else { } else {
return null; return null;
} }
@@ -45,14 +45,11 @@ public final class InventoryUtil {
*/ */
@NotNull @NotNull
public static ItemStack getHeldBook(@NotNull Player player, boolean signedBook) { public static ItemStack getHeldBook(@NotNull Player player, boolean signedBook) {
ItemSlot heldSlot = getHeldSlotBook(player, false, false, true, signedBook); ItemSlot heldSlot = getHeldBookSlot(player, signedBook ? Material.WRITTEN_BOOK : Material.WRITABLE_BOOK);
if (heldSlot == ItemSlot.MAIN_HAND) { if (heldSlot == ItemSlot.NONE) {
return getHeldItem(player, true);
} else if (heldSlot == ItemSlot.OFF_HAND) {
return getHeldItem(player, false);
} else {
throw new IllegalArgumentException(StaticMessage.EXCEPTION_NOT_HOLDING_ONE_BOOK.toString()); throw new IllegalArgumentException(StaticMessage.EXCEPTION_NOT_HOLDING_ONE_BOOK.toString());
} }
return getHeldItem(player, heldSlot);
} }
/** /**
@@ -62,11 +59,11 @@ public final class InventoryUtil {
* @param newBook <p>The new book the player should hold</p> * @param newBook <p>The new book the player should hold</p>
*/ */
public static void setHeldWrittenBook(@NotNull Player player, @NotNull ItemStack newBook) { 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) { if (itemSlot == ItemSlot.MAIN_HAND) {
replaceHeldItem(player, newBook, true); replaceHeldItem(player, newBook, itemSlot);
} else if (itemSlot == ItemSlot.OFF_HAND) { } else if (itemSlot == ItemSlot.OFF_HAND) {
replaceHeldItem(player, newBook, false); replaceHeldItem(player, newBook, itemSlot);
} else { } else {
new FormatBuilder(Translatable.ERROR_BOOK_NOT_FOUND).error(player); new FormatBuilder(Translatable.ERROR_BOOK_NOT_FOUND).error(player);
} }
@@ -124,66 +121,117 @@ public final class InventoryUtil {
return false; return false;
} }
/**
* Gets metadata about the player's held book
*
* @param player <p>The player holding the book</p>
* @param itemSlot <p>The item slot of the item to get metadata about</p>
* @return <p>Information about the held book</p>
*/
@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 <p>The player to get from</p>
* @param itemSlot <p>The item slot of the item to get</p>
* @return <p>The item the player is holding in the given hand</p>
*/
@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 <p>The player to replace the item for</p>
* @param newBook <p>The new book the player should hold</p>
* @param itemSlot <p>The item slot of the item to replace</p>
*/
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 * Gets the slot of the player's held book
* *
* @param player <p>The player holding the book</p> * @param player <p>The player holding the book</p>
* @param handMatters <p>Whether the differentiation between the main hand and the off hand is relevant</p> * @param requiredMaterial <p>The required material of the book to look for. Set to null if it doesn't matter</p>
* @param mainHand <p>Whether to search the player's main hand or off hand, if it's relevant</p>
* @param typeMatters <p>Whether the differentiation between signed and unsigned books is relevant</p>
* @param writtenBook <p>Whether to search for written or unwritten books, if it's relevant</p>
* @return <p>The slot of the player's held book</p> * @return <p>The slot of the player's held book</p>
*/ */
@NotNull @NotNull
public static ItemSlot getHeldSlotBook(@NotNull Player player, boolean handMatters, boolean mainHand, public static ItemSlot getHeldBookSlot(@NotNull Player player, @Nullable Material requiredMaterial) {
boolean typeMatters, boolean writtenBook) {
BookHoldingState state = getBookHoldingState(player); BookHoldingState state = getBookHoldingState(player);
ItemStack mainHandItem = getHeldItem(player, true);
ItemStack offHandItem = getHeldItem(player, false); if (requiredMaterial != null) {
Material requiredMaterial = writtenBook ? Material.WRITTEN_BOOK : Material.WRITABLE_BOOK; return switch (requiredMaterial) {
//Ambiguous or empty case WRITTEN_BOOK -> getSignedBookSlot(state);
if (state == BookHoldingState.SIGNED_BOTH_HANDS || case WRITABLE_BOOK -> getUnsignedBookSlot(state);
state == BookHoldingState.UNSIGNED_BOTH_HANDS || default ->
state == BookHoldingState.NONE) { throw new IllegalArgumentException(StaticMessage.EXCEPTION_GET_HELD_BOOK_INVALID_MATERIAL.toString());
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 { } else {
return ItemSlot.OFF_HAND; return getBookSlot(state);
}
} 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;
}
} 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 ItemSlot.NONE;
/**
* Gets the slot of the held book
*
* @param bookHoldingState <p>The book holding state to check</p>
* @return <p>The item slot of the held book</p>
*/
@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 <p>The book holding state to check</p>
* @return <p>The item slot of the held signed book</p>
*/
@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 <p>The book holding state to check</p>
* @return <p>The item slot of the held unsigned book</p>
*/
@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 @NotNull
private static BookHoldingState getBookHoldingState(@NotNull Player player) { private static BookHoldingState getBookHoldingState(@NotNull Player player) {
ItemStack mainHandItem = getHeldItem(player, true); ItemStack mainHandItem = getHeldItem(player, ItemSlot.MAIN_HAND);
ItemStack offHandItem = getHeldItem(player, false); ItemStack offHandItem = getHeldItem(player, ItemSlot.OFF_HAND);
boolean hasSignedBookInMainHand = mainHandItem.getType() == Material.WRITTEN_BOOK; boolean hasSignedBookInMainHand = mainHandItem.getType() == Material.WRITTEN_BOOK;
boolean hasUnsignedBookInMainHand = mainHandItem.getType() == Material.WRITABLE_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 <p>The player holding the book</p>
* @param mainHand <p>Whether to get information about a book in the player's main hand or off hand</p>
* @return <p>Information about the held book</p>
*/
@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 <p>The player to get from</p>
* @param mainHand <p>Whether to get the item in the player's main hand or off hand</p>
* @return <p>The item the player is holding in the given hand</p>
*/
@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 <p>The player to replace the item for</p>
* @param newBook <p>The new book the player should hold</p>
* @param mainHand <p>Whether to replace the item in the player's main hand or off hand</p>
*/
public static void replaceHeldItem(@NotNull Player player, @NotNull ItemStack newBook, boolean mainHand) {
if (mainHand) {
player.getInventory().setItemInMainHand(newBook);
} else {
player.getInventory().setItemInOffHand(newBook);
}
}
} }