diff --git a/bukkit/src/main/java/net/draycia/carbon/bukkit/CarbonChatBukkitModule.java b/bukkit/src/main/java/net/draycia/carbon/bukkit/CarbonChatBukkitModule.java index 22f0a3ac..c442073d 100644 --- a/bukkit/src/main/java/net/draycia/carbon/bukkit/CarbonChatBukkitModule.java +++ b/bukkit/src/main/java/net/draycia/carbon/bukkit/CarbonChatBukkitModule.java @@ -2,10 +2,12 @@ package net.draycia.carbon.bukkit; import cloud.commandframework.CommandManager; import cloud.commandframework.brigadier.CloudBrigadierManager; +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.execution.AsynchronousCommandExecutionCoordinator; -import cloud.commandframework.minecraft.extras.MinecraftExceptionHandler; import cloud.commandframework.paper.PaperCommandManager; import com.google.inject.AbstractModule; import com.google.inject.Provides; @@ -21,6 +23,7 @@ import net.draycia.carbon.bukkit.command.BukkitPlayerCommander; import net.draycia.carbon.common.CarbonCommonModule; import net.draycia.carbon.common.ForCarbon; import net.draycia.carbon.common.command.Commander; +import net.draycia.carbon.common.messages.CarbonMessageService; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.util.ComponentMessageThrowable; @@ -70,41 +73,7 @@ public final class CarbonChatBukkitModule extends AbstractModule { throw new RuntimeException("Failed to initialize command manager.", ex); } - new MinecraftExceptionHandler() - .withHandler(MinecraftExceptionHandler.ExceptionType.ARGUMENT_PARSING, exception -> { - final var throwableMessage = message(exception.getCause()); - - return this.carbonChat.messageService().errorCommandArgumentParsing(throwableMessage); - }) - .withHandler(MinecraftExceptionHandler.ExceptionType.INVALID_SENDER, exception -> { - final var senderType = ((InvalidCommandSenderException) exception) - .getRequiredSender().getSimpleName(); - - return this.carbonChat.messageService().errorCommandInvalidSender(senderType); - }) - .withHandler(MinecraftExceptionHandler.ExceptionType.INVALID_SYNTAX, exception -> { - final var syntax = - Component.text(((InvalidSyntaxException) exception).getCorrectSyntax()).replaceText( - config -> config.match(SPECIAL_CHARACTERS_PATTERN) - .replacement(match -> match.color(NamedTextColor.WHITE))); - - return this.carbonChat.messageService().errorCommandInvalidSyntax(syntax); - }) - .withHandler(MinecraftExceptionHandler.ExceptionType.NO_PERMISSION, exception -> { - return this.carbonChat.messageService().errorCommandNoPermission(); - }) - .withHandler(MinecraftExceptionHandler.ExceptionType.COMMAND_EXECUTION, exception -> { - final Throwable cause = exception.getCause(); - cause.printStackTrace(); - - final StringWriter writer = new StringWriter(); - cause.printStackTrace(new PrintWriter(writer)); - final String stackTrace = writer.toString().replaceAll("\t", " "); - final @Nullable Component throwableMessage = message(cause); - - return this.carbonChat.messageService().errorCommandCommandExecution(throwableMessage, stackTrace); - }) - .apply(commandManager, commander -> commander); + decorateCommandManager(commandManager, this.carbonChat.messageService()); commandManager.registerAsynchronousCompletions(); commandManager.registerBrigadier(); @@ -119,6 +88,52 @@ public final class CarbonChatBukkitModule extends AbstractModule { return commandManager; } + // This should be in common and applied to every platforms command manager, not in bukkit module + private static void decorateCommandManager( + final CommandManager commandManager, + final CarbonMessageService messageService + ) { + registerExceptionHandlers(commandManager, messageService); + } + + private static void registerExceptionHandlers( + final CommandManager commandManager, + final CarbonMessageService messageService + ) { + commandManager.registerExceptionHandler(ArgumentParseException.class, (sender, exception) -> { + final var throwableMessage = message(exception.getCause()); + + messageService.errorCommandArgumentParsing(sender, throwableMessage); + }); + commandManager.registerExceptionHandler(InvalidCommandSenderException.class, (sender, exception) -> { + final var senderType = exception.getRequiredSender().getSimpleName(); + + messageService.errorCommandInvalidSender(sender, senderType); + }); + commandManager.registerExceptionHandler(InvalidSyntaxException.class, (sender, exception) -> { + final var syntax = + Component.text(exception.getCorrectSyntax()).replaceText( + config -> config.match(SPECIAL_CHARACTERS_PATTERN) + .replacement(match -> match.color(NamedTextColor.WHITE))); + + messageService.errorCommandInvalidSyntax(sender, syntax); + }); + commandManager.registerExceptionHandler(NoPermissionException.class, (sender, exception) -> { + messageService.errorCommandNoPermission(sender); + }); + commandManager.registerExceptionHandler(CommandExecutionException.class, (sender, exception) -> { + final Throwable cause = exception.getCause(); + cause.printStackTrace(); + + final StringWriter writer = new StringWriter(); + cause.printStackTrace(new PrintWriter(writer)); + final String stackTrace = writer.toString().replaceAll("\t", " "); + final @Nullable Component throwableMessage = message(cause); + + messageService.errorCommandCommandExecution(sender, throwableMessage, stackTrace); + }); + } + @Override public void configure() { this.install(new CarbonCommonModule()); diff --git a/common/src/main/java/net/draycia/carbon/common/command/arguments/CarbonPlayerArgument.java b/common/src/main/java/net/draycia/carbon/common/command/argument/CarbonPlayerArgument.java similarity index 98% rename from common/src/main/java/net/draycia/carbon/common/command/arguments/CarbonPlayerArgument.java rename to common/src/main/java/net/draycia/carbon/common/command/argument/CarbonPlayerArgument.java index bbb9a062..aba1bc92 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/arguments/CarbonPlayerArgument.java +++ b/common/src/main/java/net/draycia/carbon/common/command/argument/CarbonPlayerArgument.java @@ -1,4 +1,4 @@ -package net.draycia.carbon.common.command.arguments; +package net.draycia.carbon.common.command.argument; import cloud.commandframework.arguments.CommandArgument; import cloud.commandframework.arguments.parser.ArgumentParseResult; @@ -8,7 +8,6 @@ import com.google.inject.Inject; import java.util.List; import java.util.Queue; import java.util.function.BiPredicate; -import java.util.function.Predicate; import net.draycia.carbon.api.CarbonServer; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.common.command.Commander; diff --git a/common/src/main/java/net/draycia/carbon/common/command/arguments/OptionValueParser.java b/common/src/main/java/net/draycia/carbon/common/command/argument/OptionValueParser.java similarity index 98% rename from common/src/main/java/net/draycia/carbon/common/command/arguments/OptionValueParser.java rename to common/src/main/java/net/draycia/carbon/common/command/argument/OptionValueParser.java index 0ce175da..89d97522 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/arguments/OptionValueParser.java +++ b/common/src/main/java/net/draycia/carbon/common/command/argument/OptionValueParser.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package net.draycia.carbon.common.command.arguments; +package net.draycia.carbon.common.command.argument; import cloud.commandframework.arguments.parser.ArgumentParseResult; import cloud.commandframework.arguments.parser.ArgumentParser; diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/DebugCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/DebugCommand.java index 237783e8..1d6d7fc7 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/DebugCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/DebugCommand.java @@ -6,7 +6,7 @@ import java.util.ArrayList; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.PlayerCommander; -import net.draycia.carbon.common.command.arguments.CarbonPlayerArgument; +import net.draycia.carbon.common.command.argument.CarbonPlayerArgument; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.JoinConfiguration; import net.kyori.adventure.text.format.NamedTextColor; diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/HelpCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/HelpCommand.java index dd10ce9f..c191f63f 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/HelpCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/HelpCommand.java @@ -9,7 +9,6 @@ import cloud.commandframework.minecraft.extras.MinecraftHelp; import com.google.inject.Inject; import java.util.List; import net.draycia.carbon.common.command.Commander; -import net.draycia.carbon.common.command.PlayerCommander; import net.draycia.carbon.common.messages.CarbonMessageSource; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.Template; @@ -81,13 +80,6 @@ public final class HelpCommand { help.messageProvider((sender, key, args) -> { final String messageKey = "command.help." + key; - final String forAudience; - // todo: Should CarbonMessageSource unwrap PlayerCommanders? - if (sender instanceof PlayerCommander playerCommander) { - forAudience = messageSource.messageOf(playerCommander.carbonPlayer(), messageKey); - } else { - forAudience = messageSource.messageOf(sender, messageKey); - } final TemplateResolver resolver; // Total hack but works for now @@ -100,7 +92,7 @@ public final class HelpCommand { resolver = TemplateResolver.empty(); } - return MiniMessage.miniMessage().deserialize(forAudience, resolver); + return MiniMessage.miniMessage().deserialize(messageSource.messageOf(sender, messageKey), resolver); }); return help; diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/MuteCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/MuteCommand.java index 6402dfe6..fb0e5be9 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/MuteCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/MuteCommand.java @@ -2,7 +2,6 @@ package net.draycia.carbon.common.command.commands; import cloud.commandframework.CommandManager; import cloud.commandframework.arguments.standard.UUIDArgument; -import cloud.commandframework.permission.Permission; import com.google.inject.Inject; import java.util.Objects; import java.util.UUID; @@ -12,7 +11,7 @@ import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.punishments.MuteEntry; import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.PlayerCommander; -import net.draycia.carbon.common.command.arguments.CarbonPlayerArgument; +import net.draycia.carbon.common.command.argument.CarbonPlayerArgument; import net.draycia.carbon.common.messages.CarbonMessageService; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java index 6a0c1db1..bd3e2e5b 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java @@ -17,8 +17,8 @@ import java.util.regex.Pattern; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.PlayerCommander; -import net.draycia.carbon.common.command.arguments.CarbonPlayerArgument; -import net.draycia.carbon.common.command.arguments.OptionValueParser; +import net.draycia.carbon.common.command.argument.CarbonPlayerArgument; +import net.draycia.carbon.common.command.argument.OptionValueParser; import net.draycia.carbon.common.messages.CarbonMessageService; import net.kyori.adventure.text.minimessage.MiniMessage; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/UnmuteCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/UnmuteCommand.java index 43393a47..0679fd5a 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/UnmuteCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/UnmuteCommand.java @@ -5,12 +5,11 @@ import cloud.commandframework.arguments.standard.UUIDArgument; import com.google.inject.Inject; import java.util.Objects; import java.util.UUID; -import java.util.function.BiPredicate; import net.draycia.carbon.api.CarbonChat; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.PlayerCommander; -import net.draycia.carbon.common.command.arguments.CarbonPlayerArgument; +import net.draycia.carbon.common.command.argument.CarbonPlayerArgument; import net.draycia.carbon.common.messages.CarbonMessageService; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.framework.qual.DefaultQualifier; diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/WhisperCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/WhisperCommand.java index 7745860f..3f228dda 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/WhisperCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/WhisperCommand.java @@ -7,7 +7,7 @@ import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.util.SourcedAudience; import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.PlayerCommander; -import net.draycia.carbon.common.command.arguments.CarbonPlayerArgument; +import net.draycia.carbon.common.command.argument.CarbonPlayerArgument; import net.draycia.carbon.common.messages.CarbonMessageService; public class WhisperCommand { diff --git a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessageService.java b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessageService.java index ebf80cff..7b2c8a97 100644 --- a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessageService.java +++ b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessageService.java @@ -262,21 +262,22 @@ public interface CarbonMessageService { ); @Message("error.command.no_permission") - Component errorCommandNoPermission(); + void errorCommandNoPermission(final Audience audience); @Message("error.command.command_execution") - Component errorCommandCommandExecution( + void errorCommandCommandExecution( + final Audience audience, final @Placeholder("throwable_message") Component throwableMessage, final @Placeholder String stacktrace ); @Message("error.command.argument_parsing") - Component errorCommandArgumentParsing(final @Placeholder("throwable_message") Component throwableMessage); + void errorCommandArgumentParsing(final Audience audience, final @Placeholder("throwable_message") Component throwableMessage); @Message("error.command.invalid_sender") - Component errorCommandInvalidSender(final @Placeholder String sender_type); + void errorCommandInvalidSender(final Audience audience, final @Placeholder String sender_type); @Message("error.command.invalid_syntax") - Component errorCommandInvalidSyntax(final @Placeholder Component syntax); + void errorCommandInvalidSyntax(final Audience audience, final @Placeholder Component syntax); } diff --git a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessageSource.java b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessageSource.java index 5731fe0c..5e671e7e 100644 --- a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessageSource.java +++ b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessageSource.java @@ -24,6 +24,7 @@ import java.util.function.Consumer; import java.util.stream.Stream; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.common.ForCarbon; +import net.draycia.carbon.common.command.PlayerCommander; import net.draycia.carbon.common.config.PrimaryConfig; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.translation.Translator; @@ -110,10 +111,17 @@ public final class CarbonMessageSource implements IMessageSourceYou have been muted! mute.notify.player.unmuted=You are no longer muted! mute.notify.unmuted= has been unmuted! error.command.no_permission=I'm sorry, but you do not have permission to perform this command.\nPlease contact the server administrators if you believe that this is in error. -error.command.command_execution=\n\n Click to copy">>An internal error occurred while attempting to perform this command. -error.command.argument_parsing=Invalid command argument: +error.command.command_execution=\n\n Click to copy">>An internal error occurred while attempting to perform this command. +error.command.argument_parsing=Invalid command argument: error.command.invalid_sender=Invalid command sender. You must be of type error.command.invalid_syntax=Invalid command syntax. Correct command syntax is: / nickname.reset=Your nickname was reset.