|
@@ -0,0 +1,353 @@
|
|
|
|
|
+package me.lethunderhawk.storeMore.gui;
|
|
|
|
|
+
|
|
|
|
|
+import me.lethunderhawk.fluxapi.util.UnItalic;
|
|
|
|
|
+import net.kyori.adventure.text.Component;
|
|
|
|
|
+import net.kyori.adventure.text.format.NamedTextColor;
|
|
|
|
|
+import org.bukkit.Bukkit;
|
|
|
|
|
+import org.bukkit.Material;
|
|
|
|
|
+import org.bukkit.entity.Player;
|
|
|
|
|
+import org.bukkit.event.inventory.ClickType;
|
|
|
|
|
+import org.bukkit.event.inventory.InventoryClickEvent;
|
|
|
|
|
+import org.bukkit.inventory.Inventory;
|
|
|
|
|
+import org.bukkit.inventory.InventoryHolder;
|
|
|
|
|
+import org.bukkit.inventory.ItemStack;
|
|
|
|
|
+import org.bukkit.inventory.meta.ItemMeta;
|
|
|
|
|
+import org.jetbrains.annotations.ApiStatus;
|
|
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+import java.util.function.BiConsumer;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Generic, reusable Inventory GUI.
|
|
|
|
|
+ */
|
|
|
|
|
+public abstract class InventoryGUI implements InventoryHolder {
|
|
|
|
|
+
|
|
|
|
|
+ private final String title;
|
|
|
|
|
+ private final int size;
|
|
|
|
|
+ private final Inventory inventory;
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Slots with the action to trigger upon clicking
|
|
|
|
|
+ */
|
|
|
|
|
+ private final Map<Integer, BiConsumer<Player, ClickType>> slotActions = new HashMap<>();
|
|
|
|
|
+
|
|
|
|
|
+ private final Stack<InventoryGUI> previousGuis = new Stack<>();
|
|
|
|
|
+ /** Slots that the player may edit (drag‑and‑drop, shift‑click, etc.) */
|
|
|
|
|
+ private final Set<Integer> editableSlots = new HashSet<>();
|
|
|
|
|
+
|
|
|
|
|
+ /** Register a slot as editable – call this from the subclass before the GUI opens */
|
|
|
|
|
+ protected final void setEditable(int... slots) {
|
|
|
|
|
+ for (int s : slots) editableSlots.add(s);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** Helper to check whether a slot is editable */
|
|
|
|
|
+ protected final boolean isEditable(int slot) {
|
|
|
|
|
+ return editableSlots.contains(slot);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public InventoryGUI(String title, int size, Player p) {
|
|
|
|
|
+ this.title = title;
|
|
|
|
|
+ this.size = size;
|
|
|
|
|
+ this.inventory = Bukkit.createInventory(this, size, title);
|
|
|
|
|
+ performAdditionalComputationOnPlayer(p);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public InventoryGUI(String title, int size) {
|
|
|
|
|
+ this.title = title;
|
|
|
|
|
+ this.size = size;
|
|
|
|
|
+ this.inventory = Bukkit.createInventory(this, size, title);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Can be overwritten by subclasses
|
|
|
|
|
+ * @param p The Player for whom the GUI is for. May not exist if the super didn't specify a player
|
|
|
|
|
+ */
|
|
|
|
|
+ public void performAdditionalComputationOnPlayer(Player p) {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void handleClick(Player player, InventoryClickEvent event) {
|
|
|
|
|
+ int rawSlot = event.getRawSlot();
|
|
|
|
|
+ // If the click is inside the top inventory (our GUI)
|
|
|
|
|
+ if (rawSlot < inventory.getSize()) {
|
|
|
|
|
+ // Editable slots: let Minecraft handle the action (no cancel)
|
|
|
|
|
+ if (isEditable(rawSlot)) {
|
|
|
|
|
+ // Do NOT cancel – the player can move items freely.
|
|
|
|
|
+ // However we still want to run any custom click‑action that
|
|
|
|
|
+ // might be attached (e.g. a “clear slot” button). The
|
|
|
|
|
+ // default implementation in your base class usually stores
|
|
|
|
|
+ // a map of slot → Consumer<Player,ClickType>. If you have
|
|
|
|
|
+ // that, call it here *after* the normal behaviour.
|
|
|
|
|
+ runClickActionIfPresent(player, rawSlot, event.getClick());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Non‑editable slots – keep the GUI read‑only
|
|
|
|
|
+ event.setCancelled(true);
|
|
|
|
|
+ runClickActionIfPresent(player, rawSlot, event.getClick());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ onPlayerInventoryClick(player, event);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ @ApiStatus.OverrideOnly
|
|
|
|
|
+ public void onPlayerInventoryClick(Player player, InventoryClickEvent event){}
|
|
|
|
|
+
|
|
|
|
|
+ private void runClickActionIfPresent(Player player, int rawSlot, ClickType click) {
|
|
|
|
|
+ BiConsumer<Player, ClickType> action = slotActions.get(rawSlot);
|
|
|
|
|
+ if (action != null) {
|
|
|
|
|
+ action.accept(player, click);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Sets an item in a specific slot.
|
|
|
|
|
+ */
|
|
|
|
|
+ public void setItem(int slot, ItemStack item) {
|
|
|
|
|
+ inventory.setItem(slot, item);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Helper method
|
|
|
|
|
+ * @param slot The slot Number
|
|
|
|
|
+ * @return The row this slot is in
|
|
|
|
|
+ */
|
|
|
|
|
+ protected int getRow(int slot) {
|
|
|
|
|
+ return slot / 9;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Helper method
|
|
|
|
|
+ * @param slot The slot Number
|
|
|
|
|
+ * @return The column this slot is in
|
|
|
|
|
+ */
|
|
|
|
|
+ protected int getColumn(int slot) {
|
|
|
|
|
+ return slot % 9;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Helper method
|
|
|
|
|
+ * @param row The row Number
|
|
|
|
|
+ * @param column The column Number
|
|
|
|
|
+ * @return The slot this corresponds to
|
|
|
|
|
+ */
|
|
|
|
|
+ protected int getSlot(int row, int column) {
|
|
|
|
|
+ return row * 9 + column;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Helper method
|
|
|
|
|
+ * @param startSlot The start slot of the Rectangle (top-left)
|
|
|
|
|
+ * @param endSlot The end slot of the Rectangle (bottom-right)
|
|
|
|
|
+ */
|
|
|
|
|
+ protected List<Integer> computeRectangleSlots(int startSlot, int endSlot) {
|
|
|
|
|
+
|
|
|
|
|
+ List<Integer> slots = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ int startRow = startSlot / 9;
|
|
|
|
|
+ int startCol = startSlot % 9;
|
|
|
|
|
+
|
|
|
|
|
+ int endRow = endSlot / 9;
|
|
|
|
|
+ int endCol = endSlot % 9;
|
|
|
|
|
+
|
|
|
|
|
+ for (int row = startRow; row <= endRow; row++) {
|
|
|
|
|
+ for (int col = startCol; col <= endCol; col++) {
|
|
|
|
|
+
|
|
|
|
|
+ slots.add(row * 9 + col);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return slots;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Copies the same Click Action onto multiple slots
|
|
|
|
|
+ * @param startSlot The start slot of the Rectangle (top-left)
|
|
|
|
|
+ * @param endSlot The end slot of the Rectangle (bottom-right)
|
|
|
|
|
+ * @param item The items to fill this Rectangle with
|
|
|
|
|
+ * @param action The action to perform upon clicking any of these slots
|
|
|
|
|
+ */
|
|
|
|
|
+ public void fillRectangleWithClickAction(int startSlot, int endSlot, ItemStack item, BiConsumer<Player, ClickType> action) {
|
|
|
|
|
+ int startRow = getRow(startSlot);
|
|
|
|
|
+ int startCol = getColumn(startSlot);
|
|
|
|
|
+
|
|
|
|
|
+ int endRow = getRow(endSlot);
|
|
|
|
|
+ int endCol = getColumn(endSlot);
|
|
|
|
|
+
|
|
|
|
|
+ for (int row = startRow; row <= endRow; row++) {
|
|
|
|
|
+ for (int col = startCol; col <= endCol; col++) {
|
|
|
|
|
+ int slot = getSlot(row, col);
|
|
|
|
|
+ setItemWithClickAction(slot, item, action);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Copies the same {@link ItemStack} onto multiple slots
|
|
|
|
|
+ * @param startSlot The start slot of the Rectangle (top-left)
|
|
|
|
|
+ * @param endSlot The end slot of the Rectangle (bottom-right)
|
|
|
|
|
+ * @param item The items to fill this Rectangle with
|
|
|
|
|
+ */
|
|
|
|
|
+ public void fillRectangle(int startSlot, int endSlot, ItemStack item) {
|
|
|
|
|
+ int startRow = getRow(startSlot);
|
|
|
|
|
+ int startCol = getColumn(startSlot);
|
|
|
|
|
+
|
|
|
|
|
+ int endRow = getRow(endSlot);
|
|
|
|
|
+ int endCol = getColumn(endSlot);
|
|
|
|
|
+
|
|
|
|
|
+ for (int row = startRow; row <= endRow; row++) {
|
|
|
|
|
+ for (int col = startCol; col <= endCol; col++) {
|
|
|
|
|
+ int slot = getSlot(row, col);
|
|
|
|
|
+ setItem(slot, item);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Sets a full Rectangle to be editable
|
|
|
|
|
+ * @param startSlot The start slot of the Rectangle (top-left)
|
|
|
|
|
+ * @param endSlot The end slot of the Rectangle (bottom-right)
|
|
|
|
|
+ */
|
|
|
|
|
+ public void setRectangleEditable(int startSlot, int endSlot) {
|
|
|
|
|
+ int startRow = getRow(startSlot);
|
|
|
|
|
+ int startCol = getColumn(startSlot);
|
|
|
|
|
+
|
|
|
|
|
+ int endRow = getRow(endSlot);
|
|
|
|
|
+ int endCol = getColumn(endSlot);
|
|
|
|
|
+
|
|
|
|
|
+ for (int row = startRow; row <= endRow; row++) {
|
|
|
|
|
+ for (int col = startCol; col <= endCol; col++) {
|
|
|
|
|
+ int slot = getSlot(row, col);
|
|
|
|
|
+ setEditable(slot);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Sets an item and assigns a click action for that slot.
|
|
|
|
|
+ */
|
|
|
|
|
+ public void setItemWithClickAction(int slot, ItemStack item, BiConsumer<Player, ClickType> action) {
|
|
|
|
|
+ inventory.setItem(slot, item);
|
|
|
|
|
+ slotActions.put(slot, action);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Fills all empty slots with a background item.
|
|
|
|
|
+ */
|
|
|
|
|
+ public void fillGlassPaneBackground(){
|
|
|
|
|
+ ItemStack background = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
|
|
|
|
|
+ ItemMeta meta = background.getItemMeta();
|
|
|
|
|
+ meta.displayName(Component.text(" "));
|
|
|
|
|
+ meta.setHideTooltip(true);
|
|
|
|
|
+ background.setItemMeta(meta);
|
|
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
|
|
+ if (inventory.getItem(i) == null) {
|
|
|
|
|
+ inventory.setItem(i, background);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ public void fillBackground(Material material, String displayName) {
|
|
|
|
|
+ ItemStack background = new ItemStack(material);
|
|
|
|
|
+ ItemMeta meta = background.getItemMeta();
|
|
|
|
|
+ meta.displayName(Component.text(displayName));
|
|
|
|
|
+ background.setItemMeta(meta);
|
|
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
|
|
+ if (inventory.getItem(i) == null) {
|
|
|
|
|
+ inventory.setItem(i, background);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Quick way to open this GUI with the correct Listeners, so it actually works
|
|
|
|
|
+ * @param player The player to open the Inventory for
|
|
|
|
|
+ */
|
|
|
|
|
+ public void openWithListener(Player player){
|
|
|
|
|
+ InventoryManager.openFor(player, this);
|
|
|
|
|
+ }
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Opens this GUI for a player.
|
|
|
|
|
+ * <br>NOTE: This doesn't make the Inventory responsive by itself, since it only displays the container to the player.
|
|
|
|
|
+ * <br>for that, use {@code openWithListener(player);}
|
|
|
|
|
+ */
|
|
|
|
|
+ public void open(Player player) {
|
|
|
|
|
+ buildContent();
|
|
|
|
|
+ player.openInventory(inventory);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Opens another GUI and remembers this one for navigation.
|
|
|
|
|
+ */
|
|
|
|
|
+ public void openNext(Player player, InventoryGUI nextGui) {
|
|
|
|
|
+ nextGui.previousGuis.push(this);
|
|
|
|
|
+ InventoryManager.openFor(player, nextGui);
|
|
|
|
|
+ }
|
|
|
|
|
+ public boolean hasPreviousGUI() {
|
|
|
|
|
+ return !previousGuis.isEmpty();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Opens the previous GUI if available.
|
|
|
|
|
+ */
|
|
|
|
|
+ public void openPrevious(Player player) {
|
|
|
|
|
+ if (hasPreviousGUI()) {
|
|
|
|
|
+ InventoryGUI previousGui = previousGuis.pop();
|
|
|
|
|
+ previousGui.update();
|
|
|
|
|
+ InventoryManager.openFor(player, previousGui);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ public void setNextPageButton(int slot, InventoryGUI gui){
|
|
|
|
|
+ ItemStack item = new ItemStack(Material.ARROW);
|
|
|
|
|
+ ItemMeta meta = item.getItemMeta();
|
|
|
|
|
+ meta.displayName(Component.text("Next Page", NamedTextColor.GREEN));
|
|
|
|
|
+ item.setItemMeta(UnItalic.removeItalicFromMeta(meta));
|
|
|
|
|
+ setItemWithClickAction(slot, item, (p, type) -> {
|
|
|
|
|
+ openNext(p, gui);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setBackButton(int slot){
|
|
|
|
|
+ ItemStack item = new ItemStack(Material.ARROW);
|
|
|
|
|
+ ItemMeta meta = item.getItemMeta();
|
|
|
|
|
+ meta.displayName(Component.text("Go Back", NamedTextColor.GREEN));
|
|
|
|
|
+ item.setItemMeta(UnItalic.removeItalicFromMeta(meta));
|
|
|
|
|
+ setItemWithClickAction(slot, item, (p, type) -> {
|
|
|
|
|
+ openPrevious(p);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ public void setCloseButton(int slot){
|
|
|
|
|
+ ItemStack item = new ItemStack(Material.BARRIER);
|
|
|
|
|
+ ItemMeta meta = item.getItemMeta();
|
|
|
|
|
+ meta.displayName(Component.text("Close", NamedTextColor.RED));
|
|
|
|
|
+ item.setItemMeta(UnItalic.removeItalicFromMeta(meta));
|
|
|
|
|
+ setItemWithClickAction(slot, item, (p, type) -> {
|
|
|
|
|
+ InventoryManager.close(p.getUniqueId());
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void openPrevious(Player player, ClickType type) {
|
|
|
|
|
+ openPrevious(player);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public @NotNull Inventory getInventory() {
|
|
|
|
|
+ return inventory;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Builds the Content of the Inventory, called everytime a player is shown the GUI
|
|
|
|
|
+ */
|
|
|
|
|
+ @ApiStatus.OverrideOnly
|
|
|
|
|
+ public abstract void buildContent();
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Usually only called when traversing through multiple GUIs
|
|
|
|
|
+ */
|
|
|
|
|
+ @ApiStatus.OverrideOnly
|
|
|
|
|
+ public abstract void update();
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * This interface can be implemented as well to handle cases where the player closes the inventory
|
|
|
|
|
+ * <br>You can e.g. stop this behavior etc.
|
|
|
|
|
+ */
|
|
|
|
|
+ @FunctionalInterface
|
|
|
|
|
+ public interface AutoCloseHandler {
|
|
|
|
|
+ void onClosedByPlayer(Player player);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|