-
-
Notifications
You must be signed in to change notification settings - Fork 107
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
feat(mini-message): add Formatter#joining #938
base: main/4
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a nice addition -- just a few comments. Could you also open a PR on adventure-docs
documenting this addition?
text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/resolver/Formatter.java
Outdated
Show resolved
Hide resolved
text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/resolver/Formatter.java
Outdated
Show resolved
Hide resolved
text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/resolver/Formatter.java
Outdated
Show resolved
Hide resolved
from what I can see, there is also a lastSeparatorIfSerial method which is mutually exclusive, in terms of functionality, with lastSeparator |
IIRC |
I already use such a resolver for my project but I don't use a collection of ComponentLike. Instead I use a Collection of TagResolvers instead, with: public static TagResolver joining(@TagPattern final @NotNull String key, final @NotNull Iterable<? extends TagResolver> components) {
return TagResolver.resolver(key, (arguments, ctx) -> {
String format = arguments.popOr("expected list format").value();
String separator = arguments.popOr("expected separator").value();
if (arguments.hasNext()) {
String emptyVal = arguments.pop().value();
if (resolvers.isEmpty()) return Tag.inserting(ctx.deserialize(emptyVal));
}
List<Component> components = resolvers.stream().map(r -> ctx.deserialize(format, r)).toList();
Component separatorComponent = ctx.deserialize(separator);
Component all = components.stream().collect(Component.toComponent(separatorComponent));
if (arguments.hasNext()) {
int amount = arguments.popOr("maximum amount").asInt().orElse(5);
String lastInfo = arguments.popOr("last entry").value();
String hoverFormat = arguments.popOr("hover").value();
if (resolvers.size() > amount) {
Component hover = ctx.deserialize(hoverFormat,
Formatter.number("total", resolvers.size()),
Formatter.number("remaining", resolvers.size() - amount),
Placeholder.component("all", all));
Component remaining = ctx.deserialize(lastInfo,
Formatter.number("total", resolvers.size()),
Formatter.number("remaining", resolvers.size() - amount)).hoverEvent(hover);
return Tag.selfClosingInserting(components.stream().limit(amount).collect(Component.toComponent(separatorComponent)).append(remaining));
}
}
return Tag.selfClosingInserting(all);
});
} My implementation is complex and I don't think we should use that as api. However you can easily change the format of the list entries, e.g. with such a minimessage format string
This will be formatted to:
Where I don't think we should add such complex logic to minimessage however I would add the TagResolver approach as you can easily modify the formatting. |
I added an argument for lastSeparatorIfSerial. I'm not sure if TagResolvers are the best approach here. |
any love? 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor changes that can be done before merge
text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/resolver/Formatter.java
Outdated
Show resolved
Hide resolved
text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/resolver/Formatter.java
Outdated
Show resolved
Hide resolved
43adecb
to
73c9e85
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made the changes myself, LGTM :p
This PR adds a method to Formatter to join components using JoinConfiguration.