| 1 | package net.minecraft.commands.arguments;
|
| 2 |
|
| 3 | import com.google.gson.JsonObject;
|
| 4 | import com.mojang.brigadier.StringReader;
|
| 5 | import com.mojang.brigadier.arguments.ArgumentType;
|
| 6 | import com.mojang.brigadier.context.CommandContext;
|
| 7 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 8 | import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
|
| 9 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 10 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 11 | import java.util.Collection;
|
| 12 | import java.util.List;
|
| 13 | import java.util.concurrent.CompletableFuture;
|
| 14 | import net.minecraft.commands.CommandBuildContext;
|
| 15 | import net.minecraft.commands.CommandSourceStack;
|
| 16 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 17 | import net.minecraft.commands.synchronization.ArgumentTypeInfo;
|
| 18 | import net.minecraft.core.Holder;
|
| 19 | import net.minecraft.core.HolderLookup;
|
| 20 | import net.minecraft.core.Registry;
|
| 21 | import net.minecraft.network.FriendlyByteBuf;
|
| 22 | import net.minecraft.network.chat.Component;
|
| 23 | import net.minecraft.resources.Identifier;
|
| 24 | import net.minecraft.resources.ResourceKey;
|
| 25 | import org.apache.commons.io.FilenameUtils;
|
| 26 |
|
| 27 | public class ResourceSelectorArgument<T> implements ArgumentType<Collection<Holder.Reference<T>>> {
|
| 28 | private static final Collection<String> EXAMPLES = List.of("minecraft:*", "*:asset", "*");
|
| 29 | public static final Dynamic2CommandExceptionType ERROR_NO_MATCHES = new Dynamic2CommandExceptionType(
|
| 30 | (selector, registry) -> Component.translatableEscape("argument.resource_selector.not_found", selector, registry)
|
| 31 | );
|
| 32 | private final ResourceKey<? extends Registry<T>> registryKey;
|
| 33 | private final HolderLookup<T> registryLookup;
|
| 34 |
|
| 35 | private ResourceSelectorArgument(final CommandBuildContext context, final ResourceKey<? extends Registry<T>> registryKey) {
|
| 36 | this.registryKey = registryKey;
|
| 37 | this.registryLookup = context.lookupOrThrow(registryKey);
|
| 38 | }
|
| 39 |
|
| 40 | public Collection<Holder.Reference<T>> parse(final StringReader reader) throws CommandSyntaxException {
|
| 41 | String pattern = ensureNamespaced(readPattern(reader));
|
| 42 | List<Holder.Reference<T>> results = this.registryLookup.listElements().filter(element -> matches(pattern, element.key().identifier())).toList();
|
| 43 | if (results.isEmpty()) {
|
| 44 | throw ERROR_NO_MATCHES.createWithContext(reader, pattern, this.registryKey.identifier());
|
| 45 | } else {
|
| 46 | return results;
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | public static <T> Collection<Holder.Reference<T>> parse(final StringReader reader, final HolderLookup<T> registry) {
|
| 51 | String pattern = ensureNamespaced(readPattern(reader));
|
| 52 | return registry.listElements().filter(element -> matches(pattern, element.key().identifier())).toList();
|
| 53 | }
|
| 54 |
|
| 55 | private static String readPattern(final StringReader reader) {
|
| 56 | int start = reader.getCursor();
|
| 57 |
|
| 58 | while (reader.canRead() && isAllowedPatternCharacter(reader.peek())) {
|
| 59 | reader.skip();
|
| 60 | }
|
| 61 |
|
| 62 | return reader.getString().substring(start, reader.getCursor());
|
| 63 | }
|
| 64 |
|
| 65 | private static boolean isAllowedPatternCharacter(final char character) {
|
| 66 | return Identifier.isAllowedInIdentifier(character) || character == '*' || character == '?';
|
| 67 | }
|
| 68 |
|
| 69 | private static String ensureNamespaced(final String input) {
|
| 70 | return !input.contains(":") ? "minecraft:" + input : input;
|
| 71 | }
|
| 72 |
|
| 73 | private static boolean matches(final String pattern, final Identifier key) {
|
| 74 | return FilenameUtils.wildcardMatch(key.toString(), pattern);
|
| 75 | }
|
| 76 |
|
| 77 | public static <T> ResourceSelectorArgument<T> resourceSelector(final CommandBuildContext context, final ResourceKey<? extends Registry<T>> registry) {
|
| 78 | return new ResourceSelectorArgument<>(context, registry);
|
| 79 | }
|
| 80 |
|
| 81 | public static <T> Collection<Holder.Reference<T>> getSelectedResources(final CommandContext<CommandSourceStack> context, final String name) {
|
| 82 | return context.getArgument(name, Collection.class);
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 87 | return SharedSuggestionProvider.listSuggestions(context, builder, this.registryKey, SharedSuggestionProvider.ElementSuggestionType.ELEMENTS);
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public Collection<String> getExamples() {
|
| 92 | return EXAMPLES;
|
| 93 | }
|
| 94 |
|
| 95 | public static class Info<T> implements ArgumentTypeInfo<ResourceSelectorArgument<T>, ResourceSelectorArgument.Info<T>.Template> {
|
| 96 | public void serializeToNetwork(final ResourceSelectorArgument.Info<T>.Template template, final FriendlyByteBuf out) {
|
| 97 | out.writeResourceKey(template.registryKey);
|
| 98 | }
|
| 99 |
|
| 100 | public ResourceSelectorArgument.Info<T>.Template deserializeFromNetwork(final FriendlyByteBuf in) {
|
| 101 | return new ResourceSelectorArgument.Info.Template(in.readRegistryKey());
|
| 102 | }
|
| 103 |
|
| 104 | public void serializeToJson(final ResourceSelectorArgument.Info<T>.Template template, final JsonObject out) {
|
| 105 | out.addProperty("registry", template.registryKey.identifier().toString());
|
| 106 | }
|
| 107 |
|
| 108 | public ResourceSelectorArgument.Info<T>.Template unpack(final ResourceSelectorArgument<T> argument) {
|
| 109 | return new ResourceSelectorArgument.Info.Template(argument.registryKey);
|
| 110 | }
|
| 111 |
|
| 112 | public final class Template implements ArgumentTypeInfo.Template<ResourceSelectorArgument<T>> {
|
| 113 | private final ResourceKey<? extends Registry<T>> registryKey;
|
| 114 |
|
| 115 | private Template(final ResourceKey<? extends Registry<T>> registryKey) {
|
| 116 | this.registryKey = registryKey;
|
| 117 | }
|
| 118 |
|
| 119 | public ResourceSelectorArgument<T> instantiate(final CommandBuildContext context) {
|
| 120 | return new ResourceSelectorArgument<>(context, this.registryKey);
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public ArgumentTypeInfo<ResourceSelectorArgument<T>, ?> type() {
|
| 125 | return Info.this;
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 | }
|
| 130 |
|