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
10 changes: 8 additions & 2 deletions src/main/java/me/touchie771/minecraftGUI/api/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
* Menu menu = Menu.newBuilder()
* .size(27)
* .title(Component.text("My Menu"))
* .items(new SlotItem(Component.text("Diamond"), (short) 0, Material.DIAMOND, 1))
* .items(SlotItem.builder(0)
* .itemName(Component.text("Diamond"))
* .material(Material.DIAMOND)
* .build())
* .build();
* }</pre></p>
*/
Expand Down Expand Up @@ -276,7 +279,10 @@ public void unregisterEvents() {
* Menu menu = new MenuBuilder()
* .size(27)
* .title(Component.text("My Menu"))
* .items(new SlotItem(Component.text("Diamond"), (short) 0, Material.DIAMOND, 1))
* .items(SlotItem.builder(0)
* .itemName(Component.text("Diamond"))
* .material(Material.DIAMOND)
* .build())
* .build();
* }</pre></p>
*/
Expand Down
170 changes: 170 additions & 0 deletions src/main/java/me/touchie771/minecraftGUI/api/SlotItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
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;

Expand Down Expand Up @@ -46,4 +48,172 @@ public record SlotItem(
public SlotItem(@NotNull Component itemName, int itemSlot, @NotNull Material material, int quantity) {
this(itemName, itemSlot, material, quantity, null, null, null, null, null);
}

/**
* Creates a new builder for constructing SlotItem instances.
*
* @param itemSlot The slot position where this item should be placed
* @return A new Builder instance
*/
public static Builder builder(int itemSlot) {
return new Builder(itemSlot);
}

/**
* Builder class for creating SlotItem instances with a fluent API.
*/
public static class Builder {
private @Nullable Component itemName;
private final int itemSlot;
private @Nullable Material material;
private int quantity = 1;
private @Nullable List<Component> lore;
private @Nullable Map<Enchantment, Integer> enchantments;
private @Nullable ItemStack customItemStack;
private @Nullable Integer customModelData;
private @Nullable Integer damage;

private Builder(int itemSlot) {
this.itemSlot = itemSlot;
}

/**
* Sets the display name of the item.
*
* @param itemName The display name component
* @return This builder instance for chaining
*/
public Builder itemName(@Nullable Component itemName) {
this.itemName = itemName;
return this;
}

/**
* Sets the material type for this item.
*
* @param material The material type
* @return This builder instance for chaining
*/
public Builder material(@Nullable Material material) {
this.material = material;
return this;
}

/**
* Sets the stack size of this item.
*
* @param quantity The stack size (1-64)
* @return This builder instance for chaining
*/
public Builder quantity(int quantity) {
this.quantity = quantity;
return this;
}

/**
* Sets the lore lines for the item.
*
* @param lore The list of lore components
* @return This builder instance for chaining
*/
public Builder lore(@Nullable List<Component> lore) {
this.lore = lore;
return this;
}

/**
* Adds a single line of lore to the item.
*
* @param line The lore line to add
* @return This builder instance for chaining
*/
public Builder addLore(@NotNull Component line) {
if (this.lore == null) {
this.lore = new ArrayList<>();
} else if (!(this.lore instanceof ArrayList)) {
this.lore = new ArrayList<>(this.lore);
}
this.lore.add(line);
return this;
}

/**
* Adds multiple lines of lore to the item.
*
* @param lines The lore lines to add
* @return This builder instance for chaining
*/
public Builder addLore(@NotNull Component... lines) {
if (this.lore == null) {
this.lore = new ArrayList<>();
} else if (!(this.lore instanceof ArrayList)) {
this.lore = new ArrayList<>(this.lore);
}
this.lore.addAll(Arrays.asList(lines));
return this;
}

/**
* Sets the enchantments to apply to the item.
*
* @param enchantments The map of enchantments to levels
* @return This builder instance for chaining
*/
public Builder enchantments(@Nullable Map<Enchantment, Integer> enchantments) {
this.enchantments = enchantments;
return this;
}

/**
* Sets a custom ItemStack to use as base (e.g., for items with NBT).
*
* @param customItemStack The custom ItemStack
* @return This builder instance for chaining
*/
public Builder customItemStack(@Nullable ItemStack customItemStack) {
this.customItemStack = customItemStack;
return this;
}

/**
* Sets the custom model data ID.
*
* @param customModelData The custom model data ID
* @return This builder instance for chaining
*/
public Builder customModelData(@Nullable Integer customModelData) {
this.customModelData = customModelData;
return this;
}

/**
* Sets the damage/durability value.
*
* @param damage The damage value
* @return This builder instance for chaining
*/
public Builder damage(@Nullable Integer damage) {
this.damage = damage;
return this;
}

/**
* Builds the SlotItem instance with the configured values.
*
* @return A new SlotItem instance
*/
public SlotItem build() {
return new SlotItem(
itemName,
itemSlot,
material,
quantity,
lore != null ? List.copyOf(lore) : null,
enchantments != null ? Map.copyOf(enchantments) : null,
customItemStack,
customModelData,
damage
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,15 @@ public class ConfirmationMenu {
* @return A configured Menu instance
*/
public static Menu create(@NotNull Plugin plugin, @NotNull Component title, @NotNull Runnable onConfirm, @NotNull Runnable onCancel) {
SlotItem confirmItem = new SlotItem(
Component.text("Confirm", NamedTextColor.GREEN),
(short) CONFIRM_SLOT,
Material.LIME_WOOL,
1
);
SlotItem confirmItem = SlotItem.builder(CONFIRM_SLOT)
.itemName(Component.text("Confirm", NamedTextColor.GREEN))
.material(Material.LIME_WOOL)
.build();

SlotItem cancelItem = new SlotItem(
Component.text("Cancel", NamedTextColor.RED),
(short) CANCEL_SLOT,
Material.RED_WOOL,
1
);
SlotItem cancelItem = SlotItem.builder(CANCEL_SLOT)
.itemName(Component.text("Cancel", NamedTextColor.RED))
.material(Material.RED_WOOL)
.build();

ClickHandler confirmHandler = ClickHandler.newBuilder()
.callback(event -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ private void update() {
PageItem item = items.get(i);
int slot = i - startIndex;

menu.addItems(new SlotItem(item.name(), (short) slot, item.material(), item.quantity()));
menu.addItems(SlotItem.builder(slot)
.itemName(item.name())
.material(item.material())
.quantity(item.quantity())
.build());

if (item.action() != null) {
menu.onClick(slot, ClickHandler.newBuilder()
Expand All @@ -90,12 +94,10 @@ private void update() {
}

if (currentPage > 0) {
menu.addItems(new SlotItem(
Component.text("Previous Page", NamedTextColor.YELLOW),
(short) PREV_SLOT,
Material.ARROW,
1
));
menu.addItems(SlotItem.builder(PREV_SLOT)
.itemName(Component.text("Previous Page", NamedTextColor.YELLOW))
.material(Material.ARROW)
.build());
menu.onClick(PREV_SLOT, ClickHandler.newBuilder()
.callback(e -> {
currentPage--;
Expand All @@ -106,12 +108,10 @@ private void update() {
}

if (currentPage < totalPages - 1) {
menu.addItems(new SlotItem(
Component.text("Next Page", NamedTextColor.YELLOW),
(short) NEXT_SLOT,
Material.ARROW,
1
));
menu.addItems(SlotItem.builder(NEXT_SLOT)
.itemName(Component.text("Next Page", NamedTextColor.YELLOW))
.material(Material.ARROW)
.build());
menu.onClick(NEXT_SLOT, ClickHandler.newBuilder()
.callback(e -> {
currentPage++;
Expand Down
Loading