diff --git a/src/main/java/world/bentobox/challenges/panel/util/SelectChallengeGUI.java b/src/main/java/world/bentobox/challenges/panel/util/SelectChallengeGUI.java new file mode 100644 index 0000000..f7563e2 --- /dev/null +++ b/src/main/java/world/bentobox/challenges/panel/util/SelectChallengeGUI.java @@ -0,0 +1,138 @@ +package world.bentobox.challenges.panel.util; + + +import org.bukkit.Material; +import java.util.List; +import java.util.function.BiConsumer; + +import world.bentobox.bentobox.api.panels.PanelItem; +import world.bentobox.bentobox.api.panels.builders.PanelBuilder; +import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.challenges.database.object.Challenges; + + +/** + * This class creates new GUI that allows to select single challenge, which is returned via consumer. + */ +public class SelectChallengeGUI +{ + public SelectChallengeGUI(User user, List challengesList, BiConsumer consumer) + { + this.consumer = consumer; + this.user = user; + this.challengesList = challengesList; + + this.build(0); + } + + + /** + * This method builds panel that allows to select single challenge from input challenges. + */ + private void build(int pageIndex) + { + PanelBuilder panelBuilder = new PanelBuilder().user(this.user).name(this.user.getTranslation("challenges.gui.choose-challenge-title")); + + // Maximal elements in page. + final int MAX_ELEMENTS = 36; + + final int correctPage; + + if (pageIndex < 0) + { + correctPage = this.challengesList.size() / MAX_ELEMENTS; + } + else if (pageIndex > (this.challengesList.size() / MAX_ELEMENTS)) + { + correctPage = 0; + } + else + { + correctPage = pageIndex; + } + + // Navigation buttons + + panelBuilder.item(3, + new PanelItemBuilder(). + icon(Material.SIGN). + name(this.user.getTranslation("challenges.gui.buttons.previous")). + clickHandler( (panel, user1, clickType, slot) -> { + this.build(correctPage - 1); + return true; + }).build()); + + panelBuilder.item(4, + new PanelItemBuilder(). + icon(Material.OAK_DOOR). + name(this.user.getTranslation("challenges.gui.buttons.return")). + clickHandler( (panel, user1, clickType, slot) -> { + this.consumer.accept(false, null); + return true; + }).build()); + + panelBuilder.item(5, + new PanelItemBuilder(). + icon(Material.SIGN). + name(this.user.getTranslation("challenges.gui.buttons.next")). + clickHandler( (panel, user1, clickType, slot) -> { + this.build(correctPage + 1); + return true; + }).build()); + + int challengesIndex = MAX_ELEMENTS * correctPage; + + // I want first row to be only for navigation and return button. + int index = 9; + + while (challengesIndex < ((correctPage + 1) * MAX_ELEMENTS) && + challengesIndex < this.challengesList.size()) + { + panelBuilder.item(index++, this.createChallengeButton(this.challengesList.get(challengesIndex++))); + } + + panelBuilder.build(); + + panelBuilder.build(); + } + + + /** + * This method builds PanelItem for given challenge. + * @param challenge Challenge which PanelItem must be created. + * @return new PanelItem for given Challenge. + */ + private PanelItem createChallengeButton(Challenges challenge) + { + return new PanelItemBuilder(). + name(challenge.getFriendlyName()). + description(challenge.getDescription()). + icon(challenge.getIcon()). + clickHandler((panel, user1, clickType, slot) -> { + this.consumer.accept(true, challenge); + return true; + }).build(); + } + + +// --------------------------------------------------------------------- +// Section: Variables +// --------------------------------------------------------------------- + + + /** + * This variable stores consumer. + */ + private BiConsumer consumer; + + /** + * User who runs GUI. + */ + private User user; + + /** + * Current value. + */ + private List challengesList; +}