| 1 | package net.minecraft.commands.arguments.blocks;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.arguments.ArgumentType;
|
| 5 | import com.mojang.brigadier.context.CommandContext;
|
| 6 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 7 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 8 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 9 | import java.util.Arrays;
|
| 10 | import java.util.Collection;
|
| 11 | import java.util.Map;
|
| 12 | import java.util.Set;
|
| 13 | import java.util.Map.Entry;
|
| 14 | import java.util.concurrent.CompletableFuture;
|
| 15 | import java.util.function.Predicate;
|
| 16 | import net.minecraft.commands.CommandBuildContext;
|
| 17 | import net.minecraft.commands.CommandSourceStack;
|
| 18 | import net.minecraft.core.HolderLookup;
|
| 19 | import net.minecraft.core.HolderSet;
|
| 20 | import net.minecraft.core.registries.Registries;
|
| 21 | import net.minecraft.nbt.CompoundTag;
|
| 22 | import net.minecraft.nbt.NbtUtils;
|
| 23 | import net.minecraft.world.level.block.Block;
|
| 24 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 25 | import net.minecraft.world.level.block.state.BlockState;
|
| 26 | import net.minecraft.world.level.block.state.pattern.BlockInWorld;
|
| 27 | import net.minecraft.world.level.block.state.properties.Property;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | public class BlockPredicateArgument implements ArgumentType<BlockPredicateArgument.Result> {
|
| 31 | private static final Collection<String> EXAMPLES = Arrays.asList("stone", "minecraft:stone", "stone[foo=bar]", "#stone", "#stone[foo=bar]{baz=nbt}");
|
| 32 | private final HolderLookup<Block> blocks;
|
| 33 |
|
| 34 | public BlockPredicateArgument(final CommandBuildContext context) {
|
| 35 | this.blocks = context.lookupOrThrow(Registries.BLOCK);
|
| 36 | }
|
| 37 |
|
| 38 | public static BlockPredicateArgument blockPredicate(final CommandBuildContext context) {
|
| 39 | return new BlockPredicateArgument(context);
|
| 40 | }
|
| 41 |
|
| 42 | public BlockPredicateArgument.Result parse(final StringReader reader) throws CommandSyntaxException {
|
| 43 | return parse(this.blocks, reader);
|
| 44 | }
|
| 45 |
|
| 46 | public static BlockPredicateArgument.Result parse(final HolderLookup<Block> blocks, final StringReader reader) throws CommandSyntaxException {
|
| 47 | return BlockStateParser.parseForTesting(blocks, reader, true)
|
| 48 | .map(
|
| 49 | block -> new BlockPredicateArgument.BlockPredicate(block.blockState(), block.properties().keySet(), block.nbt()),
|
| 50 | tag -> new BlockPredicateArgument.TagPredicate(tag.tag(), tag.vagueProperties(), tag.nbt())
|
| 51 | );
|
| 52 | }
|
| 53 |
|
| 54 | public static Predicate<BlockInWorld> getBlockPredicate(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 55 | return context.getArgument(name, BlockPredicateArgument.Result.class);
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 60 | return BlockStateParser.fillSuggestions(this.blocks, builder, true, true);
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public Collection<String> getExamples() {
|
| 65 | return EXAMPLES;
|
| 66 | }
|
| 67 |
|
| 68 | private static class BlockPredicate implements BlockPredicateArgument.Result {
|
| 69 | private final BlockState state;
|
| 70 | private final Set<Property<?>> properties;
|
| 71 | private final @Nullable CompoundTag nbt;
|
| 72 |
|
| 73 | public BlockPredicate(final BlockState state, final Set<Property<?>> properties, final @Nullable CompoundTag nbt) {
|
| 74 | this.state = state;
|
| 75 | this.properties = properties;
|
| 76 | this.nbt = nbt;
|
| 77 | }
|
| 78 |
|
| 79 | public boolean test(final BlockInWorld blockInWorld) {
|
| 80 | BlockState state = blockInWorld.getState();
|
| 81 | if (!state.is(this.state.getBlock())) {
|
| 82 | return false;
|
| 83 | }
|
| 84 |
|
| 85 | for (Property<?> property : this.properties) {
|
| 86 | if (state.getValue(property) != this.state.getValue(property)) {
|
| 87 | return false;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | if (this.nbt == null) {
|
| 92 | return true;
|
| 93 | }
|
| 94 |
|
| 95 | BlockEntity entity = blockInWorld.getEntity();
|
| 96 | return entity != null && NbtUtils.compareNbt(this.nbt, entity.saveWithFullMetadata(blockInWorld.getLevel().registryAccess()), true);
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public boolean requiresNbt() {
|
| 101 | return this.nbt != null;
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | public interface Result extends Predicate<BlockInWorld> {
|
| 106 | boolean requiresNbt();
|
| 107 | }
|
| 108 |
|
| 109 | private static class TagPredicate implements BlockPredicateArgument.Result {
|
| 110 | private final HolderSet<Block> tag;
|
| 111 | private final @Nullable CompoundTag nbt;
|
| 112 | private final Map<String, String> vagueProperties;
|
| 113 |
|
| 114 | private TagPredicate(final HolderSet<Block> tag, final Map<String, String> vagueProperties, final @Nullable CompoundTag nbt) {
|
| 115 | this.tag = tag;
|
| 116 | this.vagueProperties = vagueProperties;
|
| 117 | this.nbt = nbt;
|
| 118 | }
|
| 119 |
|
| 120 | public boolean test(final BlockInWorld blockInWorld) {
|
| 121 | BlockState state = blockInWorld.getState();
|
| 122 | if (!state.is(this.tag)) {
|
| 123 | return false;
|
| 124 | }
|
| 125 |
|
| 126 | for (Entry<String, String> entry : this.vagueProperties.entrySet()) {
|
| 127 | Property<?> property = state.getBlock().getStateDefinition().getProperty(entry.getKey());
|
| 128 | if (property == null) {
|
| 129 | return false;
|
| 130 | }
|
| 131 |
|
| 132 | Comparable<?> value = (Comparable<?>)property.getValue(entry.getValue()).orElse(null);
|
| 133 | if (value == null) {
|
| 134 | return false;
|
| 135 | }
|
| 136 |
|
| 137 | if (state.getValue(property) != value) {
|
| 138 | return false;
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | if (this.nbt == null) {
|
| 143 | return true;
|
| 144 | }
|
| 145 |
|
| 146 | BlockEntity entity = blockInWorld.getEntity();
|
| 147 | return entity != null && NbtUtils.compareNbt(this.nbt, entity.saveWithFullMetadata(blockInWorld.getLevel().registryAccess()), true);
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public boolean requiresNbt() {
|
| 152 | return this.nbt != null;
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 |
|