-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable tab completion for setting values based on key
Signed-off-by: Pablete1234 <[email protected]>
- Loading branch information
1 parent
14d1f65
commit 28dbaa8
Showing
7 changed files
with
77 additions
and
34 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
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
56 changes: 56 additions & 0 deletions
56
core/src/main/java/tc/oc/pgm/command/parsers/SettingValueParser.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 |
---|---|---|
@@ -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()); | ||
} | ||
} |
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