Minor formatting changes to MinecraftHelp

This commit is contained in:
jmp 2020-12-21 15:42:34 -08:00 committed by Alexander Söderberg
parent df52e99a8b
commit f7765d8fe9

View file

@ -29,10 +29,9 @@ import cloud.commandframework.CommandHelpHandler;
import cloud.commandframework.CommandManager; import cloud.commandframework.CommandManager;
import cloud.commandframework.arguments.CommandArgument; import cloud.commandframework.arguments.CommandArgument;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.LinearComponents;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration;
@ -47,6 +46,10 @@ import java.util.Map;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import static net.kyori.adventure.text.Component.space;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.event.ClickEvent.runCommand;
/** /**
* Opinionated extension of {@link CommandHelpHandler} for Minecraft * Opinionated extension of {@link CommandHelpHandler} for Minecraft
* *
@ -281,14 +284,13 @@ public final class MinecraftHelp<C> {
final @NonNull String query final @NonNull String query
) { ) {
final Audience audience = this.getAudience(sender); final Audience audience = this.getAudience(sender);
audience.sendMessage(Identity.nil(), this.basicHeader(sender)); audience.sendMessage(this.basicHeader(sender));
audience.sendMessage(Identity.nil(), Component.text( audience.sendMessage(LinearComponents.linear(
this.messageProvider.apply(sender, MESSAGE_NO_RESULTS_FOR_QUERY) + ": \"", text(this.messageProvider.apply(sender, MESSAGE_NO_RESULTS_FOR_QUERY) + ": \"", this.colors.text),
this.colors.text this.highlight(text("/" + query, this.colors.highlight)),
) text("\"", this.colors.text)
.append(this.highlight(Component.text("/" + query, this.colors.highlight))) ));
.append(Component.text("\"", this.colors.text))); audience.sendMessage(this.footer(sender));
audience.sendMessage(Identity.nil(), this.footer(sender));
} }
private void printIndexHelpTopic( private void printIndexHelpTopic(
@ -308,11 +310,14 @@ public final class MinecraftHelp<C> {
final List<Component> header = new ArrayList<>(); final List<Component> header = new ArrayList<>();
header.add(this.paginatedHeader(sender, currentPage, maxPages)); header.add(this.paginatedHeader(sender, currentPage, maxPages));
header.add(this.showingResults(sender, query)); header.add(this.showingResults(sender, query));
header.add(this.lastBranch() header.add(text()
.append(Component.text( .append(this.lastBranch())
.append(text(
String.format(" %s:", this.messageProvider.apply(sender, MESSAGE_AVAILABLE_COMMANDS)), String.format(" %s:", this.messageProvider.apply(sender, MESSAGE_AVAILABLE_COMMANDS)),
this.colors.text this.colors.text
))); ))
.build()
);
return header; return header;
}, },
(helpEntry, isLastOfPage) -> { (helpEntry, isLastOfPage) -> {
@ -323,19 +328,21 @@ public final class MinecraftHelp<C> {
final boolean lastBranch = final boolean lastBranch =
isLastOfPage || helpTopic.getEntries().indexOf(helpEntry) == helpTopic.getEntries().size() - 1; isLastOfPage || helpTopic.getEntries().indexOf(helpEntry) == helpTopic.getEntries().size() - 1;
return Component.text(" ") return text()
.append(text(" "))
.append(lastBranch ? this.lastBranch() : this.branch()) .append(lastBranch ? this.lastBranch() : this.branch())
.append(this.highlight(Component.text( .append(this.highlight(text(
String.format(" /%s", helpEntry.getSyntaxString()), String.format(" /%s", helpEntry.getSyntaxString()),
this.colors.highlight this.colors.highlight
)) ))
.hoverEvent(Component.text(description, this.colors.text)) .hoverEvent(text(description, this.colors.text))
.clickEvent(ClickEvent.runCommand(this.commandPrefix + " " + helpEntry.getSyntaxString()))); .clickEvent(runCommand(this.commandPrefix + " " + helpEntry.getSyntaxString()))
)
.build();
}, },
(currentPage, maxPages) -> this.paginatedFooter(sender, currentPage, maxPages, query), (currentPage, maxPages) -> this.paginatedFooter(sender, currentPage, maxPages, query),
(attemptedPage, maxPages) -> this.pageOutOfRange(sender, attemptedPage, maxPages) (attemptedPage, maxPages) -> this.pageOutOfRange(sender, attemptedPage, maxPages)
).render(helpTopic.getEntries(), page, this.maxResultsPerPage).forEach(line -> ).render(helpTopic.getEntries(), page, this.maxResultsPerPage).forEach(audience::sendMessage);
audience.sendMessage(Identity.nil(), line));
} }
private void printMultiHelpTopic( private void printMultiHelpTopic(
@ -357,26 +364,25 @@ public final class MinecraftHelp<C> {
header.add(this.paginatedHeader(sender, currentPage, maxPages)); header.add(this.paginatedHeader(sender, currentPage, maxPages));
header.add(this.showingResults(sender, query)); header.add(this.showingResults(sender, query));
header.add(this.lastBranch() header.add(this.lastBranch()
.append(this.highlight(Component.text(" /" + helpTopic.getLongestPath(), this.colors.highlight)))); .append(this.highlight(text(" /" + helpTopic.getLongestPath(), this.colors.highlight))));
return header; return header;
}, },
(suggestion, isLastOfPage) -> { (suggestion, isLastOfPage) -> {
final boolean lastBranch = isLastOfPage final boolean lastBranch = isLastOfPage
|| helpTopic.getChildSuggestions().indexOf(suggestion) == helpTopic.getChildSuggestions().size() - 1; || helpTopic.getChildSuggestions().indexOf(suggestion) == helpTopic.getChildSuggestions().size() - 1;
return ComponentHelper.repeat(Component.space(), headerIndentation) return ComponentHelper.repeat(space(), headerIndentation)
.append(lastBranch ? this.lastBranch() : this.branch()) .append(lastBranch ? this.lastBranch() : this.branch())
.append(this.highlight(Component.text(" /" + suggestion, this.colors.highlight)) .append(this.highlight(text(" /" + suggestion, this.colors.highlight))
.hoverEvent(Component.text( .hoverEvent(text(
this.messageProvider.apply(sender, MESSAGE_CLICK_TO_SHOW_HELP), this.messageProvider.apply(sender, MESSAGE_CLICK_TO_SHOW_HELP),
this.colors.text this.colors.text
)) ))
.clickEvent(ClickEvent.runCommand(this.commandPrefix + " " + suggestion))); .clickEvent(runCommand(this.commandPrefix + " " + suggestion)));
}, },
(currentPage, maxPages) -> this.paginatedFooter(sender, currentPage, maxPages, query), (currentPage, maxPages) -> this.paginatedFooter(sender, currentPage, maxPages, query),
(attemptedPage, maxPages) -> this.pageOutOfRange(sender, attemptedPage, maxPages) (attemptedPage, maxPages) -> this.pageOutOfRange(sender, attemptedPage, maxPages)
).render(helpTopic.getChildSuggestions(), page, this.maxResultsPerPage).forEach(line -> ).render(helpTopic.getChildSuggestions(), page, this.maxResultsPerPage).forEach(audience::sendMessage);
audience.sendMessage(Identity.nil(), line));
} }
private void printVerboseHelpTopic( private void printVerboseHelpTopic(
@ -385,39 +391,30 @@ public final class MinecraftHelp<C> {
final CommandHelpHandler.@NonNull VerboseHelpTopic<C> helpTopic final CommandHelpHandler.@NonNull VerboseHelpTopic<C> helpTopic
) { ) {
final Audience audience = this.getAudience(sender); final Audience audience = this.getAudience(sender);
audience.sendMessage(Identity.nil(), this.basicHeader(sender)); audience.sendMessage(this.basicHeader(sender));
audience.sendMessage(Identity.nil(), this.showingResults(sender, query)); audience.sendMessage(this.showingResults(sender, query));
final String command = this.commandManager.getCommandSyntaxFormatter() final String command = this.commandManager.getCommandSyntaxFormatter()
.apply(helpTopic.getCommand().getArguments(), null); .apply(helpTopic.getCommand().getArguments(), null);
audience.sendMessage( audience.sendMessage(text()
Identity.nil(), this.lastBranch() .append(this.lastBranch())
.append(Component.text( .append(text(" " + this.messageProvider.apply(sender, MESSAGE_COMMAND) + ": ", this.colors.primary))
" " + this.messageProvider.apply(sender, MESSAGE_COMMAND) + ": ", .append(this.highlight(text("/" + command, this.colors.highlight)))
this.colors.primary
))
.append(this.highlight(Component.text("/" + command, this.colors.highlight)))
); );
final String topicDescription = helpTopic.getDescription().isEmpty() final String topicDescription = helpTopic.getDescription().isEmpty()
? this.messageProvider.apply(sender, MESSAGE_NO_DESCRIPTION) ? this.messageProvider.apply(sender, MESSAGE_NO_DESCRIPTION)
: helpTopic.getDescription(); : helpTopic.getDescription();
final boolean hasArguments = helpTopic.getCommand().getArguments().size() > 1; final boolean hasArguments = helpTopic.getCommand().getArguments().size() > 1;
audience.sendMessage( audience.sendMessage(text()
Identity.nil(), Component.text(" ") .append(text(" "))
.append(hasArguments ? this.branch() : this.lastBranch()) .append(hasArguments ? this.branch() : this.lastBranch())
.append(Component.text( .append(text(" " + this.messageProvider.apply(sender, MESSAGE_DESCRIPTION) + ": ", this.colors.primary))
" " + this.messageProvider.apply(sender, MESSAGE_DESCRIPTION) + ": ", .append(text(topicDescription, this.colors.text))
this.colors.primary
))
.append(Component.text(topicDescription, this.colors.text))
); );
if (hasArguments) { if (hasArguments) {
audience.sendMessage( audience.sendMessage(text()
Identity.nil(), Component.text(" ") .append(text(" "))
.append(this.lastBranch()) .append(this.lastBranch())
.append(Component.text( .append(text(" " + this.messageProvider.apply(sender, MESSAGE_ARGUMENTS) + ":", this.colors.primary))
" " + this.messageProvider.apply(sender, MESSAGE_ARGUMENTS) + ":",
this.colors.primary
))
); );
final Iterator<CommandComponent<C>> iterator = helpTopic.getCommand().getComponents().iterator(); final Iterator<CommandComponent<C>> iterator = helpTopic.getCommand().getComponents().iterator();
@ -431,36 +428,37 @@ public final class MinecraftHelp<C> {
String syntax = this.commandManager.getCommandSyntaxFormatter() String syntax = this.commandManager.getCommandSyntaxFormatter()
.apply(Collections.singletonList(argument), null); .apply(Collections.singletonList(argument), null);
final TextComponent.Builder textComponent = Component.text() final TextComponent.Builder textComponent = text()
.append(Component.text(" ")) .append(text(" "))
.append(iterator.hasNext() ? this.branch() : this.lastBranch()) .append(iterator.hasNext() ? this.branch() : this.lastBranch())
.append(this.highlight(Component.text(" " + syntax, this.colors.highlight))); .append(this.highlight(text(" " + syntax, this.colors.highlight)));
if (!argument.isRequired()) { if (!argument.isRequired()) {
textComponent.append(Component.text( textComponent.append(text(
" (" + this.messageProvider.apply(sender, MESSAGE_OPTIONAL) + ")", " (" + this.messageProvider.apply(sender, MESSAGE_OPTIONAL) + ")",
this.colors.alternateHighlight this.colors.alternateHighlight
)); ));
} }
final String description = component.getDescription().getDescription(); final String description = component.getDescription().getDescription();
if (!description.isEmpty()) { if (!description.isEmpty()) {
textComponent textComponent.append(text(" - ", this.colors.accent));
.append(Component.text(" - ", this.colors.accent)) textComponent.append(text(description, this.colors.text));
.append(Component.text(description, this.colors.text));
} }
audience.sendMessage(Identity.nil(), textComponent); audience.sendMessage(textComponent);
} }
} }
audience.sendMessage(Identity.nil(), this.footer(sender)); audience.sendMessage(this.footer(sender));
} }
private @NonNull Component showingResults( private @NonNull Component showingResults(
final @NonNull C sender, final @NonNull C sender,
final @NonNull String query final @NonNull String query
) { ) {
return Component.text(this.messageProvider.apply(sender, MESSAGE_SHOWING_RESULTS_FOR_QUERY) + ": \"", this.colors.text) return text()
.append(this.highlight(Component.text("/" + query, this.colors.highlight))) .append(text(this.messageProvider.apply(sender, MESSAGE_SHOWING_RESULTS_FOR_QUERY) + ": \"", this.colors.text))
.append(Component.text("\"", this.colors.text)); .append(this.highlight(text("/" + query, this.colors.highlight)))
.append(text("\"", this.colors.text))
.build();
} }
private @NonNull Component button( private @NonNull Component button(
@ -468,14 +466,14 @@ public final class MinecraftHelp<C> {
final @NonNull String command, final @NonNull String command,
final @NonNull String hoverText final @NonNull String hoverText
) { ) {
return Component.text() return text()
.append(Component.space()) .append(space())
.append(Component.text('[', this.colors.accent)) .append(text('[', this.colors.accent))
.append(Component.text(icon, this.colors.alternateHighlight)) .append(text(icon, this.colors.alternateHighlight))
.append(Component.text(']', this.colors.accent)) .append(text(']', this.colors.accent))
.append(Component.space()) .append(space())
.clickEvent(ClickEvent.runCommand(command)) .clickEvent(runCommand(command))
.hoverEvent(Component.text(hoverText, this.colors.text)) .hoverEvent(text(hoverText, this.colors.text))
.build(); .build();
} }
@ -512,7 +510,7 @@ public final class MinecraftHelp<C> {
return this.header(sender, previousPageButton); return this.header(sender, previousPageButton);
} }
final Component buttons = Component.text() final Component buttons = text()
.append(previousPageButton) .append(previousPageButton)
.append(this.line(3)) .append(this.line(3))
.append(nextPageButton).build(); .append(nextPageButton).build();
@ -524,7 +522,7 @@ public final class MinecraftHelp<C> {
final @NonNull Component title final @NonNull Component title
) { ) {
final int sideLength = (this.headerFooterLength - ComponentHelper.length(title)) / 2; final int sideLength = (this.headerFooterLength - ComponentHelper.length(title)) / 2;
return Component.text() return text()
.append(this.line(sideLength)) .append(this.line(sideLength))
.append(title) .append(title)
.append(this.line(sideLength)) .append(this.line(sideLength))
@ -532,7 +530,7 @@ public final class MinecraftHelp<C> {
} }
private @NonNull Component basicHeader(final @NonNull C sender) { private @NonNull Component basicHeader(final @NonNull C sender) {
return this.header(sender, Component.text( return this.header(sender, text(
" " + this.messageProvider.apply(sender, MESSAGE_HELP_TITLE) + " ", " " + this.messageProvider.apply(sender, MESSAGE_HELP_TITLE) + " ",
this.colors.highlight this.colors.highlight
)); ));
@ -543,34 +541,34 @@ public final class MinecraftHelp<C> {
final int currentPage, final int currentPage,
final int pages final int pages
) { ) {
return this.header(sender, Component.text() return this.header(sender, text()
.append(Component.text( .append(text(
" " + this.messageProvider.apply(sender, MESSAGE_HELP_TITLE) + " ", " " + this.messageProvider.apply(sender, MESSAGE_HELP_TITLE) + " ",
this.colors.highlight this.colors.highlight
)) ))
.append(Component.text("(", this.colors.alternateHighlight)) .append(text("(", this.colors.alternateHighlight))
.append(Component.text(currentPage, this.colors.text)) .append(text(currentPage, this.colors.text))
.append(Component.text("/", this.colors.alternateHighlight)) .append(text("/", this.colors.alternateHighlight))
.append(Component.text(pages, this.colors.text)) .append(text(pages, this.colors.text))
.append(Component.text(")", this.colors.alternateHighlight)) .append(text(")", this.colors.alternateHighlight))
.append(Component.space()) .append(space())
.build() .build()
); );
} }
private @NonNull Component line(final int length) { private @NonNull Component line(final int length) {
return ComponentHelper.repeat( return ComponentHelper.repeat(
Component.text("-", this.colors.primary, TextDecoration.STRIKETHROUGH), text("-", this.colors.primary, TextDecoration.STRIKETHROUGH),
length length
); );
} }
private @NonNull Component branch() { private @NonNull Component branch() {
return Component.text("├─", this.colors.accent); return text("├─", this.colors.accent);
} }
private @NonNull Component lastBranch() { private @NonNull Component lastBranch() {
return Component.text("└─", this.colors.accent); return text("└─", this.colors.accent);
} }
private @NonNull Component highlight(final @NonNull Component component) { private @NonNull Component highlight(final @NonNull Component component) {
@ -582,14 +580,12 @@ public final class MinecraftHelp<C> {
final int attemptedPage, final int attemptedPage,
final int maxPages final int maxPages
) { ) {
return this.highlight( return this.highlight(text(
Component.text( this.messageProvider.apply(sender, MESSAGE_PAGE_OUT_OF_RANGE)
this.messageProvider.apply(sender, MESSAGE_PAGE_OUT_OF_RANGE) .replace("<page>", String.valueOf(attemptedPage))
.replace("<page>", String.valueOf(attemptedPage)) .replace("<max_pages>", String.valueOf(maxPages)),
.replace("<max_pages>", String.valueOf(maxPages)), this.colors.text
this.colors.text ));
)
);
} }
/** /**