From 7e09351cf108c92b07f6b130b4a8098f69a7692a Mon Sep 17 00:00:00 2001 From: jmp Date: Fri, 9 Oct 2020 16:26:41 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20highlighting=20to=20invalid?= =?UTF-8?q?=20syntax=20exception?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MinecraftExceptionHandler.java | 51 ++++++++----------- .../examples/bukkit/ExamplePlugin.java | 12 ++--- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/MinecraftExceptionHandler.java b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/MinecraftExceptionHandler.java index 1929a4a6..de50d7ca 100644 --- a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/MinecraftExceptionHandler.java +++ b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/MinecraftExceptionHandler.java @@ -35,6 +35,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import java.util.HashMap; import java.util.Map; import java.util.function.Function; +import java.util.regex.Pattern; /** * Exception handler that sends {@link Component} to the sender. All component builders @@ -44,57 +45,47 @@ import java.util.function.Function; */ public final class MinecraftExceptionHandler { + private static final Pattern SPECIAL_CHARACTERS_PATTERN = Pattern.compile("[^\\s\\w\\-]"); + /** * Default component builder for {@link InvalidSyntaxException} */ public static final Function DEFAULT_INVALID_SYNTAX_FUNCTION = e -> Component.text() - .append( - Component.text("Invalid command syntax. Correct command syntax is: ", NamedTextColor.RED) - ).append( - Component.text( - String.format("/%s", ((InvalidSyntaxException) e).getCorrectSyntax()), - NamedTextColor.GRAY - ) - ) + .append(Component.text("Invalid command syntax. Correct command syntax is: ", NamedTextColor.RED)) + .append(Component.text( + String.format("/%s", ((InvalidSyntaxException) e).getCorrectSyntax()), + NamedTextColor.GRAY + ).replaceText(SPECIAL_CHARACTERS_PATTERN, match -> match.color(NamedTextColor.WHITE))) .build(); /** * Default component builder for {@link InvalidCommandSenderException} */ public static final Function DEFAULT_INVALID_SENDER_FUNCTION = e -> Component.text() - .append( - Component.text("Invalid command sender. You must be of type ", NamedTextColor.RED) - ).append( - Component.text( - ((InvalidCommandSenderException) e).getRequiredSender().getSimpleName(), - NamedTextColor.GRAY - ) - ) + .append(Component.text("Invalid command sender. You must be of type ", NamedTextColor.RED)) + .append(Component.text( + ((InvalidCommandSenderException) e).getRequiredSender().getSimpleName(), + NamedTextColor.GRAY + )) .build(); /** * Default component builder for {@link NoPermissionException} */ public static final Function DEFAULT_NO_PERMISSION_FUNCTION = - e -> Component.text() - .append( - Component.text( - "I'm sorry, but you do not have permission to perform this command. \n" - + "Please contact the server administrators if you believe that this is in error.", - NamedTextColor.RED - ) - ) - .build(); + e -> Component.text( + "I'm sorry, but you do not have permission to perform this command. \n" + + "Please contact the server administrators if you believe that this is in error.", + NamedTextColor.RED + ); /** * Default component builder for {@link ArgumentParseException} */ public static final Function DEFAULT_ARGUMENT_PARSING_FUNCTION = e -> Component.text() - .append( - Component.text("Invalid command argument: ", NamedTextColor.RED) - ).append( - Component.text(e.getCause().getMessage(), NamedTextColor.GRAY) - ).build(); + .append(Component.text("Invalid command argument: ", NamedTextColor.RED)) + .append(Component.text(e.getCause().getMessage(), NamedTextColor.GRAY)) + .build(); private final Map> componentBuilders = new HashMap<>(); private Function decorator = Function.identity(); diff --git a/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/ExamplePlugin.java b/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/ExamplePlugin.java index 023ed717..b134a702 100644 --- a/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/ExamplePlugin.java +++ b/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/ExamplePlugin.java @@ -196,13 +196,11 @@ public final class ExamplePlugin extends JavaPlugin { .withNoPermissionHandler() .withArgumentParsingHandler() .withDecorator( - component -> Component.text().append( - Component.text("[", NamedTextColor.DARK_GRAY) - ).append( - Component.text("Example", NamedTextColor.GOLD) - ).append( - Component.text("] ", NamedTextColor.DARK_GRAY) - ).append(component).build() + component -> Component.text() + .append(Component.text("[", NamedTextColor.DARK_GRAY)) + .append(Component.text("Example", NamedTextColor.GOLD)) + .append(Component.text("] ", NamedTextColor.DARK_GRAY)) + .append(component).build() ).apply(manager, bukkitAudiences::sender); // // Create the commands