Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/me/touchie771/minecraftGUI/api/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
76 changes: 69 additions & 7 deletions src/main/java/me/touchie771/minecraftGUI/api/SlotItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -35,7 +34,8 @@ public record SlotItem(
@Nullable Map<Enchantment, Integer> enchantments,
@Nullable ItemStack customItemStack,
@Nullable Integer customModelData,
@Nullable Integer damage
@Nullable Integer damage,
@Nullable Set<ItemFlag> itemFlags
) {
/**
* Legacy constructor for backward compatibility.
Expand All @@ -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);
}

/**
Expand All @@ -72,6 +72,7 @@ public static class Builder {
private @Nullable ItemStack customItemStack;
private @Nullable Integer customModelData;
private @Nullable Integer damage;
private @Nullable Set<ItemFlag> itemFlags;

private Builder(int itemSlot) {
this.itemSlot = itemSlot;
Expand Down Expand Up @@ -164,6 +165,23 @@ public Builder enchantments(@Nullable Map<Enchantment, Integer> 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).
*
Expand Down Expand Up @@ -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<ItemFlag> 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.
*
Expand All @@ -212,7 +273,8 @@ public SlotItem build() {
enchantments != null ? Map.copyOf(enchantments) : null,
customItemStack,
customModelData,
damage
damage,
itemFlags != null ? Set.copyOf(itemFlags) : null
);
}
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/me/touchie771/minecraftGUI/api/SlotItemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}