✨ Add highlighting to invalid syntax exception
This commit is contained in:
parent
c3d679d5ec
commit
7e09351cf1
2 changed files with 26 additions and 37 deletions
|
|
@ -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(
|
String.format("/%s", ((InvalidSyntaxException) e).getCorrectSyntax()),
|
||||||
Component.text(
|
NamedTextColor.GRAY
|
||||||
String.format("/%s", ((InvalidSyntaxException) e).getCorrectSyntax()),
|
).replaceText(SPECIAL_CHARACTERS_PATTERN, match -> match.color(NamedTextColor.WHITE)))
|
||||||
NamedTextColor.GRAY
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.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(
|
((InvalidCommandSenderException) e).getRequiredSender().getSimpleName(),
|
||||||
Component.text(
|
NamedTextColor.GRAY
|
||||||
((InvalidCommandSenderException) e).getRequiredSender().getSimpleName(),
|
))
|
||||||
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(
|
"I'm sorry, but you do not have permission to perform this command. \n"
|
||||||
Component.text(
|
+ "Please contact the server administrators if you believe that this is in error.",
|
||||||
"I'm sorry, but you do not have permission to perform this command. \n"
|
NamedTextColor.RED
|
||||||
+ "Please contact the server administrators if you believe that this is in error.",
|
);
|
||||||
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();
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue