| 1 | package net.minecraft.advancements.predicates;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.function.Predicate;
|
| 8 | import net.minecraft.core.component.DataComponentExactPredicate;
|
| 9 | import net.minecraft.core.component.DataComponentGetter;
|
| 10 | import net.minecraft.core.component.DataComponentType;
|
| 11 | import net.minecraft.core.component.predicates.DataComponentPredicate;
|
| 12 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 13 | import net.minecraft.network.codec.StreamCodec;
|
| 14 |
|
| 15 | public record DataComponentMatchers(DataComponentExactPredicate exact, Map<DataComponentPredicate.Type<?>, DataComponentPredicate> partial)
|
| 16 | implements Predicate<DataComponentGetter> {
|
| 17 | public static final DataComponentMatchers ANY = new DataComponentMatchers(DataComponentExactPredicate.EMPTY, Map.of());
|
| 18 | public static final MapCodec<DataComponentMatchers> CODEC = RecordCodecBuilder.mapCodec(
|
| 19 | i -> i.group(
|
| 20 | DataComponentExactPredicate.CODEC.optionalFieldOf("components", DataComponentExactPredicate.EMPTY).forGetter(DataComponentMatchers::exact),
|
| 21 | DataComponentPredicate.CODEC.optionalFieldOf("predicates", Map.of()).forGetter(DataComponentMatchers::partial)
|
| 22 | )
|
| 23 | .apply(i, DataComponentMatchers::new)
|
| 24 | );
|
| 25 | public static final StreamCodec<RegistryFriendlyByteBuf, DataComponentMatchers> STREAM_CODEC = StreamCodec.composite(
|
| 26 | DataComponentExactPredicate.STREAM_CODEC,
|
| 27 | DataComponentMatchers::exact,
|
| 28 | DataComponentPredicate.STREAM_CODEC,
|
| 29 | DataComponentMatchers::partial,
|
| 30 | DataComponentMatchers::new
|
| 31 | );
|
| 32 |
|
| 33 | public boolean test(final DataComponentGetter values) {
|
| 34 | if (!this.exact.test(values)) {
|
| 35 | return false;
|
| 36 | }
|
| 37 |
|
| 38 | for (DataComponentPredicate predicate : this.partial.values()) {
|
| 39 | if (!predicate.matches(values)) {
|
| 40 | return false;
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | return true;
|
| 45 | }
|
| 46 |
|
| 47 | public boolean isEmpty() {
|
| 48 | return this.exact.isEmpty() && this.partial.isEmpty();
|
| 49 | }
|
| 50 |
|
| 51 | public static class Builder {
|
| 52 | private DataComponentExactPredicate exact = DataComponentExactPredicate.EMPTY;
|
| 53 | private final ImmutableMap.Builder<DataComponentPredicate.Type<?>, DataComponentPredicate> partial = ImmutableMap.builder();
|
| 54 |
|
| 55 | private Builder() {
|
| 56 | }
|
| 57 |
|
| 58 | public static DataComponentMatchers.Builder components() {
|
| 59 | return new DataComponentMatchers.Builder();
|
| 60 | }
|
| 61 |
|
| 62 | public <T extends DataComponentType<?>> DataComponentMatchers.Builder any(final DataComponentType<?> type) {
|
| 63 | DataComponentPredicate.AnyValueType predicateType = DataComponentPredicate.AnyValueType.create(type);
|
| 64 | this.partial.put(predicateType, predicateType.predicate());
|
| 65 | return this;
|
| 66 | }
|
| 67 |
|
| 68 | public <T extends DataComponentPredicate> DataComponentMatchers.Builder partial(final DataComponentPredicate.Type<T> type, final T predicate) {
|
| 69 | this.partial.put(type, predicate);
|
| 70 | return this;
|
| 71 | }
|
| 72 |
|
| 73 | public DataComponentMatchers.Builder exact(final DataComponentExactPredicate exact) {
|
| 74 | this.exact = exact;
|
| 75 | return this;
|
| 76 | }
|
| 77 |
|
| 78 | public DataComponentMatchers build() {
|
| 79 | return new DataComponentMatchers(this.exact, this.partial.buildOrThrow());
|
| 80 | }
|
| 81 | }
|
| 82 | }
|
| 83 |
|