| 1 | package net.minecraft.advancements.predicates;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.Predicate;
|
| 7 |
|
| 8 | public interface CollectionCountsPredicate<T, P extends Predicate<T>> extends Predicate<Iterable<? extends T>> {
|
| 9 | List<CollectionCountsPredicate.Entry<T, P>> unpack();
|
| 10 |
|
| 11 | static <T, P extends Predicate<T>> Codec<CollectionCountsPredicate<T, P>> codec(final Codec<P> elementCodec) {
|
| 12 | return CollectionCountsPredicate.Entry.<T, P>codec(elementCodec).listOf().xmap(CollectionCountsPredicate::of, CollectionCountsPredicate::unpack);
|
| 13 | }
|
| 14 |
|
| 15 | @SafeVarargs
|
| 16 | static <T, P extends Predicate<T>> CollectionCountsPredicate<T, P> of(final CollectionCountsPredicate.Entry<T, P>... predicates) {
|
| 17 | return of(List.of(predicates));
|
| 18 | }
|
| 19 |
|
| 20 | static <T, P extends Predicate<T>> CollectionCountsPredicate<T, P> of(final List<CollectionCountsPredicate.Entry<T, P>> predicates) {
|
| 21 | return switch (predicates.size()) {
|
| 22 | case 0 -> new CollectionCountsPredicate.Zero();
|
| 23 | case 1 -> new CollectionCountsPredicate.Single(predicates.getFirst());
|
| 24 | default -> new CollectionCountsPredicate.Multiple(predicates);
|
| 25 | };
|
| 26 | }
|
| 27 |
|
| 28 | record Entry<T, P extends Predicate<T>>(P test, MinMaxBounds.Ints count) {
|
| 29 | public static <T, P extends Predicate<T>> Codec<CollectionCountsPredicate.Entry<T, P>> codec(final Codec<P> elementCodec) {
|
| 30 | return RecordCodecBuilder.create(
|
| 31 | i -> i.group(
|
| 32 | elementCodec.fieldOf("test").forGetter(CollectionCountsPredicate.Entry::test),
|
| 33 | MinMaxBounds.Ints.CODEC.fieldOf("count").forGetter(CollectionCountsPredicate.Entry::count)
|
| 34 | )
|
| 35 | .apply(i, CollectionCountsPredicate.Entry::new)
|
| 36 | );
|
| 37 | }
|
| 38 |
|
| 39 | public boolean test(final Iterable<? extends T> values) {
|
| 40 | int count = 0;
|
| 41 |
|
| 42 | for (T value : values) {
|
| 43 | if (this.test.test(value)) {
|
| 44 | count++;
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | return this.count.matches(count);
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | record Multiple<T, P extends Predicate<T>>(List<CollectionCountsPredicate.Entry<T, P>> entries) implements CollectionCountsPredicate<T, P> {
|
| 53 | public boolean test(final Iterable<? extends T> values) {
|
| 54 | for (CollectionCountsPredicate.Entry<T, P> entry : this.entries) {
|
| 55 | if (!entry.test(values)) {
|
| 56 | return false;
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | return true;
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public List<CollectionCountsPredicate.Entry<T, P>> unpack() {
|
| 65 | return this.entries;
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | record Single<T, P extends Predicate<T>>(CollectionCountsPredicate.Entry<T, P> entry) implements CollectionCountsPredicate<T, P> {
|
| 70 | public boolean test(final Iterable<? extends T> values) {
|
| 71 | return this.entry.test(values);
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public List<CollectionCountsPredicate.Entry<T, P>> unpack() {
|
| 76 | return List.of(this.entry);
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | class Zero<T, P extends Predicate<T>> implements CollectionCountsPredicate<T, P> {
|
| 81 | public boolean test(final Iterable<? extends T> values) {
|
| 82 | return true;
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public List<CollectionCountsPredicate.Entry<T, P>> unpack() {
|
| 87 | return List.of();
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 |
|