| 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.Optional;
|
| 6 | import net.minecraft.advancements.predicates.DataComponentMatchers;
|
| 7 | import net.minecraft.advancements.predicates.ItemPredicate;
|
| 8 | import net.minecraft.core.HolderGetter;
|
| 9 | import net.minecraft.core.component.DataComponentExactPredicate;
|
| 10 | import net.minecraft.core.component.DataComponents;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.world.entity.Entity;
|
| 13 | import net.minecraft.world.entity.EquipmentSlot;
|
| 14 | import net.minecraft.world.entity.LivingEntity;
|
| 15 | import net.minecraft.world.entity.raid.Raid;
|
| 16 | import net.minecraft.world.item.Item;
|
| 17 | import net.minecraft.world.item.Items;
|
| 18 | import net.minecraft.world.level.block.entity.BannerPattern;
|
| 19 | import net.minecraft.world.phys.Vec3;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | public record EntityEquipmentPredicate(
|
| 23 | Optional<ItemPredicate> head,
|
| 24 | Optional<ItemPredicate> chest,
|
| 25 | Optional<ItemPredicate> legs,
|
| 26 | Optional<ItemPredicate> feet,
|
| 27 | Optional<ItemPredicate> body,
|
| 28 | Optional<ItemPredicate> mainhand,
|
| 29 | Optional<ItemPredicate> offhand
|
| 30 | ) implements EntitySubPredicate {
|
| 31 | public static final Codec<EntityEquipmentPredicate> CODEC = RecordCodecBuilder.create(
|
| 32 | i -> i.group(
|
| 33 | ItemPredicate.CODEC.optionalFieldOf("head").forGetter(EntityEquipmentPredicate::head),
|
| 34 | ItemPredicate.CODEC.optionalFieldOf("chest").forGetter(EntityEquipmentPredicate::chest),
|
| 35 | ItemPredicate.CODEC.optionalFieldOf("legs").forGetter(EntityEquipmentPredicate::legs),
|
| 36 | ItemPredicate.CODEC.optionalFieldOf("feet").forGetter(EntityEquipmentPredicate::feet),
|
| 37 | ItemPredicate.CODEC.optionalFieldOf("body").forGetter(EntityEquipmentPredicate::body),
|
| 38 | ItemPredicate.CODEC.optionalFieldOf("mainhand").forGetter(EntityEquipmentPredicate::mainhand),
|
| 39 | ItemPredicate.CODEC.optionalFieldOf("offhand").forGetter(EntityEquipmentPredicate::offhand)
|
| 40 | )
|
| 41 | .apply(i, EntityEquipmentPredicate::new)
|
| 42 | );
|
| 43 |
|
| 44 | public static EntityEquipmentPredicate captainPredicate(final HolderGetter<Item> items, final HolderGetter<BannerPattern> patternGetter) {
|
| 45 | return EntityEquipmentPredicate.Builder.equipment()
|
| 46 | .head(
|
| 47 | ItemPredicate.Builder.item()
|
| 48 | .of(items, Items.BANNER.white())
|
| 49 | .withComponents(
|
| 50 | DataComponentMatchers.Builder.components()
|
| 51 | .exact(
|
| 52 | DataComponentExactPredicate.someOf(
|
| 53 | Raid.getBannerComponentPatch(patternGetter).split().added(), DataComponents.BANNER_PATTERNS, DataComponents.ITEM_NAME
|
| 54 | )
|
| 55 | )
|
| 56 | .build()
|
| 57 | )
|
| 58 | )
|
| 59 | .build();
|
| 60 | }
|
| 61 |
|
| 62 | public boolean matches(final @Nullable Entity entity) {
|
| 63 | if (entity instanceof LivingEntity livingEntity) {
|
| 64 | if (this.head.isPresent() && !this.head.get().test(livingEntity.getItemBySlot(EquipmentSlot.HEAD))) {
|
| 65 | return false;
|
| 66 | } else if (this.chest.isPresent() && !this.chest.get().test(livingEntity.getItemBySlot(EquipmentSlot.CHEST))) {
|
| 67 | return false;
|
| 68 | } else if (this.legs.isPresent() && !this.legs.get().test(livingEntity.getItemBySlot(EquipmentSlot.LEGS))) {
|
| 69 | return false;
|
| 70 | } else if (this.feet.isPresent() && !this.feet.get().test(livingEntity.getItemBySlot(EquipmentSlot.FEET))) {
|
| 71 | return false;
|
| 72 | } else if (this.body.isPresent() && !this.body.get().test(livingEntity.getItemBySlot(EquipmentSlot.BODY))) {
|
| 73 | return false;
|
| 74 | } else {
|
| 75 | return this.mainhand.isPresent() && !this.mainhand.get().test(livingEntity.getItemBySlot(EquipmentSlot.MAINHAND))
|
| 76 | ? false
|
| 77 | : !this.offhand.isPresent() || this.offhand.get().test(livingEntity.getItemBySlot(EquipmentSlot.OFFHAND));
|
| 78 | }
|
| 79 | } else {
|
| 80 | return false;
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public boolean matches(final Entity entity, final ServerLevel level, final @Nullable Vec3 position) {
|
| 86 | return this.matches(entity);
|
| 87 | }
|
| 88 |
|
| 89 | public static class Builder {
|
| 90 | private Optional<ItemPredicate> head = Optional.empty();
|
| 91 | private Optional<ItemPredicate> chest = Optional.empty();
|
| 92 | private Optional<ItemPredicate> legs = Optional.empty();
|
| 93 | private Optional<ItemPredicate> feet = Optional.empty();
|
| 94 | private Optional<ItemPredicate> body = Optional.empty();
|
| 95 | private Optional<ItemPredicate> mainhand = Optional.empty();
|
| 96 | private Optional<ItemPredicate> offhand = Optional.empty();
|
| 97 |
|
| 98 | public static EntityEquipmentPredicate.Builder equipment() {
|
| 99 | return new EntityEquipmentPredicate.Builder();
|
| 100 | }
|
| 101 |
|
| 102 | public EntityEquipmentPredicate.Builder head(final ItemPredicate.Builder head) {
|
| 103 | this.head = Optional.of(head.build());
|
| 104 | return this;
|
| 105 | }
|
| 106 |
|
| 107 | public EntityEquipmentPredicate.Builder chest(final ItemPredicate.Builder chest) {
|
| 108 | this.chest = Optional.of(chest.build());
|
| 109 | return this;
|
| 110 | }
|
| 111 |
|
| 112 | public EntityEquipmentPredicate.Builder legs(final ItemPredicate.Builder legs) {
|
| 113 | this.legs = Optional.of(legs.build());
|
| 114 | return this;
|
| 115 | }
|
| 116 |
|
| 117 | public EntityEquipmentPredicate.Builder feet(final ItemPredicate.Builder feet) {
|
| 118 | this.feet = Optional.of(feet.build());
|
| 119 | return this;
|
| 120 | }
|
| 121 |
|
| 122 | public EntityEquipmentPredicate.Builder body(final ItemPredicate.Builder body) {
|
| 123 | this.body = Optional.of(body.build());
|
| 124 | return this;
|
| 125 | }
|
| 126 |
|
| 127 | public EntityEquipmentPredicate.Builder mainhand(final ItemPredicate.Builder mainhand) {
|
| 128 | this.mainhand = Optional.of(mainhand.build());
|
| 129 | return this;
|
| 130 | }
|
| 131 |
|
| 132 | public EntityEquipmentPredicate.Builder offhand(final ItemPredicate.Builder offhand) {
|
| 133 | this.offhand = Optional.of(offhand.build());
|
| 134 | return this;
|
| 135 | }
|
| 136 |
|
| 137 | public EntityEquipmentPredicate build() {
|
| 138 | return new EntityEquipmentPredicate(this.head, this.chest, this.legs, this.feet, this.body, this.mainhand, this.offhand);
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 |
|