Skip to content

Commit

Permalink
Fix bug with target distance and clean up method (SkriptLang#6742)
Browse files Browse the repository at this point in the history
Co-authored-by: Moderocky <[email protected]>
  • Loading branch information
sovdeeth and Moderocky committed May 30, 2024
1 parent 4f878c3 commit 95a483c
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/main/java/ch/njol/skript/expressions/ExprTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,31 +197,31 @@ public static <T extends Entity> T getTarget(LivingEntity origin, @Nullable Enti
public static <T extends Entity> T getTarget(LivingEntity origin, @Nullable EntityData<T> type, double raysize) {
if (origin instanceof Mob)
return ((Mob) origin).getTarget() == null || type != null && !type.isInstance(((Mob) origin).getTarget()) ? null : (T) ((Mob) origin).getTarget();
Location location = origin.getLocation();
RayTraceResult result = null;
// TODO when DisplayData is added.
// if (type.getClass().equals(DisplayData.class))
// raysize = 1.0D;

Predicate<Entity> predicate = entity -> {
if (entity.equals(origin))
return false;
if (type != null && !type.isInstance(entity))
return false;
//noinspection RedundantIfStatement
if (entity instanceof Player && ((Player) entity).getGameMode() == GameMode.SPECTATOR)
return false;
return true;
};

Location eyes = origin.getEyeLocation();
Vector direction = origin.getLocation().getDirection();

double distance = targetBlockDistance;
if (!ignoreBlocks) {
RayTraceResult blockResult = origin.getWorld().rayTraceBlocks(origin.getEyeLocation(), location.getDirection(), targetBlockDistance);
RayTraceResult blockResult = origin.getWorld().rayTraceBlocks(eyes, direction, targetBlockDistance);
if (blockResult != null) {
Vector hit = blockResult.getHitPosition();
Location eyes = origin.getEyeLocation();
if (hit != null)
result = origin.getWorld().rayTraceEntities(eyes, location.getDirection(), eyes.toVector().distance(hit), raysize, predicate);
distance = eyes.toVector().distance(hit);
}
} else {
result = origin.getWorld().rayTraceEntities(origin.getEyeLocation(), location.getDirection(), targetBlockDistance, raysize, predicate);
}

RayTraceResult result = origin.getWorld().rayTraceEntities(eyes, direction, distance, raysize, predicate);
if (result == null)
return null;
Entity hitEntity = result.getHitEntity();
Expand Down

0 comments on commit 95a483c

Please sign in to comment.