From 507c68d68302251b72294ed77ee2800e5057227c Mon Sep 17 00:00:00 2001 From: BONNe Date: Fri, 26 Apr 2019 20:59:52 +0300 Subject: [PATCH] Add ability to edit string messages from StringListGUI via Chat. --- .../challenges/panel/util/StringListGUI.java | 154 ++++++++++++++++-- src/main/resources/locales/en-US.yml | 5 + 2 files changed, 142 insertions(+), 17 deletions(-) diff --git a/src/main/java/world/bentobox/challenges/panel/util/StringListGUI.java b/src/main/java/world/bentobox/challenges/panel/util/StringListGUI.java index 6f34299..2e61681 100644 --- a/src/main/java/world/bentobox/challenges/panel/util/StringListGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/util/StringListGUI.java @@ -2,13 +2,19 @@ package world.bentobox.challenges.panel.util; import org.bukkit.Material; +import org.bukkit.conversations.*; import org.bukkit.inventory.ItemStack; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.function.BiConsumer; +import java.util.function.Consumer; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.TextComponent; import net.wesjd.anvilgui.AnvilGUI; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.panels.PanelItem; @@ -66,6 +72,8 @@ public class StringListGUI panelBuilder.item(5, this.getButton(Button.REMOVE)); panelBuilder.item(6, this.getButton(Button.CLEAR)); + panelBuilder.item(8, this.getButton(Button.MODE)); + panelBuilder.item(44, this.getButton(Button.CANCEL)); int slot = 10; @@ -139,14 +147,25 @@ public class StringListGUI description = Collections.emptyList(); icon = new ItemStack(Material.WHITE_STAINED_GLASS_PANE); clickHandler = (panel, user, clickType, slot) -> { - new AnvilGUI(BentoBox.getInstance(), - this.user.getPlayer(), - " ", - (player, reply) -> { - this.value.add(reply); - this.build(); - return reply; - }); + + if (this.useAnvil) + { + new AnvilGUI(BentoBox.getInstance(), + this.user.getPlayer(), + " ", + (player, reply) -> { + this.value.add(reply); + this.build(); + return reply; + }); + } + else + { + this.startConversion(value -> + this.value.add(value), + this.user.getTranslation("challenges.gui.descriptions.admin.add-text-line")); + } + return true; }; break; @@ -176,6 +195,18 @@ public class StringListGUI }; break; } + case MODE: + { + name = this.user.getTranslation("challenges.gui.buttons.admin.input-mode"); + description = Collections.singletonList(this.user.getTranslation("challenges.gui.descriptions.admin.input-mode")); + icon = this.useAnvil ? new ItemStack(Material.ANVIL) : new ItemStack(Material.MAP); + clickHandler = (panel, user, clickType, slot) -> { + this.useAnvil = !this.useAnvil; + panel.getInventory().setItem(slot, this.getButton(button).getItem()); + return true; + }; + break; + } default: return null; } @@ -201,19 +232,102 @@ public class StringListGUI name(element). icon(Material.PAPER). clickHandler((panel, user1, clickType, i) -> { - new AnvilGUI(BentoBox.getInstance(), - this.user.getPlayer(), - element, - (player, reply) -> { - this.value.set(stringIndex, reply); - this.build(); - return reply; - }); + + if (this.useAnvil) + { + new AnvilGUI(BentoBox.getInstance(), + this.user.getPlayer(), + element, + (player, reply) -> { + this.value.set(stringIndex, reply); + this.build(); + return reply; + }); + } + else + { + this.startConversion( + value -> this.value.set(stringIndex, value), + this.user.getTranslation("challenges.gui.descriptions.admin.edit-text-line"), + element); + } + return true; }).build(); } + /** + * This method will close opened gui and writes inputText in chat. After players answers on inputText in + * chat, message will trigger consumer and gui will reopen. + * @param consumer Consumer that accepts player output text. + * @param question Message that will be displayed in chat when player triggers conversion. + */ + private void startConversion(Consumer consumer, @NonNull String question) + { + this.startConversion(consumer, question, null); + } + + + /** + * This method will close opened gui and writes inputText in chat. After players answers on inputText in + * chat, message will trigger consumer and gui will reopen. + * @param consumer Consumer that accepts player output text. + * @param question Message that will be displayed in chat when player triggers conversion. + * @param message Message that will be set in player text field when clicked on question. + */ + private void startConversion(Consumer consumer, @NonNull String question, @Nullable String message) + { + final User user = this.user; + + Conversation conversation = + new ConversationFactory(BentoBox.getInstance()).withFirstPrompt( + new StringPrompt() + { + /** + * @see Prompt#getPromptText(ConversationContext) + */ + @Override + public String getPromptText(ConversationContext conversationContext) + { + // Close input GUI. + user.closeInventory(); + + if (message != null) + { + // Create Edit Text message. + TextComponent component = new TextComponent(user.getTranslation("challenges.gui.descriptions.admin.click-to-edit")); + component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message)); + // Send question and message to player. + user.getPlayer().spigot().sendMessage(component); + } + + // There are no editable message. Just return question. + return question; + } + + + /** + * @see Prompt#acceptInput(ConversationContext, String) + */ + @Override + public Prompt acceptInput(ConversationContext conversationContext, String answer) + { + // Add answer to consumer. + consumer.accept(answer); + // Reopen GUI + StringListGUI.this.build(); + // End conversation + return Prompt.END_OF_CONVERSATION; + } + }). + withLocalEcho(false). + buildConversation(user.getPlayer()); + + conversation.begin(); + } + + // --------------------------------------------------------------------- // Section: Enums // --------------------------------------------------------------------- @@ -229,7 +343,8 @@ public class StringListGUI REMOVE, CANCEL, CLEAR, - SAVE + SAVE, + MODE } @@ -248,6 +363,11 @@ public class StringListGUI */ private User user; + /** + * Boolean that indicate if editing should happen in anvil. + */ + private boolean useAnvil; + /** * Current value. */ diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 4840883..36344ca 100755 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -141,6 +141,7 @@ challenges: history-lifespan: 'History LifeSpan' island-store: 'Store per Island' default-locked-icon: 'Locked Level Icon' + input-mode: 'Switch input mode' next: 'Next' previous: 'Previous' return: 'Return' @@ -228,6 +229,10 @@ challenges: island-store: 'Allows to enable/disable challenges data string per island. This means that challenges will be the same on whole team, if this is enabled.|Will NOT convert data on click. PROGRESS WILL BE LOST.' default-locked-icon: 'Allows to change default locked level icon.|This option can be overwritten by each level.' gui-mode: 'Allows to enable/disable single challenges GUI.|&2Requires server restart.' + click-to-edit: '&4Click here to edit input.' + edit-text-line: '&6 Edit text message!' + add-text-line: '&6 Add new text message!' + input-mode: 'Switch between chat and anvil input modes.' current-value: '|&6Current value: [value].' enabled: 'Active' disabled: 'Disabled'