Skip to content

Commit

Permalink
Cloud 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Feb 16, 2024
1 parent c176e42 commit d8509d9
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 317 deletions.
14 changes: 9 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ group = "xyz.jpenilla"
version = "1.8.4-SNAPSHOT".decorateVersion()
description = "Customizable trades for Wandering Traders."

val mcVersion = "1.20.2"
val mcVersion = "1.20.4"

repositories {
mavenCentral()
Expand All @@ -40,9 +40,10 @@ dependencies {
implementation("xyz.jpenilla", "legacy-plugin-base", "0.0.1+98-SNAPSHOT")
implementation("org.bstats", "bstats-bukkit", "3.0.2")

implementation(platform("cloud.commandframework:cloud-bom:1.8.4"))
implementation("cloud.commandframework", "cloud-paper")
implementation("cloud.commandframework", "cloud-minecraft-extras")
implementation(platform("org.incendo:cloud-bom:2.0.0-beta.2"))
implementation(platform("org.incendo:cloud-minecraft-bom:2.0.0-beta.3"))
implementation("org.incendo:cloud-paper")
implementation("org.incendo:cloud-minecraft-extras")

implementation("org.incendo.interfaces", "interfaces-paper", "1.0.0-SNAPSHOT")

Expand Down Expand Up @@ -90,7 +91,7 @@ tasks {
archiveFileName.set("${project.name}-${project.version}.jar")
sequenceOf(
"org.bstats",
"cloud.commandframework",
"org.incendo",
"xyz.jpenilla.pluginbase",
"net.kyori",
"io.papermc.lib",
Expand All @@ -116,6 +117,9 @@ tasks {
}
}
}
compileJava {
options.compilerArgs.add("-Xlint:-classfile,-processing")
}
}

fun lastCommitHash(): String = indraGit.commit()?.name?.substring(0, 7)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package xyz.jpenilla.wanderingtrades.command;

import cloud.commandframework.paper.PaperCommandManager;
import org.bukkit.command.CommandSender;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.CommandManager;
import xyz.jpenilla.pluginbase.legacy.Chat;
import xyz.jpenilla.wanderingtrades.WanderingTrades;

@DefaultQualifier(NonNull.class)
public abstract class BaseCommand {
protected final WanderingTrades plugin;
protected final Commands commands;
protected final PaperCommandManager<CommandSender> commandManager;
protected final CommandManager<CommandSender> commandManager;
protected final Chat chat;

protected BaseCommand(final WanderingTrades plugin, final Commands commands) {
Expand Down
85 changes: 31 additions & 54 deletions src/main/java/xyz/jpenilla/wanderingtrades/command/Commands.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
package xyz.jpenilla.wanderingtrades.command;

import cloud.commandframework.Command;
import cloud.commandframework.arguments.flags.CommandFlag;
import cloud.commandframework.brigadier.CloudBrigadierManager;
import cloud.commandframework.bukkit.BukkitCaptionKeys;
import cloud.commandframework.bukkit.CloudBukkitCapabilities;
import cloud.commandframework.captions.SimpleCaptionRegistry;
import cloud.commandframework.captions.StandardCaptionKeys;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.execution.FilteringCommandSuggestionProcessor;
import cloud.commandframework.execution.preprocessor.CommandPreprocessingContext;
import cloud.commandframework.keys.CloudKey;
import cloud.commandframework.keys.SimpleCloudKey;
import cloud.commandframework.paper.PaperCommandManager;
import io.leangen.geantyref.TypeToken;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.command.CommandSender;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.Command;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.bukkit.BukkitCaptionKeys;
import org.incendo.cloud.bukkit.CloudBukkitCapabilities;
import org.incendo.cloud.caption.CaptionProvider;
import org.incendo.cloud.caption.CaptionRegistry;
import org.incendo.cloud.caption.StandardCaptionKeys;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.execution.preprocessor.CommandPreprocessingContext;
import org.incendo.cloud.key.CloudKey;
import org.incendo.cloud.paper.PaperCommandManager;
import org.incendo.cloud.parser.flag.CommandFlag;
import xyz.jpenilla.wanderingtrades.WanderingTrades;
import xyz.jpenilla.wanderingtrades.command.argument.TradeConfigArgument;
import xyz.jpenilla.wanderingtrades.command.argument.TradeConfigParser;
import xyz.jpenilla.wanderingtrades.command.commands.AboutCommand;
import xyz.jpenilla.wanderingtrades.command.commands.ConfigCommands;
import xyz.jpenilla.wanderingtrades.command.commands.HelpCommand;
import xyz.jpenilla.wanderingtrades.command.commands.ReloadCommand;
import xyz.jpenilla.wanderingtrades.command.commands.SummonCommands;
import xyz.jpenilla.wanderingtrades.command.commands.TradeCommands;
import xyz.jpenilla.wanderingtrades.config.Messages;
import xyz.jpenilla.wanderingtrades.config.TradeConfig;

@DefaultQualifier(NonNull.class)
public final class Commands {
public static final CloudKey<WanderingTrades> PLUGIN = SimpleCloudKey.of("wt:plugin", TypeToken.get(WanderingTrades.class));
public static final CloudKey<WanderingTrades> PLUGIN = CloudKey.of("wt:plugin", TypeToken.get(WanderingTrades.class));

private final WanderingTrades plugin;
private final PaperCommandManager<CommandSender> commandManager;
Expand All @@ -44,17 +41,12 @@ private Commands(final WanderingTrades plugin, final PaperCommandManager<Command
this.plugin = plugin;
this.commandManager = commandManager;

this.commandManager.commandSuggestionProcessor(new FilteringCommandSuggestionProcessor<>(
FilteringCommandSuggestionProcessor.Filter.<CommandSender>contains(true).andTrimBeforeLastSpace()
));
new ExceptionHandler(plugin, commandManager).register();
this.registerMessageFactories();
if (this.commandManager.hasCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) {
this.commandManager.registerBrigadier();
final @Nullable CloudBrigadierManager<CommandSender, ?> brigManager = this.commandManager.brigadierManager();
if (brigManager != null) {
brigManager.setNativeNumberSuggestions(false);
}
} else if (this.commandManager.hasCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION)) {
this.commandManager.registerAsynchronousCompletions();
}
this.registerParsers();
this.commandManager.registerCommandPreProcessor(this::preProcessContext);
Expand All @@ -73,37 +65,27 @@ private void registerCommands() {
commands.forEach(BaseCommand::register);
}

public PaperCommandManager<CommandSender> commandManager() {
public CommandManager<CommandSender> commandManager() {
return this.commandManager;
}

private void registerMessageFactories() {
if (!(this.commandManager.captionRegistry() instanceof final SimpleCaptionRegistry<CommandSender> registry)) {
return;
}
registry.registerMessageFactory(
StandardCaptionKeys.ARGUMENT_PARSE_FAILURE_ENUM,
(caption, sender) -> Messages.COMMAND_ARGUMENT_PARSE_FAILURE_ENUM.message()
);
registry.registerMessageFactory(
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_LOCATION_MIXED_LOCAL_ABSOLUTE,
(caption, sender) -> Messages.COMMAND_ARGUMENT_PARSE_FAILURE_LOCATION_MIXED_LOCAL_ABSOLUTE.message()
);
registry.registerMessageFactory(
BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_LOCATION_INVALID_FORMAT,
(caption, sender) -> Messages.COMMAND_ARGUMENT_PARSE_FAILURE_LOCATION_INVALID_FORMAT.message()
final CaptionRegistry<CommandSender> registry = this.commandManager.captionRegistry();
registry.registerProvider(
CaptionProvider.<CommandSender>constantProvider()
.putCaption(StandardCaptionKeys.ARGUMENT_PARSE_FAILURE_ENUM, Messages.COMMAND_ARGUMENT_PARSE_FAILURE_ENUM.message())
.putCaption(BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_LOCATION_MIXED_LOCAL_ABSOLUTE, Messages.COMMAND_ARGUMENT_PARSE_FAILURE_LOCATION_MIXED_LOCAL_ABSOLUTE.message())
.putCaption(BukkitCaptionKeys.ARGUMENT_PARSE_FAILURE_LOCATION_INVALID_FORMAT, Messages.COMMAND_ARGUMENT_PARSE_FAILURE_LOCATION_INVALID_FORMAT.message())
.build()
);
}

private void registerParsers() {
this.commandManager.parserRegistry().registerParserSupplier(
TypeToken.get(TradeConfig.class),
parameters -> new TradeConfigArgument.Parser()
);
this.commandManager.parserRegistry().registerParser(TradeConfigParser.tradeConfigParser());
}

private void preProcessContext(final CommandPreprocessingContext<CommandSender> context) {
context.getCommandContext().store(PLUGIN, this.plugin);
context.commandContext().store(PLUGIN, this.plugin);
}

public CommandFlag.Builder<?> getFlag(final String name) {
Expand All @@ -114,20 +96,15 @@ public void registerFlag(final String name, final CommandFlag.Builder<?> flagBui
this.flagRegistry.put(name, flagBuilder);
}

public void register(final List<Command<CommandSender>> commands) {
public void register(final List<Command<? extends CommandSender>> commands) {
commands.forEach(this.commandManager::command);
}

public static void setup(final WanderingTrades plugin) {
final PaperCommandManager<CommandSender> manager;
try {
manager = PaperCommandManager.createNative(
plugin,
CommandExecutionCoordinator.simpleCoordinator()
);
} catch (final Exception ex) {
throw new RuntimeException("Failed to initialize command manager", ex);
}
final PaperCommandManager<CommandSender> manager = PaperCommandManager.createNative(
plugin,
ExecutionCoordinator.simpleCoordinator()
);
new Commands(plugin, manager);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package xyz.jpenilla.wanderingtrades.command;

import cloud.commandframework.CommandManager;
import cloud.commandframework.exceptions.ArgumentParseException;
import cloud.commandframework.exceptions.InvalidCommandSenderException;
import cloud.commandframework.exceptions.InvalidSyntaxException;
import cloud.commandframework.exceptions.NoPermissionException;
import cloud.commandframework.minecraft.extras.MinecraftExceptionHandler;
import java.util.Objects;
import java.util.regex.Pattern;
import net.kyori.adventure.text.Component;
Expand All @@ -14,7 +8,19 @@
import net.kyori.adventure.util.ComponentMessageThrowable;
import org.bukkit.command.CommandSender;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.CommandManager;
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.handling.ExceptionContextFactory;
import org.incendo.cloud.minecraft.extras.MinecraftExceptionHandler;
import org.incendo.cloud.minecraft.extras.caption.ComponentCaptionFormatter;
import org.incendo.cloud.util.TypeUtils;
import xyz.jpenilla.wanderingtrades.WanderingTrades;
import xyz.jpenilla.wanderingtrades.config.Messages;
import xyz.jpenilla.wanderingtrades.util.Components;
Expand All @@ -33,36 +39,37 @@ final class ExceptionHandler {
}

void register() {
new MinecraftExceptionHandler<CommandSender>()
.withHandler(MinecraftExceptionHandler.ExceptionType.NO_PERMISSION, ExceptionHandler::noPermission)
.withHandler(MinecraftExceptionHandler.ExceptionType.INVALID_SYNTAX, this::invalidSyntax)
.withHandler(MinecraftExceptionHandler.ExceptionType.INVALID_SENDER, this::invalidSender)
.withHandler(MinecraftExceptionHandler.ExceptionType.ARGUMENT_PARSING, this::argumentParsing)
.withHandler(MinecraftExceptionHandler.ExceptionType.COMMAND_EXECUTION, this::commandExecution)
.withDecorator(ExceptionHandler::decorate)
.apply(this.commandManager, this.plugin.audiences()::sender);
MinecraftExceptionHandler.create(this.plugin.audiences()::sender)
.handler(NoPermissionException.class, ExceptionHandler::noPermission)
.handler(InvalidSyntaxException.class, this::invalidSyntax)
.handler(InvalidCommandSenderException.class, this::invalidSender)
.handler(ArgumentParseException.class, this::argumentParsing)
.handler(CommandExecutionException.class, this::commandExecution)
.decorator(ExceptionHandler::decorate)
.registerTo(this.commandManager);
}

private Component commandExecution(final CommandSender commandSender, final Exception ex) {
final Throwable cause = ex.getCause();
private @Nullable ComponentLike commandExecution(final ComponentCaptionFormatter<CommandSender> formatter, final ExceptionContext<CommandSender, CommandExecutionException> e) {
final Throwable cause = e.exception().getCause();

final ExceptionContextFactory<CommandSender> factory = new ExceptionContextFactory<>(this.commandManager.exceptionController());
if (cause instanceof NoPermissionException noPermissionException) {
return noPermission(noPermissionException);
return noPermission(formatter, factory.createContext(e.context(), noPermissionException));
} else if (cause instanceof InvalidSyntaxException invalidSyntaxException) {
return this.invalidSyntax(invalidSyntaxException);
return this.invalidSyntax(formatter, factory.createContext(e.context(), invalidSyntaxException));
} else if (cause instanceof InvalidCommandSenderException invalidCommandSenderException) {
return this.invalidSender(invalidCommandSenderException);
return this.invalidSender(formatter, factory.createContext(e.context(), invalidCommandSenderException));
} else if (cause instanceof ArgumentParseException argumentParseException) {
return this.argumentParsing(argumentParseException);
return this.argumentParsing(formatter, factory.createContext(e.context(), argumentParseException));
}

return MinecraftExceptionHandler.DEFAULT_COMMAND_EXECUTION_FUNCTION.apply(ex);
return MinecraftExceptionHandler.<CommandSender>createDefaultCommandExecutionHandler().message(formatter, e);
}

private Component invalidSyntax(final Exception ex) {
final InvalidSyntaxException exception = (InvalidSyntaxException) ex;
private Component invalidSyntax(final ComponentCaptionFormatter<CommandSender> formatter, final ExceptionContext<CommandSender, InvalidSyntaxException> e) {
final InvalidSyntaxException exception = e.exception();
final Component correctSyntaxMessage = Component.text(
String.format("/%s", exception.getCorrectSyntax()),
String.format("/%s", exception.correctSyntax()),
NamedTextColor.GRAY
).replaceText(config -> {
config.match(SYNTAX_HIGHLIGHT_PATTERN);
Expand All @@ -73,22 +80,22 @@ private Component invalidSyntax(final Exception ex) {
);
}

private Component invalidSender(final Exception ex) {
final InvalidCommandSenderException exception = (InvalidCommandSenderException) ex;
private Component invalidSender(final ComponentCaptionFormatter<CommandSender> formatter, final ExceptionContext<CommandSender, InvalidCommandSenderException> e) {
final InvalidCommandSenderException exception = e.exception();
return Messages.COMMAND_INVALID_SENDER.withPlaceholders(
Components.placeholder("type", exception.getRequiredSender().getSimpleName())
Components.placeholder("type", TypeUtils.simpleName(exception.requiredSender()))
);
}

private Component argumentParsing(final Exception ex) {
final Component causeMessage = Objects.requireNonNull(ComponentMessageThrowable.getOrConvertMessage(ex.getCause()))
private Component argumentParsing(final ComponentCaptionFormatter<CommandSender> formatter, final ExceptionContext<CommandSender, ArgumentParseException> ex) {
final Component causeMessage = Objects.requireNonNull(ComponentMessageThrowable.getOrConvertMessage(ex.exception().getCause()))
.colorIfAbsent(NamedTextColor.GRAY);
return Messages.COMMAND_INVALID_ARGUMENT.withPlaceholders(
Components.placeholder("error", causeMessage)
);
}

private static Component noPermission(final Exception e) {
private static Component noPermission(final ComponentCaptionFormatter<CommandSender> formatter, final ExceptionContext<CommandSender, NoPermissionException> e) {
return Component.translatable("commands.help.failed", NamedTextColor.RED);
}

Expand Down
Loading

0 comments on commit d8509d9

Please sign in to comment.