Skip to content

Commit

Permalink
Add contextual messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Nov 25, 2021
1 parent 34b3a8c commit bed51c3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
public class LiteCommands {

private final Set<LiteSection> resolvers;
private final LitePlatformManager commandManager;
private final LitePlatformManager platformManager;
private final ValidationMessagesService messagesService;
private final Injector injector;
private final Logger logger;

LiteCommands(Set<LiteSection> resolvers, LitePlatformManager commandManager, ValidationMessagesService messagesService, Injector injector, Logger logger) {
LiteCommands(Set<LiteSection> 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;
Expand All @@ -29,8 +29,8 @@ public Collection<LiteSection> getResolvers() {
return Collections.unmodifiableCollection(resolvers);
}

public LitePlatformManager getCommandManager() {
return commandManager;
public LitePlatformManager getPlatformManager() {
return platformManager;
}

public ValidationMessagesService getMessagesService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<LiteInvocation, String> {

@Override
String apply(LiteInvocation invocation);

}

}
Original file line number Diff line number Diff line change
@@ -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<ValidationInfo, String> customMessages = new EnumMap<>(ValidationInfo.class);
private final EnumMap<ValidationInfo, Function<LiteInvocation, String>> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit bed51c3

Please sign in to comment.