| 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.stream.Collectors;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.world.entity.EntitySelector;
|
| 10 | import net.minecraft.world.entity.LivingEntity;
|
| 11 | import net.minecraft.world.entity.ai.Brain;
|
| 12 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 13 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 14 | import net.minecraft.world.entity.player.Player;
|
| 15 |
|
| 16 | public class PlayerSensor extends Sensor<LivingEntity> {
|
| 17 | @Override
|
| 18 | public Set<MemoryModuleType<?>> requires() {
|
| 19 | return ImmutableSet.of(
|
| 20 | MemoryModuleType.NEAREST_PLAYERS,
|
| 21 | MemoryModuleType.NEAREST_VISIBLE_PLAYER,
|
| 22 | MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER,
|
| 23 | MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYERS
|
| 24 | );
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | protected void doTick(final ServerLevel level, final LivingEntity body) {
|
| 29 | List<Player> players = level.players()
|
| 30 | .stream()
|
| 31 | .filter(EntitySelector.NO_SPECTATORS)
|
| 32 | .filter(player -> body.closerThan(player, this.getFollowDistance(body)))
|
| 33 | .sorted(Comparator.comparingDouble(body::distanceToSqr))
|
| 34 | .collect(Collectors.toList());
|
| 35 | Brain<?> brain = body.getBrain();
|
| 36 | brain.setMemory(MemoryModuleType.NEAREST_PLAYERS, players);
|
| 37 | List<Player> visiblePlayers = players.stream().filter(livingEntity -> isEntityTargetable(level, body, livingEntity)).collect(Collectors.toList());
|
| 38 | brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_PLAYER, visiblePlayers.isEmpty() ? null : visiblePlayers.get(0));
|
| 39 | List<Player> visibleAttackablePlayers = visiblePlayers.stream().filter(livingEntity -> isEntityAttackable(level, body, livingEntity)).toList();
|
| 40 | brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYERS, visibleAttackablePlayers);
|
| 41 | brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER, visibleAttackablePlayers.isEmpty() ? null : visibleAttackablePlayers.get(0));
|
| 42 | }
|
| 43 |
|
| 44 | protected double getFollowDistance(final LivingEntity body) {
|
| 45 | return body.getAttributeValue(Attributes.FOLLOW_RANGE);
|
| 46 | }
|
| 47 | }
|
| 48 |
|