package net.minecraft.commands.synchronization; import com.google.common.collect.Maps; 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.Map; import java.util.concurrent.CompletableFuture; import net.minecraft.Util; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.EntityType; public class SuggestionProviders { private static final Map> PROVIDERS_BY_NAME = Maps.newHashMap(); private static final ResourceLocation DEFAULT_NAME = ResourceLocation.withDefaultNamespace("ask_server"); public static final SuggestionProvider ASK_SERVER = register( DEFAULT_NAME, (p_121673_, p_121674_) -> p_121673_.getSource().customSuggestion(p_121673_) ); public static final SuggestionProvider AVAILABLE_SOUNDS = register( ResourceLocation.withDefaultNamespace("available_sounds"), (p_121667_, p_121668_) -> SharedSuggestionProvider.suggestResource(p_121667_.getSource().getAvailableSounds(), p_121668_) ); public static final SuggestionProvider SUMMONABLE_ENTITIES = register( ResourceLocation.withDefaultNamespace("summonable_entities"), (p_358078_, p_358079_) -> SharedSuggestionProvider.suggestResource( BuiltInRegistries.ENTITY_TYPE.stream().filter(p_247987_ -> p_247987_.isEnabled(p_358078_.getSource().enabledFeatures()) && p_247987_.canSummon()), p_358079_, EntityType::getKey, p_212436_ -> Component.translatable(Util.makeDescriptionId("entity", EntityType.getKey(p_212436_))) ) ); public static SuggestionProvider register( ResourceLocation p_121659_, SuggestionProvider p_121660_ ) { if (PROVIDERS_BY_NAME.containsKey(p_121659_)) { throw new IllegalArgumentException("A command suggestion provider is already registered with the name " + p_121659_); } else { PROVIDERS_BY_NAME.put(p_121659_, p_121660_); return (SuggestionProvider)new Wrapper(p_121659_, p_121660_); } } public static SuggestionProvider getProvider(ResourceLocation p_121657_) { return PROVIDERS_BY_NAME.getOrDefault(p_121657_, ASK_SERVER); } public static ResourceLocation getName(SuggestionProvider p_121655_) { return p_121655_ instanceof SuggestionProviders.Wrapper ? ((SuggestionProviders.Wrapper)p_121655_).name : DEFAULT_NAME; } public static SuggestionProvider safelySwap(SuggestionProvider p_121665_) { return p_121665_ instanceof SuggestionProviders.Wrapper ? p_121665_ : ASK_SERVER; } protected static class Wrapper implements SuggestionProvider { private final SuggestionProvider delegate; final ResourceLocation name; public Wrapper(ResourceLocation p_121678_, SuggestionProvider p_121679_) { this.delegate = p_121679_; this.name = p_121678_; } @Override public CompletableFuture getSuggestions(CommandContext p_121683_, SuggestionsBuilder p_121684_) throws CommandSyntaxException { return this.delegate.getSuggestions(p_121683_, p_121684_); } } }