From 7fbd793c859007cc24974fef1b85448ce0fb8aaf Mon Sep 17 00:00:00 2001 From: PikaMug <2267126+PikaMug@users.noreply.github.com> Date: Mon, 24 Feb 2020 20:37:48 -0500 Subject: [PATCH] Better input validation for some Options and Requirements prompts --- .../convo/quests/prompts/OptionsPrompt.java | 18 ++++++++---------- .../quests/prompts/RequirementsPrompt.java | 10 +++------- main/src/main/resources/strings.yml | 1 - 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/main/src/main/java/me/blackvein/quests/convo/quests/prompts/OptionsPrompt.java b/main/src/main/java/me/blackvein/quests/convo/quests/prompts/OptionsPrompt.java index 4f594b0dd..419c99256 100644 --- a/main/src/main/java/me/blackvein/quests/convo/quests/prompts/OptionsPrompt.java +++ b/main/src/main/java/me/blackvein/quests/convo/quests/prompts/OptionsPrompt.java @@ -162,16 +162,14 @@ public class OptionsPrompt extends QuestsEditorNumericPrompt { public Prompt acceptInput(ConversationContext context, String input) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false && input.equalsIgnoreCase(Lang.get("cmdClear")) == false) { - try { - boolean b = Boolean.parseBoolean(input); - if (input.equalsIgnoreCase("t") || input.equalsIgnoreCase("true") - || input.equalsIgnoreCase(Lang.get("true")) - || input.equalsIgnoreCase(Lang.get("yesWord"))) { - b = true; - } - context.setSessionData(tempKey, b); - } catch (Exception e) { - e.printStackTrace(); + if (input.startsWith("t") || input.equalsIgnoreCase(Lang.get("true")) + || input.equalsIgnoreCase(Lang.get("yesWord"))) { + context.setSessionData(tempKey, true); + } else if (input.startsWith("f") || input.equalsIgnoreCase(Lang.get("false")) + || input.equalsIgnoreCase(Lang.get("noWord"))) { + context.setSessionData(tempKey, false); + } else { + context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput")); return new TrueFalsePrompt(plugin, context, factory); } } else if (input.equalsIgnoreCase(Lang.get("cmdClear"))) { diff --git a/main/src/main/java/me/blackvein/quests/convo/quests/prompts/RequirementsPrompt.java b/main/src/main/java/me/blackvein/quests/convo/quests/prompts/RequirementsPrompt.java index c98fd613a..5e972baee 100644 --- a/main/src/main/java/me/blackvein/quests/convo/quests/prompts/RequirementsPrompt.java +++ b/main/src/main/java/me/blackvein/quests/convo/quests/prompts/RequirementsPrompt.java @@ -663,18 +663,14 @@ public class RequirementsPrompt extends QuestsEditorNumericPrompt { String[] args = input.split(" "); LinkedList booleans = new LinkedList(); for (String s : args) { - if (input.equalsIgnoreCase("t") || input.equalsIgnoreCase("true") - || s.equalsIgnoreCase(Lang.get("true")) + if (input.startsWith("t") || s.equalsIgnoreCase(Lang.get("true")) || s.equalsIgnoreCase(Lang.get("yesWord"))) { booleans.add(true); - } else if (input.equalsIgnoreCase("f") || input.equalsIgnoreCase("false") - || s.equalsIgnoreCase(Lang.get("false")) + } else if (input.startsWith("f") || s.equalsIgnoreCase(Lang.get("false")) || s.equalsIgnoreCase(Lang.get("noWord"))) { booleans.add(false); } else { - String text = Lang.get("reqTrueFalseError"); - text = text.replace("", ChatColor.LIGHT_PURPLE + s + ChatColor.RED); - context.getForWhom().sendRawMessage(ChatColor.RED + text); + context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput")); return new RemoveItemsPrompt(); } } diff --git a/main/src/main/resources/strings.yml b/main/src/main/resources/strings.yml index 773422304..61a8b8fc2 100644 --- a/main/src/main/resources/strings.yml +++ b/main/src/main/resources/strings.yml @@ -435,7 +435,6 @@ reqHeroesPrimaryDisplay: "Primary Class:" reqHeroesSecondaryDisplay: "Secondary Class:" reqNotAQuestName: " is not a Quest name!" reqItemCleared: "Item requirements cleared." -reqTrueFalseError: ' is not a true or false value!%br%Example: true false true true' reqCustomAlreadyAdded: "That custom requirement has already been added!" reqCustomNotFound: "Custom requirement module not found." reqCustomCleared: "Custom requirements cleared."