From a04e3f92cb24347cf95fa6b9cde883c894fb537c Mon Sep 17 00:00:00 2001 From: jmp Date: Fri, 30 Oct 2020 13:11:48 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Allow=20for=20use=20of=20a=20BiFunc?= =?UTF-8?q?tion=20instead=20of=20just=20a=20Function=20in=20MinecraftExceptionHandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../extras/MinecraftExceptionHandler.java | 46 +++++++++++++------ 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 908b159d..c7a47d00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Allow for use of `@Completions` annotation with argument types other than String + - Allow for use of a BiFunction instead of just a Function in MinecraftExceptionHandler ### Fixed - Use the correct default range for Double and Float parsers in the StandardParserRegistry diff --git a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/MinecraftExceptionHandler.java b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/MinecraftExceptionHandler.java index f9d21f2a..d1527cb4 100644 --- a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/MinecraftExceptionHandler.java +++ b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/MinecraftExceptionHandler.java @@ -36,6 +36,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import java.util.HashMap; import java.util.Map; +import java.util.function.BiFunction; import java.util.function.Function; /** @@ -89,7 +90,7 @@ public final class MinecraftExceptionHandler { .append(Component.text(e.getCause().getMessage(), NamedTextColor.GRAY)) .build(); - private final Map> componentBuilders = new HashMap<>(); + private final Map> componentBuilders = new HashMap<>(); private Function decorator = Function.identity(); /** @@ -98,8 +99,7 @@ public final class MinecraftExceptionHandler { * @return {@code this} */ public @NonNull MinecraftExceptionHandler withInvalidSyntaxHandler() { - this.componentBuilders.put(ExceptionType.INVALID_SYNTAX, DEFAULT_INVALID_SYNTAX_FUNCTION); - return this; + return this.withHandler(ExceptionType.INVALID_SYNTAX, DEFAULT_INVALID_SYNTAX_FUNCTION); } /** @@ -108,8 +108,7 @@ public final class MinecraftExceptionHandler { * @return {@code this} */ public @NonNull MinecraftExceptionHandler withInvalidSenderHandler() { - this.componentBuilders.put(ExceptionType.INVALID_SENDER, DEFAULT_INVALID_SENDER_FUNCTION); - return this; + return this.withHandler(ExceptionType.INVALID_SENDER, DEFAULT_INVALID_SENDER_FUNCTION); } /** @@ -118,8 +117,7 @@ public final class MinecraftExceptionHandler { * @return {@code this} */ public @NonNull MinecraftExceptionHandler withNoPermissionHandler() { - this.componentBuilders.put(ExceptionType.NO_PERMISSION, DEFAULT_NO_PERMISSION_FUNCTION); - return this; + return this.withHandler(ExceptionType.NO_PERMISSION, DEFAULT_NO_PERMISSION_FUNCTION); } /** @@ -128,8 +126,7 @@ public final class MinecraftExceptionHandler { * @return {@code this} */ public @NonNull MinecraftExceptionHandler withArgumentParsingHandler() { - this.componentBuilders.put(ExceptionType.ARGUMENT_PARSING, DEFAULT_ARGUMENT_PARSING_FUNCTION); - return this; + return this.withHandler(ExceptionType.ARGUMENT_PARSING, DEFAULT_ARGUMENT_PARSING_FUNCTION); } /** @@ -156,7 +153,28 @@ public final class MinecraftExceptionHandler { final @NonNull ExceptionType type, final @NonNull Function<@NonNull Exception, @NonNull Component> componentBuilder ) { - this.componentBuilders.put(type, componentBuilder); + return this.withHandler( + type, + (sender, exception) -> componentBuilder.apply(exception) + ); + } + + /** + * Specify an exception handler + * + * @param type Exception type + * @param componentBuilder Component builder + * @return {@code this} + * @since 1.2.0 + */ + public @NonNull MinecraftExceptionHandler withHandler( + final @NonNull ExceptionType type, + final @NonNull BiFunction<@NonNull C, @NonNull Exception, @NonNull Component> componentBuilder + ) { + this.componentBuilders.put( + type, + componentBuilder + ); return this; } @@ -188,7 +206,7 @@ public final class MinecraftExceptionHandler { InvalidSyntaxException.class, (c, e) -> audienceMapper.apply(c).sendMessage( Identity.nil(), - this.decorator.apply(this.componentBuilders.get(ExceptionType.INVALID_SYNTAX).apply(e)) + this.decorator.apply(this.componentBuilders.get(ExceptionType.INVALID_SYNTAX).apply(c, e)) ) ); } @@ -197,7 +215,7 @@ public final class MinecraftExceptionHandler { InvalidCommandSenderException.class, (c, e) -> audienceMapper.apply(c).sendMessage( Identity.nil(), - this.decorator.apply(this.componentBuilders.get(ExceptionType.INVALID_SENDER).apply(e)) + this.decorator.apply(this.componentBuilders.get(ExceptionType.INVALID_SENDER).apply(c, e)) ) ); } @@ -206,7 +224,7 @@ public final class MinecraftExceptionHandler { NoPermissionException.class, (c, e) -> audienceMapper.apply(c).sendMessage( Identity.nil(), - this.decorator.apply(this.componentBuilders.get(ExceptionType.NO_PERMISSION).apply(e)) + this.decorator.apply(this.componentBuilders.get(ExceptionType.NO_PERMISSION).apply(c, e)) ) ); } @@ -215,7 +233,7 @@ public final class MinecraftExceptionHandler { ArgumentParseException.class, (c, e) -> audienceMapper.apply(c).sendMessage( Identity.nil(), - this.decorator.apply(this.componentBuilders.get(ExceptionType.ARGUMENT_PARSING).apply(e)) + this.decorator.apply(this.componentBuilders.get(ExceptionType.ARGUMENT_PARSING).apply(c, e)) ) ); }