Skip to content

Commit

Permalink
Merge pull request #727 from KyoriPowered/feature/mm-parse-error-impr…
Browse files Browse the repository at this point in the history
…ovement
  • Loading branch information
zml2008 committed Mar 7, 2022
1 parent ef0d837 commit 5c43855
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,17 @@ public UnaryOperator<Component> postProcessor() {

@Override
public @NotNull ParsingException newException(@NotNull final String message) {
return new ParsingExceptionImpl(message, this.message, null, EMPTY_TOKEN_ARRAY);
return new ParsingExceptionImpl(message, this.message, null, false, EMPTY_TOKEN_ARRAY);
}

@Override
public @NotNull ParsingException newException(final @NotNull String message, final @NotNull ArgumentQueue tags) {
return new ParsingExceptionImpl(message, this.message, tagsToTokens(((ArgumentQueueImpl<?>) tags).args));
return new ParsingExceptionImpl(message, this.message, null, false, tagsToTokens(((ArgumentQueueImpl<?>) tags).args));
}

@Override
public @NotNull ParsingException newException(final @NotNull String message, final @Nullable Throwable cause, final @NotNull ArgumentQueue tags) {
return new ParsingExceptionImpl(message, this.message, cause, tagsToTokens(((ArgumentQueueImpl<?>) tags).args));
return new ParsingExceptionImpl(message, this.message, cause, false, tagsToTokens(((ArgumentQueueImpl<?>) tags).args));
}

private static Token[] tagsToTokens(final List<? extends Tag.Argument> tags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ParsingExceptionImpl extends ParsingException {

private final String originalText;
private Token @NotNull [] tokens;
private final boolean withStackTrace;

/**
* Create a new parsing exception.
Expand All @@ -57,6 +58,7 @@ public ParsingExceptionImpl(
super(message);
this.tokens = tokens;
this.originalText = originalText;
this.withStackTrace = false;
}

/**
Expand All @@ -65,18 +67,21 @@ public ParsingExceptionImpl(
* @param message the detail message
* @param originalText the original text which was parsed
* @param cause the cause
* @param withStackTrace whether to generate a stacktrace
* @param tokens the token which caused the error
* @since 4.10.0
*/
public ParsingExceptionImpl(
final String message,
final @Nullable String originalText,
final @Nullable Throwable cause,
final boolean withStackTrace,
final @NotNull Token @NotNull ... tokens
) {
super(message, cause);
this.tokens = tokens;
this.originalText = originalText;
this.withStackTrace = withStackTrace;
}

@Override
Expand Down Expand Up @@ -142,7 +147,10 @@ private String arrow() {
}

@Override
public ParsingExceptionImpl fillInStackTrace() { // no stacktrace
public Throwable fillInStackTrace() {
if (this.withStackTrace) {
return super.fillInStackTrace();
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,21 @@ public static void parseString(final String message, final MatchedTokenConsumer<
final int length = message.length();
for (int i = 0; i < length; i++) {
final int codePoint = message.codePointAt(i);
if (codePoint == '§') {
throw new ParsingExceptionImpl(
"Legacy formatting codes have been detected in a component - this is unsupported behaviour. Please refer to the Adventure documentation (https://docs.adventure.kyori.net) for more information.",
message,
new Token(i, i + 2, TokenType.TEXT)
);
if (codePoint == '§' && i + 1 < length) {
final int nextChar = Character.toLowerCase(message.codePointAt(i + 1));
// Only throw an exception if the next character is actually going to make a legacy color code
if ((nextChar >= '0' && nextChar <= '9')
|| (nextChar >= 'a' && nextChar <= 'f')
|| nextChar == 'r'
|| (nextChar >= 'k' && nextChar <= 'o')) {
throw new ParsingExceptionImpl(
"Legacy formatting codes have been detected in a MiniMessage string - this is unsupported behaviour. Please refer to the Adventure documentation (https://docs.adventure.kyori.net) for more information.",
message,
null,
true,
new Token(i, i + 2, TokenType.TEXT)
);
}
}

if (!Character.isBmpCodePoint(codePoint)) {
Expand Down

0 comments on commit 5c43855

Please sign in to comment.