package net.minecraft.commands.arguments; import com.google.gson.JsonObject; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import java.util.Collection; import java.util.List; import java.util.concurrent.CompletableFuture; import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.commands.SharedSuggestionProvider.ElementSuggestionType; import net.minecraft.commands.synchronization.ArgumentTypeInfo; import net.minecraft.core.HolderLookup; import net.minecraft.core.Registry; import net.minecraft.core.Holder.Reference; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import org.apache.commons.io.FilenameUtils; public class ResourceSelectorArgument implements ArgumentType>> { private static final Collection EXAMPLES = List.of("minecraft:*", "*:asset", "*"); public static final Dynamic2CommandExceptionType ERROR_NO_MATCHES = new Dynamic2CommandExceptionType( (object, object2) -> Component.translatableEscape("argument.resource_selector.not_found", object, object2) ); final ResourceKey> registryKey; private final HolderLookup registryLookup; ResourceSelectorArgument(CommandBuildContext buildContext, ResourceKey> registryKey) { this.registryKey = registryKey; this.registryLookup = buildContext.lookupOrThrow(registryKey); } public Collection> parse(StringReader stringReader) throws CommandSyntaxException { String string = ensureNamespaced(readPattern(stringReader)); List> list = this.registryLookup.listElements().filter(reference -> matches(string, reference.key().location())).toList(); if (list.isEmpty()) { throw ERROR_NO_MATCHES.createWithContext(stringReader, string, this.registryKey.location()); } else { return list; } } public static Collection> parse(StringReader parse, HolderLookup lookup) { String string = ensureNamespaced(readPattern(parse)); return lookup.listElements().filter(reference -> matches(string, reference.key().location())).toList(); } private static String readPattern(StringReader reader) { int i = reader.getCursor(); while (reader.canRead() && isAllowedPatternCharacter(reader.peek())) { reader.skip(); } return reader.getString().substring(i, reader.getCursor()); } private static boolean isAllowedPatternCharacter(char c) { return ResourceLocation.isAllowedInResourceLocation(c) || c == '*' || c == '?'; } private static String ensureNamespaced(String name) { return !name.contains(":") ? "minecraft:" + name : name; } private static boolean matches(String string, ResourceLocation location) { return FilenameUtils.wildcardMatch(location.toString(), string); } public static ResourceSelectorArgument resourceSelector(CommandBuildContext buildContext, ResourceKey> registryKey) { return new ResourceSelectorArgument<>(buildContext, registryKey); } public static Collection> getSelectedResources( CommandContext context, String argument, ResourceKey> registryKey ) { return context.getArgument(argument, Collection.class); } @Override public CompletableFuture listSuggestions(CommandContext commandContext, SuggestionsBuilder suggestionsBuilder) { return commandContext.getSource() instanceof SharedSuggestionProvider sharedSuggestionProvider ? sharedSuggestionProvider.suggestRegistryElements(this.registryKey, ElementSuggestionType.ELEMENTS, suggestionsBuilder, commandContext) : SharedSuggestionProvider.suggest(this.registryLookup.listElementIds().map(ResourceKey::location).map(ResourceLocation::toString), suggestionsBuilder); } @Override public Collection getExamples() { return EXAMPLES; } public static class Info implements ArgumentTypeInfo, ResourceSelectorArgument.Info.Template> { public void serializeToNetwork(ResourceSelectorArgument.Info.Template template, FriendlyByteBuf friendlyByteBuf) { friendlyByteBuf.writeResourceKey(template.registryKey); } public ResourceSelectorArgument.Info.Template deserializeFromNetwork(FriendlyByteBuf friendlyByteBuf) { return new net.minecraft.commands.arguments.ResourceSelectorArgument.Info.Template(this, friendlyByteBuf.readRegistryKey()); } public void serializeToJson(ResourceSelectorArgument.Info.Template template, JsonObject jsonObject) { jsonObject.addProperty("registry", template.registryKey.location().toString()); } public ResourceSelectorArgument.Info.Template unpack(ResourceSelectorArgument resourceSelectorArgument) { return new net.minecraft.commands.arguments.ResourceSelectorArgument.Info.Template(this, resourceSelectorArgument.registryKey); } } }