Skip to content

Commit

Permalink
Enable tab completion for setting values based on key
Browse files Browse the repository at this point in the history
Signed-off-by: Pablete1234 <[email protected]>
  • Loading branch information
Pablete1234 committed Nov 3, 2022
1 parent 14d1f65 commit 28dbaa8
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 34 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/tc/oc/pgm/api/setting/SettingKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public List<String> 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;
Expand Down
20 changes: 1 addition & 19 deletions core/src/main/java/tc/oc/pgm/api/setting/SettingValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
}
}
9 changes: 3 additions & 6 deletions core/src/main/java/tc/oc/pgm/command/SettingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class MatchPlayerParser implements ArgumentParser<CommandSender, Ma
final String input = inputQueue.peek();

if (input == null) {
return failure(new NoInputProvidedException(DurationParser.class, context));
return failure(new NoInputProvidedException(MatchPlayerParser.class, context));
}

CommandSender sender = context.getSender();
Expand Down
18 changes: 12 additions & 6 deletions core/src/main/java/tc/oc/pgm/command/parsers/SettingKeyParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.keys.CloudKey;
import cloud.commandframework.keys.SimpleCloudKey;
import io.leangen.geantyref.TypeToken;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;
Expand All @@ -20,22 +23,25 @@

public final class SettingKeyParser implements ArgumentParser<CommandSender, SettingKey> {

public static final CloudKey<SettingKey> SETTING_KEY =
SimpleCloudKey.of("_pgm_setting_key_param_", new TypeToken<SettingKey>() {});

@Override
public @NonNull ArgumentParseResult<SettingKey> parse(
final @NonNull CommandContext<CommandSender> context,
final @NonNull Queue<String> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CommandSender, SettingValue> {

@Override
public @NonNull ArgumentParseResult<SettingValue> parse(
final @NonNull CommandContext<CommandSender> context,
final @NonNull Queue<String> 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<CommandSender> 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());
}
}
4 changes: 3 additions & 1 deletion core/src/main/java/tc/oc/pgm/command/util/CommandGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 <T> void registerParser(Class<T> type, ArgumentParser<CommandSender, T> parser) {
Expand Down

0 comments on commit 28dbaa8

Please sign in to comment.