Improve parsing from registries.

If multiple namespaces are present, suggestions will first suggest a
namespace, then once a namespace is selected, keys within that namespace.

Starting an argument with ":" will instead search across all namespaces
for matching keys.
This commit is contained in:
wizjany
2019-05-27 11:12:46 -04:00
parent ab1e09fdaf
commit 5e857b3547
5 changed files with 123 additions and 79 deletions

View File

@ -20,8 +20,8 @@
package com.sk89q.worldedit.command.argument;
import com.google.common.collect.ImmutableList;
import com.sk89q.worldedit.command.util.SuggestionHelper;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.registry.Registry;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
@ -45,8 +45,7 @@ import org.enginehub.piston.inject.Key;
import java.lang.reflect.Field;
import java.util.List;
import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix;
import java.util.stream.Collectors;
public final class RegistryConverter<V extends Keyed> implements ArgumentConverter<V> {
@ -85,12 +84,10 @@ public final class RegistryConverter<V extends Keyed> implements ArgumentConvert
private final Registry<V> registry;
private final TextComponent choices;
private final boolean namespaced;
private RegistryConverter(Registry<V> registry) {
this.registry = registry;
this.choices = TextComponent.of("any " + registry.getName());
this.namespaced = registry instanceof NamespacedRegistry;
}
@Override
@ -109,9 +106,6 @@ public final class RegistryConverter<V extends Keyed> implements ArgumentConvert
@Override
public List<String> getSuggestions(String input) {
if (namespaced && input.indexOf(':') < 0) {
input = "minecraft:" + input;
}
return limitByPrefix(registry.keySet().stream(), input);
return SuggestionHelper.getRegistrySuggestions(registry, input).collect(Collectors.toList());
}
}