-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Cloud 2 * Use cloud-translations * Use AudienceLocaleExtractor * Update dependencies * Update cloud * Update cloud * merge service files
- Loading branch information
Showing
18 changed files
with
258 additions
and
523 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/main/java/xyz/jpenilla/wanderingtrades/command/BaseCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 28 additions & 75 deletions
103
src/main/java/xyz/jpenilla/wanderingtrades/command/ExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,51 @@ | ||
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 java.util.logging.Level; | ||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.ComponentLike; | ||
import net.kyori.adventure.text.format.NamedTextColor; | ||
import net.kyori.adventure.util.ComponentMessageThrowable; | ||
import org.bukkit.command.CommandSender; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
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.minecraft.extras.MinecraftExceptionHandler; | ||
import xyz.jpenilla.wanderingtrades.WanderingTrades; | ||
import xyz.jpenilla.wanderingtrades.config.Messages; | ||
import xyz.jpenilla.wanderingtrades.util.Components; | ||
import xyz.jpenilla.wanderingtrades.util.Constants; | ||
|
||
import static org.incendo.cloud.exception.handling.ExceptionHandler.unwrappingHandler; | ||
|
||
@DefaultQualifier(NonNull.class) | ||
final class ExceptionHandler { | ||
private static final Pattern SYNTAX_HIGHLIGHT_PATTERN = Pattern.compile("[^\\s\\w\\-]"); | ||
|
||
private final WanderingTrades plugin; | ||
private final CommandManager<CommandSender> commandManager; | ||
|
||
ExceptionHandler(final WanderingTrades plugin, final CommandManager<CommandSender> commandManager) { | ||
ExceptionHandler( | ||
final WanderingTrades plugin, | ||
final CommandManager<CommandSender> commandManager | ||
) { | ||
this.plugin = plugin; | ||
this.commandManager = commandManager; | ||
} | ||
|
||
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); | ||
} | ||
|
||
private Component commandExecution(final CommandSender commandSender, final Exception ex) { | ||
final Throwable cause = ex.getCause(); | ||
|
||
if (cause instanceof NoPermissionException noPermissionException) { | ||
return noPermission(noPermissionException); | ||
} else if (cause instanceof InvalidSyntaxException invalidSyntaxException) { | ||
return this.invalidSyntax(invalidSyntaxException); | ||
} else if (cause instanceof InvalidCommandSenderException invalidCommandSenderException) { | ||
return this.invalidSender(invalidCommandSenderException); | ||
} else if (cause instanceof ArgumentParseException argumentParseException) { | ||
return this.argumentParsing(argumentParseException); | ||
} | ||
|
||
return MinecraftExceptionHandler.DEFAULT_COMMAND_EXECUTION_FUNCTION.apply(ex); | ||
} | ||
|
||
private Component invalidSyntax(final Exception ex) { | ||
final InvalidSyntaxException exception = (InvalidSyntaxException) ex; | ||
final Component correctSyntaxMessage = Component.text( | ||
String.format("/%s", exception.getCorrectSyntax()), | ||
NamedTextColor.GRAY | ||
).replaceText(config -> { | ||
config.match(SYNTAX_HIGHLIGHT_PATTERN); | ||
config.replacement(builder -> builder.color(NamedTextColor.WHITE)); | ||
}); | ||
return Messages.COMMAND_INVALID_SYNTAX.withPlaceholders( | ||
Components.placeholder("syntax", correctSyntaxMessage) | ||
); | ||
} | ||
|
||
private Component invalidSender(final Exception ex) { | ||
final InvalidCommandSenderException exception = (InvalidCommandSenderException) ex; | ||
return Messages.COMMAND_INVALID_SENDER.withPlaceholders( | ||
Components.placeholder("type", exception.getRequiredSender().getSimpleName()) | ||
); | ||
} | ||
|
||
private Component argumentParsing(final Exception ex) { | ||
final Component causeMessage = Objects.requireNonNull(ComponentMessageThrowable.getOrConvertMessage(ex.getCause())) | ||
.colorIfAbsent(NamedTextColor.GRAY); | ||
return Messages.COMMAND_INVALID_ARGUMENT.withPlaceholders( | ||
Components.placeholder("error", causeMessage) | ||
); | ||
} | ||
|
||
private static Component noPermission(final Exception e) { | ||
return Component.translatable("commands.help.failed", NamedTextColor.RED); | ||
} | ||
|
||
private static Component decorate(final ComponentLike component) { | ||
return Component.textOfChildren(Constants.PREFIX_COMPONENT, component); | ||
MinecraftExceptionHandler.create(this.plugin.audiences()::sender) | ||
.handler(NoPermissionException.class, (formatter, ctx) -> Component.translatable("commands.help.failed", NamedTextColor.RED)) | ||
.defaultInvalidSyntaxHandler() | ||
.defaultInvalidSenderHandler() | ||
.defaultArgumentParsingHandler() | ||
.defaultCommandExecutionHandler(ctx -> this.plugin.getLogger().log(Level.WARNING, "Unexpected exception during command execution", ctx.exception().getCause())) | ||
.decorator(msg -> Component.textOfChildren(Constants.PREFIX_COMPONENT, msg)) | ||
.registerTo(this.commandManager); | ||
|
||
this.commandManager.exceptionController() | ||
.registerHandler(CommandExecutionException.class, unwrappingHandler(NoPermissionException.class)) | ||
.registerHandler(CommandExecutionException.class, unwrappingHandler(InvalidSyntaxException.class)) | ||
.registerHandler(CommandExecutionException.class, unwrappingHandler(InvalidCommandSenderException.class)) | ||
.registerHandler(CommandExecutionException.class, unwrappingHandler(ArgumentParseException.class)); | ||
} | ||
} |
Oops, something went wrong.