-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
41 additions
and
17 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
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
25 changes: 18 additions & 7 deletions
25
litecommands-core/src/main/java/dev/rollczi/litecommands/valid/ValidationInfo.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,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); | ||
|
||
} | ||
|
||
} |
14 changes: 11 additions & 3 deletions
14
...commands-core/src/main/java/dev/rollczi/litecommands/valid/ValidationMessagesService.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,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); | ||
} | ||
|
||
} |
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