| 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 java.util.Arrays;
|
| 12 | import java.util.Collection;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.concurrent.CompletableFuture;
|
| 15 | import java.util.function.Predicate;
|
| 16 | import net.minecraft.advancements.Advancement;
|
| 17 | import net.minecraft.advancements.AdvancementHolder;
|
| 18 | import net.minecraft.commands.CommandBuildContext;
|
| 19 | import net.minecraft.commands.CommandSourceStack;
|
| 20 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 21 | import net.minecraft.commands.synchronization.ArgumentTypeInfo;
|
| 22 | import net.minecraft.core.Holder;
|
| 23 | import net.minecraft.core.Registry;
|
| 24 | import net.minecraft.core.registries.Registries;
|
| 25 | import net.minecraft.network.FriendlyByteBuf;
|
| 26 | import net.minecraft.network.chat.Component;
|
| 27 | import net.minecraft.resources.Identifier;
|
| 28 | import net.minecraft.resources.ResourceKey;
|
| 29 | import net.minecraft.world.item.crafting.Recipe;
|
| 30 | import net.minecraft.world.item.crafting.RecipeHolder;
|
| 31 | import net.minecraft.world.item.crafting.RecipeManager;
|
| 32 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 33 | import net.minecraft.world.level.levelgen.structure.pools.StructureTemplatePool;
|
| 34 |
|
| 35 | public class ResourceKeyArgument<T> implements ArgumentType<ResourceKey<T>> {
|
| 36 | private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo:bar", "012");
|
| 37 | private static final DynamicCommandExceptionType ERROR_INVALID_STRUCTURE = new DynamicCommandExceptionType(
|
| 38 | value -> Component.translatableEscape("commands.place.structure.invalid", value)
|
| 39 | );
|
| 40 | private static final DynamicCommandExceptionType ERROR_INVALID_TEMPLATE_POOL = new DynamicCommandExceptionType(
|
| 41 | value -> Component.translatableEscape("commands.place.jigsaw.invalid", value)
|
| 42 | );
|
| 43 | private static final DynamicCommandExceptionType ERROR_INVALID_RECIPE = new DynamicCommandExceptionType(
|
| 44 | value -> Component.translatableEscape("recipe.notFound", value)
|
| 45 | );
|
| 46 | private static final DynamicCommandExceptionType ERROR_INVALID_ADVANCEMENT = new DynamicCommandExceptionType(
|
| 47 | value -> Component.translatableEscape("advancement.advancementNotFound", value)
|
| 48 | );
|
| 49 | private final ResourceKey<? extends Registry<T>> registryKey;
|
| 50 | private final Predicate<T> filter;
|
| 51 |
|
| 52 | public ResourceKeyArgument(final ResourceKey<? extends Registry<T>> registryKey) {
|
| 53 | this.registryKey = registryKey;
|
| 54 | this.filter = var0 -> true;
|
| 55 | }
|
| 56 |
|
| 57 | public ResourceKeyArgument(final ResourceKey<? extends Registry<T>> registryKey, final Predicate<T> filter) {
|
| 58 | this.registryKey = registryKey;
|
| 59 | this.filter = filter;
|
| 60 | }
|
| 61 |
|
| 62 | public static <T> ResourceKeyArgument<T> key(final ResourceKey<? extends Registry<T>> key) {
|
| 63 | return new ResourceKeyArgument<>(key);
|
| 64 | }
|
| 65 |
|
| 66 | public static <T> ResourceKeyArgument<T> key(final ResourceKey<? extends Registry<T>> key, final Predicate<T> filter) {
|
| 67 | return new ResourceKeyArgument<>(key, filter);
|
| 68 | }
|
| 69 |
|
| 70 | public static <T> ResourceKey<T> getRegistryKey(
|
| 71 | final CommandContext<CommandSourceStack> context,
|
| 72 | final String name,
|
| 73 | final ResourceKey<Registry<T>> registryKey,
|
| 74 | final DynamicCommandExceptionType exceptionType
|
| 75 | ) throws CommandSyntaxException {
|
| 76 | ResourceKey<?> argument = context.getArgument(name, ResourceKey.class);
|
| 77 | Optional<ResourceKey<T>> value = argument.cast(registryKey);
|
| 78 | return value.orElseThrow(() -> exceptionType.create(argument.identifier()));
|
| 79 | }
|
| 80 |
|
| 81 | private static <T> Registry<T> getRegistry(final CommandContext<CommandSourceStack> context, final ResourceKey<? extends Registry<T>> registryKey) {
|
| 82 | return context.getSource().getServer().registryAccess().lookupOrThrow(registryKey);
|
| 83 | }
|
| 84 |
|
| 85 | private static <T> Holder.Reference<T> resolveKey(
|
| 86 | final CommandContext<CommandSourceStack> context, final String name, final ResourceKey<Registry<T>> registryKey, final DynamicCommandExceptionType exception
|
| 87 | ) throws CommandSyntaxException {
|
| 88 | ResourceKey<T> key = getRegistryKey(context, name, registryKey, exception);
|
| 89 | return getRegistry(context, registryKey).get(key).orElseThrow(() -> exception.create(key.identifier()));
|
| 90 | }
|
| 91 |
|
| 92 | public static Holder.Reference<Structure> getStructure(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 93 | return resolveKey(context, name, Registries.STRUCTURE, ERROR_INVALID_STRUCTURE);
|
| 94 | }
|
| 95 |
|
| 96 | public static Holder.Reference<StructureTemplatePool> getStructureTemplatePool(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 97 | return resolveKey(context, name, Registries.TEMPLATE_POOL, ERROR_INVALID_TEMPLATE_POOL);
|
| 98 | }
|
| 99 |
|
| 100 | public static RecipeHolder<?> getRecipe(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 101 | RecipeManager recipeManager = context.getSource().getServer().getRecipeManager();
|
| 102 | ResourceKey<Recipe<?>> key = getRegistryKey(context, name, Registries.RECIPE, ERROR_INVALID_RECIPE);
|
| 103 | return recipeManager.byKey(key).filter(r -> !r.value().isSpecial()).orElseThrow(() -> ERROR_INVALID_RECIPE.create(key.identifier()));
|
| 104 | }
|
| 105 |
|
| 106 | public static AdvancementHolder getAdvancement(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 107 | ResourceKey<Advancement> key = getRegistryKey(context, name, Registries.ADVANCEMENT, ERROR_INVALID_ADVANCEMENT);
|
| 108 | AdvancementHolder advancement = context.getSource().getServer().getAdvancements().get(key.identifier());
|
| 109 | if (advancement == null) {
|
| 110 | throw ERROR_INVALID_ADVANCEMENT.create(key.identifier());
|
| 111 | } else {
|
| 112 | return advancement;
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | public ResourceKey<T> parse(final StringReader reader) throws CommandSyntaxException {
|
| 117 | Identifier resourceId = Identifier.read(reader);
|
| 118 | return ResourceKey.create(this.registryKey, resourceId);
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 123 | return SharedSuggestionProvider.listSuggestions(context, builder, this.registryKey, SharedSuggestionProvider.ElementSuggestionType.ELEMENTS, this.filter);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public Collection<String> getExamples() {
|
| 128 | return EXAMPLES;
|
| 129 | }
|
| 130 |
|
| 131 | public static class Info<T> implements ArgumentTypeInfo<ResourceKeyArgument<T>, ResourceKeyArgument.Info<T>.Template> {
|
| 132 | public void serializeToNetwork(final ResourceKeyArgument.Info<T>.Template template, final FriendlyByteBuf out) {
|
| 133 | out.writeResourceKey(template.registryKey);
|
| 134 | }
|
| 135 |
|
| 136 | public ResourceKeyArgument.Info<T>.Template deserializeFromNetwork(final FriendlyByteBuf in) {
|
| 137 | return new ResourceKeyArgument.Info.Template(in.readRegistryKey());
|
| 138 | }
|
| 139 |
|
| 140 | public void serializeToJson(final ResourceKeyArgument.Info<T>.Template template, final JsonObject out) {
|
| 141 | out.addProperty("registry", template.registryKey.identifier().toString());
|
| 142 | }
|
| 143 |
|
| 144 | public ResourceKeyArgument.Info<T>.Template unpack(final ResourceKeyArgument<T> argument) {
|
| 145 | return new ResourceKeyArgument.Info.Template(argument.registryKey);
|
| 146 | }
|
| 147 |
|
| 148 | public final class Template implements ArgumentTypeInfo.Template<ResourceKeyArgument<T>> {
|
| 149 | private final ResourceKey<? extends Registry<T>> registryKey;
|
| 150 |
|
| 151 | private Template(final ResourceKey<? extends Registry<T>> registryKey) {
|
| 152 | this.registryKey = registryKey;
|
| 153 | }
|
| 154 |
|
| 155 | public ResourceKeyArgument<T> instantiate(final CommandBuildContext context) {
|
| 156 | return new ResourceKeyArgument<>(this.registryKey);
|
| 157 | }
|
| 158 |
|
| 159 | @Override
|
| 160 | public ArgumentTypeInfo<ResourceKeyArgument<T>, ?> type() {
|
| 161 | return Info.this;
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 | }
|
| 166 |
|