From a755313b2df2dd1c8a4dc67fceb50d759fe2a1c9 Mon Sep 17 00:00:00 2001 From: CJ Burkey Date: Mon, 27 May 2024 14:25:53 -0400 Subject: [PATCH] Make abstract inventory menu class --- .../claimchunk/gui/GuiMenuScreen.java | 94 +++++++++++++++++++ .../claimchunk/gui/screens/MainMenu.java | 38 +++----- 2 files changed, 109 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java diff --git a/src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java b/src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java new file mode 100644 index 0000000..e880232 --- /dev/null +++ b/src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java @@ -0,0 +1,94 @@ +package com.cjburkey.claimchunk.gui; + +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * An abstract wrapper for {@link ICCGui} to make creating inventory menus easier. + * + * @since 0.0.26 + */ +public abstract class GuiMenuScreen implements ICCGui { + + /** Anonymous callback for item click event. */ + @FunctionalInterface + public interface GuiItemAction { + + /** Called when the item is clicked. */ + void onClick( + @NotNull Inventory inventory, + @NotNull Player player, + int slot, + @NotNull ClickType clickType, + @NotNull ItemStack stack); + } + + /** Wrapper around an interactive item in the GUI menu. */ + record GuiItemWrapper(@NotNull ItemStack stack, @NotNull GuiItemAction action) {} + + private final int rowCount; + private final GuiItemWrapper[] actions; + private final String name; + + protected GuiMenuScreen(int rowCount, @NotNull String name) { + this.rowCount = Math.min(Math.max(rowCount, 1), 6); + this.actions = new GuiItemWrapper[this.rowCount * 9]; + this.name = name; + } + + /** + * Add an interactive button item to this inventory GUI. + * + * @param slot The slot of the inventory in which to put the item. + * @param stack The stack that represents this action. + * @param action The on-click callback + */ + protected void addInteractiveButton( + int slot, @NotNull ItemStack stack, @NotNull GuiItemAction action) { + if (slot >= 0 && slot < this.actions.length) { + this.actions[slot] = new GuiItemWrapper(stack, action); + } + } + + @Override + public void onOpen(@NotNull Inventory inventory, @NotNull Player player) { + for (int slot = 0; slot < actions.length; slot++) { + GuiItemWrapper action = actions[slot]; + if (action != null) { + inventory.setItem(slot, action.stack()); + } + } + } + + @Override + public void onClick( + @NotNull Inventory inventory, + @NotNull Player player, + int slot, + @NotNull ClickType clickType, + @Nullable ItemStack stack) { + assert slot >= 0 && slot < actions.length; + + GuiItemWrapper action = actions[slot]; + if (action != null && stack != null) { + action.action().onClick(inventory, player, slot, clickType, stack); + } + } + + @Override + public void onClose(@NotNull Inventory inventory, @NotNull Player player) {} + + @Override + public @NotNull String getName() { + return name; + } + + @Override + public int getRows() { + return rowCount; + } +} diff --git a/src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java b/src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java index 5a1ceeb..dfbfaa5 100644 --- a/src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java +++ b/src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java @@ -1,36 +1,28 @@ package com.cjburkey.claimchunk.gui.screens; -import com.cjburkey.claimchunk.gui.ICCGui; +import com.cjburkey.claimchunk.gui.GuiMenuScreen; +import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; -import org.jetbrains.annotations.NotNull; -public class MainMenu implements ICCGui { +public class MainMenu extends GuiMenuScreen { - @Override - public void onOpen(@NotNull Inventory inventory, @NotNull Player player) {} - - @Override - public void onClose(@NotNull Inventory inventory, @NotNull Player player) {} - - @Override - public void onClick( - @NotNull Inventory inventory, - @NotNull Player player, - int slot, - @NotNull ClickType clickType, - @NotNull ItemStack stack) {} - - @Override - public @NotNull String getName() { - return "ClaimChunk GUI"; + public MainMenu() { + super(1, "ClaimChunk Menu"); + addButtons(); } - @Override - public int getRows() { - return 0; + private void addButtons() { + addInteractiveButton( + 0, + new ItemStack(Material.PAPER), + (Inventory inventory, + Player player, + int slot, + ClickType clickType, + ItemStack stack) -> {}); } }