-
-
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
api: Advanced Component joining #344
Conversation
af553f7
to
dc1994e
Compare
Just made all those requested changes in addition to cleaning up the Javadoc a bit. I just have two further questions:
|
api/src/main/java/net/kyori/adventure/text/JoinConfiguration.java
Outdated
Show resolved
Hide resolved
api/src/main/java/net/kyori/adventure/text/JoinConfiguration.java
Outdated
Show resolved
Hide resolved
api/src/main/java/net/kyori/adventure/text/JoinConfiguration.java
Outdated
Show resolved
Hide resolved
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.
Looks great to me, would be very usefulness to be able to join components very easily. I think it might be useful though to be able to configure this from just a plain string as well as from component like objects, instead of having to construct a text component every time.
Changes since last commit:
Yeah I'm not sure about this - there's no place in the API that accepts components that also accepts methods to "easily" create components from strings, etc. |
api/src/main/java/net/kyori/adventure/text/JoinConfiguration.java
Outdated
Show resolved
Hide resolved
api/src/main/java/net/kyori/adventure/text/JoinConfiguration.java
Outdated
Show resolved
Hide resolved
*/ | ||
@Deprecated |
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.
I don't think we should deprecate this method - it is perfectly fine as a convenient shorthand.
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.
Text replacement methods got the same treatment - deprecated in favour of configuration. It is easier to maintain JoinConfiguration
and not worrying about updating things here too.
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.
I don't consider arbitrary configuration objects to be particularly idiomatic unless there's no good way to separate the logic from the configuration. To me, an API that joins things together is a natural fit for combining the two into a single interface with configuration and logic. (See a comment below for an example of my idealized version of the API.)
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 PR is simply matching how we do this in the rest of the Adventure API, and I tend to stick with consistency with how to use things.
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 brings up a good point -- perhaps we should un-deprecate replaceText
methods for the most simple case of Pattern -> Component, as a convenience option.
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.
Another solution is to just add convenience methods to TextReplacementConfig
similar to the separator
methods in JoinConfiguration
, for example.
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.
hm, I think if we were doing this from the start that would be good, but in the interest of minimizing API churn, I think maintaining the older methods would be the better way to go for both this and replacement.
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.
Thinking about this again, and with #418 I think replacing all these methods with one that takes a JoinConfiguration
is the least awkward way forward.
*/ | ||
@Deprecated |
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.
Ditto
* @since 4.8.0 | ||
*/ | ||
@Contract(value = "_, _ -> new", pure = true) | ||
static @NonNull TextComponent join(final @NonNull JoinConfiguration config, final @NonNull Iterable<? extends ComponentLike> components) { |
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.
Would this API be more idiomatic as a builder, or a modifiable (albeit immutable) ComponentJoiner
object?
private static final ComponentJoiner PLUGIN_JOINER = Component.joiner(Component.text(", "))
.prefix(prefix)
.suffix(suffix);
// ...
public static void sendPluginList(Audience target) {
target.sendMessage(PLUGIN_JOINER.join(getPluginNameComponents()));
}
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.
There is precedent here: see Guava Joiner
and Splitter
which uses the pattern I've described here.
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 PR is matching how we do this in the rest of the Adventure API, however.
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.
I'm still not a big fan of it. I guess it's something that can be revisited in Adventure 5, because I still believe this pattern feels unnatural to write.
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.
I do agree that a shorthand method to join directly from a join configuration would be nice, an earlier version of the PR did include one. Although I do understand that the syntax doesn't differ too much whether you're doing config.join(components)
or join(config, components)
and the latter is certainly more in keeping with the rest of the API, such as the text replacement system.
Remaining questions and thoughts I have about this PR:
|
*/ | ||
@Deprecated |
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 brings up a good point -- perhaps we should un-deprecate replaceText
methods for the most simple case of Pattern -> Component, as a convenience option.
api/src/main/java/net/kyori/adventure/text/JoinConfiguration.java
Outdated
Show resolved
Hide resolved
Looks good but I agree with @astei re: the deprecations |
Rebased and applied zml's suggestions. As for the deprecation of the "shorthand" methods, I believe this PR should be merged with the deprecations in place, and the removal of the deprecations for this and the |
Looking back at some of your comments:
|
Agreed, it's definitely more in keeping with
Do you mean the methods like
Yes, the return here is only done because that's what the old methods returned. |
Yeah, I think those last two should be removed too.
oh oops, nvm that then |
api/src/main/java/net/kyori/adventure/text/JoinConfiguration.java
Outdated
Show resolved
Hide resolved
Just rebased this for a 4.9.0 release. I still think the questions regarding deprecation are valid, but I do still think they are better served in another PR that would undeprecate both sets of methods or one that provides shorthand methods for |
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.
Can you deprecate TextComponent.ofChildren
as well, to be replaced by join(JoinConfiguration.noSeparator())
?
*/ | ||
@Deprecated |
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.
Thinking about this again, and with #418 I think replacing all these methods with one that takes a JoinConfiguration
is the least awkward way forward.
0718fac
to
b700073
Compare
This PR adds a new class,
JoinConfiguration
, which introduces a more advanced method of joining components. Essentially, this class would allow you to create a component likeOnline players: Bob, John, Harry and Martin.
from a simple collection of names with ease. A join configuration contains a series of component parts:ComponentLike
into aComponent
.There are also some unit tests to ensure everything works correctly.