| 1 | package net.minecraft.advancements.predicates;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Arrays;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.Optional;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.HolderGetter;
|
| 11 | import net.minecraft.core.HolderSet;
|
| 12 | import net.minecraft.core.registries.Registries;
|
| 13 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 14 | import net.minecraft.nbt.CompoundTag;
|
| 15 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 16 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 17 | import net.minecraft.network.codec.StreamCodec;
|
| 18 | import net.minecraft.server.level.ServerLevel;
|
| 19 | import net.minecraft.tags.TagKey;
|
| 20 | import net.minecraft.world.level.LevelReader;
|
| 21 | import net.minecraft.world.level.block.Block;
|
| 22 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.block.state.pattern.BlockInWorld;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public record BlockPredicate(
|
| 28 | Optional<HolderSet<Block>> blocks, Optional<StatePropertiesPredicate> properties, Optional<NbtPredicate> nbt, DataComponentMatchers components
|
| 29 | ) {
|
| 30 | public static final MapCodec<BlockPredicate> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 31 | i -> i.group(
|
| 32 | RegistryCodecs.holderSet(Registries.BLOCK).optionalFieldOf("blocks").forGetter(BlockPredicate::blocks),
|
| 33 | StatePropertiesPredicate.CODEC.optionalFieldOf("state").forGetter(BlockPredicate::properties),
|
| 34 | NbtPredicate.CODEC.optionalFieldOf("nbt").forGetter(BlockPredicate::nbt),
|
| 35 | DataComponentMatchers.CODEC.forGetter(BlockPredicate::components)
|
| 36 | )
|
| 37 | .apply(i, BlockPredicate::new)
|
| 38 | );
|
| 39 | public static final Codec<BlockPredicate> CODEC = MAP_CODEC.codec();
|
| 40 | public static final StreamCodec<RegistryFriendlyByteBuf, BlockPredicate> STREAM_CODEC = StreamCodec.composite(
|
| 41 | ByteBufCodecs.optional(ByteBufCodecs.holderSet(Registries.BLOCK)),
|
| 42 | BlockPredicate::blocks,
|
| 43 | ByteBufCodecs.optional(StatePropertiesPredicate.STREAM_CODEC),
|
| 44 | BlockPredicate::properties,
|
| 45 | ByteBufCodecs.optional(NbtPredicate.STREAM_CODEC),
|
| 46 | BlockPredicate::nbt,
|
| 47 | DataComponentMatchers.STREAM_CODEC,
|
| 48 | BlockPredicate::components,
|
| 49 | BlockPredicate::new
|
| 50 | );
|
| 51 |
|
| 52 | public boolean matches(final ServerLevel level, final BlockPos pos) {
|
| 53 | if (!level.isLoaded(pos)) {
|
| 54 | return false;
|
| 55 | }
|
| 56 |
|
| 57 | if (!this.matchesState(level.getBlockState(pos))) {
|
| 58 | return false;
|
| 59 | }
|
| 60 |
|
| 61 | if (this.willMatchBlockEntity()) {
|
| 62 | BlockEntity blockEntity = level.getBlockEntity(pos);
|
| 63 | if (!this.matchesBlockEntity(level, blockEntity)) {
|
| 64 | return false;
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | return true;
|
| 69 | }
|
| 70 |
|
| 71 | public boolean willMatchBlockEntity() {
|
| 72 | return this.nbt.isPresent() || !this.components.isEmpty();
|
| 73 | }
|
| 74 |
|
| 75 | public boolean matches(final BlockInWorld blockInWorld) {
|
| 76 | return !this.matchesState(blockInWorld.getState())
|
| 77 | ? false
|
| 78 | : !this.nbt.isPresent() || matchesBlockEntityData(blockInWorld.getLevel(), blockInWorld.getEntity(), this.nbt.get());
|
| 79 | }
|
| 80 |
|
| 81 | public boolean matchesState(final BlockState state) {
|
| 82 | return this.blocks.isPresent() && !state.is(this.blocks.get()) ? false : !this.properties.isPresent() || this.properties.get().matches(state);
|
| 83 | }
|
| 84 |
|
| 85 | public boolean matchesBlockEntity(final LevelReader level, final @Nullable BlockEntity blockEntity) {
|
| 86 | return this.nbt.isPresent() && !matchesBlockEntityData(level, blockEntity, this.nbt.get())
|
| 87 | ? false
|
| 88 | : this.components.isEmpty() || matchesComponents(blockEntity, this.components);
|
| 89 | }
|
| 90 |
|
| 91 | private static boolean matchesBlockEntityData(final LevelReader level, final @Nullable BlockEntity entity, final NbtPredicate nbt) {
|
| 92 | return entity != null && nbt.matches(entity.saveWithFullMetadata(level.registryAccess()));
|
| 93 | }
|
| 94 |
|
| 95 | private static boolean matchesComponents(final @Nullable BlockEntity entity, final DataComponentMatchers components) {
|
| 96 | return entity != null && components.test(entity.collectComponents());
|
| 97 | }
|
| 98 |
|
| 99 | public boolean requiresNbt() {
|
| 100 | return this.nbt.isPresent();
|
| 101 | }
|
| 102 |
|
| 103 | public static class Builder {
|
| 104 | private Optional<HolderSet<Block>> blocks = Optional.empty();
|
| 105 | private Optional<StatePropertiesPredicate> properties = Optional.empty();
|
| 106 | private Optional<NbtPredicate> nbt = Optional.empty();
|
| 107 | private DataComponentMatchers components = DataComponentMatchers.ANY;
|
| 108 |
|
| 109 | private Builder() {
|
| 110 | }
|
| 111 |
|
| 112 | public static BlockPredicate.Builder block() {
|
| 113 | return new BlockPredicate.Builder();
|
| 114 | }
|
| 115 |
|
| 116 | public BlockPredicate.Builder of(final HolderGetter<Block> lookup, final Block... blocks) {
|
| 117 | return this.of(lookup, Arrays.asList(blocks));
|
| 118 | }
|
| 119 |
|
| 120 | public BlockPredicate.Builder of(final HolderGetter<Block> lookup, final Collection<Block> blocks) {
|
| 121 | this.blocks = Optional.of(HolderSet.direct(Block::builtInRegistryHolder, blocks));
|
| 122 | return this;
|
| 123 | }
|
| 124 |
|
| 125 | public BlockPredicate.Builder of(final HolderGetter<Block> lookup, final TagKey<Block> tag) {
|
| 126 | this.blocks = Optional.of(lookup.getOrThrow(tag));
|
| 127 | return this;
|
| 128 | }
|
| 129 |
|
| 130 | public BlockPredicate.Builder hasNbt(final CompoundTag nbt) {
|
| 131 | this.nbt = Optional.of(new NbtPredicate(nbt));
|
| 132 | return this;
|
| 133 | }
|
| 134 |
|
| 135 | public BlockPredicate.Builder setProperties(final StatePropertiesPredicate.Builder properties) {
|
| 136 | this.properties = properties.build();
|
| 137 | return this;
|
| 138 | }
|
| 139 |
|
| 140 | public BlockPredicate.Builder components(final DataComponentMatchers components) {
|
| 141 | this.components = components;
|
| 142 | return this;
|
| 143 | }
|
| 144 |
|
| 145 | public BlockPredicate build() {
|
| 146 | return new BlockPredicate(this.blocks, this.properties, this.nbt, this.components);
|
| 147 | }
|
| 148 | }
|
| 149 | }
|
| 150 |
|