Search in sources :

Example 1 with EntityPredicate

use of net.minecraft.entity.EntityPredicate in project Vampirism by TeamLapen.

the class CrucifixItem method onUsingTick.

@Override
public void onUsingTick(ItemStack stack, LivingEntity player, int count) {
    for (LivingEntity nearbyEntity : player.level.getNearbyEntities(LivingEntity.class, new EntityPredicate().selector(this::affectsEntity), player, player.getBoundingBox().inflate(getRange(stack)))) {
        // Normalized horizontal (xz) vector giving the direction towards the holder of this crucifix
        Vector3d baseVector = player.position().subtract(nearbyEntity.position()).multiply(1, 0, 1).normalize();
        Vector3d oldDelta = nearbyEntity.getDeltaMovement();
        Vector3d horizontalDelta = oldDelta.multiply(1, 0, 1);
        double parallelScale = baseVector.dot(horizontalDelta);
        if (parallelScale > 0) {
            // Part of delta that is parallel to baseVector
            Vector3d parallelPart = baseVector.scale(parallelScale);
            double scale = determineSlowdown(determineEntityTier(nearbyEntity));
            // Substract parallel part from old Delta (scaled to still allow some movement)
            Vector3d newDelta = oldDelta.subtract(parallelPart.scale(scale));
            if (newDelta.lengthSqr() > oldDelta.lengthSqr()) {
                // Just to make sure we do not speed up the movement even though this should not be possible
                newDelta = Vector3d.ZERO;
            }
            // Unfortunately, Vanilla converts y-collision with ground into forward movement later on (in #move)
            // Therefore, we check for collision here and remove any y component if entity would collide with ground
            Vector3d collisionDelta = nearbyEntity.collide(newDelta);
            if (collisionDelta.y != newDelta.y && newDelta.y < 0) {
                newDelta = newDelta.multiply(1, 0, 1);
            }
            nearbyEntity.setDeltaMovement(newDelta);
        }
    }
}
Also used : LivingEntity(net.minecraft.entity.LivingEntity) Vector3d(net.minecraft.util.math.vector.Vector3d) EntityPredicate(net.minecraft.entity.EntityPredicate)

Aggregations

EntityPredicate (net.minecraft.entity.EntityPredicate)1 LivingEntity (net.minecraft.entity.LivingEntity)1 Vector3d (net.minecraft.util.math.vector.Vector3d)1