Skip to content

Commit

Permalink
cloud 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Feb 4, 2024
1 parent ae93d66 commit 87ac77e
Show file tree
Hide file tree
Showing 26 changed files with 236 additions and 236 deletions.
1 change: 1 addition & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
compileOnlyApi(libs.gson)
compileOnlyApi(libs.guava)
api(platform(libs.cloudBom))
api(platform(libs.cloudMinecraftBom))
api(libs.cloudCore)
api(libs.cloudMinecraftExtras)
api(libs.configurateHocon)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
package xyz.jpenilla.tabtps.common;

import cloud.commandframework.CommandManager;
import java.nio.file.Path;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.CommandManager;
import org.slf4j.Logger;
import xyz.jpenilla.tabtps.common.command.Commander;
import xyz.jpenilla.tabtps.common.service.TickTimeService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
*/
package xyz.jpenilla.tabtps.common.command;

import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import java.util.function.UnaryOperator;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.Command;
import org.incendo.cloud.CommandManager;
import xyz.jpenilla.tabtps.common.TabTPS;

public final class Commands {
Expand All @@ -51,7 +51,7 @@ public void registerSubcommand(final @NonNull UnaryOperator<Command.Builder<Comm
return this.commandManager.commandBuilder("tabtps");
}

public void register(final Command.@NonNull Builder<Commander> builder) {
public void register(final Command.@NonNull Builder<? extends Commander> builder) {
this.commandManager.command(builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@
*/
package xyz.jpenilla.tabtps.common.command;

import cloud.commandframework.CommandManager;
import cloud.commandframework.captions.CaptionVariable;
import cloud.commandframework.exceptions.ArgumentParseException;
import cloud.commandframework.exceptions.CommandExecutionException;
import cloud.commandframework.exceptions.InvalidCommandSenderException;
import cloud.commandframework.exceptions.InvalidSyntaxException;
import cloud.commandframework.exceptions.NoPermissionException;
import cloud.commandframework.exceptions.parsing.ParserException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
Expand All @@ -41,6 +33,15 @@
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.util.ComponentMessageThrowable;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.caption.CaptionVariable;
import org.incendo.cloud.exception.ArgumentParseException;
import org.incendo.cloud.exception.CommandExecutionException;
import org.incendo.cloud.exception.InvalidCommandSenderException;
import org.incendo.cloud.exception.InvalidSyntaxException;
import org.incendo.cloud.exception.NoPermissionException;
import org.incendo.cloud.exception.handling.ExceptionContext;
import org.incendo.cloud.exception.parsing.ParserException;
import xyz.jpenilla.tabtps.common.Messages;
import xyz.jpenilla.tabtps.common.TabTPS;
import xyz.jpenilla.tabtps.common.command.exception.CommandCompletedException;
Expand Down Expand Up @@ -69,20 +70,20 @@ private static void decorateAndSend(final @NonNull Commander commander, final @N
}

public void apply(final @NonNull CommandManager<Commander> manager) {
manager.registerExceptionHandler(CommandExecutionException.class, this::commandExecution);
manager.registerExceptionHandler(NoPermissionException.class, this::noPermission);
manager.registerExceptionHandler(ArgumentParseException.class, this::argumentParsing);
manager.registerExceptionHandler(InvalidCommandSenderException.class, this::invalidSender);
manager.registerExceptionHandler(InvalidSyntaxException.class, this::invalidSyntax);
manager.exceptionController().registerHandler(CommandExecutionException.class, this::commandExecution);
manager.exceptionController().registerHandler(NoPermissionException.class, this::noPermission);
manager.exceptionController().registerHandler(ArgumentParseException.class, this::argumentParsing);
manager.exceptionController().registerHandler(InvalidCommandSenderException.class, this::invalidSender);
manager.exceptionController().registerHandler(InvalidSyntaxException.class, this::invalidSyntax);
}

private void commandExecution(final @NonNull Commander commander, final @NonNull CommandExecutionException exception) {
final Throwable cause = exception.getCause();
private void commandExecution(final @NonNull ExceptionContext<Commander, CommandExecutionException> ctx) {
final Throwable cause = ctx.exception().getCause();

if (cause instanceof CommandCompletedException) {
final Component message = ((CommandCompletedException) cause).componentMessage();
if (message != null) {
commander.sendMessage(message);
ctx.context().sender().sendMessage(message);
}
return;
}
Expand All @@ -105,49 +106,49 @@ private void commandExecution(final @NonNull Commander commander, final @NonNull
.append(Messages.MISC_TEXT_CLICK_TO_COPY.styled(GRAY, ITALIC));
final TextComponent.Builder message = text();
message.append(Messages.COMMAND_EXCEPTION_COMMAND_EXECUTION.styled(RED));
if (commander.hasPermission(Constants.PERMISSION_COMMAND_ERROR_HOVER_STACKTRACE)) {
if (ctx.context().sender().hasPermission(Constants.PERMISSION_COMMAND_ERROR_HOVER_STACKTRACE)) {
message.hoverEvent(hoverText.build());
message.clickEvent(copyToClipboard(stackTrace));
}
decorateAndSend(commander, message);
decorateAndSend(ctx.context().sender(), message);
}

private void noPermission(final @NonNull Commander commander, final @NonNull NoPermissionException exception) {
decorateAndSend(commander, Messages.COMMAND_EXCEPTION_NO_PERMISSION.styled(RED));
private void noPermission(final @NonNull ExceptionContext<Commander, NoPermissionException> ctx) {
decorateAndSend(ctx.context().sender(), Messages.COMMAND_EXCEPTION_NO_PERMISSION.styled(RED));
}

private void argumentParsing(final @NonNull Commander commander, final @NonNull ArgumentParseException exception) {
final Throwable cause = exception.getCause();
private void argumentParsing(final @NonNull ExceptionContext<Commander, ArgumentParseException> ctx) {
final Throwable cause = ctx.exception().getCause();
final Component message;
if (cause instanceof ParserException) {
final ParserException ex = (ParserException) cause;
message = translatable(
Messages.bundleName() + "/command.caption." + ex.errorCaption().getKey(),
Messages.bundleName() + "/command.caption." + ex.errorCaption().key(),
GRAY,
Arrays.stream(ex.captionVariables())
.map(CaptionVariable::getValue)
.map(CaptionVariable::value)
.map(Component::text)
.collect(Collectors.toList())
);
} else {
message = Objects.requireNonNull(ComponentMessageThrowable.getOrConvertMessage(cause)).color(GRAY);
}
decorateAndSend(commander, Messages.COMMAND_EXCEPTION_INVALID_ARGUMENT.styled(RED, message));
decorateAndSend(ctx.context().sender(), Messages.COMMAND_EXCEPTION_INVALID_ARGUMENT.styled(RED, message));
}

private void invalidSender(final @NonNull Commander commander, final @NonNull InvalidCommandSenderException exception) {
private void invalidSender(final @NonNull ExceptionContext<Commander, InvalidCommandSenderException> ctx) {
final Component message = Messages.COMMAND_EXCEPTION_INVALID_SENDER_TYPE.styled(
RED,
text(exception.getRequiredSender().getSimpleName())
text(ctx.exception().requiredSender().getSimpleName())
);
decorateAndSend(commander, message);
decorateAndSend(ctx.context().sender(), message);
}

private void invalidSyntax(final @NonNull Commander commander, final @NonNull InvalidSyntaxException exception) {
private void invalidSyntax(final @NonNull ExceptionContext<Commander, InvalidSyntaxException> ctx) {
final Component message = Messages.COMMAND_EXCEPTION_INVALID_SYNTAX.styled(
RED,
Components.highlight(text(String.format("/%s", exception.getCorrectSyntax()), GRAY), WHITE)
Components.highlight(text(String.format("/%s", ctx.exception().correctSyntax()), GRAY), WHITE)
);
decorateAndSend(commander, message);
decorateAndSend(ctx.context().sender(), message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/
package xyz.jpenilla.tabtps.common.command;

import cloud.commandframework.CommandManager;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.CommandManager;
import xyz.jpenilla.tabtps.common.TabTPS;

public abstract class TabTPSCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
*/
package xyz.jpenilla.tabtps.common.command.commands;

import cloud.commandframework.context.CommandContext;
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.context.CommandContext;
import xyz.jpenilla.tabtps.common.Messages;
import xyz.jpenilla.tabtps.common.TabTPS;
import xyz.jpenilla.tabtps.common.command.Commander;
Expand All @@ -43,6 +42,7 @@
import static net.kyori.adventure.text.format.NamedTextColor.GRAY;
import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
import static net.kyori.adventure.text.format.TextDecoration.STRIKETHROUGH;
import static org.incendo.cloud.minecraft.extras.RichDescription.richDescription;
import static xyz.jpenilla.tabtps.common.util.Components.gradient;
import static xyz.jpenilla.tabtps.common.util.Serializers.MINIMESSAGE;

Expand All @@ -54,13 +54,13 @@ public AboutCommand(final @NonNull TabTPS tabTPS, final @NonNull Commands comman
@Override
public void register() {
this.commands.registerSubcommand(builder -> builder.literal("about")
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, Messages.COMMAND_ABOUT_DESCRIPTION.plain())
.commandDescription(richDescription(Messages.COMMAND_ABOUT_DESCRIPTION.plain()))
.handler(this::executeAbout));
}

private void executeAbout(final @NonNull CommandContext<Commander> ctx) {
final Component header = gradient(" ", style -> style.decorate(STRIKETHROUGH), BLUE, WHITE, BLUE);
ctx.getSender().sendMessage(Components.ofChildren(
ctx.sender().sendMessage(Components.ofChildren(
header,
newline(),
text()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,26 @@
*/
package xyz.jpenilla.tabtps.common.command.commands;

import cloud.commandframework.CommandHelpHandler;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.standard.StringArgument;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.MinecraftHelp;
import cloud.commandframework.minecraft.extras.RichDescription;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.component.TypedCommandComponent;
import org.incendo.cloud.context.CommandContext;
import org.incendo.cloud.context.CommandInput;
import org.incendo.cloud.help.result.CommandEntry;
import org.incendo.cloud.minecraft.extras.ImmutableMinecraftHelp;
import org.incendo.cloud.minecraft.extras.MinecraftHelp;
import xyz.jpenilla.tabtps.common.Messages;
import xyz.jpenilla.tabtps.common.TabTPS;
import xyz.jpenilla.tabtps.common.command.Commander;
import xyz.jpenilla.tabtps.common.command.Commands;
import xyz.jpenilla.tabtps.common.command.TabTPSCommand;

import static net.kyori.adventure.text.Component.translatable;
import static org.incendo.cloud.minecraft.extras.RichDescription.richDescription;
import static org.incendo.cloud.parser.standard.StringParser.greedyStringParser;
import static org.incendo.cloud.suggestion.SuggestionProvider.blockingStrings;

public final class HelpCommand extends TabTPSCommand {
public HelpCommand(final @NonNull TabTPS tabTPS, final @NonNull Commands commands) {
Expand All @@ -50,43 +51,44 @@ public HelpCommand(final @NonNull TabTPS tabTPS, final @NonNull Commands command

@Override
public void register() {
final CommandArgument<Commander, String> queryArgument = StringArgument.<Commander>builder("query")
.greedy()
.asOptional()
.withSuggestionsProvider(this::helpQuerySuggestions)
final TypedCommandComponent<Commander, String> queryArgument = TypedCommandComponent.<Commander, String>builder()
.name("query")
.parser(greedyStringParser())
.optional()
.suggestionProvider(blockingStrings(this::helpQuerySuggestions))
.description(richDescription(Messages.COMMAND_HELP_ARGUMENTS_QUERY))
.build();

this.commands.registerSubcommand(builder -> builder.literal("help")
.argument(queryArgument, RichDescription.of(Messages.COMMAND_HELP_ARGUMENTS_QUERY))
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, Messages.COMMAND_HELP_DESCRIPTION.plain())
.argument(queryArgument)
.commandDescription(richDescription(Messages.COMMAND_HELP_DESCRIPTION.plain()))
.handler(this::executeHelp));
}

private void executeHelp(final @NonNull CommandContext<Commander> context) {
final String query = context.getOrDefault("query", null);
this.help().queryCommands(query == null ? "" : query, context.getSender());
this.help().queryCommands(query == null ? "" : query, context.sender());
}

public @NonNull List<String> helpQuerySuggestions(final @NonNull CommandContext<Commander> context, final @NonNull String input) {
return ((CommandHelpHandler.IndexHelpTopic<Commander>) this.commands.commandManager().createCommandHelpHandler()
.queryHelp(context.getSender(), ""))
.getEntries()
public @NonNull Iterable<String> helpQuerySuggestions(final @NonNull CommandContext<Commander> context, final @NonNull CommandInput input) {
return this.commands.commandManager().createHelpHandler()
.queryRootIndex(context.sender())
.entries()
.stream()
.map(CommandHelpHandler.VerboseHelpEntry::getSyntaxString)
.map(CommandEntry::syntax)
.collect(Collectors.toList());
}

private @NonNull MinecraftHelp<Commander> help() {
final MinecraftHelp<Commander> help = MinecraftHelp.createNative("/tabtps help", this.commands.commandManager());
help.setHelpColors(this.tabTPS.configManager().pluginSettings().helpColors().toCloud());
help.messageProvider(HelpCommand::helpMessage);
return help;
return ImmutableMinecraftHelp.copyOf(help).withColors(this.tabTPS.configManager().pluginSettings().helpColors().toCloud())
.withMessageProvider(HelpCommand::helpMessage);
}

private static @NonNull Component helpMessage(final @NonNull Commander sender, final @NonNull String key, final @NonNull String... args) {
private static @NonNull Component helpMessage(final @NonNull Commander sender, final @NonNull String key, final @NonNull Map<String, String> args) {
return translatable(
Messages.bundleName() + "/help." + key,
Arrays.stream(args)
args.values().stream() // todo
.map(Component::text)
.collect(Collectors.toList())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@
*/
package xyz.jpenilla.tabtps.common.command.commands;

import cloud.commandframework.context.CommandContext;
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.context.CommandContext;
import xyz.jpenilla.tabtps.common.Messages;
import xyz.jpenilla.tabtps.common.TabTPS;
import xyz.jpenilla.tabtps.common.command.Commander;
Expand All @@ -45,6 +44,7 @@
import static net.kyori.adventure.text.Component.space;
import static net.kyori.adventure.text.format.NamedTextColor.GRAY;
import static net.kyori.adventure.text.format.TextDecoration.ITALIC;
import static org.incendo.cloud.minecraft.extras.RichDescription.richDescription;

public final class MemoryCommand extends TabTPSCommand {
public MemoryCommand(final @NonNull TabTPS tabTPS, final @NonNull Commands commands) {
Expand All @@ -55,7 +55,7 @@ public MemoryCommand(final @NonNull TabTPS tabTPS, final @NonNull Commands comma
public void register() {
this.commands.register(this.commandManager.commandBuilder("memory", "mem", "ram")
.permission(Constants.PERMISSION_COMMAND_TICKINFO)
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, Messages.COMMAND_MEMORY_DESCRIPTION.plain())
.commandDescription(richDescription(Messages.COMMAND_MEMORY_DESCRIPTION.plain()))
.handler(this::executeMemory));
}

Expand All @@ -79,6 +79,6 @@ private void executeMemory(final @NonNull CommandContext<Commander> ctx) {
.sorted(Comparator.comparing(MemoryPoolMXBean::getName))
.map(bean -> MemoryUtil.renderBar(bean.getName(), bean.getUsage(), 60))
.forEach(messages::add);
messages.forEach(ctx.getSender()::sendMessage);
messages.forEach(ctx.sender()::sendMessage);
}
}
Loading

0 comments on commit 87ac77e

Please sign in to comment.