From 2360f68edaaebe6e6e9b001d642f0ddf40ef19d9 Mon Sep 17 00:00:00 2001 From: PikaMug Date: Tue, 11 Jun 2019 13:09:12 -0400 Subject: [PATCH] Better support for quest names with color codes --- .../me/blackvein/quests/QuestFactory.java | 42 +++--------- .../main/java/me/blackvein/quests/Quests.java | 48 ++++++++++--- .../quests/actions/ActionFactory.java | 68 +++++++++---------- .../quests/listeners/CmdExecutor.java | 67 ++++++++---------- .../me/blackvein/quests/util/MiscUtil.java | 5 +- 5 files changed, 118 insertions(+), 112 deletions(-) diff --git a/main/src/main/java/me/blackvein/quests/QuestFactory.java b/main/src/main/java/me/blackvein/quests/QuestFactory.java index f9db6a5f9..3917337c9 100644 --- a/main/src/main/java/me/blackvein/quests/QuestFactory.java +++ b/main/src/main/java/me/blackvein/quests/QuestFactory.java @@ -316,23 +316,10 @@ public class QuestFactory implements ConversationAbandonedListener { @Override public Prompt acceptInput(ConversationContext context, String input) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) { - for (Quest q : plugin.getQuests()) { - if (q.getName().equalsIgnoreCase(input)) { - loadQuest(context, q); - return new CreateMenuPrompt(); - } - } - for (Quest q : plugin.getQuests()) { - if (q.getName().toLowerCase().startsWith(input.toLowerCase())) { - loadQuest(context, q); - return new CreateMenuPrompt(); - } - } - for (Quest q : plugin.getQuests()) { - if (q.getName().toLowerCase().contains(input.toLowerCase())) { - loadQuest(context, q); - return new CreateMenuPrompt(); - } + Quest q = plugin.getQuest(input); + if (q != null) { + loadQuest(context, q); + return new CreateMenuPrompt(); } return new SelectEditPrompt(); } else { @@ -533,10 +520,10 @@ public class QuestFactory implements ConversationAbandonedListener { @Override public String getPromptText(ConversationContext context) { String text = ChatColor.DARK_GREEN + Lang.get("eventTitle") + "\n"; - if (plugin.getEvents().isEmpty()) { + if (plugin.getActions().isEmpty()) { text += ChatColor.RED + "- " + Lang.get("none"); } else { - for (Action e : plugin.getEvents()) { + for (Action e : plugin.getActions()) { text += ChatColor.GREEN + "- " + e.getName() + "\n"; } } @@ -547,20 +534,13 @@ public class QuestFactory implements ConversationAbandonedListener { public Prompt acceptInput(ConversationContext context, String input) { Player player = (Player) context.getForWhom(); if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false && input.equalsIgnoreCase(Lang.get("cmdClear")) == false) { - Action found = null; - for (Action e : plugin.getEvents()) { - if (e.getName().equalsIgnoreCase(input)) { - found = e; - break; - } - } - if (found == null) { - player.sendMessage(ChatColor.RED + input + ChatColor.YELLOW + " " + Lang.get("questEditorInvalidEventName")); - return new InitialEventPrompt(); - } else { - context.setSessionData(CK.Q_INITIAL_EVENT, found.getName()); + Action a = plugin.getAction(input); + if (a != null) { + context.setSessionData(CK.Q_INITIAL_EVENT, a.getName()); return new CreateMenuPrompt(); } + player.sendMessage(ChatColor.RED + input + ChatColor.YELLOW + " " + Lang.get("questEditorInvalidEventName")); + return new InitialEventPrompt(); } else if (input.equalsIgnoreCase(Lang.get("cmdClear"))) { context.setSessionData(CK.Q_INITIAL_EVENT, null); player.sendMessage(ChatColor.YELLOW + Lang.get("questEditorEventCleared")); diff --git a/main/src/main/java/me/blackvein/quests/Quests.java b/main/src/main/java/me/blackvein/quests/Quests.java index 4e3858477..61a4e5435 100644 --- a/main/src/main/java/me/blackvein/quests/Quests.java +++ b/main/src/main/java/me/blackvein/quests/Quests.java @@ -277,10 +277,24 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener return quests; } + public LinkedList getActions() { + return events; + } + + public void setActions(LinkedList actions) { + this.events = actions; + } + + /** + * @deprecated Use getActions() + */ public LinkedList getEvents() { return events; } + /** + * @deprecated Use setActions() + */ public void setEvents(LinkedList events) { this.events = events; } @@ -3202,10 +3216,19 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener * @return Quest or null if not found */ public Quest getQuest(String name) { - for (Quest q : quests) { - if (q.getName().equalsIgnoreCase(name)) { + LinkedList qs = quests; + for (Quest q : qs) { + if (q.getName().equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', name))) { return q; - } else if (q.getName().toLowerCase().startsWith(name.toLowerCase())) { + } + } + for (Quest q : qs) { + if (q.getName().toLowerCase().startsWith(ChatColor.translateAlternateColorCodes('&', name).toLowerCase())) { + return q; + } + } + for (Quest q : qs) { + if (q.getName().toLowerCase().contains(ChatColor.translateAlternateColorCodes('&', name).toLowerCase())) { return q; } } @@ -3219,11 +3242,20 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener * @return Action or null if not found */ public Action getAction(String name) { - for (Action e : events) { - if (e.getName().equalsIgnoreCase(name)){ - return e; - } else if (e.getName().toLowerCase().startsWith(name.toLowerCase())) { - return e; + LinkedList as = events; + for (Action a : as) { + if (a.getName().equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', name))) { + return a; + } + } + for (Action a : as) { + if (a.getName().toLowerCase().startsWith(ChatColor.translateAlternateColorCodes('&', name).toLowerCase())) { + return a; + } + } + for (Action a : as) { + if (a.getName().toLowerCase().contains(ChatColor.translateAlternateColorCodes('&', name).toLowerCase())) { + return a; } } return null; 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 898300b7b..2ded0cf38 100644 --- a/main/src/main/java/me/blackvein/quests/actions/ActionFactory.java +++ b/main/src/main/java/me/blackvein/quests/actions/ActionFactory.java @@ -167,7 +167,7 @@ public class ActionFactory implements ConversationAbandonedListener { } } else if (input.equalsIgnoreCase("2")) { if (player.hasPermission("quests.editor.actions.edit") || player.hasPermission("quests.editor.events.edit")) { - if (plugin.getEvents().isEmpty()) { + if (plugin.getActions().isEmpty()) { ((Player) context.getForWhom()).sendMessage(ChatColor.YELLOW + Lang.get("eventEditorNoneToEdit")); return new MenuPrompt(); } else { @@ -179,7 +179,7 @@ public class ActionFactory implements ConversationAbandonedListener { } } else if (input.equalsIgnoreCase("3")) { if (player.hasPermission("quests.editor.actions.delete") || player.hasPermission("quests.editor.events.delete")) { - if (plugin.getEvents().isEmpty()) { + if (plugin.getActions().isEmpty()) { ((Player) context.getForWhom()).sendMessage(ChatColor.YELLOW + Lang.get("eventEditorNoneToDelete")); return new MenuPrompt(); } else { @@ -329,8 +329,8 @@ public class ActionFactory implements ConversationAbandonedListener { @Override public String getPromptText(ConversationContext context) { String text = ChatColor.GOLD + "- " + Lang.get("eventEditorEdit") + " -\n"; - for (Action evt : plugin.getEvents()) { - text += ChatColor.AQUA + evt.getName() + ChatColor.YELLOW + ", "; + for (Action a : plugin.getActions()) { + text += ChatColor.AQUA + a.getName() + ChatColor.YELLOW + ", "; } text = text.substring(0, text.length() - 2) + "\n"; text += ChatColor.YELLOW + Lang.get("eventEditorEnterEventName"); @@ -340,13 +340,12 @@ public class ActionFactory implements ConversationAbandonedListener { @Override public Prompt acceptInput(ConversationContext context, String input) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) { - for (Action evt : plugin.getEvents()) { - if (evt.getName().toLowerCase().startsWith(input.toLowerCase())) { - context.setSessionData(CK.E_OLD_EVENT, evt.getName()); - context.setSessionData(CK.E_NAME, evt.getName()); - loadData(evt, context); - return new CreateMenuPrompt(); - } + Action a = plugin.getAction(input); + if (a != null) { + context.setSessionData(CK.E_OLD_EVENT, a.getName()); + context.setSessionData(CK.E_NAME, a.getName()); + loadData(a, context); + return new CreateMenuPrompt(); } ((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorNotFound")); return new SelectEditPrompt(); @@ -361,8 +360,8 @@ public class ActionFactory implements ConversationAbandonedListener { @Override public String getPromptText(ConversationContext context) { String text = ChatColor.GOLD + "- " + Lang.get("eventEditorDelete") + " -\n"; - for (Action evt : plugin.getEvents()) { - text += ChatColor.AQUA + evt.getName() + ChatColor.YELLOW + ","; + for (Action a : plugin.getActions()) { + text += ChatColor.AQUA + a.getName() + ChatColor.YELLOW + ","; } text = text.substring(0, text.length() - 1) + "\n"; text += ChatColor.YELLOW + Lang.get("eventEditorEnterEventName"); @@ -373,27 +372,26 @@ public class ActionFactory implements ConversationAbandonedListener { public Prompt acceptInput(ConversationContext context, String input) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) { LinkedList used = new LinkedList(); - for (Action evt : plugin.getEvents()) { - if (evt.getName().equalsIgnoreCase(input)) { - for (Quest quest : plugin.getQuests()) { - for (Stage stage : quest.getStages()) { - if (stage.getFinishEvent() != null && stage.getFinishEvent().getName().equalsIgnoreCase(evt.getName())) { - used.add(quest.getName()); - break; - } + Action a = plugin.getAction(input); + if (a != null) { + for (Quest quest : plugin.getQuests()) { + for (Stage stage : quest.getStages()) { + if (stage.getFinishEvent() != null && stage.getFinishEvent().getName().equalsIgnoreCase(a.getName())) { + used.add(quest.getName()); + break; } } - if (used.isEmpty()) { - context.setSessionData(CK.ED_EVENT_DELETE, evt.getName()); - return new DeletePrompt(); - } else { - ((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorEventInUse") + " \"" + ChatColor.DARK_PURPLE + evt.getName() + ChatColor.RED + "\":"); - for (String s : used) { - ((Player) context.getForWhom()).sendMessage(ChatColor.RED + "- " + ChatColor.DARK_RED + s); - } - ((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorMustModifyQuests")); - return new SelectDeletePrompt(); + } + if (used.isEmpty()) { + context.setSessionData(CK.ED_EVENT_DELETE, a.getName()); + return new DeletePrompt(); + } else { + ((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorEventInUse") + " \"" + ChatColor.DARK_PURPLE + a.getName() + ChatColor.RED + "\":"); + for (String s : used) { + ((Player) context.getForWhom()).sendMessage(ChatColor.RED + "- " + ChatColor.DARK_RED + s); } + ((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorMustModifyQuests")); + return new SelectDeletePrompt(); } } ((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorNotFound")); @@ -913,9 +911,9 @@ public class ActionFactory implements ConversationAbandonedListener { } if (((String) context.getSessionData(CK.E_OLD_EVENT)).isEmpty() == false) { data.set(key + "." + (String) context.getSessionData(CK.E_OLD_EVENT), null); - LinkedList temp = plugin.getEvents(); + LinkedList temp = plugin.getActions(); temp.remove(plugin.getAction((String) context.getSessionData(CK.E_OLD_EVENT))); - plugin.setEvents(temp); + plugin.setActions(temp); } ConfigurationSection section = data.createSection(key + "." + (String) context.getSessionData(CK.E_NAME)); names.remove((String) context.getSessionData(CK.E_NAME)); @@ -1067,7 +1065,7 @@ public class ActionFactory implements ConversationAbandonedListener { @Override public Prompt acceptInput(ConversationContext context, String input) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) { - for (Action e : plugin.getEvents()) { + for (Action e : plugin.getActions()) { if (e.getName().equalsIgnoreCase(input)) { context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("eventEditorExists")); return new ActionNamePrompt(); @@ -1142,7 +1140,7 @@ public class ActionFactory implements ConversationAbandonedListener { @Override public Prompt acceptInput(ConversationContext context, String input) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) { - for (Action e : plugin.getEvents()) { + for (Action e : plugin.getActions()) { if (e.getName().equalsIgnoreCase(input)) { context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("eventEditorExists")); return new SetNamePrompt(); diff --git a/main/src/main/java/me/blackvein/quests/listeners/CmdExecutor.java b/main/src/main/java/me/blackvein/quests/listeners/CmdExecutor.java index c38327fce..2ec7d48d3 100644 --- a/main/src/main/java/me/blackvein/quests/listeners/CmdExecutor.java +++ b/main/src/main/java/me/blackvein/quests/listeners/CmdExecutor.java @@ -619,17 +619,17 @@ public class CmdExecutor implements CommandExecutor { } Quester quester = plugin.getQuester(player.getUniqueId()); if (quester.getCurrentQuests().isEmpty() == false) { - Quest q = plugin.getQuest(MiscUtil.concatArgArray(args, 1, args.length - 1, ' ')); - if (q != null) { - if (q.getOptions().getAllowQuitting()) { - QuestQuitEvent event = new QuestQuitEvent(q, quester); + Quest quest = plugin.getQuest(concatArgArray(args, 1, args.length - 1, ' ')); + if (quest != null) { + if (quest.getOptions().getAllowQuitting()) { + QuestQuitEvent event = new QuestQuitEvent(quest, quester); plugin.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { return; } - quester.hardQuit(q); + quester.hardQuit(quest); String msg = Lang.get("questQuit"); - msg = msg.replace("", ChatColor.DARK_PURPLE + q.getName() + ChatColor.YELLOW); + msg = msg.replace("", ChatColor.DARK_PURPLE + quest.getName() + ChatColor.YELLOW); player.sendMessage(ChatColor.YELLOW + msg); quester.saveData(); quester.loadData(); @@ -654,32 +654,7 @@ public class CmdExecutor implements CommandExecutor { if (args.length == 1) { player.sendMessage(ChatColor.YELLOW + Lang.get(player, "COMMAND_TAKE_USAGE")); } else { - String name = null; - if (args.length == 2) { - name = args[1].toLowerCase(); - } else { - boolean first = true; - int lastIndex = (args.length - 1); - int index = 0; - for (String s : args) { - if (index != 0) { - if (first) { - first = false; - if (args.length > 2) { - name = s.toLowerCase() + " "; - } else { - name = s.toLowerCase(); - } - } else if (index == lastIndex) { - name = name + s.toLowerCase(); - } else { - name = name + s.toLowerCase() + " "; - } - } - index++; - } - } - Quest questToFind = plugin.getQuest(name); + Quest questToFind = plugin.getQuest(concatArgArray(args, 1, args.length - 1, ' ')); if (questToFind != null) { final Quest q = questToFind; final Quester quester = plugin.getQuester(player.getUniqueId()); @@ -1168,7 +1143,7 @@ public class CmdExecutor implements CommandExecutor { msg = msg.replace("", target.getName()); cs.sendMessage(ChatColor.YELLOW + msg); } else { - Quest quest = plugin.getQuest(MiscUtil.concatArgArray(args, 2, args.length - 1, ' ')); + Quest quest = plugin.getQuest(concatArgArray(args, 2, args.length - 1, ' ')); if (quest == null) { cs.sendMessage(ChatColor.RED + Lang.get("questNotFound")); return; @@ -1228,7 +1203,7 @@ public class CmdExecutor implements CommandExecutor { msg = msg.replace("", target.getName()); cs.sendMessage(ChatColor.YELLOW + msg); } else { - Quest quest = plugin.getQuest(MiscUtil.concatArgArray(args, 2, args.length - 1, ' ')); + Quest quest = plugin.getQuest(concatArgArray(args, 2, args.length - 1, ' ')); if (quest == null) { cs.sendMessage(ChatColor.RED + Lang.get("questNotFound")); return; @@ -1266,7 +1241,7 @@ public class CmdExecutor implements CommandExecutor { msg = msg.replace("", target.getName()); cs.sendMessage(ChatColor.YELLOW + msg); } else { - Quest quest = plugin.getQuest(MiscUtil.concatArgArray(args, 2, args.length - 1, ' ')); + Quest quest = plugin.getQuest(concatArgArray(args, 2, args.length - 1, ' ')); if (quest == null) { cs.sendMessage(ChatColor.RED + Lang.get("questNotFound")); return; @@ -1307,7 +1282,7 @@ public class CmdExecutor implements CommandExecutor { msg = msg.replace("", target.getName()); cs.sendMessage(ChatColor.YELLOW + msg); } else { - Quest quest = plugin.getQuest(MiscUtil.concatArgArray(args, 2, args.length - 1, ' ')); + Quest quest = plugin.getQuest(concatArgArray(args, 2, args.length - 1, ' ')); if (quest == null) { cs.sendMessage(ChatColor.RED + Lang.get("questNotFound")); return; @@ -1392,7 +1367,7 @@ public class CmdExecutor implements CommandExecutor { cs.sendMessage(ChatColor.YELLOW + Lang.get("playerNotFound")); return; } - Quest toRemove = plugin.getQuest(MiscUtil.concatArgArray(args, 2, args.length - 1, ' ')); + Quest toRemove = plugin.getQuest(concatArgArray(args, 2, args.length - 1, ' ')); if (toRemove == null) { cs.sendMessage(ChatColor.RED + Lang.get("questNotFound")); return; @@ -1503,4 +1478,22 @@ public class CmdExecutor implements CommandExecutor { } return sortedMap; } + + /** + * Used to get quest names that contain spaces from command input + * + * @param args an array of Strings + * @param startingIndex the index to start combining at + * @param endingIndex the index to stop combining at + * @param delimiter the character for which the array was split + * @return a String or null + */ + private static String concatArgArray(String[] args, int startingIndex, int endingIndex, char delimiter) { + String s = ""; + for (int i = startingIndex; i <= endingIndex; i++) { + s += args[i] + delimiter; + } + s = s.substring(0, s.length()); + return s.trim().equals("") ? null : s.trim(); + } } diff --git a/main/src/main/java/me/blackvein/quests/util/MiscUtil.java b/main/src/main/java/me/blackvein/quests/util/MiscUtil.java index 070943ca8..d7b618baa 100644 --- a/main/src/main/java/me/blackvein/quests/util/MiscUtil.java +++ b/main/src/main/java/me/blackvein/quests/util/MiscUtil.java @@ -134,7 +134,10 @@ public class MiscUtil { public static String getDyeString(DyeColor dc) { return Lang.get("COLOR_" + dc.name()); } - + + /** + * @deprecated Will be removed in a future version of Quests + */ public static String concatArgArray(String[] args, int startingIndex, int endingIndex, char delimiter) { String s = ""; for (int i = startingIndex; i <= endingIndex; i++) {