From 28dbaa8c867eb35a672c6121b425a1aefb06f531 Mon Sep 17 00:00:00 2001 From: Pablete1234 Date: Tue, 1 Nov 2022 16:03:36 +0100 Subject: [PATCH] Enable tab completion for setting values based on key Signed-off-by: Pablete1234 --- .../tc/oc/pgm/api/setting/SettingKey.java | 2 +- .../tc/oc/pgm/api/setting/SettingValue.java | 20 +------ .../tc/oc/pgm/command/SettingCommand.java | 9 +-- .../command/parsers/MatchPlayerParser.java | 2 +- .../pgm/command/parsers/SettingKeyParser.java | 18 ++++-- .../command/parsers/SettingValueParser.java | 56 +++++++++++++++++++ .../tc/oc/pgm/command/util/CommandGraph.java | 4 +- 7 files changed, 77 insertions(+), 34 deletions(-) create mode 100644 core/src/main/java/tc/oc/pgm/command/parsers/SettingValueParser.java diff --git a/core/src/main/java/tc/oc/pgm/api/setting/SettingKey.java b/core/src/main/java/tc/oc/pgm/api/setting/SettingKey.java index b0939725fb..21bce58ff5 100644 --- a/core/src/main/java/tc/oc/pgm/api/setting/SettingKey.java +++ b/core/src/main/java/tc/oc/pgm/api/setting/SettingKey.java @@ -109,7 +109,7 @@ public List getAliases() { /** * Get a list of the possible {@link SettingValue}s. * - * @return A array of {@link SettingValue}s, sorted by defined order. + * @return An array of {@link SettingValue}s, sorted by defined order. */ public SettingValue[] getPossibleValues() { return values; diff --git a/core/src/main/java/tc/oc/pgm/api/setting/SettingValue.java b/core/src/main/java/tc/oc/pgm/api/setting/SettingValue.java index 8e50e64e0c..41618fa7da 100644 --- a/core/src/main/java/tc/oc/pgm/api/setting/SettingValue.java +++ b/core/src/main/java/tc/oc/pgm/api/setting/SettingValue.java @@ -2,12 +2,7 @@ import static tc.oc.pgm.util.Assert.assertNotNull; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.stream.Stream; import org.bukkit.DyeColor; -import org.jetbrains.annotations.Nullable; -import tc.oc.pgm.util.StringUtils; /** * Values of a particular {@link SettingKey}, a toggleable setting. @@ -83,7 +78,7 @@ public String getName() { /** * Get {@link DyeColor} related to this setting value . * - * @see {@link SettingMenu} for usage. + * @see tc.oc.pgm.settings.SettingsMenu for usage. * @return {@link DyeColor} for this setting value. */ public DyeColor getColor() { @@ -94,17 +89,4 @@ public DyeColor getColor() { public String toString() { return getName(); } - - public static SettingValue search(SettingKey key, @Nullable String query) { - final SettingValue value = - StringUtils.bestFuzzyMatch( - query, - Stream.of(SettingValue.values()) - .filter(entry -> entry.getKey().equals(key)) - .collect(Collectors.toMap(SettingValue::getName, Function.identity()))); - if (value == null) { - return key.getDefaultValue(); - } - return value; - } } diff --git a/core/src/main/java/tc/oc/pgm/command/SettingCommand.java b/core/src/main/java/tc/oc/pgm/command/SettingCommand.java index 8083366008..cfe27a06cb 100644 --- a/core/src/main/java/tc/oc/pgm/command/SettingCommand.java +++ b/core/src/main/java/tc/oc/pgm/command/SettingCommand.java @@ -7,7 +7,6 @@ import cloud.commandframework.annotations.Argument; import cloud.commandframework.annotations.CommandDescription; import cloud.commandframework.annotations.CommandMethod; -import cloud.commandframework.annotations.specifier.Greedy; import java.util.stream.Collectors; import java.util.stream.Stream; import net.kyori.adventure.text.format.NamedTextColor; @@ -64,16 +63,14 @@ public void setting(MatchPlayer player, @Argument("setting") SettingKey key) { public void toggle( MatchPlayer player, @Argument("setting") SettingKey key, - @Argument("value") @Greedy String query) { + @Argument("value") SettingValue value) { final Settings setting = player.getSettings(); final SettingValue old = setting.getValue(key); - final SettingValue value; - if (query == null) { + if (value == null) { setting.toggleValue(key); value = setting.getValue(key); - } else { - value = SettingValue.search(key, query); + } else if (old != value) { setting.setValue(key, value); } diff --git a/core/src/main/java/tc/oc/pgm/command/parsers/MatchPlayerParser.java b/core/src/main/java/tc/oc/pgm/command/parsers/MatchPlayerParser.java index a823fa77ae..c0af363ce3 100644 --- a/core/src/main/java/tc/oc/pgm/command/parsers/MatchPlayerParser.java +++ b/core/src/main/java/tc/oc/pgm/command/parsers/MatchPlayerParser.java @@ -24,7 +24,7 @@ public final class MatchPlayerParser implements ArgumentParser { + public static final CloudKey SETTING_KEY = + SimpleCloudKey.of("_pgm_setting_key_param_", new TypeToken() {}); + @Override public @NonNull ArgumentParseResult parse( final @NonNull CommandContext context, final @NonNull Queue inputQueue) { final String input = inputQueue.peek(); if (input == null) { - return failure(new NoInputProvidedException(DurationParser.class, context)); + return failure(new NoInputProvidedException(SettingKeyParser.class, context)); } SettingKey bestMatch = StringUtils.bestFuzzyMatch(input, SettingKey.class); - if (bestMatch != null) { - inputQueue.remove(); - return success(bestMatch); - } + if (bestMatch == null) return failure(invalidFormat(input, SettingKey.class)); - return failure(invalidFormat(input, SettingKey.class, null)); + inputQueue.remove(); + // Add it to context to allow others to query for it + context.set(SETTING_KEY, bestMatch); + return success(bestMatch); } @Override diff --git a/core/src/main/java/tc/oc/pgm/command/parsers/SettingValueParser.java b/core/src/main/java/tc/oc/pgm/command/parsers/SettingValueParser.java new file mode 100644 index 0000000000..5c7faeaa41 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/command/parsers/SettingValueParser.java @@ -0,0 +1,56 @@ +package tc.oc.pgm.command.parsers; + +import static cloud.commandframework.arguments.parser.ArgumentParseResult.failure; +import static cloud.commandframework.arguments.parser.ArgumentParseResult.success; +import static tc.oc.pgm.util.text.TextException.invalidFormat; + +import cloud.commandframework.arguments.parser.ArgumentParseResult; +import cloud.commandframework.arguments.parser.ArgumentParser; +import cloud.commandframework.context.CommandContext; +import cloud.commandframework.exceptions.parsing.NoInputProvidedException; +import com.google.common.collect.Iterators; +import java.util.Arrays; +import java.util.List; +import java.util.Queue; +import java.util.stream.Collectors; +import org.bukkit.command.CommandSender; +import org.checkerframework.checker.nullness.qual.NonNull; +import tc.oc.pgm.api.setting.SettingKey; +import tc.oc.pgm.api.setting.SettingValue; +import tc.oc.pgm.util.LiquidMetal; +import tc.oc.pgm.util.StringUtils; + +public final class SettingValueParser implements ArgumentParser { + + @Override + public @NonNull ArgumentParseResult parse( + final @NonNull CommandContext context, + final @NonNull Queue inputQueue) { + final String input = inputQueue.peek(); + if (input == null) { + return failure(new NoInputProvidedException(SettingValueParser.class, context)); + } + + SettingKey key = context.get(SettingKeyParser.SETTING_KEY); + + SettingValue value = + StringUtils.bestFuzzyMatch( + input, Iterators.forArray(key.getPossibleValues()), SettingValue::getName); + + if (value == null) return failure(invalidFormat(input, SettingValue.class)); + + inputQueue.remove(); + return success(value); + } + + @Override + public @NonNull List<@NonNull String> suggestions( + final @NonNull CommandContext context, final @NonNull String input) { + SettingKey key = context.get(SettingKeyParser.SETTING_KEY); + + return Arrays.stream(key.getPossibleValues()) + .map(SettingValue::getName) + .filter(val -> LiquidMetal.match(val, input)) + .collect(Collectors.toList()); + } +} diff --git a/core/src/main/java/tc/oc/pgm/command/util/CommandGraph.java b/core/src/main/java/tc/oc/pgm/command/util/CommandGraph.java index d251cc6406..30b0d30425 100644 --- a/core/src/main/java/tc/oc/pgm/command/util/CommandGraph.java +++ b/core/src/main/java/tc/oc/pgm/command/util/CommandGraph.java @@ -53,6 +53,7 @@ import tc.oc.pgm.api.party.VictoryCondition; import tc.oc.pgm.api.player.MatchPlayer; import tc.oc.pgm.api.setting.SettingKey; +import tc.oc.pgm.api.setting.SettingValue; import tc.oc.pgm.classes.PlayerClass; import tc.oc.pgm.command.AdminCommand; import tc.oc.pgm.command.CancelCommand; @@ -88,6 +89,7 @@ import tc.oc.pgm.command.parsers.PartyParser; import tc.oc.pgm.command.parsers.PlayerClassParser; import tc.oc.pgm.command.parsers.SettingKeyParser; +import tc.oc.pgm.command.parsers.SettingValueParser; import tc.oc.pgm.command.parsers.TeamParser; import tc.oc.pgm.command.parsers.TeamsParser; import tc.oc.pgm.command.parsers.VictoryConditionParser; @@ -273,7 +275,7 @@ private void setupParsers() { TypeFactory.parameterizedClass(Optional.class, VictoryCondition.class), new VictoryConditionParser()); registerParser(SettingKey.class, new SettingKeyParser()); - // registerParser(SettingValue.class, new EnumProvider<>(SettingValue.class)); + registerParser(SettingValue.class, new SettingValueParser()); } private void registerParser(Class type, ArgumentParser parser) {