| 1 | package net.minecraft.core.component;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Objects;
|
| 8 | import java.util.function.Predicate;
|
| 9 | import java.util.stream.Collectors;
|
| 10 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 11 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 12 | import net.minecraft.network.codec.StreamCodec;
|
| 13 |
|
| 14 | public final class DataComponentExactPredicate implements Predicate<DataComponentGetter> {
|
| 15 | public static final Codec<DataComponentExactPredicate> CODEC = DataComponentType.VALUE_MAP_CODEC
|
| 16 | .xmap(
|
| 17 | map -> new DataComponentExactPredicate(map.entrySet().stream().map(TypedDataComponent::fromEntryUnchecked).collect(Collectors.toList())),
|
| 18 | predicate -> predicate.expectedComponents
|
| 19 | .stream()
|
| 20 | .filter(e -> !e.type().isTransient())
|
| 21 | .collect(Collectors.toMap(TypedDataComponent::type, TypedDataComponent::value))
|
| 22 | );
|
| 23 | public static final StreamCodec<RegistryFriendlyByteBuf, DataComponentExactPredicate> STREAM_CODEC = TypedDataComponent.STREAM_CODEC
|
| 24 | .apply(ByteBufCodecs.list())
|
| 25 | .map(DataComponentExactPredicate::new, predicate -> predicate.expectedComponents);
|
| 26 | public static final DataComponentExactPredicate EMPTY = new DataComponentExactPredicate(List.of());
|
| 27 | private final List<TypedDataComponent<?>> expectedComponents;
|
| 28 |
|
| 29 | private DataComponentExactPredicate(final List<TypedDataComponent<?>> expectedComponents) {
|
| 30 | this.expectedComponents = expectedComponents;
|
| 31 | }
|
| 32 |
|
| 33 | public static DataComponentExactPredicate.Builder builder() {
|
| 34 | return new DataComponentExactPredicate.Builder();
|
| 35 | }
|
| 36 |
|
| 37 | public static <T> DataComponentExactPredicate expect(final DataComponentType<T> type, final T value) {
|
| 38 | return new DataComponentExactPredicate(List.of(new TypedDataComponent<>(type, value)));
|
| 39 | }
|
| 40 |
|
| 41 | public static DataComponentExactPredicate allOf(final DataComponentMap components) {
|
| 42 | return new DataComponentExactPredicate(ImmutableList.copyOf(components));
|
| 43 | }
|
| 44 |
|
| 45 | public static DataComponentExactPredicate someOf(final DataComponentMap components, final DataComponentType<?>... types) {
|
| 46 | DataComponentExactPredicate.Builder result = new DataComponentExactPredicate.Builder();
|
| 47 |
|
| 48 | for (DataComponentType<?> type : types) {
|
| 49 | TypedDataComponent<?> value = components.getTyped(type);
|
| 50 | if (value != null) {
|
| 51 | result.expect(value);
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | return result.build();
|
| 56 | }
|
| 57 |
|
| 58 | public boolean isEmpty() {
|
| 59 | return this.expectedComponents.isEmpty();
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public boolean equals(final Object obj) {
|
| 64 | return obj instanceof DataComponentExactPredicate predicate && this.expectedComponents.equals(predicate.expectedComponents);
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public int hashCode() {
|
| 69 | return this.expectedComponents.hashCode();
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public String toString() {
|
| 74 | return this.expectedComponents.toString();
|
| 75 | }
|
| 76 |
|
| 77 | public boolean test(final DataComponentGetter actualComponents) {
|
| 78 | for (TypedDataComponent<?> expected : this.expectedComponents) {
|
| 79 | Object actual = actualComponents.get(expected.type());
|
| 80 | if (!Objects.equals(expected.value(), actual)) {
|
| 81 | return false;
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | return true;
|
| 86 | }
|
| 87 |
|
| 88 | public boolean alwaysMatches() {
|
| 89 | return this.expectedComponents.isEmpty();
|
| 90 | }
|
| 91 |
|
| 92 | public DataComponentPatch asPatch() {
|
| 93 | return DataComponentPatch.builder().set(this.expectedComponents).build();
|
| 94 | }
|
| 95 |
|
| 96 | public static class Builder {
|
| 97 | private final List<TypedDataComponent<?>> expectedComponents = new ArrayList<>();
|
| 98 |
|
| 99 | private Builder() {
|
| 100 | }
|
| 101 |
|
| 102 | public <T> DataComponentExactPredicate.Builder expect(final TypedDataComponent<T> value) {
|
| 103 | return this.expect(value.type(), value.value());
|
| 104 | }
|
| 105 |
|
| 106 | public <T> DataComponentExactPredicate.Builder expect(final DataComponentType<? super T> type, final T value) {
|
| 107 | for (TypedDataComponent<?> component : this.expectedComponents) {
|
| 108 | if (component.type() == type) {
|
| 109 | throw new IllegalArgumentException("Predicate already has component of type: '" + type + "'");
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | this.expectedComponents.add(new TypedDataComponent<>(type, value));
|
| 114 | return this;
|
| 115 | }
|
| 116 |
|
| 117 | public DataComponentExactPredicate build() {
|
| 118 | return new DataComponentExactPredicate(List.copyOf(this.expectedComponents));
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|