Fix locales in command exception messages

This also removes usage of MinecraftExceptionHandler, no point in using it if we basically reimplement the whole thing, and don't even use the decorator function
This commit is contained in:
Jason Penilla committed 2021-10-19 21:20:31 -07:00
1 parent 271f954944
commit 17693466b9
12 files changed
+78 -65

No files matched your search

@@ -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<Commander>()
.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<Commander> commandManager,
final CarbonMessageService messageService
) {
registerExceptionHandlers(commandManager, messageService);
}
private static void registerExceptionHandlers(
final CommandManager<Commander> 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());
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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 {
@@ -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);
}
@@ -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 IMessageSource<Audience, Strin
@Override
public String messageOf(final Audience receiver, final String messageKey) {
if (receiver instanceof CarbonPlayer player) {
Audience audience = receiver;
// Unwrap PlayerCommanders
if (audience instanceof PlayerCommander playerCommander) {
audience = playerCommander.carbonPlayer();
}
if (audience instanceof CarbonPlayer player) {
return this.forPlayer(messageKey, player);
} else {
return this.forAudience(messageKey, receiver);
return this.forAudience(messageKey, audience);
}
}
@@ -34,8 +34,8 @@ mute.notify.player.muted=<#aa0000>You have been muted!
mute.notify.player.unmuted=<green>You are no longer muted!
mute.notify.unmuted=<gold><target> <green>has been unmuted!
error.command.no_permission=<red>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=<hover:show_text:"<throwableMessage>\n<stacktrace>\n<gray><italic> Click to copy"><click:copy_to_clipboard:<stacktrace>><red>An internal error occurred while attempting to perform this command.
error.command.argument_parsing=<red>Invalid command argument: <gray><throwableMessage>
error.command.command_execution=<hover:show_text:"<throwable_message>\n<stacktrace>\n<gray><italic> Click to copy"><click:copy_to_clipboard:<stacktrace>><red>An internal error occurred while attempting to perform this command.
error.command.argument_parsing=<red>Invalid command argument: <gray><throwable_message>
error.command.invalid_sender=<red>Invalid command sender. You must be of type <gray><senderType>
error.command.invalid_syntax=<red>Invalid command syntax. Correct command syntax is: <white>/</white><gray><syntax></gray>
nickname.reset=<gold>Your nickname was reset.</gold>