Skip to content

Commit

Permalink
feat(minecraft): support sender-aware description decorators (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret authored May 26, 2022
1 parent 043a0a2 commit ce7565d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fix missing caption registration for the regex caption ([#351](https://github.com/Incendo/cloud/pull/351))

### Changed
- Minecraft: Support sender-aware description decorators in MinecraftHelp ([#354](https://github.com/Incendo/cloud/pull/354))

## [1.6.2]

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public final class MinecraftHelp<C> {
private BiFunction<C, String, String> stringMessageProvider = (sender, key) -> this.messageMap.get(key);
private MessageProvider<C> messageProvider =
(sender, key, args) -> text(this.stringMessageProvider.apply(sender, key));
private Function<String, Component> descriptionDecorator = Component::text;
private BiFunction<C, String, Component> descriptionDecorator = (sender, description) -> Component.text(description);
private HelpColors colors = DEFAULT_HELP_COLORS;
private int headerFooterLength = DEFAULT_HEADER_FOOTER_LENGTH;
private int maxResultsPerPage = DEFAULT_MAX_RESULTS_PER_PAGE;
Expand Down Expand Up @@ -206,6 +206,18 @@ public void commandFilter(final @NonNull Predicate<Command<C>> commandPredicate)
* @since 1.4.0
*/
public void descriptionDecorator(final @NonNull Function<@NonNull String, @NonNull Component> decorator) {
this.descriptionDecorator = (sender, description) -> decorator.apply(description);
}

/**
* Set the description decorator which will turn command and argument description strings into components.
* <p>
* The default decorator simply calls {@link Component#text(String)}
*
* @param decorator description decorator
* @since 1.7.0
*/
public void descriptionDecorator(final @NonNull BiFunction<@NonNull C, @NonNull String, @NonNull Component> decorator) {
this.descriptionDecorator = decorator;
}

Expand Down Expand Up @@ -390,7 +402,7 @@ private void printIndexHelpTopic(
} else if (helpEntry.getDescription().isEmpty()) {
description = this.messageProvider.provide(sender, MESSAGE_CLICK_TO_SHOW_HELP);
} else {
description = this.descriptionDecorator.apply(helpEntry.getDescription());
description = this.descriptionDecorator.apply(sender, helpEntry.getDescription());
}

final boolean lastBranch =
Expand Down Expand Up @@ -480,7 +492,7 @@ private void printVerboseHelpTopic(
} else if (helpTopic.getDescription().isEmpty()) {
topicDescription = this.messageProvider.provide(sender, MESSAGE_NO_DESCRIPTION);
} else {
topicDescription = this.descriptionDecorator.apply(helpTopic.getDescription());
topicDescription = this.descriptionDecorator.apply(sender, helpTopic.getDescription());
}

final boolean hasArguments = helpTopic.getCommand().getArguments().size() > 1;
Expand Down Expand Up @@ -526,7 +538,7 @@ private void printVerboseHelpTopic(
final ArgumentDescription description = component.getArgumentDescription();
if (!description.isEmpty()) {
textComponent.append(text(" - ", this.colors.accent));
textComponent.append(this.formatDescription(description).colorIfAbsent(this.colors.text));
textComponent.append(this.formatDescription(sender, description).colorIfAbsent(this.colors.text));
}

audience.sendMessage(textComponent);
Expand All @@ -535,11 +547,11 @@ private void printVerboseHelpTopic(
audience.sendMessage(this.footer(sender));
}

private Component formatDescription(final ArgumentDescription description) {
private Component formatDescription(final C sender, final ArgumentDescription description) {
if (description instanceof RichDescription) {
return ((RichDescription) description).getContents();
} else {
return this.descriptionDecorator.apply(description.getDescription());
return this.descriptionDecorator.apply(sender, description.getDescription());
}
}

Expand Down

0 comments on commit ce7565d

Please sign in to comment.