diff --git a/src/main/java/me/touchie771/minecraftGUI/api/Menu.java b/src/main/java/me/touchie771/minecraftGUI/api/Menu.java index f71a985..d872076 100644 --- a/src/main/java/me/touchie771/minecraftGUI/api/Menu.java +++ b/src/main/java/me/touchie771/minecraftGUI/api/Menu.java @@ -236,6 +236,9 @@ private ItemStack createItemStack(SlotItem item) { if (item.damage() != null && meta instanceof org.bukkit.inventory.meta.Damageable damageable) { damageable.setDamage(item.damage()); } + if (item.itemFlags() != null) { + meta.addItemFlags(item.itemFlags().toArray(new org.bukkit.inventory.ItemFlag[0])); + } }); return itemStack; } diff --git a/src/main/java/me/touchie771/minecraftGUI/api/SlotItem.java b/src/main/java/me/touchie771/minecraftGUI/api/SlotItem.java index 087cdb8..c2d4de0 100644 --- a/src/main/java/me/touchie771/minecraftGUI/api/SlotItem.java +++ b/src/main/java/me/touchie771/minecraftGUI/api/SlotItem.java @@ -3,14 +3,12 @@ import net.kyori.adventure.text.Component; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; +import java.util.*; /** * Represents an item that can be placed in a specific slot of a Minecraft GUI menu. @@ -25,6 +23,7 @@ * @param customItemStack A base ItemStack to use (e.g. for items with NBT). * @param customModelData The custom model data ID. * @param damage The damage/durability value. + * @param itemFlags The item flags to apply (e.g., HIDE_ATTRIBUTES). */ public record SlotItem( @Nullable Component itemName, @@ -35,7 +34,8 @@ public record SlotItem( @Nullable Map enchantments, @Nullable ItemStack customItemStack, @Nullable Integer customModelData, - @Nullable Integer damage + @Nullable Integer damage, + @Nullable Set itemFlags ) { /** * Legacy constructor for backward compatibility. @@ -46,7 +46,7 @@ public record SlotItem( * @param quantity The quantity */ public SlotItem(@NotNull Component itemName, int itemSlot, @NotNull Material material, int quantity) { - this(itemName, itemSlot, material, quantity, null, null, null, null, null); + this(itemName, itemSlot, material, quantity, null, null, null, null, null, null); } /** @@ -72,6 +72,7 @@ public static class Builder { private @Nullable ItemStack customItemStack; private @Nullable Integer customModelData; private @Nullable Integer damage; + private @Nullable Set itemFlags; private Builder(int itemSlot) { this.itemSlot = itemSlot; @@ -164,6 +165,23 @@ public Builder enchantments(@Nullable Map enchantments) { return this; } + /** + * Adds an enchantment to the item. + * + * @param enchantment The enchantment to add + * @param level The level of the enchantment + * @return This builder instance for chaining + */ + public Builder addEnchantment(@NotNull Enchantment enchantment, int level) { + if (this.enchantments == null) { + this.enchantments = new HashMap<>(); + } else if (!(this.enchantments instanceof HashMap)) { + this.enchantments = new HashMap<>(this.enchantments); + } + this.enchantments.put(enchantment, level); + return this; + } + /** * Sets a custom ItemStack to use as base (e.g., for items with NBT). * @@ -197,6 +215,49 @@ public Builder damage(@Nullable Integer damage) { return this; } + /** + * Sets the item flags to apply. + * + * @param itemFlags The set of item flags + * @return This builder instance for chaining + */ + public Builder itemFlags(@Nullable Set itemFlags) { + this.itemFlags = itemFlags; + return this; + } + + /** + * Adds an item flag to the item. + * + * @param flag The item flag to add + * @return This builder instance for chaining + */ + public Builder addItemFlag(@NotNull ItemFlag flag) { + if (this.itemFlags == null) { + this.itemFlags = new HashSet<>(); + } else if (!(this.itemFlags instanceof HashSet)) { + this.itemFlags = new HashSet<>(this.itemFlags); + } + this.itemFlags.add(flag); + return this; + } + + /** + * Adds multiple item flags to the item. + * + * @param flags The item flags to add + * @return This builder instance for chaining + */ + public Builder addItemFlags(@NotNull ItemFlag... flags) { + if (this.itemFlags == null) { + this.itemFlags = new HashSet<>(); + } else if (!(this.itemFlags instanceof HashSet)) { + this.itemFlags = new HashSet<>(this.itemFlags); + } + this.itemFlags.addAll(Arrays.asList(flags)); + return this; + } + /** * Builds the SlotItem instance with the configured values. * @@ -212,7 +273,8 @@ public SlotItem build() { enchantments != null ? Map.copyOf(enchantments) : null, customItemStack, customModelData, - damage + damage, + itemFlags != null ? Set.copyOf(itemFlags) : null ); } } diff --git a/src/test/java/me/touchie771/minecraftGUI/api/SlotItemTest.java b/src/test/java/me/touchie771/minecraftGUI/api/SlotItemTest.java index ea90715..461d401 100644 --- a/src/test/java/me/touchie771/minecraftGUI/api/SlotItemTest.java +++ b/src/test/java/me/touchie771/minecraftGUI/api/SlotItemTest.java @@ -4,6 +4,7 @@ import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.DisplayName; @@ -278,4 +279,34 @@ void testBuilderWithDamageValue() { assertEquals(Material.IRON_SWORD, item.material()); assertEquals(12, item.itemSlot()); } + + @Test + @DisplayName("Test builder with item flags") + void testBuilderWithItemFlags() { + SlotItem item = SlotItem.builder(0) + .material(Material.DIAMOND_SWORD) + .addItemFlag(ItemFlag.HIDE_ATTRIBUTES) + .addItemFlag(ItemFlag.HIDE_ENCHANTS) + .build(); + + assertNotNull(item.itemFlags()); + assertEquals(2, item.itemFlags().size()); + assertTrue(item.itemFlags().contains(ItemFlag.HIDE_ATTRIBUTES)); + assertTrue(item.itemFlags().contains(ItemFlag.HIDE_ENCHANTS)); + } + + @Test + @DisplayName("Test addEnchantment convenience method") + void testAddEnchantment() { + SlotItem item = SlotItem.builder(0) + .material(Material.DIAMOND_PICKAXE) + .addEnchantment(Enchantment.EFFICIENCY, 5) + .addEnchantment(Enchantment.UNBREAKING, 3) + .build(); + + assertNotNull(item.enchantments()); + assertEquals(2, item.enchantments().size()); + assertEquals(5, item.enchantments().get(Enchantment.EFFICIENCY)); + assertEquals(3, item.enchantments().get(Enchantment.UNBREAKING)); + } } \ No newline at end of file