| 1 | package net.minecraft.commands.arguments.item;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
|
| 6 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 7 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 8 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 9 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 10 | import com.mojang.serialization.DataResult;
|
| 11 | import it.unimi.dsi.fastutil.objects.ReferenceArraySet;
|
| 12 | import java.util.Locale;
|
| 13 | import java.util.Objects;
|
| 14 | import java.util.Set;
|
| 15 | import java.util.concurrent.CompletableFuture;
|
| 16 | import java.util.function.Function;
|
| 17 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 18 | import net.minecraft.core.Holder;
|
| 19 | import net.minecraft.core.HolderLookup;
|
| 20 | import net.minecraft.core.component.DataComponentPatch;
|
| 21 | import net.minecraft.core.component.DataComponentType;
|
| 22 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 23 | import net.minecraft.core.registries.Registries;
|
| 24 | import net.minecraft.nbt.NbtOps;
|
| 25 | import net.minecraft.nbt.Tag;
|
| 26 | import net.minecraft.nbt.TagParser;
|
| 27 | import net.minecraft.network.chat.Component;
|
| 28 | import net.minecraft.resources.Identifier;
|
| 29 | import net.minecraft.resources.RegistryOps;
|
| 30 | import net.minecraft.resources.ResourceKey;
|
| 31 | import net.minecraft.world.item.Item;
|
| 32 | import org.apache.commons.lang3.mutable.MutableObject;
|
| 33 |
|
| 34 | public class ItemParser {
|
| 35 | private static final DynamicCommandExceptionType ERROR_UNKNOWN_ITEM = new DynamicCommandExceptionType(
|
| 36 | id -> Component.translatableEscape("argument.item.id.invalid", id)
|
| 37 | );
|
| 38 | private static final DynamicCommandExceptionType ERROR_UNKNOWN_COMPONENT = new DynamicCommandExceptionType(
|
| 39 | id -> Component.translatableEscape("arguments.item.component.unknown", id)
|
| 40 | );
|
| 41 | private static final Dynamic2CommandExceptionType ERROR_MALFORMED_COMPONENT = new Dynamic2CommandExceptionType(
|
| 42 | (type, message) -> Component.translatableEscape("arguments.item.component.malformed", type, message)
|
| 43 | );
|
| 44 | private static final SimpleCommandExceptionType ERROR_EXPECTED_COMPONENT = new SimpleCommandExceptionType(
|
| 45 | Component.translatable("arguments.item.component.expected")
|
| 46 | );
|
| 47 | private static final DynamicCommandExceptionType ERROR_REPEATED_COMPONENT = new DynamicCommandExceptionType(
|
| 48 | id -> Component.translatableEscape("arguments.item.component.repeated", id)
|
| 49 | );
|
| 50 | public static final char SYNTAX_START_COMPONENTS = '[';
|
| 51 | public static final char SYNTAX_END_COMPONENTS = ']';
|
| 52 | public static final char SYNTAX_COMPONENT_SEPARATOR = ',';
|
| 53 | public static final char SYNTAX_COMPONENT_ASSIGNMENT = '=';
|
| 54 | public static final char SYNTAX_REMOVED_COMPONENT = '!';
|
| 55 | private static final Function<SuggestionsBuilder, CompletableFuture<Suggestions>> SUGGEST_NOTHING = SuggestionsBuilder::buildFuture;
|
| 56 | private final HolderLookup.RegistryLookup<Item> items;
|
| 57 | private final RegistryOps<Tag> registryOps;
|
| 58 | private final TagParser<Tag> tagParser;
|
| 59 |
|
| 60 | public ItemParser(final HolderLookup.Provider registries) {
|
| 61 | this.items = registries.lookupOrThrow(Registries.ITEM);
|
| 62 | this.registryOps = registries.createSerializationContext(NbtOps.INSTANCE);
|
| 63 | this.tagParser = TagParser.create(this.registryOps);
|
| 64 | }
|
| 65 |
|
| 66 | public ItemInput parse(final StringReader reader) throws CommandSyntaxException {
|
| 67 | final MutableObject<Holder<Item>> itemResult = new MutableObject<>();
|
| 68 | final DataComponentPatch.Builder componentsBuilder = DataComponentPatch.builder();
|
| 69 | this.parse(reader, new ItemParser.Visitor() {
|
| 70 | @Override
|
| 71 | public void visitItem(final Holder<Item> item) {
|
| 72 | itemResult.setValue(item);
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public <T> void visitComponent(final DataComponentType<T> type, final T value) {
|
| 77 | componentsBuilder.set(type, value);
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public <T> void visitRemovedComponent(final DataComponentType<T> type) {
|
| 82 | componentsBuilder.remove(type);
|
| 83 | }
|
| 84 | });
|
| 85 | Holder<Item> item = Objects.requireNonNull(itemResult.get(), "Parser gave no item");
|
| 86 | DataComponentPatch components = componentsBuilder.build();
|
| 87 | return new ItemInput(item, components);
|
| 88 | }
|
| 89 |
|
| 90 | public void parse(final StringReader reader, final ItemParser.Visitor visitor) throws CommandSyntaxException {
|
| 91 | int cursor = reader.getCursor();
|
| 92 |
|
| 93 | try {
|
| 94 | new ItemParser.State(reader, visitor).parse();
|
| 95 | } catch (CommandSyntaxException e) {
|
| 96 | reader.setCursor(cursor);
|
| 97 | throw e;
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | public CompletableFuture<Suggestions> fillSuggestions(final SuggestionsBuilder builder) {
|
| 102 | StringReader reader = new StringReader(builder.getInput());
|
| 103 | reader.setCursor(builder.getStart());
|
| 104 | ItemParser.SuggestionsVisitor handler = new ItemParser.SuggestionsVisitor();
|
| 105 | ItemParser.State state = new ItemParser.State(reader, handler);
|
| 106 |
|
| 107 | try {
|
| 108 | state.parse();
|
| 109 | } catch (CommandSyntaxException var6) {
|
| 110 | }
|
| 111 |
|
| 112 | return handler.resolveSuggestions(builder, reader);
|
| 113 | }
|
| 114 |
|
| 115 | private class State {
|
| 116 | private final StringReader reader;
|
| 117 | private final ItemParser.Visitor visitor;
|
| 118 |
|
| 119 | private State(final StringReader reader, final ItemParser.Visitor visitor) {
|
| 120 | this.reader = reader;
|
| 121 | this.visitor = visitor;
|
| 122 | }
|
| 123 |
|
| 124 | public void parse() throws CommandSyntaxException {
|
| 125 | this.visitor.visitSuggestions(this::suggestItem);
|
| 126 | this.readItem();
|
| 127 | this.visitor.visitSuggestions(this::suggestStartComponents);
|
| 128 | if (this.reader.canRead() && this.reader.peek() == '[') {
|
| 129 | this.visitor.visitSuggestions(ItemParser.SUGGEST_NOTHING);
|
| 130 | this.readComponents();
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | private void readItem() throws CommandSyntaxException {
|
| 135 | int cursor = this.reader.getCursor();
|
| 136 | Identifier id = Identifier.read(this.reader);
|
| 137 | this.visitor.visitItem(ItemParser.this.items.get(ResourceKey.create(Registries.ITEM, id)).orElseThrow(() -> {
|
| 138 | this.reader.setCursor(cursor);
|
| 139 | return ItemParser.ERROR_UNKNOWN_ITEM.createWithContext(this.reader, id);
|
| 140 | }));
|
| 141 | }
|
| 142 |
|
| 143 | private void readComponents() throws CommandSyntaxException {
|
| 144 | this.reader.expect('[');
|
| 145 | this.visitor.visitSuggestions(this::suggestComponentAssignmentOrRemoval);
|
| 146 | Set<DataComponentType<?>> visitedComponents = new ReferenceArraySet<>();
|
| 147 |
|
| 148 | while (this.reader.canRead() && this.reader.peek() != ']') {
|
| 149 | this.reader.skipWhitespace();
|
| 150 | if (this.reader.canRead() && this.reader.peek() == '!') {
|
| 151 | this.reader.skip();
|
| 152 | this.visitor.visitSuggestions(this::suggestComponent);
|
| 153 | DataComponentType<?> componentType = readComponentType(this.reader);
|
| 154 | if (!visitedComponents.add(componentType)) {
|
| 155 | throw ItemParser.ERROR_REPEATED_COMPONENT.create(componentType);
|
| 156 | }
|
| 157 |
|
| 158 | this.visitor.visitRemovedComponent(componentType);
|
| 159 | this.visitor.visitSuggestions(ItemParser.SUGGEST_NOTHING);
|
| 160 | this.reader.skipWhitespace();
|
| 161 | } else {
|
| 162 | DataComponentType<?> componentType = readComponentType(this.reader);
|
| 163 | if (!visitedComponents.add(componentType)) {
|
| 164 | throw ItemParser.ERROR_REPEATED_COMPONENT.create(componentType);
|
| 165 | }
|
| 166 |
|
| 167 | this.visitor.visitSuggestions(this::suggestAssignment);
|
| 168 | this.reader.skipWhitespace();
|
| 169 | this.reader.expect('=');
|
| 170 | this.visitor.visitSuggestions(ItemParser.SUGGEST_NOTHING);
|
| 171 | this.reader.skipWhitespace();
|
| 172 | this.readComponent(ItemParser.this.tagParser, ItemParser.this.registryOps, componentType);
|
| 173 | this.reader.skipWhitespace();
|
| 174 | }
|
| 175 |
|
| 176 | this.visitor.visitSuggestions(this::suggestNextOrEndComponents);
|
| 177 | if (!this.reader.canRead() || this.reader.peek() != ',') {
|
| 178 | break;
|
| 179 | }
|
| 180 |
|
| 181 | this.reader.skip();
|
| 182 | this.reader.skipWhitespace();
|
| 183 | this.visitor.visitSuggestions(this::suggestComponentAssignmentOrRemoval);
|
| 184 | if (!this.reader.canRead()) {
|
| 185 | throw ItemParser.ERROR_EXPECTED_COMPONENT.createWithContext(this.reader);
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | this.reader.expect(']');
|
| 190 | this.visitor.visitSuggestions(ItemParser.SUGGEST_NOTHING);
|
| 191 | }
|
| 192 |
|
| 193 | public static DataComponentType<?> readComponentType(final StringReader reader) throws CommandSyntaxException {
|
| 194 | if (!reader.canRead()) {
|
| 195 | throw ItemParser.ERROR_EXPECTED_COMPONENT.createWithContext(reader);
|
| 196 | }
|
| 197 |
|
| 198 | int cursor = reader.getCursor();
|
| 199 | Identifier id = Identifier.read(reader);
|
| 200 | DataComponentType<?> component = BuiltInRegistries.DATA_COMPONENT_TYPE.getValue(id);
|
| 201 | if (component != null && !component.isTransient()) {
|
| 202 | return component;
|
| 203 | }
|
| 204 |
|
| 205 | reader.setCursor(cursor);
|
| 206 | throw ItemParser.ERROR_UNKNOWN_COMPONENT.createWithContext(reader, id);
|
| 207 | }
|
| 208 |
|
| 209 | private <T, O> void readComponent(final TagParser<O> tagParser, final RegistryOps<O> registryOps, final DataComponentType<T> componentType) throws CommandSyntaxException {
|
| 210 | int cursor = this.reader.getCursor();
|
| 211 | O tag = tagParser.parseAsArgument(this.reader);
|
| 212 | DataResult<T> result = componentType.codecOrThrow().parse(registryOps, tag);
|
| 213 | this.visitor.visitComponent(componentType, result.getOrThrow(message -> {
|
| 214 | this.reader.setCursor(cursor);
|
| 215 | return ItemParser.ERROR_MALFORMED_COMPONENT.createWithContext(this.reader, componentType.toString(), message);
|
| 216 | }));
|
| 217 | }
|
| 218 |
|
| 219 | private CompletableFuture<Suggestions> suggestStartComponents(final SuggestionsBuilder builder) {
|
| 220 | if (builder.getRemaining().isEmpty()) {
|
| 221 | builder.suggest(String.valueOf('['));
|
| 222 | }
|
| 223 |
|
| 224 | return builder.buildFuture();
|
| 225 | }
|
| 226 |
|
| 227 | private CompletableFuture<Suggestions> suggestNextOrEndComponents(final SuggestionsBuilder builder) {
|
| 228 | if (builder.getRemaining().isEmpty()) {
|
| 229 | builder.suggest(String.valueOf(','));
|
| 230 | builder.suggest(String.valueOf(']'));
|
| 231 | }
|
| 232 |
|
| 233 | return builder.buildFuture();
|
| 234 | }
|
| 235 |
|
| 236 | private CompletableFuture<Suggestions> suggestAssignment(final SuggestionsBuilder builder) {
|
| 237 | if (builder.getRemaining().isEmpty()) {
|
| 238 | builder.suggest(String.valueOf('='));
|
| 239 | }
|
| 240 |
|
| 241 | return builder.buildFuture();
|
| 242 | }
|
| 243 |
|
| 244 | private CompletableFuture<Suggestions> suggestItem(final SuggestionsBuilder builder) {
|
| 245 | return SharedSuggestionProvider.suggestResource(ItemParser.this.items.listElementIds().map(ResourceKey::identifier), builder);
|
| 246 | }
|
| 247 |
|
| 248 | private CompletableFuture<Suggestions> suggestComponentAssignmentOrRemoval(final SuggestionsBuilder builder) {
|
| 249 | builder.suggest(String.valueOf('!'));
|
| 250 | return this.suggestComponent(builder, String.valueOf('='));
|
| 251 | }
|
| 252 |
|
| 253 | private CompletableFuture<Suggestions> suggestComponent(final SuggestionsBuilder builder) {
|
| 254 | return this.suggestComponent(builder, "");
|
| 255 | }
|
| 256 |
|
| 257 | private CompletableFuture<Suggestions> suggestComponent(final SuggestionsBuilder builder, final String suffix) {
|
| 258 | String contents = builder.getRemaining().toLowerCase(Locale.ROOT);
|
| 259 | SharedSuggestionProvider.filterResources(BuiltInRegistries.DATA_COMPONENT_TYPE.entrySet(), contents, entry -> entry.getKey().identifier(), entry -> {
|
| 260 | DataComponentType<?> type = entry.getValue();
|
| 261 | if (type.codec() != null) {
|
| 262 | Identifier id = entry.getKey().identifier();
|
| 263 | builder.suggest(id + suffix);
|
| 264 | }
|
| 265 | });
|
| 266 | return builder.buildFuture();
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | private static class SuggestionsVisitor implements ItemParser.Visitor {
|
| 271 | private Function<SuggestionsBuilder, CompletableFuture<Suggestions>> suggestions = ItemParser.SUGGEST_NOTHING;
|
| 272 |
|
| 273 | @Override
|
| 274 | public void visitSuggestions(final Function<SuggestionsBuilder, CompletableFuture<Suggestions>> suggestions) {
|
| 275 | this.suggestions = suggestions;
|
| 276 | }
|
| 277 |
|
| 278 | public CompletableFuture<Suggestions> resolveSuggestions(final SuggestionsBuilder builder, final StringReader reader) {
|
| 279 | return this.suggestions.apply(builder.createOffset(reader.getCursor()));
|
| 280 | }
|
| 281 | }
|
| 282 |
|
| 283 | public interface Visitor {
|
| 284 | default void visitItem(final Holder<Item> item) {
|
| 285 | }
|
| 286 |
|
| 287 | default <T> void visitComponent(final DataComponentType<T> type, final T value) {
|
| 288 | }
|
| 289 |
|
| 290 | default <T> void visitRemovedComponent(final DataComponentType<T> type) {
|
| 291 | }
|
| 292 |
|
| 293 | default void visitSuggestions(final Function<SuggestionsBuilder, CompletableFuture<Suggestions>> suggestions) {
|
| 294 | }
|
| 295 | }
|
| 296 | }
|
| 297 |
|