Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text-minimessage: Add a TagResolver for boolean values #857

Merged
merged 3 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,24 @@ private Formatter() {
return Tag.inserting(context.deserialize(choiceFormat.format(number)));
});
}

/**
* Creates a choice tag. This will use the first argument when true, otherwise the second argument.
*
* <p>This tag expects two formats as attributes.</p>
*
* <p>This replacement is auto-closing, so its style will not influence the style of following components.</p>
*
* @param key the key
* @param value the value
* @return the placeholder
* @since 4.13.0
*/
public static TagResolver booleanChoice(final @NotNull String key, final boolean value) {
return TagResolver.resolver(key, (argumentQueue, context) -> {
final String trueCase = argumentQueue.popOr("True format expected.").value();
final String falseCase = argumentQueue.popOr("False format expected.").value();
return Tag.inserting(context.deserialize(value ? trueCase : falseCase));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.junit.jupiter.api.Test;

import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.minimessage.tag.resolver.Formatter.booleanChoice;
import static net.kyori.adventure.text.minimessage.tag.resolver.Formatter.choice;
import static net.kyori.adventure.text.minimessage.tag.resolver.Formatter.date;
import static net.kyori.adventure.text.minimessage.tag.resolver.Formatter.number;
Expand Down Expand Up @@ -117,4 +118,11 @@ void testChoiceFormatter() {
this.assertParsedEquals(one, input, choice("choice", 1));
this.assertParsedEquals(bigResult, input, choice("choice", 2));
}

@Test
void testBooleanChoice() {
final String input = "<first:'<second:\\'<third:\"bah\":\"\">\\':\\'\\'>':''>";
final Component expected = text("bah");
this.assertParsedEquals(expected, input, booleanChoice("first", true), booleanChoice("second", true), booleanChoice("third", true));
}
}