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

Component#replaceText not replacing content as expected #197

Closed
emilyy-dev opened this issue Nov 11, 2020 · 0 comments · Fixed by #199
Closed

Component#replaceText not replacing content as expected #197

emilyy-dev opened this issue Nov 11, 2020 · 0 comments · Fixed by #199

Comments

@emilyy-dev
Copy link
Contributor

Description

Component#replaceText does not seem to behave as one would expect, it appears it is appending the resulting replaced component to the original component, but this does not seem to happen on nested hover event TextComponents, I wouldn't know about other components (translatable, NBT, selectors, etc) or any other specifics about this issue (mainly because I want to waste as little time as possible, but I'll gladly try out other possibilities if needed!!).

This is what it looks like for playerprofiles load <profile> as only element in replacements (in the case of the hovered event).
I would expect to see /playerprofiles load <profile> like in the hover message, not /{0}/playerprofiles load <profile>.

Sample Code

public enum Message {
  USAGE_COMMAND(text().append(text("/{0}", RED))
                      .hoverEvent(text().append(text("Click to run"))
                                        .append(space())
                                        .append(text("/{0}", GRAY))
                                        .build().asHoverEvent()));

  private static final Pattern INDEX_PATTERN = Pattern.compile("\\{(\\d+)}");

  private final Component component;

  Message(final ComponentLike fromBuilder) {
    this.component = fromBuilder.asComponent();
  }

  public void send(final Audience audience, final Identity identity, final String... replacements) {
    final Component replaced = component.replaceText(INDEX_PATTERN, builder -> {
      final Matcher matcher = INDEX_PATTERN.matcher(builder.content());
      matcher.find();
      final int index = Integer.parseInt(matcher.group(1));
      return text(replacements[index]);
    });

    audience.sendMessage(identity, replaced);
  }
}

Environment info:

  • adventure-api 4.1.1
  • adventure-platform-bukkit 4.0.0-SNAPSHOT
  • Paper 1.16.4 b267
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants