Unicopia/src/main/java/com/minelittlepony/unicopia/util/VecHelper.java

40 lines
1.5 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.util;
2018-09-12 01:29:49 +02:00
import java.util.List;
2020-01-16 12:35:46 +01:00
import java.util.function.Predicate;
import javax.annotation.Nullable;
2018-09-12 01:29:49 +02:00
import net.minecraft.entity.Entity;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.player.PlayerEntity;
2019-01-20 00:07:59 +01:00
import net.minecraft.util.math.BlockPos;
2020-01-16 12:35:46 +01:00
import net.minecraft.util.math.Box;
2018-09-12 01:29:49 +02:00
import net.minecraft.util.math.Vec3d;
2019-01-20 00:07:59 +01:00
import net.minecraft.world.World;
2018-09-12 01:29:49 +02:00
2020-04-15 19:06:45 +02:00
public interface VecHelper {
2019-01-20 00:07:59 +01:00
2020-10-04 12:58:01 +02:00
static Predicate<Entity> inRange(BlockPos center, double range) {
double rad = Math.pow(range, 2);
return e -> {
return e.squaredDistanceTo(center.getX(), center.getY(), center.getZ()) <= rad
|| e.squaredDistanceTo(center.getX(), center.getY() - e.getStandingEyeHeight(), center.getZ()) <= rad;
};
}
static List<Entity> findInRange(@Nullable Entity origin, World w, BlockPos pos, double radius, @Nullable Predicate<Entity> predicate) {
return w.getOtherEntities(origin, new Box(pos).expand(radius), predicate == null ? inRange(pos, radius) : inRange(pos, radius).and(predicate));
2019-01-20 00:07:59 +01:00
}
2018-09-12 01:29:49 +02:00
/**
* Gets all entities within a given range from the player.
*/
2020-10-04 12:58:01 +02:00
static List<Entity> findInReach(PlayerEntity player, double reach, @Nullable Predicate<? super Entity> predicate) {
2020-01-16 12:35:46 +01:00
Vec3d look = player.getCameraPosVec(1).multiply(reach);
2018-09-12 01:29:49 +02:00
return player.world.getOtherEntities(player, player
2020-01-16 12:35:46 +01:00
.getBoundingBox()
.expand(look.x, look.y, look.z)
.expand(1, 1, 1), predicate);
}
2018-09-12 01:29:49 +02:00
}