From bed51c3ebb8ee481e72ebefcb788782974b266c1 Mon Sep 17 00:00:00 2001 From: Rollczi Date: Fri, 26 Nov 2021 00:44:50 +0100 Subject: [PATCH] Add contextual messages. --- .../rollczi/litecommands/LiteCommands.java | 10 ++++---- .../litecommands/LiteCommandsBuilder.java | 5 ++++ .../litecommands/component/LiteSection.java | 2 +- .../litecommands/valid/ValidationInfo.java | 25 +++++++++++++------ .../valid/ValidationMessagesService.java | 14 ++++++++--- .../LiteValidationExceptionHandler.java | 2 +- 6 files changed, 41 insertions(+), 17 deletions(-) diff --git a/litecommands-core/src/main/java/dev/rollczi/litecommands/LiteCommands.java b/litecommands-core/src/main/java/dev/rollczi/litecommands/LiteCommands.java index e76797a42..a16feb37c 100644 --- a/litecommands-core/src/main/java/dev/rollczi/litecommands/LiteCommands.java +++ b/litecommands-core/src/main/java/dev/rollczi/litecommands/LiteCommands.java @@ -12,14 +12,14 @@ public class LiteCommands { private final Set resolvers; - private final LitePlatformManager commandManager; + private final LitePlatformManager platformManager; private final ValidationMessagesService messagesService; private final Injector injector; private final Logger logger; - LiteCommands(Set resolvers, LitePlatformManager commandManager, ValidationMessagesService messagesService, Injector injector, Logger logger) { + LiteCommands(Set resolvers, LitePlatformManager platformManager, ValidationMessagesService messagesService, Injector injector, Logger logger) { this.resolvers = resolvers; - this.commandManager = commandManager; + this.platformManager = platformManager; this.messagesService = messagesService; this.injector = injector; this.logger = logger; @@ -29,8 +29,8 @@ public Collection getResolvers() { return Collections.unmodifiableCollection(resolvers); } - public LitePlatformManager getCommandManager() { - return commandManager; + public LitePlatformManager getPlatformManager() { + return platformManager; } public ValidationMessagesService getMessagesService() { diff --git a/litecommands-core/src/main/java/dev/rollczi/litecommands/LiteCommandsBuilder.java b/litecommands-core/src/main/java/dev/rollczi/litecommands/LiteCommandsBuilder.java index e28db7c24..183b9242a 100644 --- a/litecommands-core/src/main/java/dev/rollczi/litecommands/LiteCommandsBuilder.java +++ b/litecommands-core/src/main/java/dev/rollczi/litecommands/LiteCommandsBuilder.java @@ -89,6 +89,11 @@ public LiteCommandsBuilder message(ValidationInfo validationInfo, String message return this; } + public LiteCommandsBuilder message(ValidationInfo validationInfo, ValidationInfo.ContextMessage message) { + this.messagesService.registerMessage(validationInfo, message); + return this; + } + public LiteCommandsBuilder platform(LitePlatformManager platformManager) { this.platformManager = platformManager; return this; diff --git a/litecommands-core/src/main/java/dev/rollczi/litecommands/component/LiteSection.java b/litecommands-core/src/main/java/dev/rollczi/litecommands/component/LiteSection.java index d689f68a8..0e8248fa3 100644 --- a/litecommands-core/src/main/java/dev/rollczi/litecommands/component/LiteSection.java +++ b/litecommands-core/src/main/java/dev/rollczi/litecommands/component/LiteSection.java @@ -28,7 +28,7 @@ public final class LiteSection extends AbstractComponent { public void resolveExecution(MetaData data) { LiteComponent resolver = resolvers.getOrDefault(data.getCurrentPartOfCommand(), resolvers.get(StringUtils.EMPTY)); - whenWithContext(resolver == null, ValidationInfo.COMMAND_NO_FOUND, data, this); + whenWithContext(resolver == null, ValidationInfo.INVALID_USE, data, this); resolver.resolveExecution(data.resolverNestingTracing(this)); } diff --git a/litecommands-core/src/main/java/dev/rollczi/litecommands/valid/ValidationInfo.java b/litecommands-core/src/main/java/dev/rollczi/litecommands/valid/ValidationInfo.java index a9c60df5e..a55473fb3 100644 --- a/litecommands-core/src/main/java/dev/rollczi/litecommands/valid/ValidationInfo.java +++ b/litecommands-core/src/main/java/dev/rollczi/litecommands/valid/ValidationInfo.java @@ -1,22 +1,33 @@ package dev.rollczi.litecommands.valid; +import dev.rollczi.litecommands.LiteInvocation; import panda.utilities.StringUtils; +import java.util.function.Function; + public enum ValidationInfo { - COMMAND_NO_FOUND("&cCommand or subcommand not found"), - NO_PERMISSION("&cYou don't have permission to this command"), - INVALID_USE("&cInvalid use of the command"), - CUSTOM(StringUtils.EMPTY); + COMMAND_NO_FOUND(invocation -> "&cCommand or subcommand not found"), + NO_PERMISSION(invocation -> "&cYou don't have permission to this command"), + INVALID_USE(invocation -> "&cInvalid use of the command"), + CUSTOM(invocation -> StringUtils.EMPTY); - private final String defaultMessage; + private final ContextMessage defaultMessage; - ValidationInfo(String defaultMessage) { + ValidationInfo(ContextMessage defaultMessage) { this.defaultMessage = defaultMessage; } - public String getDefaultMessage() { + public ContextMessage getDefaultMessage() { return defaultMessage; } + @FunctionalInterface + public interface ContextMessage extends Function { + + @Override + String apply(LiteInvocation invocation); + + } + } diff --git a/litecommands-core/src/main/java/dev/rollczi/litecommands/valid/ValidationMessagesService.java b/litecommands-core/src/main/java/dev/rollczi/litecommands/valid/ValidationMessagesService.java index 05bbdaa18..a9b1d2d4f 100644 --- a/litecommands-core/src/main/java/dev/rollczi/litecommands/valid/ValidationMessagesService.java +++ b/litecommands-core/src/main/java/dev/rollczi/litecommands/valid/ValidationMessagesService.java @@ -1,18 +1,26 @@ package dev.rollczi.litecommands.valid; +import dev.rollczi.litecommands.LiteInvocation; + import java.util.EnumMap; +import java.util.function.Function; public class ValidationMessagesService { - private final EnumMap customMessages = new EnumMap<>(ValidationInfo.class); + private final EnumMap> customMessages = new EnumMap<>(ValidationInfo.class); public ValidationMessagesService registerMessage(ValidationInfo validationInfo, String message) { + customMessages.put(validationInfo, invocation -> message); + return this; + } + + public ValidationMessagesService registerMessage(ValidationInfo validationInfo, ValidationInfo.ContextMessage message) { customMessages.put(validationInfo, message); return this; } - public String getMessage(ValidationInfo validationInfo) { - return customMessages.getOrDefault(validationInfo, validationInfo.getDefaultMessage()); + public String getMessage(ValidationInfo validationInfo, LiteInvocation invocation) { + return customMessages.getOrDefault(validationInfo, validationInfo.getDefaultMessage()).apply(invocation); } } diff --git a/litecommands-core/src/main/java/dev/rollczi/litecommands/valid/handle/LiteValidationExceptionHandler.java b/litecommands-core/src/main/java/dev/rollczi/litecommands/valid/handle/LiteValidationExceptionHandler.java index fcb8adf5d..17a4e0bc0 100644 --- a/litecommands-core/src/main/java/dev/rollczi/litecommands/valid/handle/LiteValidationExceptionHandler.java +++ b/litecommands-core/src/main/java/dev/rollczi/litecommands/valid/handle/LiteValidationExceptionHandler.java @@ -15,7 +15,7 @@ public LiteValidationExceptionHandler(ValidationMessagesService messagesService) @Override public void handle(LiteInvocation invocation, ValidationCommandException exception) { String message = exception.getMessage() == null - ? messagesService.getMessage(exception.getValidationInfo()) + ? messagesService.getMessage(exception.getValidationInfo(), invocation) : exception.getMessage(); if (message == null || message.isEmpty()) {