From c516d53907e1691020f59525b609274a89a4c2ae Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 25 Aug 2019 22:43:15 +0300 Subject: [PATCH] Update to v1.6.0 version. Remove AnvilGUI, as it is broken. Replaced with Spigot Conversation API. Remove lore-config from GUI (broken). Remove RIGHT_CLICK to complete multiple times (broken). --- pom.xml | 12 +- .../bentobox/challenges/panel/CommonGUI.java | 70 +++++++ .../challenges/panel/admin/AdminGUI.java | 189 ++++++++++++++---- .../panel/admin/EditChallengeGUI.java | 46 ++--- .../challenges/panel/admin/EditLevelGUI.java | 78 +++----- .../panel/admin/EditSettingsGUI.java | 62 +++--- .../challenges/panel/user/ChallengesGUI.java | 59 +++--- .../challenges/panel/util/NumberGUI.java | 151 +++++++++++--- .../panel/util/SelectBlocksGUI.java | 17 +- .../challenges/panel/util/StringListGUI.java | 75 ++----- src/main/resources/locales/en-US.yml | 10 + src/main/resources/locales/es-ES.yml | 9 + src/main/resources/locales/fr-FR.yml | 9 + src/main/resources/locales/lv-LV.yml | 9 + src/main/resources/locales/zh-CN.yml | 10 + src/main/resources/locales/zh-TW.yml | 9 + 16 files changed, 522 insertions(+), 293 deletions(-) diff --git a/pom.xml b/pom.xml index df52e7a..3086e5e 100644 --- a/pom.xml +++ b/pom.xml @@ -36,10 +36,9 @@ 1.7.4 1.13.2-R0.1-SNAPSHOT - 1.6.0-SNAPSHOT + 1.6.0 1.5.0 1.7 - 1.2.2-SNAPSHOT ${build.version}-SNAPSHOT @@ -115,10 +114,6 @@ jitpack.io https://jitpack.io - - wesjd-repo - https://nexus.wesjd.net/repository/thirdparty/ - @@ -170,11 +165,6 @@ ${vault.version} provided - - net.wesjd - anvilgui - ${anvilgui.version} - diff --git a/src/main/java/world/bentobox/challenges/panel/CommonGUI.java b/src/main/java/world/bentobox/challenges/panel/CommonGUI.java index 3e663ce..7830b45 100644 --- a/src/main/java/world/bentobox/challenges/panel/CommonGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/CommonGUI.java @@ -5,9 +5,11 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.function.Consumer; import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.conversations.*; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -21,7 +23,12 @@ import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.inventory.meta.SpawnEggMeta; import org.bukkit.inventory.meta.TropicalFishBucketMeta; import org.bukkit.potion.PotionData; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.TextComponent; +import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; import world.bentobox.bentobox.api.user.User; @@ -917,5 +924,68 @@ public abstract class CommonGUI return result; } + + +// --------------------------------------------------------------------- +// Section: Chat Input Methods +// --------------------------------------------------------------------- + + + /** + * 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. + */ + protected void getFriendlyName(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); + // End conversation + return Prompt.END_OF_CONVERSATION; + } + }). + withLocalEcho(false). + withPrefix(context -> user.getTranslation("challenges.gui.questions.prefix")). + buildConversation(user.getPlayer()); + + conversation.begin(); + } } diff --git a/src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java b/src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java index 52b3ef5..0971efb 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java @@ -3,13 +3,19 @@ package world.bentobox.challenges.panel.admin; import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.conversations.*; import org.bukkit.inventory.ItemStack; +import org.eclipse.jdt.annotation.NonNull; -import net.wesjd.anvilgui.AnvilGUI; +import java.util.function.Consumer; +import java.util.function.Function; + +import world.bentobox.bentobox.BentoBox; 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.bentobox.util.Util; import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.panel.CommonGUI; import world.bentobox.challenges.panel.util.ConfirmationGUI; @@ -208,29 +214,24 @@ public class AdminGUI extends CommonGUI description = this.user.getTranslation("challenges.gui.descriptions.admin.create-challenge"); icon = new ItemStack(Material.BOOK); clickHandler = (panel, user, clickType, slot) -> { - new AnvilGUI(this.addon.getPlugin(), - this.user.getPlayer(), - "unique_id", - (player, reply) -> { - String newName = Utils.getGameMode(this.world) + "_" + reply; - if (!this.addon.getChallengesManager().containsChallenge(newName)) - { - new EditChallengeGUI(this.addon, - this.world, - this.user, - this.addon.getChallengesManager().createChallenge(newName), - this.topLabel, - this.permissionPrefix, - this).build(); - } - else - { - this.user.sendMessage("challenges.errors.unique-id", "[id]", reply); - } + this.getNewUniqueID(challenge -> { + String newName = Utils.getGameMode(this.world) + "_" + challenge; - return reply; - }); + new EditChallengeGUI(this.addon, + this.world, + this.user, + this.addon.getChallengesManager().createChallenge(newName), + this.topLabel, + this.permissionPrefix, + this).build(); + }, + input -> { + String newName = Utils.getGameMode(this.world) + "_" + input; + return !this.addon.getChallengesManager().containsChallenge(newName); + }, + this.user.getTranslation("challenges.question.admin.unique-id") + ); return true; }; @@ -244,29 +245,24 @@ public class AdminGUI extends CommonGUI description = this.user.getTranslation("challenges.gui.descriptions.admin.create-level"); icon = new ItemStack(Material.BOOK); clickHandler = (panel, user, clickType, slot) -> { - new AnvilGUI(this.addon.getPlugin(), - this.user.getPlayer(), - "unique_id", - (player, reply) -> { - String newName = Utils.getGameMode(this.world) + "_" + reply; - if (!this.addon.getChallengesManager().containsLevel(newName)) - { - new EditLevelGUI(this.addon, - this.world, - this.user, - this.addon.getChallengesManager().createLevel(newName, this.world), - this.topLabel, - this.permissionPrefix, - this).build(); - } - else - { - this.user.sendMessage("challenges.errors.unique-id", "[id]", reply); - } + this.getNewUniqueID(level -> { + String newName = Utils.getGameMode(this.world) + "_" + level; - return reply; - }); + new EditLevelGUI(this.addon, + this.world, + this.user, + this.addon.getChallengesManager().createLevel(newName, this.world), + this.topLabel, + this.permissionPrefix, + this).build(); + }, + input -> { + String newName = Utils.getGameMode(this.world) + "_" + input; + return !this.addon.getChallengesManager().containsLevel(newName); + }, + this.user.getTranslation("challenges.question.admin.unique-id") + ); return true; }; @@ -496,4 +492,111 @@ public class AdminGUI extends CommonGUI clickHandler(clickHandler). build(); } + + +// --------------------------------------------------------------------- +// Section: Conversation +// --------------------------------------------------------------------- + + + /** + * 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 getNewUniqueID(Consumer consumer, + Function stringValidation, + @NonNull String question) + { + final User user = this.user; + + Conversation conversation = + new ConversationFactory(BentoBox.getInstance()).withFirstPrompt( + new ValidatingPrompt() + { + + /** + * Gets the text to display to the user when + * this prompt is first presented. + * + * @param context Context information about the + * conversation. + * @return The text to display. + */ + @Override + public String getPromptText(ConversationContext context) + { + // Close input GUI. + user.closeInventory(); + + // There are no editable message. Just return question. + return question; + } + + + /** + * Override this method to check the validity of + * the player's input. + * + * @param context Context information about the + * conversation. + * @param input The player's raw console input. + * @return True or false depending on the + * validity of the input. + */ + @Override + protected boolean isInputValid(ConversationContext context, String input) + { + return stringValidation.apply(input); + } + + + /** + * Optionally override this method to + * display an additional message if the + * user enters an invalid input. + * + * @param context Context information + * about the conversation. + * @param invalidInput The invalid input + * provided by the user. + * @return A message explaining how to + * correct the input. + */ + @Override + protected String getFailedValidationText(ConversationContext context, + String invalidInput) + { + return user.getTranslation("challenges.errors.unique-id", "[id]", invalidInput); + } + + + /** + * Override this method to accept and processes + * the validated input from the user. Using the + * input, the next Prompt in the prompt graph + * should be returned. + * + * @param context Context information about the + * conversation. + * @param input The validated input text from + * the user. + * @return The next Prompt in the prompt graph. + */ + @Override + protected Prompt acceptValidatedInput(ConversationContext context, String input) + { + // Add answer to consumer. + consumer.accept(input); + // End conversation + return Prompt.END_OF_CONVERSATION; + } + }). + withLocalEcho(false). + withPrefix(context -> user.getTranslation("challenges.gui.questions.prefix")). + buildConversation(user.getPlayer()); + + conversation.begin(); + } } \ No newline at end of file diff --git a/src/main/java/world/bentobox/challenges/panel/admin/EditChallengeGUI.java b/src/main/java/world/bentobox/challenges/panel/admin/EditChallengeGUI.java index 72dac51..3a3f2c7 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/EditChallengeGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/EditChallengeGUI.java @@ -8,7 +8,6 @@ import org.bukkit.inventory.ItemStack; import java.util.*; import java.util.stream.Collectors; -import net.wesjd.anvilgui.AnvilGUI; import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; @@ -16,10 +15,7 @@ import world.bentobox.bentobox.api.user.User; import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.database.object.Challenge; import world.bentobox.challenges.panel.CommonGUI; -import world.bentobox.challenges.panel.util.ItemSwitchGUI; -import world.bentobox.challenges.panel.util.NumberGUI; -import world.bentobox.challenges.panel.util.SelectEnvironmentGUI; -import world.bentobox.challenges.panel.util.StringListGUI; +import world.bentobox.challenges.panel.util.*; import world.bentobox.challenges.utils.GuiUtils; import world.bentobox.challenges.utils.Utils; @@ -406,24 +402,16 @@ public class EditChallengeGUI extends CommonGUI "challenges.gui.descriptions.admin.icon-challenge")); icon = this.challenge.getIcon(); clickHandler = (panel, user, clickType, slot) -> { - new AnvilGUI(this.addon.getPlugin(), - this.user.getPlayer(), - this.challenge.getIcon().getType().name(), - (player, reply) -> { - Material material = Material.getMaterial(reply); - if (material != null) - { - this.challenge.setIcon(new ItemStack(material)); - this.build(); - } - else - { - this.user.sendMessage("challenges.errors.wrong-icon", "[value]", reply); - } + new SelectBlocksGUI(this.user, true, (status, materials) -> { + if (status) + { + materials.forEach(material -> + this.challenge.setIcon(new ItemStack(material))); + } - return reply; - }); + this.build(); + }); return true; }; @@ -543,14 +531,14 @@ public class EditChallengeGUI extends CommonGUI icon = new ItemStack(Material.DROPPER); clickHandler = (panel, user, clickType, slot) -> { - new AnvilGUI(this.addon.getPlugin(), - this.user.getPlayer(), - this.challenge.getFriendlyName(), - (player, reply) -> { - this.challenge.setFriendlyName(reply); - this.build(); - return reply; - }); + + this.getFriendlyName(reply -> { + this.challenge.setFriendlyName(reply); + this.build(); + }, + this.user.getTranslation("challenges.gui.questions.admin.challenge-name"), + this.challenge.getFriendlyName() + ); return true; }; diff --git a/src/main/java/world/bentobox/challenges/panel/admin/EditLevelGUI.java b/src/main/java/world/bentobox/challenges/panel/admin/EditLevelGUI.java index 2c8df01..fdc5eee 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/EditLevelGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/EditLevelGUI.java @@ -13,7 +13,6 @@ import org.bukkit.Material; import org.bukkit.World; import org.bukkit.inventory.ItemStack; -import net.wesjd.anvilgui.AnvilGUI; import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; @@ -23,10 +22,7 @@ import world.bentobox.challenges.ChallengesManager; import world.bentobox.challenges.database.object.Challenge; import world.bentobox.challenges.database.object.ChallengeLevel; import world.bentobox.challenges.panel.CommonGUI; -import world.bentobox.challenges.panel.util.ItemSwitchGUI; -import world.bentobox.challenges.panel.util.NumberGUI; -import world.bentobox.challenges.panel.util.SelectChallengeGUI; -import world.bentobox.challenges.panel.util.StringListGUI; +import world.bentobox.challenges.panel.util.*; import world.bentobox.challenges.utils.GuiUtils; import world.bentobox.challenges.utils.Utils; @@ -337,14 +333,14 @@ public class EditLevelGUI extends CommonGUI "[value]", this.challengeLevel.getFriendlyName())); icon = new ItemStack(Material.DROPPER); clickHandler = (panel, user, clickType, slot) -> { - new AnvilGUI(this.addon.getPlugin(), - this.user.getPlayer(), - this.challengeLevel.getFriendlyName(), - (player, reply) -> { - this.challengeLevel.setFriendlyName(reply); - this.build(); - return reply; - }); + + this.getFriendlyName(reply -> { + this.challengeLevel.setFriendlyName(reply); + this.build(); + }, + this.user.getTranslation("challenges.gui.questions.admin.level-name"), + this.challengeLevel.getFriendlyName() + ); return true; }; @@ -358,24 +354,16 @@ public class EditLevelGUI extends CommonGUI "challenges.gui.descriptions.admin.icon-level")); icon = this.challengeLevel.getIcon(); clickHandler = (panel, user, clickType, slot) -> { - new AnvilGUI(this.addon.getPlugin(), - this.user.getPlayer(), - this.challengeLevel.getIcon().getType().name(), - (player, reply) -> { - Material material = Material.getMaterial(reply); - if (material != null) - { - this.challengeLevel.setIcon(new ItemStack(material)); - this.build(); - } - else - { - this.user.sendMessage("challenges.errors.wrong-icon", "[value]", reply); - } + new SelectBlocksGUI(this.user, true, (status, materials) -> { + if (status) + { + materials.forEach(material -> + this.challengeLevel.setIcon(new ItemStack(material))); + } - return reply; - }); + this.build(); + }); return true; }; @@ -400,31 +388,15 @@ public class EditLevelGUI extends CommonGUI } clickHandler = (panel, user, clickType, slot) -> { - new AnvilGUI(this.addon.getPlugin(), - this.user.getPlayer(), - isNull ? "NULL" : icon.getType().name(), - (player, reply) -> { - if (reply.equals("NULL")) - { - this.challengeLevel.setLockedIcon(null); - this.build(); - return reply; - } + new SelectBlocksGUI(this.user, true, (status, materials) -> { + if (status) + { + materials.forEach(material -> + this.challengeLevel.setLockedIcon(new ItemStack(material))); + } - Material material = Material.getMaterial(reply); - - if (material != null) - { - this.challengeLevel.setLockedIcon(new ItemStack(material)); - this.build(); - } - else - { - this.user.sendMessage("challenges.errors.wrong-icon", "[value]", reply); - } - - return reply; - }); + this.build(); + }); return true; }; diff --git a/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsGUI.java b/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsGUI.java index 7ddbd3f..6132dbe 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsGUI.java @@ -8,7 +8,6 @@ import org.bukkit.Material; import org.bukkit.World; import org.bukkit.inventory.ItemStack; -import net.wesjd.anvilgui.AnvilGUI; import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; @@ -17,6 +16,7 @@ import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.Settings; import world.bentobox.challenges.panel.CommonGUI; import world.bentobox.challenges.panel.util.NumberGUI; +import world.bentobox.challenges.panel.util.SelectBlocksGUI; import world.bentobox.challenges.utils.GuiUtils; @@ -240,14 +240,16 @@ public class EditSettingsGUI extends CommonGUI name = this.user.getTranslation("challenges.gui.buttons.admin.level-lore"); icon = new ItemStack(Material.MAP); clickHandler = (panel, user1, clickType, i) -> { - new AnvilGUI(this.addon.getPlugin(), - this.user.getPlayer(), - this.settings.getLevelLoreMessage(), - (player, reply) -> { - this.settings.setLevelLoreMessage(reply); - panel.getInventory().setItem(i, this.getSettingsButton(button).getItem()); - return reply; - }); + + // TODO: AnvilGUI is out. Need to implement better GUI for editing this. +// new AnvilGUI(this.addon.getPlugin(), +// this.user.getPlayer(), +// this.settings.getLevelLoreMessage(), +// (player, reply) -> { +// this.settings.setLevelLoreMessage(reply); +// panel.getInventory().setItem(i, this.getSettingsButton(button).getItem()); +// return reply; +// }); return true; }; @@ -263,14 +265,16 @@ public class EditSettingsGUI extends CommonGUI name = this.user.getTranslation("challenges.gui.buttons.admin.challenge-lore"); icon = new ItemStack(Material.PAPER); clickHandler = (panel, user1, clickType, i) -> { - new AnvilGUI(this.addon.getPlugin(), - this.user.getPlayer(), - this.settings.getChallengeLoreMessage(), - (player, reply) -> { - this.settings.setChallengeLoreMessage(reply); - panel.getInventory().setItem(i, this.getSettingsButton(button).getItem()); - return reply; - }); + // TODO: AnvilGUI is out. Need to implement better GUI for editing this. + +// new AnvilGUI(this.addon.getPlugin(), +// this.user.getPlayer(), +// this.settings.getChallengeLoreMessage(), +// (player, reply) -> { +// this.settings.setChallengeLoreMessage(reply); +// panel.getInventory().setItem(i, this.getSettingsButton(button).getItem()); +// return reply; +// }); return true; }; @@ -441,24 +445,16 @@ public class EditSettingsGUI extends CommonGUI name = this.user.getTranslation("challenges.gui.buttons.admin.default-locked-icon"); icon = this.settings.getLockedLevelIcon(); clickHandler = (panel, user, clickType, slot) -> { - new AnvilGUI(this.addon.getPlugin(), - this.user.getPlayer(), - this.settings.getLockedLevelIcon().getType().name(), - (player, reply) -> { - Material material = Material.getMaterial(reply); - if (material != null) - { - this.settings.setLockedLevelIcon(new ItemStack(material)); - this.build(); - } - else - { - this.user.sendMessage("challenges.errors.wrong-icon", "[value]", reply); - } + new SelectBlocksGUI(this.user, true, (status, materials) -> { + if (status) + { + materials.forEach(material -> + this.settings.setLockedLevelIcon(new ItemStack(material))); + } - return reply; - }); + this.build(); + }); return true; }; diff --git a/src/main/java/world/bentobox/challenges/panel/user/ChallengesGUI.java b/src/main/java/world/bentobox/challenges/panel/user/ChallengesGUI.java index c9cbff9..103e73d 100644 --- a/src/main/java/world/bentobox/challenges/panel/user/ChallengesGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/user/ChallengesGUI.java @@ -2,12 +2,10 @@ package world.bentobox.challenges.panel.user; import org.bukkit.ChatColor; -import org.bukkit.Material; import org.bukkit.World; import org.bukkit.inventory.ItemStack; import java.util.List; -import net.wesjd.anvilgui.AnvilGUI; import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; @@ -360,34 +358,35 @@ public class ChallengesGUI extends CommonGUI // Add ability to input how many repeats player should do. // Do not open if challenge is not repeatable. - if (clickType.isRightClick() && challenge.isRepeatable()) - { - new AnvilGUI(this.addon.getPlugin(), - this.user.getPlayer(), - "1", - (player, reply) -> { - try - { - if (TryToComplete.complete(this.addon, - this.user, - challenge, - this.world, - this.topLabel, - this.permissionPrefix, - Integer.parseInt(reply))) - { - panel.getInventory().setItem(slot, this.getChallengeButton(challenge).getItem()); - } - } - catch (Exception e) - { - this.user.sendMessage("challenges.errors.not-a-integer", "[value]", reply); - } - - return reply; - }); - } - else + // TODO: AnvilGUI is removed. Need to use different input mode. +// if (clickType.isRightClick() && challenge.isRepeatable()) +// { +// new AnvilGUI(this.addon.getPlugin(), +// this.user.getPlayer(), +// "1", +// (player, reply) -> { +// try +// { +// if (TryToComplete.complete(this.addon, +// this.user, +// challenge, +// this.world, +// this.topLabel, +// this.permissionPrefix, +// Integer.parseInt(reply))) +// { +// panel.getInventory().setItem(slot, this.getChallengeButton(challenge).getItem()); +// } +// } +// catch (Exception e) +// { +// this.user.sendMessage("challenges.errors.not-a-integer", "[value]", reply); +// } +// +// return reply; +// }); +// } +// else { if (TryToComplete.complete(this.addon, this.user, diff --git a/src/main/java/world/bentobox/challenges/panel/util/NumberGUI.java b/src/main/java/world/bentobox/challenges/panel/util/NumberGUI.java index 6cae14d..5ae5d9b 100644 --- a/src/main/java/world/bentobox/challenges/panel/util/NumberGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/util/NumberGUI.java @@ -2,11 +2,12 @@ package world.bentobox.challenges.panel.util; import org.bukkit.Material; +import org.bukkit.conversations.*; import org.bukkit.inventory.ItemStack; -import java.util.Collections; +import org.eclipse.jdt.annotation.NonNull; import java.util.function.BiConsumer; +import java.util.function.Consumer; -import net.wesjd.anvilgui.AnvilGUI; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.builders.PanelBuilder; @@ -140,34 +141,12 @@ public class NumberGUI description = this.user.getTranslation("challenges.gui.descriptions.admin.input"); icon = new ItemStack(Material.ANVIL); clickHandler = (panel, user, clickType, slot) -> { - new AnvilGUI(BentoBox.getInstance(), - this.user.getPlayer(), - Integer.toString(this.value), - (player, reply) -> { - try - { - this.value = Integer.parseInt(reply); - if (this.value > this.maxValue || this.value < this.minValue) - { - this.user.sendMessage("challenges.errors.not-valid-integer", - "[value]", reply, - "[min]", Integer.toString(this.minValue), - "[max]", Integer.toString(this.maxValue)); - } - else - { - this.build(); - } - } - catch (Exception e) - { - reply = Integer.toString(this.value); - this.user.sendMessage("challenges.errors.not-a-integer", "[value]", reply); - } - - return reply; - }); + this.getNumberInput(number -> { + this.value = number.intValue(); + this.build(); + }, + this.user.getTranslation("challenges.gui.questions.admin.number")); return true; }; @@ -368,6 +347,120 @@ public class NumberGUI } +// --------------------------------------------------------------------- +// Section: Conversation +// --------------------------------------------------------------------- + + + /** + * 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 getNumberInput(Consumer consumer, @NonNull String question) + { + final User user = this.user; + + Conversation conversation = + new ConversationFactory(BentoBox.getInstance()).withFirstPrompt( + new NumericPrompt() + { + /** + * Override this method to perform some action + * with the user's integer response. + * + * @param context Context information about the + * conversation. + * @param input The user's response as a {@link + * Number}. + * @return The next {@link Prompt} in the prompt + * graph. + */ + @Override + protected Prompt acceptValidatedInput(ConversationContext context, Number input) + { + // Add answer to consumer. + consumer.accept(input); + // Reopen GUI + NumberGUI.this.build(); + // End conversation + return Prompt.END_OF_CONVERSATION; + } + + + /** + * Override this method to do further validation on the numeric player + * input after the input has been determined to actually be a number. + * + * @param context Context information about the conversation. + * @param input The number the player provided. + * @return The validity of the player's input. + */ + protected boolean isNumberValid(ConversationContext context, Number input) + { + return input.intValue() >= NumberGUI.this.minValue && + input.intValue() <= NumberGUI.this.maxValue; + } + + + /** + * Optionally override this method to display an additional message if the + * user enters an invalid number. + * + * @param context Context information about the conversation. + * @param invalidInput The invalid input provided by the user. + * @return A message explaining how to correct the input. + */ + @Override + protected String getInputNotNumericText(ConversationContext context, + String invalidInput) + { + return NumberGUI.this.user.getTranslation("challenges.errors.not-a-integer", "[value]", invalidInput); + } + + + /** + * Optionally override this method to display an additional message if the + * user enters an invalid numeric input. + * + * @param context Context information about the conversation. + * @param invalidInput The invalid input provided by the user. + * @return A message explaining how to correct the input. + */ + @Override + protected String getFailedValidationText(ConversationContext context, + Number invalidInput) + { + return NumberGUI.this.user.getTranslation("challenges.errors.not-valid-integer", + "[value]", invalidInput.toString(), + "[min]", Integer.toString(NumberGUI.this.minValue), + "[max]", Integer.toString(NumberGUI.this.maxValue)); + } + + + /** + * @see Prompt#getPromptText(ConversationContext) + */ + @Override + public String getPromptText(ConversationContext conversationContext) + { + // Close input GUI. + user.closeInventory(); + + // There are no editable message. Just return question. + return question; + } + }). + withLocalEcho(false). + withPrefix(context -> + NumberGUI.this.user.getTranslation("challenges.gui.questions.prefix")). + buildConversation(user.getPlayer()); + + conversation.begin(); + } + + // --------------------------------------------------------------------- // Section: Enums // --------------------------------------------------------------------- diff --git a/src/main/java/world/bentobox/challenges/panel/util/SelectBlocksGUI.java b/src/main/java/world/bentobox/challenges/panel/util/SelectBlocksGUI.java index b89761a..066cdc2 100644 --- a/src/main/java/world/bentobox/challenges/panel/util/SelectBlocksGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/util/SelectBlocksGUI.java @@ -23,14 +23,20 @@ public class SelectBlocksGUI { public SelectBlocksGUI(User user, BiConsumer> consumer) { - this(user, Collections.emptySet(), consumer); + this(user, false, new HashSet<>(), consumer); + } + + public SelectBlocksGUI(User user, boolean singleSelect, BiConsumer> consumer) + { + this(user, singleSelect, new HashSet<>(), consumer); } - public SelectBlocksGUI(User user, Set excludedMaterial, BiConsumer> consumer) + public SelectBlocksGUI(User user, boolean singleSelect, Set excludedMaterial, BiConsumer> consumer) { this.consumer = consumer; this.user = user; + this.singleSelect = singleSelect; // Current GUI cannot display air blocks. It crashes with null-pointer excludedMaterial.add(Material.AIR); @@ -185,7 +191,7 @@ public class SelectBlocksGUI this.user.getTranslation("challenges.gui.descriptions.admin.selected") : ""). icon(itemStack). clickHandler((panel, user1, clickType, slot) -> { - if (clickType.isRightClick()) + if (!this.singleSelect && clickType.isRightClick()) { if (!this.selectedMaterials.add(material)) { @@ -230,4 +236,9 @@ public class SelectBlocksGUI * User who runs GUI. */ private User user; + + /** + * This indicate that return set must contain only single item. + */ + private boolean singleSelect; } 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 59b7429..39daf73 100644 --- a/src/main/java/world/bentobox/challenges/panel/util/StringListGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/util/StringListGUI.java @@ -15,7 +15,6 @@ 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; import world.bentobox.bentobox.api.panels.builders.PanelBuilder; @@ -78,8 +77,6 @@ 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; @@ -154,23 +151,8 @@ public class StringListGUI icon = new ItemStack(Material.WHITE_STAINED_GLASS_PANE); clickHandler = (panel, user, clickType, slot) -> { - 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")); - } + this.getStringInput(value -> this.value.add(value), + this.user.getTranslation("challenges.gui.descriptions.admin.add-text-line")); return true; }; @@ -201,18 +183,6 @@ 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; } @@ -239,27 +209,13 @@ public class StringListGUI icon(Material.PAPER). clickHandler((panel, user1, clickType, i) -> { - 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); - } + this.getStringInput( + value -> this.value.set(stringIndex, value), + this.user.getTranslation("challenges.gui.descriptions.admin.edit-text-line"), + element); - return true; - }).build(); + return true; + }).build(); } @@ -269,9 +225,9 @@ public class StringListGUI * @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) + private void getStringInput(Consumer consumer, @NonNull String question) { - this.startConversion(consumer, question, null); + this.getStringInput(consumer, question, null); } @@ -282,7 +238,7 @@ public class StringListGUI * @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) + private void getStringInput(Consumer consumer, @NonNull String question, @Nullable String message) { final User user = this.user; @@ -328,6 +284,7 @@ public class StringListGUI } }). withLocalEcho(false). + withPrefix(context -> user.getTranslation("challenges.gui.questions.prefix")). buildConversation(user.getPlayer()); conversation.begin(); @@ -349,8 +306,7 @@ public class StringListGUI REMOVE, CANCEL, CLEAR, - SAVE, - MODE + SAVE } @@ -369,11 +325,6 @@ 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 de70610..dd66bfe 100755 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -321,6 +321,16 @@ challenges: skull-owner: ' [owner]' egg-meta: ' [mob]' fish-meta: ' [body-color] with [pattern-color] [pattern]' + + questions: + prefix: "&2[SERVER]: " + + admin: + number: "Write a number in chat and press enter to accept it and press enter." + unique-id: "Write object unique name and press enter." + challenge-name: "Write in chat display name for current challenge." + level-name: "Write in chat display name for current level." + titles: # Title and subtitle my contain variable in [] that will be replaced with proper message from challenge object. # [friendlyName] will be replaced with challenge friendly name. diff --git a/src/main/resources/locales/es-ES.yml b/src/main/resources/locales/es-ES.yml index de5c348..fc160b9 100755 --- a/src/main/resources/locales/es-ES.yml +++ b/src/main/resources/locales/es-ES.yml @@ -283,6 +283,15 @@ challenges: money-reward: '&6Recompensa de dinero: $[value]' reward-items: '&6Artículos de recompensa:' reward-commands: '&6Comandos de recompensa:' + + questions: + prefix: "&2[SERVER]: " + + admin: + number: "Write a number in chat and press enter to accept it and press enter." + unique-id: "Write object unique name and press enter." + challenge-name: "Write in chat display name for current challenge." + level-name: "Write in chat display name for current level." messages: admin: hit-things: 'Golpea cosas para agregarlas a la lista de cosas requeridas. Haga click derecho cuando haya terminado.' diff --git a/src/main/resources/locales/fr-FR.yml b/src/main/resources/locales/fr-FR.yml index 53825bf..41f2f61 100644 --- a/src/main/resources/locales/fr-FR.yml +++ b/src/main/resources/locales/fr-FR.yml @@ -295,6 +295,15 @@ challenges: money-reward: '&6Récompense: [value]$' reward-items: '&6Récompenses:' reward-commands: '&6Récompenses (commandes):' + + questions: + prefix: "&2[SERVER]: " + + admin: + number: "Write a number in chat and press enter to accept it and press enter." + unique-id: "Write object unique name and press enter." + challenge-name: "Write in chat display name for current challenge." + level-name: "Write in chat display name for current level." titles: # Title and subtitle my contain variable in [] that will be replaced with proper message from challenge object. # [friendlyName] will be replaced with challenge friendly name. diff --git a/src/main/resources/locales/lv-LV.yml b/src/main/resources/locales/lv-LV.yml index 011487e..d3a6f90 100755 --- a/src/main/resources/locales/lv-LV.yml +++ b/src/main/resources/locales/lv-LV.yml @@ -318,6 +318,15 @@ challenges: skull-owner: ' [owner]' egg-meta: ' [mob]' fish-meta: ' [body-color] ar [pattern-color] [pattern]' + + questions: + prefix: "&2[SERVERIS]: " + + admin: + number: "Ieraksti nummuru sarakstē." + unique-id: "Ieraksti objekta unikālo nosaukumu sarakstē." + challenge-name: "Ieraksti uzdevuma nosaukumu sarakstē." + level-name: "Ieraksti uzdevuma līmeņa nosaukumu sarakstē." titles: # Title and subtitle my contain variable in [] that will be replaced with proper message from challenge object. # [friendlyName] will be replaced with challenge friendly name. diff --git a/src/main/resources/locales/zh-CN.yml b/src/main/resources/locales/zh-CN.yml index ed48e19..2671021 100644 --- a/src/main/resources/locales/zh-CN.yml +++ b/src/main/resources/locales/zh-CN.yml @@ -315,6 +315,16 @@ challenges: skull-owner: ' [owner]' egg-meta: ' [mob]' fish-meta: ' [body-color] with [pattern-color] [pattern]' + + questions: + prefix: "&2[SERVER]: " + + admin: + number: "Write a number in chat and press enter to accept it and press enter." + unique-id: "Write object unique name and press enter." + challenge-name: "Write in chat display name for current challenge." + level-name: "Write in chat display name for current level." + titles: # Title and subtitle my contain variable in [] that will be replaced with proper message from challenge object. # [friendlyName] will be replaced with challenge friendly name. diff --git a/src/main/resources/locales/zh-TW.yml b/src/main/resources/locales/zh-TW.yml index a87ec43..d0c3051 100644 --- a/src/main/resources/locales/zh-TW.yml +++ b/src/main/resources/locales/zh-TW.yml @@ -315,6 +315,15 @@ challenges: skull-owner: ' [owner]' egg-meta: ' [mob]' fish-meta: ' [body-color] with [pattern-color] [pattern]' + + questions: + prefix: "&2[SERVER]: " + + admin: + number: "Write a number in chat and press enter to accept it and press enter." + unique-id: "Write object unique name and press enter." + challenge-name: "Write in chat display name for current challenge." + level-name: "Write in chat display name for current level." titles: # Title and subtitle my contain variable in [] that will be replaced with proper message from challenge object. # [friendlyName] will be replaced with challenge friendly name.