feat(minecraft): support sender-aware description decorators (#354)

This commit is contained in:
Alexander Söderberg 2022-05-26 06:54:49 +02:00 committed by Jason
parent d681ba5840
commit 40b51676fe
2 changed files with 21 additions and 6 deletions

View file

@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Fix missing caption registration for the regex caption ([#351](https://github.com/Incendo/cloud/pull/351)) - 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] ## [1.6.2]
### Fixed ### Fixed

View file

@ -97,7 +97,7 @@ public final class MinecraftHelp<C> {
private BiFunction<C, String, String> stringMessageProvider = (sender, key) -> this.messageMap.get(key); private BiFunction<C, String, String> stringMessageProvider = (sender, key) -> this.messageMap.get(key);
private MessageProvider<C> messageProvider = private MessageProvider<C> messageProvider =
(sender, key, args) -> text(this.stringMessageProvider.apply(sender, key)); (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 HelpColors colors = DEFAULT_HELP_COLORS;
private int headerFooterLength = DEFAULT_HEADER_FOOTER_LENGTH; private int headerFooterLength = DEFAULT_HEADER_FOOTER_LENGTH;
private int maxResultsPerPage = DEFAULT_MAX_RESULTS_PER_PAGE; private int maxResultsPerPage = DEFAULT_MAX_RESULTS_PER_PAGE;
@ -206,6 +206,18 @@ public final class MinecraftHelp<C> {
* @since 1.4.0 * @since 1.4.0
*/ */
public void descriptionDecorator(final @NonNull Function<@NonNull String, @NonNull Component> decorator) { 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; this.descriptionDecorator = decorator;
} }
@ -390,7 +402,7 @@ public final class MinecraftHelp<C> {
} else if (helpEntry.getDescription().isEmpty()) { } else if (helpEntry.getDescription().isEmpty()) {
description = this.messageProvider.provide(sender, MESSAGE_CLICK_TO_SHOW_HELP); description = this.messageProvider.provide(sender, MESSAGE_CLICK_TO_SHOW_HELP);
} else { } else {
description = this.descriptionDecorator.apply(helpEntry.getDescription()); description = this.descriptionDecorator.apply(sender, helpEntry.getDescription());
} }
final boolean lastBranch = final boolean lastBranch =
@ -480,7 +492,7 @@ public final class MinecraftHelp<C> {
} else if (helpTopic.getDescription().isEmpty()) { } else if (helpTopic.getDescription().isEmpty()) {
topicDescription = this.messageProvider.provide(sender, MESSAGE_NO_DESCRIPTION); topicDescription = this.messageProvider.provide(sender, MESSAGE_NO_DESCRIPTION);
} else { } else {
topicDescription = this.descriptionDecorator.apply(helpTopic.getDescription()); topicDescription = this.descriptionDecorator.apply(sender, helpTopic.getDescription());
} }
final boolean hasArguments = helpTopic.getCommand().getArguments().size() > 1; final boolean hasArguments = helpTopic.getCommand().getArguments().size() > 1;
@ -526,7 +538,7 @@ public final class MinecraftHelp<C> {
final ArgumentDescription description = component.getArgumentDescription(); final ArgumentDescription description = component.getArgumentDescription();
if (!description.isEmpty()) { if (!description.isEmpty()) {
textComponent.append(text(" - ", this.colors.accent)); 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); audience.sendMessage(textComponent);
@ -535,11 +547,11 @@ public final class MinecraftHelp<C> {
audience.sendMessage(this.footer(sender)); audience.sendMessage(this.footer(sender));
} }
private Component formatDescription(final ArgumentDescription description) { private Component formatDescription(final C sender, final ArgumentDescription description) {
if (description instanceof RichDescription) { if (description instanceof RichDescription) {
return ((RichDescription) description).getContents(); return ((RichDescription) description).getContents();
} else { } else {
return this.descriptionDecorator.apply(description.getDescription()); return this.descriptionDecorator.apply(sender, description.getDescription());
} }
} }