| 1 | package net.minecraft.commands.synchronization;
|
| 2 |
|
| 3 | import com.mojang.brigadier.context.CommandContext;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.brigadier.suggestion.SuggestionProvider;
|
| 6 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 7 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 8 | import java.util.HashMap;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.concurrent.CompletableFuture;
|
| 11 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 12 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 | import net.minecraft.world.entity.EntityType;
|
| 15 |
|
| 16 | public class SuggestionProviders {
|
| 17 | private static final Map<Identifier, SuggestionProvider<SharedSuggestionProvider>> PROVIDERS_BY_NAME = new HashMap<>();
|
| 18 | private static final Identifier ID_ASK_SERVER = Identifier.withDefaultNamespace("ask_server");
|
| 19 | public static final SuggestionProvider<SharedSuggestionProvider> ASK_SERVER = register(ID_ASK_SERVER, (c, p) -> c.getSource().customSuggestion(c));
|
| 20 | public static final SuggestionProvider<SharedSuggestionProvider> AVAILABLE_SOUNDS = register(
|
| 21 | Identifier.withDefaultNamespace("available_sounds"), (c, p) -> SharedSuggestionProvider.suggestResource(c.getSource().getAvailableSounds(), p)
|
| 22 | );
|
| 23 | public static final SuggestionProvider<SharedSuggestionProvider> SUMMONABLE_ENTITIES = register(
|
| 24 | Identifier.withDefaultNamespace("summonable_entities"),
|
| 25 | (c, p) -> SharedSuggestionProvider.suggestResource(
|
| 26 | BuiltInRegistries.ENTITY_TYPE.stream().filter(entityType -> entityType.isEnabled(c.getSource().enabledFeatures()) && entityType.canSummon()),
|
| 27 | p,
|
| 28 | EntityType::getKey,
|
| 29 | EntityType::getDescription
|
| 30 | )
|
| 31 | );
|
| 32 | public static final SuggestionProvider<SharedSuggestionProvider> POST_EFFECTS = register(
|
| 33 | Identifier.withDefaultNamespace("post_effects"), (c, p) -> SharedSuggestionProvider.suggestResource(c.getSource().getAvailablePostEffects(), p)
|
| 34 | );
|
| 35 |
|
| 36 | public static <S extends SharedSuggestionProvider> SuggestionProvider<S> register(
|
| 37 | final Identifier name, final SuggestionProvider<SharedSuggestionProvider> provider
|
| 38 | ) {
|
| 39 | SuggestionProvider<SharedSuggestionProvider> previous = PROVIDERS_BY_NAME.putIfAbsent(name, provider);
|
| 40 | if (previous != null) {
|
| 41 | throw new IllegalArgumentException("A command suggestion provider is already registered with the name '" + name + "'");
|
| 42 | } else {
|
| 43 | return new SuggestionProviders.RegisteredSuggestion(name, provider);
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | public static <S extends SharedSuggestionProvider> SuggestionProvider<S> cast(final SuggestionProvider<SharedSuggestionProvider> provider) {
|
| 48 | return (SuggestionProvider<S>)provider;
|
| 49 | }
|
| 50 |
|
| 51 | public static <S extends SharedSuggestionProvider> SuggestionProvider<S> getProvider(final Identifier name) {
|
| 52 | return cast(PROVIDERS_BY_NAME.getOrDefault(name, ASK_SERVER));
|
| 53 | }
|
| 54 |
|
| 55 | public static Identifier getName(final SuggestionProvider<?> provider) {
|
| 56 | return provider instanceof SuggestionProviders.RegisteredSuggestion registeredProvider ? registeredProvider.name : ID_ASK_SERVER;
|
| 57 | }
|
| 58 |
|
| 59 | private record RegisteredSuggestion(Identifier name, SuggestionProvider<SharedSuggestionProvider> delegate)
|
| 60 | implements SuggestionProvider<SharedSuggestionProvider> {
|
| 61 | @Override
|
| 62 | public CompletableFuture<Suggestions> getSuggestions(final CommandContext<SharedSuggestionProvider> context, final SuggestionsBuilder builder) throws CommandSyntaxException {
|
| 63 | return this.delegate.getSuggestions(context, builder);
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 |
|