| 1 | package net.minecraft.advancements.predicates.entity;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.Set;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.world.entity.Entity;
|
| 10 | import net.minecraft.world.phys.Vec3;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public record EntityTagPredicate(Optional<List<String>> anyOf, Optional<List<String>> allOf, Optional<List<String>> noneOf) implements EntitySubPredicate {
|
| 14 | public static final Codec<EntityTagPredicate> CODEC = RecordCodecBuilder.create(
|
| 15 | i -> i.group(
|
| 16 | Codec.STRING.listOf().optionalFieldOf("any_of").forGetter(EntityTagPredicate::anyOf),
|
| 17 | Codec.STRING.listOf().optionalFieldOf("all_of").forGetter(EntityTagPredicate::allOf),
|
| 18 | Codec.STRING.listOf().optionalFieldOf("none_of").forGetter(EntityTagPredicate::noneOf)
|
| 19 | )
|
| 20 | .apply(i, EntityTagPredicate::new)
|
| 21 | );
|
| 22 |
|
| 23 | public boolean matches(final Set<String> tags) {
|
| 24 | if (this.anyOf.isPresent() && !containsAtLeastOne(tags, this.anyOf.get())) {
|
| 25 | return false;
|
| 26 | } else {
|
| 27 | return this.noneOf.isPresent() && containsAtLeastOne(tags, this.noneOf.get()) ? false : !this.allOf.isPresent() || containsAllOf(tags, this.allOf.get());
|
| 28 | }
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public boolean matches(final Entity entity, final ServerLevel level, final @Nullable Vec3 position) {
|
| 33 | return this.matches(entity.entityTags());
|
| 34 | }
|
| 35 |
|
| 36 | private static boolean containsAtLeastOne(final Set<String> provided, final List<String> tags) {
|
| 37 | for (String tag : tags) {
|
| 38 | if (provided.contains(tag)) {
|
| 39 | return true;
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | return false;
|
| 44 | }
|
| 45 |
|
| 46 | private static boolean containsAllOf(final Set<String> provided, final List<String> tags) {
|
| 47 | for (String tag : tags) {
|
| 48 | if (!provided.contains(tag)) {
|
| 49 | return false;
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | return true;
|
| 54 | }
|
| 55 | }
|
| 56 |
|