From 879fb969c90dc94bab472009f3185a844aeb5288 Mon Sep 17 00:00:00 2001 From: PikaMug Date: Sun, 11 Aug 2019 02:10:18 -0400 Subject: [PATCH] Supply external conversation hooks, part 3. Per #570 --- .../me/blackvein/quests/QuestFactory.java | 59 +-------- .../quests/actions/ActionFactory.java | 4 +- ...estsEditorPostOpenGUIDisplayMenuEvent.java | 30 +++++ .../quests/prompts/GUIDisplayPrompt.java | 114 ++++++++++++++++++ .../quests/prompts/RequirementsPrompt.java | 4 +- .../quests/prompts/RewardsPrompt.java | 4 +- main/src/main/resources/strings.yml | 3 - 7 files changed, 152 insertions(+), 66 deletions(-) create mode 100644 main/src/main/java/me/blackvein/quests/events/editor/quests/QuestsEditorPostOpenGUIDisplayMenuEvent.java create mode 100644 main/src/main/java/me/blackvein/quests/prompts/GUIDisplayPrompt.java diff --git a/main/src/main/java/me/blackvein/quests/QuestFactory.java b/main/src/main/java/me/blackvein/quests/QuestFactory.java index 5f4f828b3..170f62773 100644 --- a/main/src/main/java/me/blackvein/quests/QuestFactory.java +++ b/main/src/main/java/me/blackvein/quests/QuestFactory.java @@ -36,7 +36,6 @@ import org.bukkit.conversations.ConversationAbandonedEvent; import org.bukkit.conversations.ConversationAbandonedListener; import org.bukkit.conversations.ConversationContext; import org.bukkit.conversations.ConversationFactory; -import org.bukkit.conversations.FixedSetPrompt; import org.bukkit.conversations.NumericPrompt; import org.bukkit.conversations.Prompt; import org.bukkit.conversations.StringPrompt; @@ -50,7 +49,7 @@ import com.sk89q.worldguard.protection.managers.RegionManager; import me.blackvein.quests.actions.Action; import me.blackvein.quests.events.editor.quests.QuestsEditorPostOpenCreateMenuEvent; import me.blackvein.quests.events.editor.quests.QuestsEditorPostOpenMainMenuEvent; -import me.blackvein.quests.prompts.ItemStackPrompt; +import me.blackvein.quests.prompts.GUIDisplayPrompt; import me.blackvein.quests.prompts.OptionsPrompt; import me.blackvein.quests.prompts.RequirementsPrompt; import me.blackvein.quests.prompts.RewardsPrompt; @@ -449,7 +448,7 @@ public class QuestFactory implements ConversationAbandonedListener { return new InitialActionPrompt(); case 8: if (plugin.getDependencies().getCitizens() != null) { - return new GUIDisplayPrompt(); + return new GUIDisplayPrompt(plugin, QuestFactory.this); } else { return new CreateMenuPrompt(); } @@ -783,60 +782,6 @@ public class QuestFactory implements ConversationAbandonedListener { } } - private class GUIDisplayPrompt extends FixedSetPrompt { - - public GUIDisplayPrompt() { - super("1", "2", "3"); - } - - @Override - public String getPromptText(ConversationContext context) { - if (context.getSessionData("tempStack") != null) { - ItemStack stack = (ItemStack) context.getSessionData("tempStack"); - boolean failed = false; - for (Quest quest : plugin.getQuests()) { - if (quest.guiDisplay != null) { - if (ItemUtil.compareItems(stack, quest.guiDisplay, false) == 0) { - String error = Lang.get("questGUIError"); - error = error.replaceAll("", ChatColor.DARK_PURPLE + quest.getName() + ChatColor.RED); - context.getForWhom().sendRawMessage(ChatColor.RED + error); - failed = true; - break; - } - } - } - if (!failed) { - context.setSessionData(CK.Q_GUIDISPLAY, context.getSessionData("tempStack")); - } - context.setSessionData("tempStack", null); - } - String text = ChatColor.GREEN + Lang.get("questGUITitle") + "\n"; - if (context.getSessionData(CK.Q_GUIDISPLAY) != null) { - ItemStack stack = (ItemStack) context.getSessionData(CK.Q_GUIDISPLAY); - text += ChatColor.DARK_GREEN + Lang.get("questCurrentItem") + " " + ChatColor.RESET + ItemUtil.getDisplayString(stack) + "\n\n"; - } else { - text += ChatColor.DARK_GREEN + Lang.get("questCurrentItem") + " " + ChatColor.GRAY + "(" + Lang.get("none") + ")\n\n"; - } - text += ChatColor.GREEN + "" + ChatColor.BOLD + "1 -" + ChatColor.RESET + ChatColor.DARK_GREEN + " " + Lang.get("questSetItem") + "\n"; - text += ChatColor.GREEN + "" + ChatColor.BOLD + "2 -" + ChatColor.RESET + ChatColor.DARK_GREEN + " " + Lang.get("questClearItem") + "\n"; - text += ChatColor.GREEN + "" + ChatColor.BOLD + "3 -" + ChatColor.RESET + ChatColor.GREEN + " " + Lang.get("done") + "\n"; - return text; - } - - @Override - protected Prompt acceptValidatedInput(ConversationContext context, String input) { - if (input.equalsIgnoreCase("1")) { - return new ItemStackPrompt(GUIDisplayPrompt.this); - } else if (input.equalsIgnoreCase("2")) { - context.setSessionData(CK.Q_GUIDISPLAY, null); - context.getForWhom().sendRawMessage(ChatColor.YELLOW + Lang.get("questGUICleared")); - return new GUIDisplayPrompt(); - } else { - return new CreateMenuPrompt(); - } - } - } - private class SavePrompt extends StringPrompt { @Override diff --git a/main/src/main/java/me/blackvein/quests/actions/ActionFactory.java b/main/src/main/java/me/blackvein/quests/actions/ActionFactory.java index fe09d2d3e..98d98502e 100644 --- a/main/src/main/java/me/blackvein/quests/actions/ActionFactory.java +++ b/main/src/main/java/me/blackvein/quests/actions/ActionFactory.java @@ -1200,14 +1200,14 @@ public class ActionFactory implements ConversationAbandonedListener { } String text = ChatColor.GOLD + Lang.get("eventEditorGiveItemsTitle") + "\n"; if (context.getSessionData(CK.E_ITEMS) == null) { - text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("eventEditorAddItem") + "\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("stageEditorDeliveryAddItem") + "\n"; text += ChatColor.BLUE + "" + ChatColor.BOLD + "2" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("clear") + "\n"; text += ChatColor.BLUE + "" + ChatColor.BOLD + "3" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("done"); } else { for (ItemStack is : getItems(context)) { text += ChatColor.GRAY + " - " + ItemUtil.getDisplayString(is) + "\n"; } - text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("eventEditorAddItem") + "\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("stageEditorDeliveryAddItem") + "\n"; text += ChatColor.BLUE + "" + ChatColor.BOLD + "2" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("clear") + "\n"; text += ChatColor.BLUE + "" + ChatColor.BOLD + "3" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("done"); } diff --git a/main/src/main/java/me/blackvein/quests/events/editor/quests/QuestsEditorPostOpenGUIDisplayMenuEvent.java b/main/src/main/java/me/blackvein/quests/events/editor/quests/QuestsEditorPostOpenGUIDisplayMenuEvent.java new file mode 100644 index 000000000..21896a96b --- /dev/null +++ b/main/src/main/java/me/blackvein/quests/events/editor/quests/QuestsEditorPostOpenGUIDisplayMenuEvent.java @@ -0,0 +1,30 @@ +package me.blackvein.quests.events.editor.quests; + +import org.bukkit.conversations.ConversationContext; +import org.bukkit.event.HandlerList; + +import me.blackvein.quests.QuestFactory; + +public class QuestsEditorPostOpenGUIDisplayMenuEvent extends QuestsEditorEvent { + private static final HandlerList handlers = new HandlerList(); + private final QuestFactory factory; + + public QuestsEditorPostOpenGUIDisplayMenuEvent(QuestFactory factory, ConversationContext context) { + super(context); + this.context = context; + this.factory = factory; + } + + public QuestFactory getQuestFactory() { + return factory; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/main/src/main/java/me/blackvein/quests/prompts/GUIDisplayPrompt.java b/main/src/main/java/me/blackvein/quests/prompts/GUIDisplayPrompt.java new file mode 100644 index 000000000..d599bd5ae --- /dev/null +++ b/main/src/main/java/me/blackvein/quests/prompts/GUIDisplayPrompt.java @@ -0,0 +1,114 @@ +package me.blackvein.quests.prompts; + +import org.bukkit.ChatColor; +import org.bukkit.conversations.ConversationContext; +import org.bukkit.conversations.NumericPrompt; +import org.bukkit.conversations.Prompt; +import org.bukkit.inventory.ItemStack; + +import me.blackvein.quests.Quest; +import me.blackvein.quests.QuestFactory; +import me.blackvein.quests.Quests; +import me.blackvein.quests.events.editor.quests.QuestsEditorPostOpenGUIDisplayMenuEvent; +import me.blackvein.quests.util.CK; +import me.blackvein.quests.util.ItemUtil; +import me.blackvein.quests.util.Lang; + +public class GUIDisplayPrompt extends NumericPrompt { + private final Quests plugin; + private final QuestFactory questFactory; + + public GUIDisplayPrompt(Quests plugin, QuestFactory qf) { + this.plugin = plugin; + this.questFactory = qf; + } + + private final int maxNumber = 3; + + public int getMaxNumber() { + return maxNumber; + } + + public String getTitle() { + return Lang.get("questGUITitle"); + } + + public ChatColor getNumberColor(ConversationContext context, int number) { + switch (number) { + case 1: + return ChatColor.BLUE; + case 2: + return ChatColor.RED; + case 3: + return ChatColor.GREEN; + default: + return null; + } + } + + public String getSelectionText(ConversationContext context, int number) { + switch (number) { + case 1: + return ChatColor.YELLOW + Lang.get("stageEditorDeliveryAddItem"); + case 2: + return ChatColor.YELLOW + Lang.get("clear"); + case 3: + return ChatColor.YELLOW + Lang.get("done"); + default: + return null; + } + } + + @Override + public String getPromptText(ConversationContext context) { + QuestsEditorPostOpenGUIDisplayMenuEvent event = new QuestsEditorPostOpenGUIDisplayMenuEvent(questFactory, context); + plugin.getServer().getPluginManager().callEvent(event); + + if (context.getSessionData("tempStack") != null) { + ItemStack stack = (ItemStack) context.getSessionData("tempStack"); + boolean failed = false; + for (Quest quest : plugin.getQuests()) { + if (quest.getGUIDisplay() != null) { + if (ItemUtil.compareItems(stack, quest.getGUIDisplay(), false) == 0) { + String error = Lang.get("questGUIError"); + error = error.replaceAll("", ChatColor.DARK_PURPLE + quest.getName() + ChatColor.RED); + context.getForWhom().sendRawMessage(ChatColor.RED + error); + failed = true; + break; + } + } + } + if (!failed) { + context.setSessionData(CK.Q_GUIDISPLAY, context.getSessionData("tempStack")); + } + context.setSessionData("tempStack", null); + } + String text = ChatColor.GOLD + getTitle() + "\n"; + if (context.getSessionData(CK.Q_GUIDISPLAY) != null) { + ItemStack stack = (ItemStack) context.getSessionData(CK.Q_GUIDISPLAY); + text += " " + ChatColor.RESET + ItemUtil.getDisplayString(stack) + "\n"; + } else { + text += " " + ChatColor.GRAY + "(" + Lang.get("noneSet") + ")\n"; + } + for (int i = 1; i <= maxNumber; i++) { + text += getNumberColor(context, i) + "" + ChatColor.BOLD + i + ChatColor.RESET + " - " + getSelectionText(context, i) + "\n"; + } + return text; + } + + @Override + protected Prompt acceptValidatedInput(ConversationContext context, Number input) { + switch (input.intValue()) { + case 1: + return new ItemStackPrompt(GUIDisplayPrompt.this); + case 2: + context.setSessionData(CK.Q_GUIDISPLAY, null); + context.getForWhom().sendRawMessage(ChatColor.YELLOW + Lang.get("questGUICleared")); + return new GUIDisplayPrompt(plugin, questFactory); + case 3: + return questFactory.returnToMenu(); + default: + return null; + } + } +} \ No newline at end of file diff --git a/main/src/main/java/me/blackvein/quests/prompts/RequirementsPrompt.java b/main/src/main/java/me/blackvein/quests/prompts/RequirementsPrompt.java index da4d26d4a..c34971203 100644 --- a/main/src/main/java/me/blackvein/quests/prompts/RequirementsPrompt.java +++ b/main/src/main/java/me/blackvein/quests/prompts/RequirementsPrompt.java @@ -354,7 +354,7 @@ public class RequirementsPrompt extends FixedSetPrompt { } String text = ChatColor.GOLD + Lang.get("itemRequirementsTitle") + "\n"; if (context.getSessionData(CK.REQ_ITEMS) == null) { - text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("reqAddItem") + "\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("stageEditorDeliveryAddItem") + "\n"; text += ChatColor.GRAY + "2 - " + Lang.get("reqSetRemoveItems") + " (" + Lang.get("reqNoItemsSet") + ")\n"; text += ChatColor.BLUE + "" + ChatColor.BOLD + "3" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("clear") + "\n"; text += ChatColor.BLUE + "" + ChatColor.BOLD + "4" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("done"); @@ -362,7 +362,7 @@ public class RequirementsPrompt extends FixedSetPrompt { for (ItemStack is : getItems(context)) { text += ChatColor.GRAY + " - " + ItemUtil.getDisplayString(is) + "\n"; } - text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("reqAddItem") + "\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("stageEditorDeliveryAddItem") + "\n"; if (context.getSessionData(CK.REQ_ITEMS_REMOVE) == null) { text += ChatColor.BLUE + "" + ChatColor.BOLD + "2" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("reqSetRemoveItems") + " (" + Lang.get("reqNoValuesSet") + ")\n"; } else { diff --git a/main/src/main/java/me/blackvein/quests/prompts/RewardsPrompt.java b/main/src/main/java/me/blackvein/quests/prompts/RewardsPrompt.java index 00e943146..1213fd01c 100644 --- a/main/src/main/java/me/blackvein/quests/prompts/RewardsPrompt.java +++ b/main/src/main/java/me/blackvein/quests/prompts/RewardsPrompt.java @@ -317,14 +317,14 @@ public class RewardsPrompt extends FixedSetPrompt { String text = ChatColor.GOLD + Lang.get("itemRewardsTitle") + "\n"; if (context.getSessionData(CK.REW_ITEMS) == null) { text += ChatColor.GRAY + " (" + Lang.get("noneSet") + ")\n"; - text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("reqAddItem") + "\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("stageEditorDeliveryAddItem") + "\n"; text += ChatColor.BLUE + "" + ChatColor.BOLD + "2" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("clear") + "\n"; text += ChatColor.BLUE + "" + ChatColor.BOLD + "3" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("done"); } else { for (ItemStack is : getItems(context)) { text += ChatColor.GRAY + "- " + ItemUtil.getDisplayString(is) + "\n"; } - text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("reqAddItem") + "\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("stageEditorDeliveryAddItem") + "\n"; text += ChatColor.BLUE + "" + ChatColor.BOLD + "2" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("clear") + "\n"; text += ChatColor.BLUE + "" + ChatColor.BOLD + "3" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("done"); } diff --git a/main/src/main/resources/strings.yml b/main/src/main/resources/strings.yml index bb48718fa..82907aa92 100644 --- a/main/src/main/resources/strings.yml +++ b/main/src/main/resources/strings.yml @@ -90,8 +90,6 @@ questWGInvalidRegion: " is not a valid WorldGuard region!" questWGRegionCleared: "Quest region cleared." questGUIError: "Error: That item is already being used as the GUI Display for the Quest ." questCurrentItem: "Current item:" -questSetItem: "Set Item" -questClearItem: "Clear Item" questGUICleared: "Quest GUI Item Display cleared." questDeleted: "Quest deleted! Quests and Actions have been reloaded." questEditorNameExists: "A Quest with that name already exists!" @@ -340,7 +338,6 @@ eventEditorSetCommands: "Execute commands" eventEditorItems: "Action Items" eventEditorSetItems: "Give items" eventEditorItemsCleared: "Action items cleared." -eventEditorAddItem: "Add item" eventEditorSetItemNames: "Set item names" eventEditorSetItemAmounts: "Set item amounts" eventEditorNoNames: "No names set"