| 1 | package net.minecraft.world.entity.ai.sensing;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableSet;
|
| 4 | import java.util.Comparator;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Set;
|
| 7 | import java.util.function.BiPredicate;
|
| 8 | import java.util.function.Predicate;
|
| 9 | import java.util.stream.Collectors;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.world.entity.EntitySelector;
|
| 12 | import net.minecraft.world.entity.PathfinderMob;
|
| 13 | import net.minecraft.world.entity.ai.Brain;
|
| 14 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 15 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 16 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 17 | import net.minecraft.world.entity.animal.Animal;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.item.ItemStack;
|
| 20 |
|
| 21 | public class TemptingSensor extends Sensor<PathfinderMob> {
|
| 22 | private static final TargetingConditions TEMPT_TARGETING = TargetingConditions.forNonCombat().ignoreLineOfSight();
|
| 23 | private final BiPredicate<PathfinderMob, ItemStack> temptations;
|
| 24 |
|
| 25 | public TemptingSensor(final Predicate<ItemStack> tt) {
|
| 26 | this((m, i) -> tt.test(i));
|
| 27 | }
|
| 28 |
|
| 29 | public static TemptingSensor forAnimal() {
|
| 30 | return new TemptingSensor((m, i) -> m instanceof Animal animal ? animal.isFood(i) : false);
|
| 31 | }
|
| 32 |
|
| 33 | private TemptingSensor(final BiPredicate<PathfinderMob, ItemStack> temptations) {
|
| 34 | this.temptations = temptations;
|
| 35 | }
|
| 36 |
|
| 37 | protected void doTick(final ServerLevel level, final PathfinderMob body) {
|
| 38 | Brain<?> brain = body.getBrain();
|
| 39 | TargetingConditions targeting = TEMPT_TARGETING.copy().range((float)body.getAttributeValue(Attributes.TEMPT_RANGE));
|
| 40 | List<Player> players = level.players()
|
| 41 | .stream()
|
| 42 | .filter(EntitySelector.NO_SPECTATORS)
|
| 43 | .filter(playerx -> targeting.test(level, body, playerx))
|
| 44 | .filter(p -> this.playerHoldingTemptation(body, p))
|
| 45 | .filter(playerx -> !body.hasPassenger(playerx))
|
| 46 | .sorted(Comparator.comparingDouble(body::distanceToSqr))
|
| 47 | .collect(Collectors.toList());
|
| 48 | if (!players.isEmpty()) {
|
| 49 | Player player = players.get(0);
|
| 50 | brain.setMemory(MemoryModuleType.TEMPTING_PLAYER, player);
|
| 51 | } else {
|
| 52 | brain.eraseMemory(MemoryModuleType.TEMPTING_PLAYER);
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | private boolean playerHoldingTemptation(final PathfinderMob mob, final Player player) {
|
| 57 | return this.isTemptation(mob, player.getMainHandItem()) || this.isTemptation(mob, player.getOffhandItem());
|
| 58 | }
|
| 59 |
|
| 60 | private boolean isTemptation(final PathfinderMob mob, final ItemStack itemStack) {
|
| 61 | return this.temptations.test(mob, itemStack);
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public Set<MemoryModuleType<?>> requires() {
|
| 66 | return ImmutableSet.of(MemoryModuleType.TEMPTING_PLAYER);
|
| 67 | }
|
| 68 | }
|
| 69 |
|