| 1 | package net.minecraft.commands.arguments.blocks;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.mojang.brigadier.StringReader;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
|
| 7 | import com.mojang.brigadier.exceptions.Dynamic3CommandExceptionType;
|
| 8 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 9 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 10 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 11 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 12 | import com.mojang.datafixers.util.Either;
|
| 13 | import java.util.Locale;
|
| 14 | import java.util.Map;
|
| 15 | import java.util.Optional;
|
| 16 | import java.util.concurrent.CompletableFuture;
|
| 17 | import java.util.function.Consumer;
|
| 18 | import java.util.function.Function;
|
| 19 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 20 | import net.minecraft.core.Holder;
|
| 21 | import net.minecraft.core.HolderLookup;
|
| 22 | import net.minecraft.core.HolderSet;
|
| 23 | import net.minecraft.core.registries.Registries;
|
| 24 | import net.minecraft.nbt.CompoundTag;
|
| 25 | import net.minecraft.nbt.TagParser;
|
| 26 | import net.minecraft.network.chat.Component;
|
| 27 | import net.minecraft.resources.Identifier;
|
| 28 | import net.minecraft.resources.ResourceKey;
|
| 29 | import net.minecraft.tags.TagKey;
|
| 30 | import net.minecraft.world.level.block.Block;
|
| 31 | import net.minecraft.world.level.block.state.BlockState;
|
| 32 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 33 | import net.minecraft.world.level.block.state.properties.Property;
|
| 34 | import org.jspecify.annotations.Nullable;
|
| 35 |
|
| 36 | public class BlockStateParser {
|
| 37 | public static final SimpleCommandExceptionType ERROR_NO_TAGS_ALLOWED = new SimpleCommandExceptionType(Component.translatable("argument.block.tag.disallowed"));
|
| 38 | public static final DynamicCommandExceptionType ERROR_UNKNOWN_BLOCK = new DynamicCommandExceptionType(
|
| 39 | id -> Component.translatableEscape("argument.block.id.invalid", id)
|
| 40 | );
|
| 41 | public static final Dynamic2CommandExceptionType ERROR_UNKNOWN_PROPERTY = new Dynamic2CommandExceptionType(
|
| 42 | (block, property) -> Component.translatableEscape("argument.block.property.unknown", block, property)
|
| 43 | );
|
| 44 | public static final Dynamic2CommandExceptionType ERROR_DUPLICATE_PROPERTY = new Dynamic2CommandExceptionType(
|
| 45 | (block, property) -> Component.translatableEscape("argument.block.property.duplicate", property, block)
|
| 46 | );
|
| 47 | public static final Dynamic3CommandExceptionType ERROR_INVALID_VALUE = new Dynamic3CommandExceptionType(
|
| 48 | (block, property, value) -> Component.translatableEscape("argument.block.property.invalid", block, value, property)
|
| 49 | );
|
| 50 | public static final Dynamic2CommandExceptionType ERROR_EXPECTED_VALUE = new Dynamic2CommandExceptionType(
|
| 51 | (block, property) -> Component.translatableEscape("argument.block.property.novalue", property, block)
|
| 52 | );
|
| 53 | public static final SimpleCommandExceptionType ERROR_EXPECTED_END_OF_PROPERTIES = new SimpleCommandExceptionType(
|
| 54 | Component.translatable("argument.block.property.unclosed")
|
| 55 | );
|
| 56 | public static final DynamicCommandExceptionType ERROR_UNKNOWN_TAG = new DynamicCommandExceptionType(
|
| 57 | tag -> Component.translatableEscape("arguments.block.tag.unknown", tag)
|
| 58 | );
|
| 59 | private static final char SYNTAX_START_PROPERTIES = '[';
|
| 60 | private static final char SYNTAX_START_NBT = '{';
|
| 61 | private static final char SYNTAX_END_PROPERTIES = ']';
|
| 62 | private static final char SYNTAX_EQUALS = '=';
|
| 63 | private static final char SYNTAX_PROPERTY_SEPARATOR = ',';
|
| 64 | private static final char SYNTAX_TAG = '#';
|
| 65 | private static final Function<SuggestionsBuilder, CompletableFuture<Suggestions>> SUGGEST_NOTHING = SuggestionsBuilder::buildFuture;
|
| 66 | private final HolderLookup<Block> blocks;
|
| 67 | private final StringReader reader;
|
| 68 | private final boolean forTesting;
|
| 69 | private final boolean allowNbt;
|
| 70 | private final Map<Property<?>, Comparable<?>> properties = Maps.newHashMap();
|
| 71 | private final Map<String, String> vagueProperties = Maps.newHashMap();
|
| 72 | private Identifier id = Identifier.withDefaultNamespace("");
|
| 73 | private @Nullable StateDefinition<Block, BlockState> definition;
|
| 74 | private @Nullable BlockState state;
|
| 75 | private @Nullable CompoundTag nbt;
|
| 76 | private @Nullable HolderSet<Block> tag;
|
| 77 | private Function<SuggestionsBuilder, CompletableFuture<Suggestions>> suggestions = SUGGEST_NOTHING;
|
| 78 |
|
| 79 | private BlockStateParser(final HolderLookup<Block> blocks, final StringReader reader, final boolean forTesting, final boolean allowNbt) {
|
| 80 | this.blocks = blocks;
|
| 81 | this.reader = reader;
|
| 82 | this.forTesting = forTesting;
|
| 83 | this.allowNbt = allowNbt;
|
| 84 | }
|
| 85 |
|
| 86 | public static BlockStateParser.BlockResult parseForBlock(final HolderLookup<Block> blocks, final String value, final boolean allowNbt) throws CommandSyntaxException {
|
| 87 | return parseForBlock(blocks, new StringReader(value), allowNbt);
|
| 88 | }
|
| 89 |
|
| 90 | public static BlockStateParser.BlockResult parseForBlock(final HolderLookup<Block> blocks, final StringReader reader, final boolean allowNbt) throws CommandSyntaxException {
|
| 91 | int cursor = reader.getCursor();
|
| 92 |
|
| 93 | try {
|
| 94 | BlockStateParser parser = new BlockStateParser(blocks, reader, false, allowNbt);
|
| 95 | parser.parse();
|
| 96 | return new BlockStateParser.BlockResult(parser.state, parser.properties, parser.nbt);
|
| 97 | } catch (CommandSyntaxException e) {
|
| 98 | reader.setCursor(cursor);
|
| 99 | throw e;
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | public static Either<BlockStateParser.BlockResult, BlockStateParser.TagResult> parseForTesting(
|
| 104 | final HolderLookup<Block> blocks, final String value, final boolean allowNbt
|
| 105 | ) throws CommandSyntaxException {
|
| 106 | return parseForTesting(blocks, new StringReader(value), allowNbt);
|
| 107 | }
|
| 108 |
|
| 109 | public static Either<BlockStateParser.BlockResult, BlockStateParser.TagResult> parseForTesting(
|
| 110 | final HolderLookup<Block> blocks, final StringReader reader, final boolean allowNbt
|
| 111 | ) throws CommandSyntaxException {
|
| 112 | int cursor = reader.getCursor();
|
| 113 |
|
| 114 | try {
|
| 115 | BlockStateParser parser = new BlockStateParser(blocks, reader, true, allowNbt);
|
| 116 | parser.parse();
|
| 117 | return parser.tag != null
|
| 118 | ? Either.right(new BlockStateParser.TagResult(parser.tag, parser.vagueProperties, parser.nbt))
|
| 119 | : Either.left(new BlockStateParser.BlockResult(parser.state, parser.properties, parser.nbt));
|
| 120 | } catch (CommandSyntaxException e) {
|
| 121 | reader.setCursor(cursor);
|
| 122 | throw e;
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | public static CompletableFuture<Suggestions> fillSuggestions(
|
| 127 | final HolderLookup<Block> blocks, final SuggestionsBuilder builder, final boolean forTesting, final boolean allowNbt
|
| 128 | ) {
|
| 129 | StringReader reader = new StringReader(builder.getInput());
|
| 130 | reader.setCursor(builder.getStart());
|
| 131 | BlockStateParser parser = new BlockStateParser(blocks, reader, forTesting, allowNbt);
|
| 132 |
|
| 133 | try {
|
| 134 | parser.parse();
|
| 135 | } catch (CommandSyntaxException var7) {
|
| 136 | }
|
| 137 |
|
| 138 | return parser.suggestions.apply(builder.createOffset(reader.getCursor()));
|
| 139 | }
|
| 140 |
|
| 141 | private void parse() throws CommandSyntaxException {
|
| 142 | if (this.forTesting) {
|
| 143 | this.suggestions = this::suggestBlockIdOrTag;
|
| 144 | } else {
|
| 145 | this.suggestions = this::suggestItem;
|
| 146 | }
|
| 147 |
|
| 148 | if (this.reader.canRead() && this.reader.peek() == '#') {
|
| 149 | this.readTag();
|
| 150 | this.suggestions = this::suggestOpenVaguePropertiesOrNbt;
|
| 151 | if (this.reader.canRead() && this.reader.peek() == '[') {
|
| 152 | this.readVagueProperties();
|
| 153 | this.suggestions = this::suggestOpenNbt;
|
| 154 | }
|
| 155 | } else {
|
| 156 | this.readBlock();
|
| 157 | this.suggestions = this::suggestOpenPropertiesOrNbt;
|
| 158 | if (this.reader.canRead() && this.reader.peek() == '[') {
|
| 159 | this.readProperties();
|
| 160 | this.suggestions = this::suggestOpenNbt;
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | if (this.allowNbt && this.reader.canRead() && this.reader.peek() == '{') {
|
| 165 | this.suggestions = SUGGEST_NOTHING;
|
| 166 | this.readNbt();
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | private CompletableFuture<Suggestions> suggestPropertyNameOrEnd(final SuggestionsBuilder builder) {
|
| 171 | if (builder.getRemaining().isEmpty()) {
|
| 172 | builder.suggest(String.valueOf(']'));
|
| 173 | }
|
| 174 |
|
| 175 | return this.suggestPropertyName(builder);
|
| 176 | }
|
| 177 |
|
| 178 | private CompletableFuture<Suggestions> suggestVaguePropertyNameOrEnd(final SuggestionsBuilder builder) {
|
| 179 | if (builder.getRemaining().isEmpty()) {
|
| 180 | builder.suggest(String.valueOf(']'));
|
| 181 | }
|
| 182 |
|
| 183 | return this.suggestVaguePropertyName(builder);
|
| 184 | }
|
| 185 |
|
| 186 | private CompletableFuture<Suggestions> suggestPropertyName(final SuggestionsBuilder builder) {
|
| 187 | String prefix = builder.getRemaining().toLowerCase(Locale.ROOT);
|
| 188 |
|
| 189 | for (Property<?> property : this.state.getProperties()) {
|
| 190 | if (!this.properties.containsKey(property) && property.getName().startsWith(prefix)) {
|
| 191 | builder.suggest(property.getName() + "=");
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | return builder.buildFuture();
|
| 196 | }
|
| 197 |
|
| 198 | private CompletableFuture<Suggestions> suggestVaguePropertyName(final SuggestionsBuilder builder) {
|
| 199 | String prefix = builder.getRemaining().toLowerCase(Locale.ROOT);
|
| 200 | if (this.tag != null) {
|
| 201 | for (Holder<Block> block : this.tag) {
|
| 202 | for (Property<?> property : block.value().getStateDefinition().getProperties()) {
|
| 203 | if (!this.vagueProperties.containsKey(property.getName()) && property.getName().startsWith(prefix)) {
|
| 204 | builder.suggest(property.getName() + "=");
|
| 205 | }
|
| 206 | }
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | return builder.buildFuture();
|
| 211 | }
|
| 212 |
|
| 213 | private CompletableFuture<Suggestions> suggestOpenNbt(final SuggestionsBuilder builder) {
|
| 214 | if (builder.getRemaining().isEmpty() && this.hasBlockEntity()) {
|
| 215 | builder.suggest(String.valueOf('{'));
|
| 216 | }
|
| 217 |
|
| 218 | return builder.buildFuture();
|
| 219 | }
|
| 220 |
|
| 221 | private boolean hasBlockEntity() {
|
| 222 | if (this.state != null) {
|
| 223 | return this.state.hasBlockEntity();
|
| 224 | }
|
| 225 |
|
| 226 | if (this.tag != null) {
|
| 227 | for (Holder<Block> block : this.tag) {
|
| 228 | if (block.value().defaultBlockState().hasBlockEntity()) {
|
| 229 | return true;
|
| 230 | }
|
| 231 | }
|
| 232 | }
|
| 233 |
|
| 234 | return false;
|
| 235 | }
|
| 236 |
|
| 237 | private CompletableFuture<Suggestions> suggestEquals(final SuggestionsBuilder builder) {
|
| 238 | if (builder.getRemaining().isEmpty()) {
|
| 239 | builder.suggest(String.valueOf('='));
|
| 240 | }
|
| 241 |
|
| 242 | return builder.buildFuture();
|
| 243 | }
|
| 244 |
|
| 245 | private CompletableFuture<Suggestions> suggestNextPropertyOrEnd(final SuggestionsBuilder builder) {
|
| 246 | if (builder.getRemaining().isEmpty()) {
|
| 247 | builder.suggest(String.valueOf(']'));
|
| 248 | }
|
| 249 |
|
| 250 | if (builder.getRemaining().isEmpty() && this.properties.size() < this.state.getProperties().size()) {
|
| 251 | builder.suggest(String.valueOf(','));
|
| 252 | }
|
| 253 |
|
| 254 | return builder.buildFuture();
|
| 255 | }
|
| 256 |
|
| 257 | private static <T extends Comparable<T>> SuggestionsBuilder addSuggestions(final SuggestionsBuilder builder, final Property<T> property) {
|
| 258 | for (T value : property.getPossibleValues()) {
|
| 259 | if (value instanceof Integer v) {
|
| 260 | builder.suggest(v);
|
| 261 | } else {
|
| 262 | builder.suggest(property.getName(value));
|
| 263 | }
|
| 264 | }
|
| 265 |
|
| 266 | return builder;
|
| 267 | }
|
| 268 |
|
| 269 | private CompletableFuture<Suggestions> suggestVaguePropertyValue(final SuggestionsBuilder builder, final String key) {
|
| 270 | boolean hasMoreProperties = false;
|
| 271 | if (this.tag != null) {
|
| 272 | for (Holder<Block> blockHolder : this.tag) {
|
| 273 | Block block = blockHolder.value();
|
| 274 | Property<?> property = block.getStateDefinition().getProperty(key);
|
| 275 | if (property != null) {
|
| 276 | addSuggestions(builder, property);
|
| 277 | }
|
| 278 |
|
| 279 | if (!hasMoreProperties) {
|
| 280 | for (Property<?> prop : block.getStateDefinition().getProperties()) {
|
| 281 | if (!this.vagueProperties.containsKey(prop.getName())) {
|
| 282 | hasMoreProperties = true;
|
| 283 | break;
|
| 284 | }
|
| 285 | }
|
| 286 | }
|
| 287 | }
|
| 288 | }
|
| 289 |
|
| 290 | if (hasMoreProperties) {
|
| 291 | builder.suggest(String.valueOf(','));
|
| 292 | }
|
| 293 |
|
| 294 | builder.suggest(String.valueOf(']'));
|
| 295 | return builder.buildFuture();
|
| 296 | }
|
| 297 |
|
| 298 | private CompletableFuture<Suggestions> suggestOpenVaguePropertiesOrNbt(final SuggestionsBuilder builder) {
|
| 299 | if (builder.getRemaining().isEmpty() && this.tag != null) {
|
| 300 | boolean hasProperties = false;
|
| 301 | boolean hasEntity = false;
|
| 302 |
|
| 303 | for (Holder<Block> blockHolder : this.tag) {
|
| 304 | Block block = blockHolder.value();
|
| 305 | hasProperties |= !block.getStateDefinition().getProperties().isEmpty();
|
| 306 | hasEntity |= block.defaultBlockState().hasBlockEntity();
|
| 307 | if (hasProperties && hasEntity) {
|
| 308 | break;
|
| 309 | }
|
| 310 | }
|
| 311 |
|
| 312 | if (hasProperties) {
|
| 313 | builder.suggest(String.valueOf('['));
|
| 314 | }
|
| 315 |
|
| 316 | if (hasEntity) {
|
| 317 | builder.suggest(String.valueOf('{'));
|
| 318 | }
|
| 319 | }
|
| 320 |
|
| 321 | return builder.buildFuture();
|
| 322 | }
|
| 323 |
|
| 324 | private CompletableFuture<Suggestions> suggestOpenPropertiesOrNbt(final SuggestionsBuilder builder) {
|
| 325 | if (builder.getRemaining().isEmpty()) {
|
| 326 | if (!this.definition.getProperties().isEmpty()) {
|
| 327 | builder.suggest(String.valueOf('['));
|
| 328 | }
|
| 329 |
|
| 330 | if (this.state.hasBlockEntity()) {
|
| 331 | builder.suggest(String.valueOf('{'));
|
| 332 | }
|
| 333 | }
|
| 334 |
|
| 335 | return builder.buildFuture();
|
| 336 | }
|
| 337 |
|
| 338 | private CompletableFuture<Suggestions> suggestTag(final SuggestionsBuilder builder) {
|
| 339 | return SharedSuggestionProvider.suggestResource(this.blocks.listTagIds().map(TagKey::location), builder, String.valueOf('#'));
|
| 340 | }
|
| 341 |
|
| 342 | private CompletableFuture<Suggestions> suggestItem(final SuggestionsBuilder builder) {
|
| 343 | return SharedSuggestionProvider.suggestResource(this.blocks.listElementIds().map(ResourceKey::identifier), builder);
|
| 344 | }
|
| 345 |
|
| 346 | private CompletableFuture<Suggestions> suggestBlockIdOrTag(final SuggestionsBuilder builder) {
|
| 347 | this.suggestTag(builder);
|
| 348 | this.suggestItem(builder);
|
| 349 | return builder.buildFuture();
|
| 350 | }
|
| 351 |
|
| 352 | private void readBlock() throws CommandSyntaxException {
|
| 353 | int start = this.reader.getCursor();
|
| 354 | this.id = Identifier.read(this.reader);
|
| 355 | Block block = this.blocks.get(ResourceKey.create(Registries.BLOCK, this.id)).orElseThrow(() -> {
|
| 356 | this.reader.setCursor(start);
|
| 357 | return ERROR_UNKNOWN_BLOCK.createWithContext(this.reader, this.id.toString());
|
| 358 | }).value();
|
| 359 | this.definition = block.getStateDefinition();
|
| 360 | this.state = block.defaultBlockState();
|
| 361 | }
|
| 362 |
|
| 363 | private void readTag() throws CommandSyntaxException {
|
| 364 | if (!this.forTesting) {
|
| 365 | throw ERROR_NO_TAGS_ALLOWED.createWithContext(this.reader);
|
| 366 | }
|
| 367 |
|
| 368 | int start = this.reader.getCursor();
|
| 369 | this.reader.expect('#');
|
| 370 | this.suggestions = this::suggestTag;
|
| 371 | Identifier id = Identifier.read(this.reader);
|
| 372 | this.tag = this.blocks.get(TagKey.create(Registries.BLOCK, id)).orElseThrow(() -> {
|
| 373 | this.reader.setCursor(start);
|
| 374 | return ERROR_UNKNOWN_TAG.createWithContext(this.reader, id.toString());
|
| 375 | });
|
| 376 | }
|
| 377 |
|
| 378 | private void readProperties() throws CommandSyntaxException {
|
| 379 | this.reader.skip();
|
| 380 | this.suggestions = this::suggestPropertyNameOrEnd;
|
| 381 | this.reader.skipWhitespace();
|
| 382 |
|
| 383 | while (this.reader.canRead() && this.reader.peek() != ']') {
|
| 384 | this.reader.skipWhitespace();
|
| 385 | int keyStart = this.reader.getCursor();
|
| 386 | String key = this.reader.readString();
|
| 387 | Property<?> property = this.definition.getProperty(key);
|
| 388 | if (property == null) {
|
| 389 | this.reader.setCursor(keyStart);
|
| 390 | throw ERROR_UNKNOWN_PROPERTY.createWithContext(this.reader, this.id.toString(), key);
|
| 391 | }
|
| 392 |
|
| 393 | if (this.properties.containsKey(property)) {
|
| 394 | this.reader.setCursor(keyStart);
|
| 395 | throw ERROR_DUPLICATE_PROPERTY.createWithContext(this.reader, this.id.toString(), key);
|
| 396 | }
|
| 397 |
|
| 398 | this.reader.skipWhitespace();
|
| 399 | this.suggestions = this::suggestEquals;
|
| 400 | if (!this.reader.canRead() || this.reader.peek() != '=') {
|
| 401 | throw ERROR_EXPECTED_VALUE.createWithContext(this.reader, this.id.toString(), key);
|
| 402 | }
|
| 403 |
|
| 404 | this.reader.skip();
|
| 405 | this.reader.skipWhitespace();
|
| 406 | this.suggestions = builder -> addSuggestions(builder, property).buildFuture();
|
| 407 | int start = this.reader.getCursor();
|
| 408 | this.setValue(property, this.reader.readString(), start);
|
| 409 | this.suggestions = this::suggestNextPropertyOrEnd;
|
| 410 | this.reader.skipWhitespace();
|
| 411 | if (this.reader.canRead()) {
|
| 412 | if (this.reader.peek() != ',') {
|
| 413 | if (this.reader.peek() != ']') {
|
| 414 | throw ERROR_EXPECTED_END_OF_PROPERTIES.createWithContext(this.reader);
|
| 415 | }
|
| 416 | break;
|
| 417 | }
|
| 418 |
|
| 419 | this.reader.skip();
|
| 420 | this.suggestions = this::suggestPropertyName;
|
| 421 | }
|
| 422 | }
|
| 423 |
|
| 424 | if (this.reader.canRead()) {
|
| 425 | this.reader.skip();
|
| 426 | } else {
|
| 427 | throw ERROR_EXPECTED_END_OF_PROPERTIES.createWithContext(this.reader);
|
| 428 | }
|
| 429 | }
|
| 430 |
|
| 431 | private void readVagueProperties() throws CommandSyntaxException {
|
| 432 | this.reader.skip();
|
| 433 | this.suggestions = this::suggestVaguePropertyNameOrEnd;
|
| 434 | int valueStart = -1;
|
| 435 | this.reader.skipWhitespace();
|
| 436 |
|
| 437 | while (this.reader.canRead() && this.reader.peek() != ']') {
|
| 438 | this.reader.skipWhitespace();
|
| 439 | int keyStart = this.reader.getCursor();
|
| 440 | String key = this.reader.readString();
|
| 441 | if (this.vagueProperties.containsKey(key)) {
|
| 442 | this.reader.setCursor(keyStart);
|
| 443 | throw ERROR_DUPLICATE_PROPERTY.createWithContext(this.reader, this.id.toString(), key);
|
| 444 | }
|
| 445 |
|
| 446 | this.reader.skipWhitespace();
|
| 447 | if (!this.reader.canRead() || this.reader.peek() != '=') {
|
| 448 | this.reader.setCursor(keyStart);
|
| 449 | throw ERROR_EXPECTED_VALUE.createWithContext(this.reader, this.id.toString(), key);
|
| 450 | }
|
| 451 |
|
| 452 | this.reader.skip();
|
| 453 | this.reader.skipWhitespace();
|
| 454 | this.suggestions = builder -> this.suggestVaguePropertyValue(builder, key);
|
| 455 | valueStart = this.reader.getCursor();
|
| 456 | String value = this.reader.readString();
|
| 457 | this.vagueProperties.put(key, value);
|
| 458 | this.reader.skipWhitespace();
|
| 459 | if (this.reader.canRead()) {
|
| 460 | valueStart = -1;
|
| 461 | if (this.reader.peek() != ',') {
|
| 462 | if (this.reader.peek() != ']') {
|
| 463 | throw ERROR_EXPECTED_END_OF_PROPERTIES.createWithContext(this.reader);
|
| 464 | }
|
| 465 | break;
|
| 466 | }
|
| 467 |
|
| 468 | this.reader.skip();
|
| 469 | this.suggestions = this::suggestVaguePropertyName;
|
| 470 | }
|
| 471 | }
|
| 472 |
|
| 473 | if (this.reader.canRead()) {
|
| 474 | this.reader.skip();
|
| 475 | } else {
|
| 476 | if (valueStart >= 0) {
|
| 477 | this.reader.setCursor(valueStart);
|
| 478 | }
|
| 479 |
|
| 480 | throw ERROR_EXPECTED_END_OF_PROPERTIES.createWithContext(this.reader);
|
| 481 | }
|
| 482 | }
|
| 483 |
|
| 484 | private void readNbt() throws CommandSyntaxException {
|
| 485 | this.nbt = TagParser.parseCompoundAsArgument(this.reader);
|
| 486 | }
|
| 487 |
|
| 488 | private <T extends Comparable<T>> void setValue(final Property<T> property, final String raw, final int start) throws CommandSyntaxException {
|
| 489 | Optional<T> value = property.getValue(raw);
|
| 490 | if (value.isPresent()) {
|
| 491 | this.state = this.state.setValue(property, value.get());
|
| 492 | this.properties.put(property, value.get());
|
| 493 | } else {
|
| 494 | this.reader.setCursor(start);
|
| 495 | throw ERROR_INVALID_VALUE.createWithContext(this.reader, this.id.toString(), property.getName(), raw);
|
| 496 | }
|
| 497 | }
|
| 498 |
|
| 499 | public static String serialize(final BlockState state) {
|
| 500 | final StringBuilder result = new StringBuilder(state.typeHolder().unwrapKey().map(r -> r.identifier().toString()).orElse("air"));
|
| 501 | if (!state.isSingletonState()) {
|
| 502 | result.append('[');
|
| 503 | state.getValues().forEach(new Consumer<Property.Value<?>>() {
|
| 504 | private boolean separate = false;
|
| 505 |
|
| 506 | public void accept(final Property.Value<?> value) {
|
| 507 | if (this.separate) {
|
| 508 | result.append(',');
|
| 509 | }
|
| 510 |
|
| 511 | result.append(value.property().getName());
|
| 512 | result.append('=');
|
| 513 | result.append(value.valueName());
|
| 514 | this.separate = true;
|
| 515 | }
|
| 516 | });
|
| 517 | result.append(']');
|
| 518 | }
|
| 519 |
|
| 520 | return result.toString();
|
| 521 | }
|
| 522 |
|
| 523 | public record BlockResult(BlockState blockState, Map<Property<?>, Comparable<?>> properties, @Nullable CompoundTag nbt) {
|
| 524 | }
|
| 525 |
|
| 526 | public record TagResult(HolderSet<Block> tag, Map<String, String> vagueProperties, @Nullable CompoundTag nbt) {
|
| 527 | }
|
| 528 | }
|
| 529 |
|