package net.minecraft.advancements.critereon; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import java.util.List; import java.util.function.Predicate; public interface CollectionCountsPredicate> extends Predicate> { List> unpack(); static > Codec> codec(Codec

testCodec) { return CollectionCountsPredicate.Entry.codec(testCodec).listOf().xmap(CollectionCountsPredicate::of, CollectionCountsPredicate::unpack); } @SafeVarargs static > CollectionCountsPredicate of(CollectionCountsPredicate.Entry... entries) { return of(List.of(entries)); } static > CollectionCountsPredicate of(List> entries) { return (CollectionCountsPredicate)(switch (entries.size()) { case 0 -> new CollectionCountsPredicate.Zero(); case 1 -> new CollectionCountsPredicate.Single((CollectionCountsPredicate.Entry)entries.getFirst()); default -> new CollectionCountsPredicate.Multiple(entries); }); } public record Entry>(P test, MinMaxBounds.Ints count) { public static > Codec> codec(Codec

testCodec) { return RecordCodecBuilder.create( instance -> instance.group( testCodec.fieldOf("test").forGetter(CollectionCountsPredicate.Entry::test), MinMaxBounds.Ints.CODEC.fieldOf("count").forGetter(CollectionCountsPredicate.Entry::count) ) .apply(instance, CollectionCountsPredicate.Entry::new) ); } public boolean test(Iterable collection) { int i = 0; for (T object : collection) { if (this.test.test(object)) { i++; } } return this.count.matches(i); } } public record Multiple>(List> entries) implements CollectionCountsPredicate { public boolean test(Iterable collection) { for (CollectionCountsPredicate.Entry entry : this.entries) { if (!entry.test(collection)) { return false; } } return true; } @Override public List> unpack() { return this.entries; } } public record Single>(CollectionCountsPredicate.Entry entry) implements CollectionCountsPredicate { public boolean test(Iterable collection) { return this.entry.test(collection); } @Override public List> unpack() { return List.of(this.entry); } } public static class Zero> implements CollectionCountsPredicate { public boolean test(Iterable collection) { return true; } @Override public List> unpack() { return List.of(); } } }