| 1 | package net.minecraft.advancements.predicates;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.advancements.predicates.entity.EntityPredicate;
|
| 9 | import net.minecraft.core.registries.Registries;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.server.level.ServerPlayer;
|
| 12 | import net.minecraft.world.damagesource.DamageSource;
|
| 13 | import net.minecraft.world.damagesource.DamageType;
|
| 14 | import net.minecraft.world.phys.Vec3;
|
| 15 |
|
| 16 | public record DamageSourcePredicate(
|
| 17 | List<TagPredicate<DamageType>> tags, Optional<EntityPredicate> directEntity, Optional<EntityPredicate> sourceEntity, Optional<Boolean> isDirect
|
| 18 | ) {
|
| 19 | public static final Codec<DamageSourcePredicate> CODEC = RecordCodecBuilder.create(
|
| 20 | i -> i.group(
|
| 21 | TagPredicate.codec(Registries.DAMAGE_TYPE).listOf().optionalFieldOf("tags", List.of()).forGetter(DamageSourcePredicate::tags),
|
| 22 | EntityPredicate.CODEC.optionalFieldOf("direct_entity").forGetter(DamageSourcePredicate::directEntity),
|
| 23 | EntityPredicate.CODEC.optionalFieldOf("source_entity").forGetter(DamageSourcePredicate::sourceEntity),
|
| 24 | Codec.BOOL.optionalFieldOf("is_direct").forGetter(DamageSourcePredicate::isDirect)
|
| 25 | )
|
| 26 | .apply(i, DamageSourcePredicate::new)
|
| 27 | );
|
| 28 |
|
| 29 | public boolean matches(final ServerPlayer player, final DamageSource source) {
|
| 30 | return this.matches(player.level(), player.position(), source);
|
| 31 | }
|
| 32 |
|
| 33 | public boolean matches(final ServerLevel level, final Vec3 position, final DamageSource source) {
|
| 34 | for (TagPredicate<DamageType> tag : this.tags) {
|
| 35 | if (!tag.matches(source.typeHolder())) {
|
| 36 | return false;
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | if (this.directEntity.isPresent() && !this.directEntity.get().matches(level, position, source.getDirectEntity())) {
|
| 41 | return false;
|
| 42 | } else {
|
| 43 | return this.sourceEntity.isPresent() && !this.sourceEntity.get().matches(level, position, source.getEntity())
|
| 44 | ? false
|
| 45 | : !this.isDirect.isPresent() || this.isDirect.get() == source.isDirect();
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | public static class Builder {
|
| 50 | private final ImmutableList.Builder<TagPredicate<DamageType>> tags = ImmutableList.builder();
|
| 51 | private Optional<EntityPredicate> directEntity = Optional.empty();
|
| 52 | private Optional<EntityPredicate> sourceEntity = Optional.empty();
|
| 53 | private Optional<Boolean> isDirect = Optional.empty();
|
| 54 |
|
| 55 | public static DamageSourcePredicate.Builder damageType() {
|
| 56 | return new DamageSourcePredicate.Builder();
|
| 57 | }
|
| 58 |
|
| 59 | public DamageSourcePredicate.Builder tag(final TagPredicate<DamageType> tag) {
|
| 60 | this.tags.add(tag);
|
| 61 | return this;
|
| 62 | }
|
| 63 |
|
| 64 | public DamageSourcePredicate.Builder direct(final EntityPredicate.Builder directEntity) {
|
| 65 | this.directEntity = Optional.of(directEntity.build());
|
| 66 | return this;
|
| 67 | }
|
| 68 |
|
| 69 | public DamageSourcePredicate.Builder source(final EntityPredicate.Builder sourceEntity) {
|
| 70 | this.sourceEntity = Optional.of(sourceEntity.build());
|
| 71 | return this;
|
| 72 | }
|
| 73 |
|
| 74 | public DamageSourcePredicate.Builder isDirect(final boolean direct) {
|
| 75 | this.isDirect = Optional.of(direct);
|
| 76 | return this;
|
| 77 | }
|
| 78 |
|
| 79 | public DamageSourcePredicate build() {
|
| 80 | return new DamageSourcePredicate(this.tags.build(), this.directEntity, this.sourceEntity, this.isDirect);
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 |
|