package de.themoep.inventorygui;

/*
 * Copyright 2017 Max Lee (https://github.com/Phoenix616/)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Mozilla Public License as published by
 * the Mozilla Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Mozilla Public License v2.0 for more details.
 *
 * You should have received a copy of the Mozilla Public License v2.0
 * along with this program. If not, see <http://mozilla.org/MPL/2.0/>.
 */

import org.bukkit.inventory.ItemStack;

/**
 * This is an element that allows for controlling the pagination of the gui.
 * <b>Untested und potentially unfinished.</b>
 */
public class GuiPageElement extends GuiStaticElement {
    private PageAction pageAction;

    /**
     * An element that allows for controlling the pagination of the gui.
     * @param slotChar      The character to replace in the gui setup string
     * @param item          The {@link ItemStack} representing this element
     * @param pageAction    What kind of page action you want to happen when interacting with the element.
     * @param text          The text to display on this element, placeholders are automatically
     *                      replaced, see {@link InventoryGui#replaceVars(String)} for a list of
     *                      the placeholder variables. Empty text strings are also filter out, use
     *                      a single space if you want to add an empty line!<br>
     *                      If it's not set/empty the item's default name will be used
     */
    public GuiPageElement(char slotChar, ItemStack item, PageAction pageAction, String... text) {
        super(slotChar, item, click -> {
            switch (pageAction) {
                case NEXT:
                    if (click.getGui().getPageNumber() + 1 < click.getGui().getPageAmount()) {
                        click.getGui().setPageNumber(click.getGui().getPageNumber() + 1);
                    }
                    break;
                case PREVIOUS:
                    if (click.getGui().getPageNumber() > 0) {
                        click.getGui().setPageNumber(click.getGui().getPageNumber() - 1);
                    }
                    break;
                case FIRST:
                    click.getGui().setPageNumber(0);
                    break;
                case LAST:
                    click.getGui().setPageNumber(click.getGui().getPageAmount() - 1);
                    break;
            }
            click.getGui().playClickSound();
            return true;
        }, text);
        this.pageAction = pageAction;
    }

    @Override
    public ItemStack getItem(int slot) {
        if ((pageAction == PageAction.NEXT && gui.getPageNumber() + 1 >= gui.getPageAmount())
                || (pageAction == PageAction.PREVIOUS && gui.getPageNumber() == 0)) {
            return gui.getFiller().getItem(slot);
        }
        try {
            if (pageAction == PageAction.PREVIOUS) {
                setNumber(gui.getPageNumber());
            } else if (pageAction == PageAction.NEXT) {
                setNumber(gui.getPageNumber() + 2);
            } else if (pageAction == PageAction.LAST) {
                setNumber(gui.getPageAmount() + 1);
            }
        } catch (IllegalArgumentException e) {
            // cannot set that item amount/number as it isn't supported in Minecraft
        }
        return super.getItem(slot).clone();
    }

    public enum PageAction {
        NEXT,
        PREVIOUS,
        FIRST,
        LAST;
    }
}

@@ -2031,24 +2031,81 @@
 Amount()) %7B%0A
+                        click.getGui().playClickSound();%0A
             
@@ -2174,32 +2174,32 @@
 eNumber() + 1);%0A
-
                 
@@ -2320,24 +2320,81 @@
 er() %3E 0) %7B%0A
+                        click.getGui().playClickSound();%0A
             
@@ -2544,24 +2544,77 @@
 case FIRST:%0A
+                    click.getGui().playClickSound();%0A
             
@@ -2704,24 +2704,77 @@
  case LAST:%0A
+                    click.getGui().playClickSound();%0A
             
@@ -2866,32 +2866,32 @@
          break;%0A
+
             %7D%0A  
@@ -2892,53 +2892,8 @@
   %7D%0A
-            click.getGui().playClickSound();%0A
     

Fix sound getting played even when no page action is performed