Add highlighting to invalid syntax exception

This commit is contained in:
jmp 2020-10-09 16:26:41 -07:00 committed by Alexander Söderberg
parent c3d679d5ec
commit 7e09351cf1
2 changed files with 26 additions and 37 deletions

View file

@ -35,6 +35,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Pattern;
/** /**
* Exception handler that sends {@link Component} to the sender. All component builders * 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<C> { public final class MinecraftExceptionHandler<C> {
private static final Pattern SPECIAL_CHARACTERS_PATTERN = Pattern.compile("[^\\s\\w\\-]");
/** /**
* Default component builder for {@link InvalidSyntaxException} * Default component builder for {@link InvalidSyntaxException}
*/ */
public static final Function<Exception, Component> DEFAULT_INVALID_SYNTAX_FUNCTION = public static final Function<Exception, Component> DEFAULT_INVALID_SYNTAX_FUNCTION =
e -> Component.text() e -> Component.text()
.append( .append(Component.text("Invalid command syntax. Correct command syntax is: ", NamedTextColor.RED))
Component.text("Invalid command syntax. Correct command syntax is: ", NamedTextColor.RED) .append(Component.text(
).append(
Component.text(
String.format("/%s", ((InvalidSyntaxException) e).getCorrectSyntax()), String.format("/%s", ((InvalidSyntaxException) e).getCorrectSyntax()),
NamedTextColor.GRAY NamedTextColor.GRAY
) ).replaceText(SPECIAL_CHARACTERS_PATTERN, match -> match.color(NamedTextColor.WHITE)))
)
.build(); .build();
/** /**
* Default component builder for {@link InvalidCommandSenderException} * Default component builder for {@link InvalidCommandSenderException}
*/ */
public static final Function<Exception, Component> DEFAULT_INVALID_SENDER_FUNCTION = public static final Function<Exception, Component> DEFAULT_INVALID_SENDER_FUNCTION =
e -> Component.text() e -> Component.text()
.append( .append(Component.text("Invalid command sender. You must be of type ", NamedTextColor.RED))
Component.text("Invalid command sender. You must be of type ", NamedTextColor.RED) .append(Component.text(
).append(
Component.text(
((InvalidCommandSenderException) e).getRequiredSender().getSimpleName(), ((InvalidCommandSenderException) e).getRequiredSender().getSimpleName(),
NamedTextColor.GRAY NamedTextColor.GRAY
) ))
)
.build(); .build();
/** /**
* Default component builder for {@link NoPermissionException} * Default component builder for {@link NoPermissionException}
*/ */
public static final Function<Exception, Component> DEFAULT_NO_PERMISSION_FUNCTION = public static final Function<Exception, Component> DEFAULT_NO_PERMISSION_FUNCTION =
e -> Component.text() e -> Component.text(
.append(
Component.text(
"I'm sorry, but you do not have permission to perform this command. \n" "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.", + "Please contact the server administrators if you believe that this is in error.",
NamedTextColor.RED NamedTextColor.RED
) );
)
.build();
/** /**
* Default component builder for {@link ArgumentParseException} * Default component builder for {@link ArgumentParseException}
*/ */
public static final Function<Exception, Component> DEFAULT_ARGUMENT_PARSING_FUNCTION = public static final Function<Exception, Component> DEFAULT_ARGUMENT_PARSING_FUNCTION =
e -> Component.text() e -> Component.text()
.append( .append(Component.text("Invalid command argument: ", NamedTextColor.RED))
Component.text("Invalid command argument: ", NamedTextColor.RED) .append(Component.text(e.getCause().getMessage(), NamedTextColor.GRAY))
).append( .build();
Component.text(e.getCause().getMessage(), NamedTextColor.GRAY)
).build();
private final Map<ExceptionType, Function<Exception, Component>> componentBuilders = new HashMap<>(); private final Map<ExceptionType, Function<Exception, Component>> componentBuilders = new HashMap<>();
private Function<Component, Component> decorator = Function.identity(); private Function<Component, Component> decorator = Function.identity();

View file

@ -196,13 +196,11 @@ public final class ExamplePlugin extends JavaPlugin {
.withNoPermissionHandler() .withNoPermissionHandler()
.withArgumentParsingHandler() .withArgumentParsingHandler()
.withDecorator( .withDecorator(
component -> Component.text().append( component -> Component.text()
Component.text("[", NamedTextColor.DARK_GRAY) .append(Component.text("[", NamedTextColor.DARK_GRAY))
).append( .append(Component.text("Example", NamedTextColor.GOLD))
Component.text("Example", NamedTextColor.GOLD) .append(Component.text("] ", NamedTextColor.DARK_GRAY))
).append( .append(component).build()
Component.text("] ", NamedTextColor.DARK_GRAY)
).append(component).build()
).apply(manager, bukkitAudiences::sender); ).apply(manager, bukkitAudiences::sender);
// //
// Create the commands // Create the commands