| 1 | package net.minecraft.advancements.triggers;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.Iterator;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.Set;
|
| 12 | import net.minecraft.advancements.predicates.ItemPredicate;
|
| 13 | import net.minecraft.advancements.predicates.MinMaxBounds;
|
| 14 | import net.minecraft.advancements.predicates.entity.EntityPredicate;
|
| 15 | import net.minecraft.core.Holder;
|
| 16 | import net.minecraft.core.HolderGetter;
|
| 17 | import net.minecraft.server.level.ServerPlayer;
|
| 18 | import net.minecraft.world.entity.Entity;
|
| 19 | import net.minecraft.world.entity.EntityType;
|
| 20 | import net.minecraft.world.item.Item;
|
| 21 | import net.minecraft.world.item.ItemStack;
|
| 22 | import net.minecraft.world.item.Items;
|
| 23 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 24 | import net.minecraft.world.level.storage.loot.Validatable;
|
| 25 | import net.minecraft.world.level.storage.loot.ValidationContextSource;
|
| 26 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 |
|
| 29 | public class KilledByArrowTrigger extends SimpleCriterionTrigger<KilledByArrowTrigger.TriggerInstance> {
|
| 30 | @Override
|
| 31 | public Codec<KilledByArrowTrigger.TriggerInstance> codec() {
|
| 32 | return KilledByArrowTrigger.TriggerInstance.CODEC;
|
| 33 | }
|
| 34 |
|
| 35 | public void trigger(final ServerPlayer player, final Collection<Entity> victims, final @Nullable ItemStack firedByWeapon) {
|
| 36 | List<LootContext> victimContexts = Lists.newArrayList();
|
| 37 | Set<EntityType<?>> entityTypes = Sets.newHashSet();
|
| 38 |
|
| 39 | for (Entity victim : victims) {
|
| 40 | entityTypes.add(victim.getType());
|
| 41 | victimContexts.add(EntityPredicate.createContext(player, victim));
|
| 42 | }
|
| 43 |
|
| 44 | this.trigger(player, t -> t.matches(victimContexts, entityTypes.size(), firedByWeapon));
|
| 45 | }
|
| 46 |
|
| 47 | public record TriggerInstance(
|
| 48 | Optional<Holder<LootItemCondition>> player,
|
| 49 | List<Holder<LootItemCondition>> victims,
|
| 50 | MinMaxBounds.Ints uniqueEntityTypes,
|
| 51 | Optional<ItemPredicate> firedFromWeapon
|
| 52 | ) implements SimpleCriterionTrigger.SimpleInstance {
|
| 53 | public static final Codec<KilledByArrowTrigger.TriggerInstance> CODEC = RecordCodecBuilder.create(
|
| 54 | i -> i.group(
|
| 55 | LootItemCondition.CODEC.optionalFieldOf("player").forGetter(KilledByArrowTrigger.TriggerInstance::player),
|
| 56 | LootItemCondition.CODEC.listOf().optionalFieldOf("victims", List.of()).forGetter(KilledByArrowTrigger.TriggerInstance::victims),
|
| 57 | MinMaxBounds.Ints.CODEC.optionalFieldOf("unique_entity_types", MinMaxBounds.Ints.ANY).forGetter(KilledByArrowTrigger.TriggerInstance::uniqueEntityTypes),
|
| 58 | ItemPredicate.CODEC.optionalFieldOf("fired_from_weapon").forGetter(KilledByArrowTrigger.TriggerInstance::firedFromWeapon)
|
| 59 | )
|
| 60 | .apply(i, KilledByArrowTrigger.TriggerInstance::new)
|
| 61 | );
|
| 62 |
|
| 63 | public static Criterion<KilledByArrowTrigger.TriggerInstance> crossbowKilled(final HolderGetter<Item> items, final EntityPredicate.Builder... victims) {
|
| 64 | return CriteriaTriggers.KILLED_BY_ARROW
|
| 65 | .createCriterion(
|
| 66 | new KilledByArrowTrigger.TriggerInstance(
|
| 67 | Optional.empty(), EntityPredicate.wrap(victims), MinMaxBounds.Ints.ANY, Optional.of(ItemPredicate.Builder.item().of(items, Items.CROSSBOW).build())
|
| 68 | )
|
| 69 | );
|
| 70 | }
|
| 71 |
|
| 72 | public static Criterion<KilledByArrowTrigger.TriggerInstance> crossbowKilled(final HolderGetter<Item> items, final MinMaxBounds.Ints uniqueEntityTypes) {
|
| 73 | return CriteriaTriggers.KILLED_BY_ARROW
|
| 74 | .createCriterion(
|
| 75 | new KilledByArrowTrigger.TriggerInstance(
|
| 76 | Optional.empty(), List.of(), uniqueEntityTypes, Optional.of(ItemPredicate.Builder.item().of(items, Items.CROSSBOW).build())
|
| 77 | )
|
| 78 | );
|
| 79 | }
|
| 80 |
|
| 81 | public boolean matches(final Collection<LootContext> victims, final int uniqueEntityTypes, final @Nullable ItemStack firedFromWeapon) {
|
| 82 | if (!this.firedFromWeapon.isPresent() || firedFromWeapon != null && this.firedFromWeapon.get().test(firedFromWeapon)) {
|
| 83 | if (!this.victims.isEmpty()) {
|
| 84 | List<LootContext> victimsCopy = Lists.newArrayList(victims);
|
| 85 |
|
| 86 | for (Holder<LootItemCondition> predicate : this.victims) {
|
| 87 | boolean found = false;
|
| 88 | Iterator<LootContext> iterator = victimsCopy.iterator();
|
| 89 |
|
| 90 | while (iterator.hasNext()) {
|
| 91 | LootContext entity = iterator.next();
|
| 92 | if (predicate.value().test(entity)) {
|
| 93 | iterator.remove();
|
| 94 | found = true;
|
| 95 | break;
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | if (!found) {
|
| 100 | return false;
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | return this.uniqueEntityTypes.matches(uniqueEntityTypes);
|
| 106 | } else {
|
| 107 | return false;
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public void validate(final ValidationContextSource validator) {
|
| 113 | SimpleCriterionTrigger.SimpleInstance.super.validate(validator);
|
| 114 | Validatable.validateHolder(validator.entityContext(), "victims", this.victims);
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 |
|