| 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.DynamicCommandExceptionType;
|
| 9 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 10 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 11 | import com.mojang.datafixers.util.Either;
|
| 12 | import java.util.Arrays;
|
| 13 | import java.util.Collection;
|
| 14 | import java.util.Optional;
|
| 15 | import java.util.concurrent.CompletableFuture;
|
| 16 | import java.util.function.Predicate;
|
| 17 | import net.minecraft.commands.CommandBuildContext;
|
| 18 | import net.minecraft.commands.CommandSourceStack;
|
| 19 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 20 | import net.minecraft.commands.synchronization.ArgumentTypeInfo;
|
| 21 | import net.minecraft.core.Holder;
|
| 22 | import net.minecraft.core.Registry;
|
| 23 | import net.minecraft.network.FriendlyByteBuf;
|
| 24 | import net.minecraft.resources.Identifier;
|
| 25 | import net.minecraft.resources.ResourceKey;
|
| 26 | import net.minecraft.tags.TagKey;
|
| 27 |
|
| 28 | public class ResourceOrTagKeyArgument<T> implements ArgumentType<ResourceOrTagKeyArgument.Result<T>> {
|
| 29 | private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo:bar", "012", "#skeletons", "#minecraft:skeletons");
|
| 30 | private final ResourceKey<? extends Registry<T>> registryKey;
|
| 31 |
|
| 32 | public ResourceOrTagKeyArgument(final ResourceKey<? extends Registry<T>> registryKey) {
|
| 33 | this.registryKey = registryKey;
|
| 34 | }
|
| 35 |
|
| 36 | public static <T> ResourceOrTagKeyArgument<T> resourceOrTagKey(final ResourceKey<? extends Registry<T>> key) {
|
| 37 | return new ResourceOrTagKeyArgument<>(key);
|
| 38 | }
|
| 39 |
|
| 40 | public static <T> ResourceOrTagKeyArgument.Result<T> getResourceOrTagKey(
|
| 41 | final CommandContext<CommandSourceStack> context,
|
| 42 | final String name,
|
| 43 | final ResourceKey<Registry<T>> registryKey,
|
| 44 | final DynamicCommandExceptionType exceptionType
|
| 45 | ) throws CommandSyntaxException {
|
| 46 | ResourceOrTagKeyArgument.Result<?> argument = context.getArgument(name, ResourceOrTagKeyArgument.Result.class);
|
| 47 | Optional<ResourceOrTagKeyArgument.Result<T>> value = argument.cast(registryKey);
|
| 48 | return value.orElseThrow(() -> exceptionType.create(argument));
|
| 49 | }
|
| 50 |
|
| 51 | public ResourceOrTagKeyArgument.Result<T> parse(final StringReader reader) throws CommandSyntaxException {
|
| 52 | if (reader.canRead() && reader.peek() == '#') {
|
| 53 | int cursor = reader.getCursor();
|
| 54 |
|
| 55 | try {
|
| 56 | reader.skip();
|
| 57 | Identifier tagId = Identifier.read(reader);
|
| 58 | return new ResourceOrTagKeyArgument.TagResult<>(TagKey.create(this.registryKey, tagId));
|
| 59 | } catch (CommandSyntaxException e) {
|
| 60 | reader.setCursor(cursor);
|
| 61 | throw e;
|
| 62 | }
|
| 63 | } else {
|
| 64 | Identifier resourceId = Identifier.read(reader);
|
| 65 | return new ResourceOrTagKeyArgument.ResourceResult<>(ResourceKey.create(this.registryKey, resourceId));
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 71 | return SharedSuggestionProvider.listSuggestions(context, builder, this.registryKey, SharedSuggestionProvider.ElementSuggestionType.ALL);
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public Collection<String> getExamples() {
|
| 76 | return EXAMPLES;
|
| 77 | }
|
| 78 |
|
| 79 | public static class Info<T> implements ArgumentTypeInfo<ResourceOrTagKeyArgument<T>, ResourceOrTagKeyArgument.Info<T>.Template> {
|
| 80 | public void serializeToNetwork(final ResourceOrTagKeyArgument.Info<T>.Template template, final FriendlyByteBuf out) {
|
| 81 | out.writeResourceKey(template.registryKey);
|
| 82 | }
|
| 83 |
|
| 84 | public ResourceOrTagKeyArgument.Info<T>.Template deserializeFromNetwork(final FriendlyByteBuf in) {
|
| 85 | return new ResourceOrTagKeyArgument.Info.Template(in.readRegistryKey());
|
| 86 | }
|
| 87 |
|
| 88 | public void serializeToJson(final ResourceOrTagKeyArgument.Info<T>.Template template, final JsonObject out) {
|
| 89 | out.addProperty("registry", template.registryKey.identifier().toString());
|
| 90 | }
|
| 91 |
|
| 92 | public ResourceOrTagKeyArgument.Info<T>.Template unpack(final ResourceOrTagKeyArgument<T> argument) {
|
| 93 | return new ResourceOrTagKeyArgument.Info.Template(argument.registryKey);
|
| 94 | }
|
| 95 |
|
| 96 | public final class Template implements ArgumentTypeInfo.Template<ResourceOrTagKeyArgument<T>> {
|
| 97 | private final ResourceKey<? extends Registry<T>> registryKey;
|
| 98 |
|
| 99 | private Template(final ResourceKey<? extends Registry<T>> registryKey) {
|
| 100 | this.registryKey = registryKey;
|
| 101 | }
|
| 102 |
|
| 103 | public ResourceOrTagKeyArgument<T> instantiate(final CommandBuildContext context) {
|
| 104 | return new ResourceOrTagKeyArgument<>(this.registryKey);
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public ArgumentTypeInfo<ResourceOrTagKeyArgument<T>, ?> type() {
|
| 109 | return Info.this;
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | private record ResourceResult<T>(ResourceKey<T> key) implements ResourceOrTagKeyArgument.Result<T> {
|
| 115 | @Override
|
| 116 | public Either<ResourceKey<T>, TagKey<T>> unwrap() {
|
| 117 | return Either.left(this.key);
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public <E> Optional<ResourceOrTagKeyArgument.Result<E>> cast(final ResourceKey<? extends Registry<E>> registryKey) {
|
| 122 | return this.key.cast(registryKey).map(ResourceOrTagKeyArgument.ResourceResult::new);
|
| 123 | }
|
| 124 |
|
| 125 | public boolean test(final Holder<T> holder) {
|
| 126 | return holder.is(this.key);
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | public String asPrintable() {
|
| 131 | return this.key.identifier().toString();
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | public interface Result<T> extends Predicate<Holder<T>> {
|
| 136 | Either<ResourceKey<T>, TagKey<T>> unwrap();
|
| 137 |
|
| 138 | <E> Optional<ResourceOrTagKeyArgument.Result<E>> cast(final ResourceKey<? extends Registry<E>> registryKey);
|
| 139 |
|
| 140 | String asPrintable();
|
| 141 | }
|
| 142 |
|
| 143 | private record TagResult<T>(TagKey<T> key) implements ResourceOrTagKeyArgument.Result<T> {
|
| 144 | @Override
|
| 145 | public Either<ResourceKey<T>, TagKey<T>> unwrap() {
|
| 146 | return Either.right(this.key);
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | public <E> Optional<ResourceOrTagKeyArgument.Result<E>> cast(final ResourceKey<? extends Registry<E>> registryKey) {
|
| 151 | return this.key.cast(registryKey).map(ResourceOrTagKeyArgument.TagResult::new);
|
| 152 | }
|
| 153 |
|
| 154 | public boolean test(final Holder<T> holder) {
|
| 155 | return holder.is(this.key);
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public String asPrintable() {
|
| 160 | return "#" + this.key.location();
|
| 161 | }
|
| 162 | }
|
| 163 | }
|
| 164 |
|