From e1f6cc8b47b4c9cf1f891a6c079a0470b7dd29bc Mon Sep 17 00:00:00 2001 From: BONNe1704 Date: Thu, 17 Jan 2019 13:16:42 +0200 Subject: [PATCH] Create ConfirmationGUI class that creates GUI with 2 buttons. On Cancel it should return to previous GUI. On Accept it should process given command. If command does not exist, it should throw error message in chat. At the end it should return to previous GUI. --- .../panel/util/ConfirmationGUI.java | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 src/main/java/world/bentobox/challenges/panel/util/ConfirmationGUI.java diff --git a/src/main/java/world/bentobox/challenges/panel/util/ConfirmationGUI.java b/src/main/java/world/bentobox/challenges/panel/util/ConfirmationGUI.java new file mode 100644 index 0000000..9ba7ac1 --- /dev/null +++ b/src/main/java/world/bentobox/challenges/panel/util/ConfirmationGUI.java @@ -0,0 +1,138 @@ +package world.bentobox.challenges.panel.util; + + + + +import org.bukkit.Material; + +import java.util.*; + +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.api.commands.CompositeCommand; +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.panel.CommonGUI; + + +/** + * This GUI is used to confirm that user wants to run command, that should be created from + * command string list. + */ +public class ConfirmationGUI +{ + /** + * This constructor inits and opens ConfirmationGUI. + * @param user Gui Caller. + * @param parentGUI Parent GUI. + * @param commandLabels Command labels. + * @param variables Variables at the end of command. + */ + public ConfirmationGUI(BentoBox plugin, + User user, + CommonGUI parentGUI, + List commandLabels, + String... variables) + { + this.plugin = plugin; + this.user = user; + this.parentGUI = parentGUI; + this.commandLabels = commandLabels; + this.variables = variables; + + if (this.commandLabels.isEmpty()) + { + this.user.sendMessage("challenges.errors.missing-command"); + } + else + { + this.build(); + } + } + + + /** + * This method builds confirmation panel with 2 buttons. + */ + public void build() + { + PanelBuilder panelBuilder = new PanelBuilder().name(this.user.getTranslation("challenges.gui.admin.confirm-title")); + + panelBuilder.item(3, new PanelItemBuilder(). + name(this.user.getTranslation("challenges.gui.admin.buttons.proceed")). + icon(Material.GREEN_STAINED_GLASS_PANE). + clickHandler((panel, user1, clickType, index) -> + { + Iterator iterator = this.commandLabels.iterator(); + + CompositeCommand command = this.plugin.getCommandsManager().getCommand(iterator.next()); + + while (iterator.hasNext() && command != null) + { + Optional commandOptional = command.getSubCommand(iterator.next()); + + if (commandOptional.isPresent()) + { + command = commandOptional.get(); + } + else + { + this.user.sendMessage("challenges.errors.missing-command"); + command = null; + } + } + + if (command != null) + { + command.execute(this.user, "CONFIRMATION", Arrays.asList(this.variables)); + } + + this.user.closeInventory(); + this.parentGUI.build(); + return true; + }). + build()); + + panelBuilder.item(5, new PanelItemBuilder(). + name(this.user.getTranslation("challenges.gui.admin.buttons.cancel")). + icon(Material.RED_STAINED_GLASS_PANE). + clickHandler((panel, user1, clickType, i) -> + { + this.parentGUI.build(); + return true; + }). + build()); + + panelBuilder.build(); + } + + +// --------------------------------------------------------------------- +// Section: Variables +// --------------------------------------------------------------------- + + /** + * BentoBox plugin. + */ + private BentoBox plugin; + + /** + * User who wants to run command. + */ + private User user; + + /** + * Parent GUI where should return on cancel or proceed. + */ + private CommonGUI parentGUI; + + /** + * List of command labels. + */ + private List commandLabels; + + /** + * List of variables. + */ + private String[] variables; +}