| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import com.google.common.base.Predicates;
|
| 4 | import java.util.function.Predicate;
|
| 5 | import net.minecraft.world.Container;
|
| 6 | import net.minecraft.world.entity.player.Player;
|
| 7 | import net.minecraft.world.scores.Team;
|
| 8 |
|
| 9 | public final class EntitySelector {
|
| 10 | public static final Predicate<Entity> ENTITY_STILL_ALIVE = Entity::isAlive;
|
| 11 | public static final Predicate<Entity> LIVING_ENTITY_STILL_ALIVE = entity -> entity.isAlive() && entity instanceof LivingEntity;
|
| 12 | public static final Predicate<Entity> ENTITY_NOT_BEING_RIDDEN = entity -> entity.isAlive() && !entity.isVehicle() && !entity.isPassenger();
|
| 13 | public static final Predicate<Entity> CONTAINER_ENTITY_SELECTOR = entity -> entity instanceof Container && entity.isAlive();
|
| 14 | public static final Predicate<Entity> NO_CREATIVE_OR_SPECTATOR = entity -> !(entity instanceof Player player && (entity.isSpectator() || player.isCreative()));
|
| 15 | public static final Predicate<Entity> NO_SPECTATORS = entity -> !entity.isSpectator();
|
| 16 | public static final Predicate<Entity> CAN_BE_COLLIDED_WITH = NO_SPECTATORS.and(entity -> entity.canBeCollidedWith(null));
|
| 17 | public static final Predicate<Entity> CAN_BE_PICKED = Entity::isPickable;
|
| 18 |
|
| 19 | private EntitySelector() {
|
| 20 | }
|
| 21 |
|
| 22 | public static Predicate<Entity> withinDistance(final double centerX, final double centerY, final double centerZ, final double distance) {
|
| 23 | double distanceSqr = distance * distance;
|
| 24 | return input -> input.distanceToSqr(centerX, centerY, centerZ) <= distanceSqr;
|
| 25 | }
|
| 26 |
|
| 27 | public static Predicate<Entity> pushableBy(final Entity entity) {
|
| 28 | Team ownTeam = entity.getTeam();
|
| 29 | Team.CollisionRule ownCollisionRule = ownTeam == null ? Team.CollisionRule.ALWAYS : ownTeam.getCollisionRule();
|
| 30 | return ownCollisionRule == Team.CollisionRule.NEVER
|
| 31 | ? Predicates.alwaysFalse()
|
| 32 | : NO_SPECTATORS.and(
|
| 33 | input -> {
|
| 34 | if (!input.isPushable()) {
|
| 35 | return false;
|
| 36 | }
|
| 37 |
|
| 38 | if (!entity.level().isClientSide() || input instanceof Player player && player.isLocalPlayer()) {
|
| 39 | Team theirTeam = input.getTeam();
|
| 40 | Team.CollisionRule theirCollisionRule = theirTeam == null ? Team.CollisionRule.ALWAYS : theirTeam.getCollisionRule();
|
| 41 | if (theirCollisionRule == Team.CollisionRule.NEVER) {
|
| 42 | return false;
|
| 43 | }
|
| 44 |
|
| 45 | boolean sameTeam = ownTeam != null && ownTeam.isAlliedTo(theirTeam);
|
| 46 | return (ownCollisionRule == Team.CollisionRule.PUSH_OWN_TEAM || theirCollisionRule == Team.CollisionRule.PUSH_OWN_TEAM) && sameTeam
|
| 47 | ? false
|
| 48 | : ownCollisionRule != Team.CollisionRule.PUSH_OTHER_TEAMS && theirCollisionRule != Team.CollisionRule.PUSH_OTHER_TEAMS || sameTeam;
|
| 49 | } else {
|
| 50 | return false;
|
| 51 | }
|
| 52 | }
|
| 53 | );
|
| 54 | }
|
| 55 |
|
| 56 | public static Predicate<Entity> notRiding(final Entity entity) {
|
| 57 | return input -> {
|
| 58 | while (input.isPassenger()) {
|
| 59 | input = input.getVehicle();
|
| 60 | if (input == entity) {
|
| 61 | return false;
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | return true;
|
| 66 | };
|
| 67 | }
|
| 68 | }
|
| 69 |
|