package net.minecraft.commands.synchronization; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CompletableFuture; import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.EntityType; public class SuggestionProviders { private static final Map> PROVIDERS_BY_NAME = new HashMap(); private static final ResourceLocation ID_ASK_SERVER = ResourceLocation.withDefaultNamespace("ask_server"); public static final SuggestionProvider ASK_SERVER = register( ID_ASK_SERVER, (commandContext, suggestionsBuilder) -> commandContext.getSource().customSuggestion(commandContext) ); public static final SuggestionProvider AVAILABLE_SOUNDS = register( ResourceLocation.withDefaultNamespace("available_sounds"), (commandContext, suggestionsBuilder) -> SharedSuggestionProvider.suggestResource(commandContext.getSource().getAvailableSounds(), suggestionsBuilder) ); public static final SuggestionProvider SUMMONABLE_ENTITIES = register( ResourceLocation.withDefaultNamespace("summonable_entities"), (commandContext, suggestionsBuilder) -> SharedSuggestionProvider.suggestResource( BuiltInRegistries.ENTITY_TYPE.stream().filter(entityType -> entityType.isEnabled(commandContext.getSource().enabledFeatures()) && entityType.canSummon()), suggestionsBuilder, EntityType::getKey, EntityType::getDescription ) ); public static SuggestionProvider register(ResourceLocation name, SuggestionProvider provider) { SuggestionProvider suggestionProvider = (SuggestionProvider)PROVIDERS_BY_NAME.putIfAbsent(name, provider); if (suggestionProvider != null) { throw new IllegalArgumentException("A command suggestion provider is already registered with the name '" + name + "'"); } else { return new SuggestionProviders.RegisteredSuggestion(name, provider); } } public static SuggestionProvider cast(SuggestionProvider provider) { return (SuggestionProvider)provider; } public static SuggestionProvider getProvider(ResourceLocation name) { return cast((SuggestionProvider)PROVIDERS_BY_NAME.getOrDefault(name, ASK_SERVER)); } /** * Gets the ID for the given provider. If the provider is not a wrapped one created via {@link #register}, then it returns {@link #ASK_SERVER_ID} instead, as there is no known ID but ASK_SERVER always works. */ public static ResourceLocation getName(SuggestionProvider provider) { return provider instanceof SuggestionProviders.RegisteredSuggestion registeredSuggestion ? registeredSuggestion.name : ID_ASK_SERVER; } record RegisteredSuggestion(ResourceLocation name, SuggestionProvider delegate) implements SuggestionProvider { @Override public CompletableFuture getSuggestions(CommandContext commandContext, SuggestionsBuilder suggestionsBuilder) throws CommandSyntaxException { return this.delegate.getSuggestions(commandContext, suggestionsBuilder); } } }