Untitled

mail@pastecode.io avatar
unknown
java
a month ago
3.2 kB
1
Indexable
Never
    public static void handleExplosionKnockBack(EntityExplodeEvent event) {
        if (event.getEntity().getType() == EntityType.FIREBALL) {
            Fireball fireball = (Fireball) event.getEntity();
            Vector explosionOrigin = fireball.getLocation().toVector();

            Vector jump;

            for (Player player : fireball.getWorld().getPlayers()) {
                Vector playerLocation = player.getLocation().toVector();
                Vector direction = playerLocation.clone().subtract(explosionOrigin);

                Vector playerVelocity = player.getVelocity();

                double distance = direction.length();
                double impactFactor = 1 - (distance / fireball.getYield());

                if (impactFactor > 0) {
                    if (playerVelocity.getX() == 0 && playerVelocity.getZ() == 0) {
                        jump = direction.normalize().multiply(impactFactor * 1.6).setY(1.7);
                    } else {
                        jump = direction.normalize().multiply(impactFactor * 1.6).setY(0.85);
                    }
                    player.setVelocity(jump);
                }
            }
        } else if (event.getEntity() instanceof TNTPrimed) {
            for (Entity entity : event.getEntity().getNearbyEntities(5, 5, 5)) {
                if (entity instanceof Player) {
                    Player player = (Player) entity;
                    Location tntLocation = event.getLocation();
                    Location playerLocation = player.getLocation();

                    Vector direction = playerLocation.toVector().subtract(tntLocation.toVector()).normalize();
                    double distance = tntLocation.distance(playerLocation);

                    Vector jumpVector = getVector(distance, direction);

                    player.setVelocity(player.getVelocity().add(jumpVector));
                }
            }
        }
    }

    @NotNull
    private static Vector getVector(double distance, Vector direction) {
        int horizontalStrength;
        int verticalStrength = (int) (1.5 / (distance / 1.5));
        double constJump;

        if (distance < 1.0) {
            horizontalStrength = (int) (0.5 / (distance / 2.0));
            constJump = 1.7;
            verticalStrength = 0;
        } else if (distance > 2.7) {
            horizontalStrength = (int) (4.0 / (distance / 2.0));
            constJump = 0.5;
            if (verticalStrength > 10) {
                verticalStrength = (int) (1.5 / (distance * 10 / 1.5));
            } else {
                verticalStrength = (int) (1.5 / (distance / 1.5));
            }
        } else {
            constJump = 0.5;
            horizontalStrength = (int) (1.5 / (distance / 2.0));
            if (verticalStrength > 10) {
                verticalStrength = (int) (1.5 / (distance * 10 / 1.5));
            } else {
                verticalStrength = (int) (1.5 / (distance / 1.5));
            }
        }

        double x = direction.getX() * horizontalStrength;
        double y = (direction.getY() * verticalStrength) + constJump;
        double z = direction.getZ() * horizontalStrength;

        return new Vector(x, y, z);
    }
}
Leave a Comment