| 1 | package net.minecraft.advancements.predicates.entity;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Iterables;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import java.util.Comparator;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.Objects;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.Map.Entry;
|
| 12 | import java.util.stream.Stream;
|
| 13 | import net.minecraft.advancements.predicates.DataComponentMatchers;
|
| 14 | import net.minecraft.advancements.predicates.DistancePredicate;
|
| 15 | import net.minecraft.advancements.predicates.LocationPredicate;
|
| 16 | import net.minecraft.advancements.predicates.MobEffectsPredicate;
|
| 17 | import net.minecraft.advancements.predicates.NbtPredicate;
|
| 18 | import net.minecraft.advancements.predicates.SlotsPredicate;
|
| 19 | import net.minecraft.core.Holder;
|
| 20 | import net.minecraft.core.HolderGetter;
|
| 21 | import net.minecraft.core.component.DataComponentExactPredicate;
|
| 22 | import net.minecraft.core.component.predicates.DataComponentPredicate;
|
| 23 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 24 | import net.minecraft.server.level.ServerLevel;
|
| 25 | import net.minecraft.server.level.ServerPlayer;
|
| 26 | import net.minecraft.tags.TagKey;
|
| 27 | import net.minecraft.world.entity.Entity;
|
| 28 | import net.minecraft.world.entity.EntityType;
|
| 29 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 30 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 31 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 32 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 33 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 34 | import net.minecraft.world.level.storage.loot.predicates.LootItemEntityPropertyCondition;
|
| 35 | import net.minecraft.world.phys.Vec3;
|
| 36 | import org.jspecify.annotations.Nullable;
|
| 37 |
|
| 38 | public final class EntityPredicate {
|
| 39 | private static final Codec<Map<Codec<? extends EntitySubPredicate>, EntitySubPredicate>> MAP_CODEC = Codec.dispatchedMap(
|
| 40 | BuiltInRegistries.ENTITY_SUB_PREDICATE_TYPE.byNameCodec(), c -> c
|
| 41 | );
|
| 42 | public static final Codec<EntityPredicate> CODEC = MAP_CODEC.xmap(EntityPredicate::new, p -> p.parts);
|
| 43 | private static final Comparator<Entry<Codec<? extends EntitySubPredicate>, EntitySubPredicate>> PREDICATE_TYPE_ORDER = Entry.comparingByKey(
|
| 44 | Comparator.comparing(
|
| 45 | codec -> {
|
| 46 | if (codec == EntityTypePredicate.CODEC) {
|
| 47 | return -1;
|
| 48 | } else if (codec == VehiclePredicate.CODEC
|
| 49 | || codec == PassengerPredicate.CODEC
|
| 50 | || codec == TargetedEntityPredicate.CODEC
|
| 51 | || codec == LightningBoltPredicate.CODEC
|
| 52 | || codec == PlayerPredicate.CODEC) {
|
| 53 | return 1;
|
| 54 | } else {
|
| 55 | return codec == EntityNbtPredicate.CODEC ? 2 : 0;
|
| 56 | }
|
| 57 | }
|
| 58 | )
|
| 59 | );
|
| 60 | private final Map<Codec<? extends EntitySubPredicate>, EntitySubPredicate> parts;
|
| 61 | private final EntitySubPredicate combinedPart;
|
| 62 |
|
| 63 | public EntityPredicate(final Map<Codec<? extends EntitySubPredicate>, EntitySubPredicate> parts) {
|
| 64 | this.parts = parts;
|
| 65 | this.combinedPart = combine(parts);
|
| 66 | }
|
| 67 |
|
| 68 | public static Holder<LootItemCondition> wrap(final EntityPredicate.Builder singlePredicate) {
|
| 69 | return wrap(singlePredicate.build());
|
| 70 | }
|
| 71 |
|
| 72 | public static Optional<Holder<LootItemCondition>> wrap(final Optional<EntityPredicate> singlePredicate) {
|
| 73 | return singlePredicate.map(EntityPredicate::wrap);
|
| 74 | }
|
| 75 |
|
| 76 | public static List<Holder<LootItemCondition>> wrap(final EntityPredicate.Builder... predicates) {
|
| 77 | return Stream.of(predicates).map(EntityPredicate::wrap).toList();
|
| 78 | }
|
| 79 |
|
| 80 | public static Holder<LootItemCondition> wrap(final EntityPredicate singlePredicate) {
|
| 81 | return Holder.direct(LootItemEntityPropertyCondition.hasProperties(LootContext.EntityTarget.THIS, singlePredicate).build());
|
| 82 | }
|
| 83 |
|
| 84 | public boolean matches(final ServerPlayer player, final @Nullable Entity entity) {
|
| 85 | return this.matches(player.level(), player.position(), entity);
|
| 86 | }
|
| 87 |
|
| 88 | public boolean matches(final ServerLevel level, final @Nullable Vec3 position, final @Nullable Entity entity) {
|
| 89 | return entity == null ? false : this.combinedPart.matches(entity, level, position);
|
| 90 | }
|
| 91 |
|
| 92 | public static LootContext createContext(final ServerPlayer player, final Entity entity) {
|
| 93 | LootParams lootParams = new LootParams.Builder(player.level())
|
| 94 | .withParameter(LootContextParams.THIS_ENTITY, entity)
|
| 95 | .withParameter(LootContextParams.ORIGIN, player.position())
|
| 96 | .create(LootContextParamSets.ADVANCEMENT_ENTITY);
|
| 97 | return new LootContext.Builder(lootParams).create(Optional.empty());
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public boolean equals(final Object o) {
|
| 102 | return o == this || o instanceof EntityPredicate that && Objects.equals(this.parts, that.parts);
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public int hashCode() {
|
| 107 | return this.parts.hashCode();
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public String toString() {
|
| 112 | return "EntityPredicate[parts=" + this.parts + "]";
|
| 113 | }
|
| 114 |
|
| 115 | private static EntitySubPredicate combine(final Map<Codec<? extends EntitySubPredicate>, EntitySubPredicate> predicateMap) {
|
| 116 | if (predicateMap.isEmpty()) {
|
| 117 | return EntitySubPredicate.ALWAYS_TRUE;
|
| 118 | }
|
| 119 |
|
| 120 | if (predicateMap.size() == 1) {
|
| 121 | return Iterables.getOnlyElement(predicateMap.values());
|
| 122 | }
|
| 123 |
|
| 124 | EntitySubPredicate[] predicates = predicateMap.entrySet().stream().sorted(PREDICATE_TYPE_ORDER).map(Entry::getValue).toArray(EntitySubPredicate[]::new);
|
| 125 | return predicates.length == 2 ? predicates[0].and(predicates[1]) : (entity, level, position) -> {
|
| 126 | for (EntitySubPredicate part : predicates) {
|
| 127 | if (!part.matches(entity, level, position)) {
|
| 128 | return false;
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | return true;
|
| 133 | };
|
| 134 | }
|
| 135 |
|
| 136 | public static class Builder {
|
| 137 | private final ImmutableMap.Builder<Codec<? extends EntitySubPredicate>, EntitySubPredicate> parts = ImmutableMap.builder();
|
| 138 |
|
| 139 | public static EntityPredicate.Builder entity() {
|
| 140 | return new EntityPredicate.Builder();
|
| 141 | }
|
| 142 |
|
| 143 | public <T extends EntitySubPredicate> EntityPredicate.Builder put(final Codec<T> key, final T predicate) {
|
| 144 | this.parts.put(key, predicate);
|
| 145 | return this;
|
| 146 | }
|
| 147 |
|
| 148 | public EntityPredicate.Builder of(final HolderGetter<EntityType<?>> lookup, final EntityType<?> entityType) {
|
| 149 | return this.entityType(EntityTypePredicate.of(lookup, entityType));
|
| 150 | }
|
| 151 |
|
| 152 | public EntityPredicate.Builder of(final HolderGetter<EntityType<?>> lookup, final TagKey<EntityType<?>> entityTypeTag) {
|
| 153 | return this.entityType(EntityTypePredicate.of(lookup, entityTypeTag));
|
| 154 | }
|
| 155 |
|
| 156 | public EntityPredicate.Builder entityType(final EntityTypePredicate entityType) {
|
| 157 | return this.put(EntityTypePredicate.CODEC, entityType);
|
| 158 | }
|
| 159 |
|
| 160 | public EntityPredicate.Builder distance(final DistancePredicate distanceToPlayer) {
|
| 161 | return this.put(DistanceToPlayerPredicate.CODEC, new DistanceToPlayerPredicate(distanceToPlayer));
|
| 162 | }
|
| 163 |
|
| 164 | public EntityPredicate.Builder moving(final MovementPredicate movement) {
|
| 165 | return this.put(MovementPredicate.CODEC, movement);
|
| 166 | }
|
| 167 |
|
| 168 | public EntityPredicate.Builder located(final LocationPredicate.Builder location) {
|
| 169 | return this.put(EntityLocationPredicate.CODEC, new EntityLocationPredicate(location.build()));
|
| 170 | }
|
| 171 |
|
| 172 | public EntityPredicate.Builder steppingOn(final LocationPredicate.Builder location) {
|
| 173 | return this.put(SteppingOnPredicate.CODEC, new SteppingOnPredicate(location.build()));
|
| 174 | }
|
| 175 |
|
| 176 | public EntityPredicate.Builder movementAffectedBy(final LocationPredicate.Builder location) {
|
| 177 | return this.put(MovementAffectedByPredicate.CODEC, new MovementAffectedByPredicate(location.build()));
|
| 178 | }
|
| 179 |
|
| 180 | public EntityPredicate.Builder effects(final MobEffectsPredicate.Builder effects) {
|
| 181 | return this.put(EntityEffectsPredicate.CODEC, new EntityEffectsPredicate(effects.build()));
|
| 182 | }
|
| 183 |
|
| 184 | public EntityPredicate.Builder nbt(final NbtPredicate nbt) {
|
| 185 | return this.put(EntityNbtPredicate.CODEC, new EntityNbtPredicate(nbt));
|
| 186 | }
|
| 187 |
|
| 188 | public EntityPredicate.Builder flags(final EntityFlagsPredicate.Builder flags) {
|
| 189 | return this.put(EntityFlagsPredicate.CODEC, flags.build());
|
| 190 | }
|
| 191 |
|
| 192 | public EntityPredicate.Builder equipment(final EntityEquipmentPredicate.Builder equipment) {
|
| 193 | return this.put(EntityEquipmentPredicate.CODEC, equipment.build());
|
| 194 | }
|
| 195 |
|
| 196 | public EntityPredicate.Builder equipment(final EntityEquipmentPredicate equipment) {
|
| 197 | return this.put(EntityEquipmentPredicate.CODEC, equipment);
|
| 198 | }
|
| 199 |
|
| 200 | public EntityPredicate.Builder periodicTick(final int period) {
|
| 201 | return this.put(PeriodicEntityTickPredicate.CODEC, new PeriodicEntityTickPredicate(period));
|
| 202 | }
|
| 203 |
|
| 204 | public EntityPredicate.Builder vehicle(final EntityPredicate.Builder vehicle) {
|
| 205 | return this.put(VehiclePredicate.CODEC, new VehiclePredicate(vehicle.build()));
|
| 206 | }
|
| 207 |
|
| 208 | public EntityPredicate.Builder passenger(final EntityPredicate.Builder passenger) {
|
| 209 | return this.put(PassengerPredicate.CODEC, new PassengerPredicate(passenger.build()));
|
| 210 | }
|
| 211 |
|
| 212 | public EntityPredicate.Builder targetedEntity(final EntityPredicate.Builder targetedEntity) {
|
| 213 | return this.put(TargetedEntityPredicate.CODEC, new TargetedEntityPredicate(targetedEntity.build()));
|
| 214 | }
|
| 215 |
|
| 216 | public EntityPredicate.Builder team(final String team) {
|
| 217 | return this.put(TeamPredicate.CODEC, new TeamPredicate(team));
|
| 218 | }
|
| 219 |
|
| 220 | public EntityPredicate.Builder slots(final SlotsPredicate slots) {
|
| 221 | return this.put(EntitySlotsPredicate.CODEC, new EntitySlotsPredicate(slots));
|
| 222 | }
|
| 223 |
|
| 224 | public EntityPredicate.Builder components(final DataComponentMatchers components) {
|
| 225 | if (!components.exact().isEmpty()) {
|
| 226 | this.components(components.exact());
|
| 227 | }
|
| 228 |
|
| 229 | if (!components.partial().isEmpty()) {
|
| 230 | this.components(components.partial());
|
| 231 | }
|
| 232 |
|
| 233 | return this;
|
| 234 | }
|
| 235 |
|
| 236 | public EntityPredicate.Builder components(final DataComponentExactPredicate components) {
|
| 237 | return this.put(EntityExactDataComponentsPredicate.CODEC, new EntityExactDataComponentsPredicate(components));
|
| 238 | }
|
| 239 |
|
| 240 | public EntityPredicate.Builder components(final Map<DataComponentPredicate.Type<?>, DataComponentPredicate> components) {
|
| 241 | return this.put(EntityPartialComponentsPredicate.CODEC, new EntityPartialComponentsPredicate(components));
|
| 242 | }
|
| 243 |
|
| 244 | public EntityPredicate.Builder lightingBolt(final LightningBoltPredicate lightningBolt) {
|
| 245 | return this.put(LightningBoltPredicate.CODEC, lightningBolt);
|
| 246 | }
|
| 247 |
|
| 248 | public EntityPredicate.Builder player(final PlayerPredicate player) {
|
| 249 | return this.put(PlayerPredicate.CODEC, player);
|
| 250 | }
|
| 251 |
|
| 252 | public EntityPredicate.Builder sheep(final SheepPredicate sheep) {
|
| 253 | return this.put(SheepPredicate.CODEC, sheep);
|
| 254 | }
|
| 255 |
|
| 256 | public EntityPredicate.Builder cubeMob(final CubeMobPredicate cubeMob) {
|
| 257 | return this.put(CubeMobPredicate.CODEC, cubeMob);
|
| 258 | }
|
| 259 |
|
| 260 | public EntityPredicate.Builder raider(final RaiderPredicate raider) {
|
| 261 | return this.put(RaiderPredicate.CODEC, raider);
|
| 262 | }
|
| 263 |
|
| 264 | public EntityPredicate.Builder fishingHook(final FishingHookPredicate fishingHook) {
|
| 265 | return this.put(FishingHookPredicate.CODEC, fishingHook);
|
| 266 | }
|
| 267 |
|
| 268 | public EntityPredicate build() {
|
| 269 | return new EntityPredicate(this.parts.buildOrThrow());
|
| 270 | }
|
| 271 | }
|
| 272 | }
|
| 273 |
|