From db971d81ab330be5b35bad50f6c670d86829b523 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sat, 8 Feb 2020 22:28:57 +0100 Subject: [PATCH] Added a uniqueId sanitization when creating challenges/levels This will help fixing issues with spaces, hyphens and accents in non-English languages. --- .../challenges/panel/admin/AdminGUI.java | 25 +++++++++++++------ src/main/resources/locales/en-US.yml | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) 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 c65e9f7..b01d43f 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java @@ -1,6 +1,7 @@ package world.bentobox.challenges.panel.admin; +import java.util.Locale; import java.util.function.Consumer; import java.util.function.Function; @@ -252,7 +253,7 @@ public class AdminGUI extends CommonGUI clickHandler = (panel, user, clickType, slot) -> { this.getNewUniqueID(challenge -> { - String newName = Utils.getGameMode(this.world) + "_" + challenge; + String uniqueId = Utils.getGameMode(this.world) + "_" + challenge; ChallengeTypeGUI.open(user, this.addon.getChallengesSettings().getLoreLineLength(), @@ -260,15 +261,15 @@ public class AdminGUI extends CommonGUI new EditChallengeGUI(this.addon, this.world, this.user, - this.addon.getChallengesManager().createChallenge(newName, type, requirements), + this.addon.getChallengesManager().createChallenge(uniqueId, type, requirements), this.topLabel, this.permissionPrefix, this).build(); }); }, input -> { - String newName = Utils.getGameMode(this.world) + "_" + input; - return !this.addon.getChallengesManager().containsChallenge(newName); + String uniqueId = Utils.getGameMode(this.world) + "_" + input; + return !this.addon.getChallengesManager().containsChallenge(uniqueId); }, this.user.getTranslation("challenges.gui.questions.admin.unique-id") ); @@ -659,7 +660,7 @@ public class AdminGUI extends CommonGUI @Override protected boolean isInputValid(ConversationContext context, String input) { - return stringValidation.apply(input); + return stringValidation.apply(sanitizeInput(input)); } @@ -679,7 +680,7 @@ public class AdminGUI extends CommonGUI protected String getFailedValidationText(ConversationContext context, String invalidInput) { - return user.getTranslation("challenges.errors.unique-id", "[id]", invalidInput); + return user.getTranslation("challenges.errors.unique-id", "[id]", sanitizeInput(invalidInput)); } @@ -699,7 +700,7 @@ public class AdminGUI extends CommonGUI protected Prompt acceptValidatedInput(ConversationContext context, String input) { // Add answer to consumer. - consumer.accept(input); + consumer.accept(sanitizeInput(input)); // End conversation return Prompt.END_OF_CONVERSATION; } @@ -710,4 +711,14 @@ public class AdminGUI extends CommonGUI conversation.begin(); } + + /** + * Sanitizes the provided input. + * It replaces spaces and hyphens with underscores and lowercases the input. + * @param input input to sanitize + * @return sanitized input + */ + private String sanitizeInput(String input) { + return input.toLowerCase(Locale.ENGLISH).replace(" ", "_").replace("-", "_"); + } } \ No newline at end of file diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index addefe8..ba5f5e2 100755 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -507,7 +507,7 @@ challenges: admin: number: "Write a number in the chat and press enter." - unique-id: "Write the object's unique name and press enter." + unique-id: "Write the object's unique id and press enter." challenge-name: "Write the display name in the chat for the current challenge." level-name: "Write the display name in chat for the current level."