Skip to content

Commit

Permalink
Made the requested changes.
Browse files Browse the repository at this point in the history
Moved the nag to `TokenParser` and to end of `buildTree` method.
  • Loading branch information
LinoxGH authored and zml2008 committed Feb 28, 2022
1 parent 2280a7b commit 20e19ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ final class MiniMessageParser {

@NotNull String escapeTokens(final @NotNull String richMessage, final @NotNull ContextImpl context) {
final StringBuilder sb = new StringBuilder(richMessage.length());
this.tryToNag(richMessage, context.strict());
this.escapeTokens(sb, richMessage, context);
return sb.toString();
}
Expand All @@ -85,14 +84,13 @@ void escapeTokens(final StringBuilder sb, final @NotNull String richMessage, fin

@NotNull String stripTokens(final @NotNull String richMessage, final @NotNull ContextImpl context) {
final StringBuilder sb = new StringBuilder(richMessage.length());
this.tryToNag(richMessage, context.strict());
this.processTokens(sb, richMessage, context, (token, builder) -> {});
return sb.toString();
}

private void processTokens(final @NotNull StringBuilder sb, final @NotNull String richMessage, final @NotNull ContextImpl context, final BiConsumer<Token, StringBuilder> tagHandler) {
final TagResolver combinedResolver = TagResolver.resolver(this.tagResolver, context.extraTags());
final List<Token> root = TokenParser.tokenize(richMessage);
final List<Token> root = TokenParser.tokenize(richMessage, context.strict());
for (final Token token : root) {
switch (token.type()) {
case TEXT:
Expand Down Expand Up @@ -127,7 +125,6 @@ private void processTokens(final @NotNull StringBuilder sb, final @NotNull Strin
debug.accept(richMessage);
debug.accept("\n");
}
this.tryToNag(richMessage, context.strict());

final TokenParser.TagProvider transformationFactory;
if (debug != null) {
Expand Down Expand Up @@ -268,10 +265,4 @@ private Component handleModifying(final Modifying modTransformation, final Compo
}
return newComp;
}

private void tryToNag(final String input, final boolean strict) {
if (input.contains("§") && strict) {
throw new IllegalArgumentException("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.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static RootNode parse(
final boolean strict
) throws ParsingException {
// collect tokens...
final List<Token> tokens = tokenize(message);
final List<Token> tokens = tokenize(message, strict);

// then build the tree!
return buildTree(tagProvider, tagNameChecker, tokens, message, originalMessage, strict);
Expand Down Expand Up @@ -120,10 +120,12 @@ public static String resolvePreProcessTags(final String message, final TagProvid
* Tokenize a minimessage string into a list of tokens.
*
* @param message the minimessage string to parse
* @param strict whether to be strict in tokenizing
* @return the root tokens
* @since 4.10.0
*/
public static List<Token> tokenize(final String message) {
public static List<Token> tokenize(final String message, final boolean strict) {
tryToNag(message, strict);
final TokenListProducingMatchedTokenConsumer listProducer = new TokenListProducingMatchedTokenConsumer(message);
parseString(message, listProducer);
final List<Token> tokens = listProducer.result();
Expand Down Expand Up @@ -612,6 +614,19 @@ public static String unescape(final String text, final int startIndex, final int
return sb.toString();
}

/*
* Nags the user about using legacy symbol, if they used it.
*
* @param input the input text
* @param strict strict mode
* @since 4.10.0
*/
private static void tryToNag(final String input, final boolean strict) {
if (input.contains("§") && strict) {
throw new IllegalArgumentException("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.");
}
}

/**
* Normalizing provider for tag information.
*
Expand Down

0 comments on commit 20e19ab

Please sign in to comment.