From 829c3108727e3dc7ccff02496c36ec1105412d4a Mon Sep 17 00:00:00 2001 From: jmp Date: Tue, 27 Oct 2020 21:19:15 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20a=20method=20to=20CloudBrigad?= =?UTF-8?q?ierManager=20to=20enable/disable=20native=20suggestions=20for?= =?UTF-8?q?=20argument=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../brigadier/CloudBrigadierManager.java | 80 +++++++++++++++++-- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c3a1cc9..c5ba3ffb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added access to the CloudBrigadierManager from Brigadier-enabled command managers - Added parameter injectors - Store currently parsing command argument in the command context + - Added a method to CloudBrigadierManager to enable or disable Brigadier native suggestions for specific argument types ### Changed - Allow for use of `@Completions` annotation with argument types other than String diff --git a/cloud-minecraft/cloud-brigadier/src/main/java/cloud/commandframework/brigadier/CloudBrigadierManager.java b/cloud-minecraft/cloud-brigadier/src/main/java/cloud/commandframework/brigadier/CloudBrigadierManager.java index a1d7417a..f4000a64 100644 --- a/cloud-minecraft/cloud-brigadier/src/main/java/cloud/commandframework/brigadier/CloudBrigadierManager.java +++ b/cloud-minecraft/cloud-brigadier/src/main/java/cloud/commandframework/brigadier/CloudBrigadierManager.java @@ -146,12 +146,12 @@ public final class CloudBrigadierManager { /* Map float to FloatArgumentType */ this.registerMapping(new TypeToken>() { }, true, argument -> { - final boolean hasMin = argument.getMin() != Float.MIN_VALUE; - final boolean hasMax = argument.getMax() != Float.MAX_VALUE; + final boolean hasMin = argument.getMin() != Float.NEGATIVE_INFINITY; + final boolean hasMax = argument.getMax() != Float.POSITIVE_INFINITY; if (hasMin) { return FloatArgumentType.floatArg(argument.getMin(), argument.getMax()); } else if (hasMax) { - return FloatArgumentType.floatArg(Float.MIN_VALUE, argument.getMax()); + return FloatArgumentType.floatArg(Float.NEGATIVE_INFINITY, argument.getMax()); } else { return FloatArgumentType.floatArg(); } @@ -159,12 +159,12 @@ public final class CloudBrigadierManager { /* Map double to DoubleArgumentType */ this.registerMapping(new TypeToken>() { }, true, argument -> { - final boolean hasMin = argument.getMin() != Double.MIN_VALUE; - final boolean hasMax = argument.getMax() != Double.MAX_VALUE; + final boolean hasMin = argument.getMin() != Double.NEGATIVE_INFINITY; + final boolean hasMax = argument.getMax() != Double.POSITIVE_INFINITY; if (hasMin) { return DoubleArgumentType.doubleArg(argument.getMin(), argument.getMax()); } else if (hasMax) { - return DoubleArgumentType.doubleArg(Double.MIN_VALUE, argument.getMax()); + return DoubleArgumentType.doubleArg(Double.NEGATIVE_INFINITY, argument.getMax()); } else { return DoubleArgumentType.doubleArg(); } @@ -192,6 +192,74 @@ public final class CloudBrigadierManager { }, false, argument -> StringArgumentType.greedyString()); } + /** + * Set whether to use Brigadier's native suggestions for number argument types. + *

+ * If Brigadier's suggestions are not used, cloud's default number suggestion provider will be used. + * + * @param nativeNumberSuggestions Whether or not Brigadier suggestions should be used for numbers + * @since 1.2.0 + */ + public void setNativeNumberSuggestions(final boolean nativeNumberSuggestions) { + this.setNativeSuggestions( + new TypeToken>() { + }, + nativeNumberSuggestions + ); + this.setNativeSuggestions( + new TypeToken>() { + }, + nativeNumberSuggestions + ); + this.setNativeSuggestions( + new TypeToken>() { + }, + nativeNumberSuggestions + ); + this.setNativeSuggestions( + new TypeToken>() { + }, + nativeNumberSuggestions + ); + this.setNativeSuggestions( + new TypeToken>() { + }, + nativeNumberSuggestions + ); + } + + /** + * Set whether to use Brigadier's native suggestions for an argument type with an already registered mapper. + *

+ * If Brigadier's suggestions are not used, suggestions will fall back to the cloud suggestion provider. + * + * @param argumentType cloud argument parser type + * @param nativeSuggestions Whether or not Brigadier suggestions should be used + * @param argument type + * @param cloud argument parser type + * @throws IllegalArgumentException when there is no mapper registered for the provided argument type + * @since 1.2.0 + */ + public > void setNativeSuggestions( + final @NonNull TypeToken argumentType, + final boolean nativeSuggestions + ) throws IllegalArgumentException { + final Pair, ? extends ArgumentType>, Boolean> pair = this.mappers.get( + GenericTypeReflector.erase(argumentType.getType()) + ); + if (pair == null) { + throw new IllegalArgumentException( + "No mapper registered for type: " + GenericTypeReflector + .erase(argumentType.getType()) + .toGenericString() + ); + } + this.mappers.put( + GenericTypeReflector.erase(argumentType.getType()), + Pair.of(pair.getFirst(), nativeSuggestions) + ); + } + /** * Register a cloud-Brigadier mapping *