diff --git a/src/main/java/net/torocraft/torohealth/bars/HealthBarRenderer.java b/src/main/java/net/torocraft/torohealth/bars/HealthBarRenderer.java index 801f494..8794128 100644 --- a/src/main/java/net/torocraft/torohealth/bars/HealthBarRenderer.java +++ b/src/main/java/net/torocraft/torohealth/bars/HealthBarRenderer.java @@ -44,7 +44,7 @@ public static void renderInWorld(MatrixStack matrix, LivingEntity entity, Camera MinecraftClient client = MinecraftClient.getInstance(); - if (client.player == entity) { + if (!EntityUtil.showHealthBar(entity, client)) { return; } diff --git a/src/main/java/net/torocraft/torohealth/util/EntityUtil.java b/src/main/java/net/torocraft/torohealth/util/EntityUtil.java index ddd6990..8d0d6de 100644 --- a/src/main/java/net/torocraft/torohealth/util/EntityUtil.java +++ b/src/main/java/net/torocraft/torohealth/util/EntityUtil.java @@ -1,7 +1,11 @@ package net.torocraft.torohealth.util; +import net.minecraft.client.MinecraftClient; import net.minecraft.entity.Entity; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.decoration.ArmorStandEntity; import net.minecraft.entity.mob.AmbientEntity; +import net.minecraft.entity.mob.CreeperEntity; import net.minecraft.entity.mob.GhastEntity; import net.minecraft.entity.mob.HostileEntity; import net.minecraft.entity.mob.SlimeEntity; @@ -10,6 +14,8 @@ import net.minecraft.entity.passive.PassiveEntity; import net.minecraft.entity.passive.SquidEntity; +import java.util.stream.StreamSupport; + public class EntityUtil { public enum Relation { @@ -37,4 +43,16 @@ public static Relation determineRelation(Entity entity) { return Relation.UNKNOWN; } } + + public static boolean showHealthBar(Entity entity, MinecraftClient client) { + return entity instanceof LivingEntity + && !(entity instanceof ArmorStandEntity) + && (!entity.isInvisibleTo(client.player) + || entity.isGlowing() + || entity.isOnFire() + || entity instanceof CreeperEntity && ((CreeperEntity) entity).shouldRenderOverlay() // charged creeper + || StreamSupport.stream(entity.getItemsEquipped().spliterator(), false).anyMatch(is -> !is.isEmpty())) + && entity != client.player + && !entity.isSpectator(); + } } diff --git a/src/main/java/net/torocraft/torohealth/util/RayTrace.java b/src/main/java/net/torocraft/torohealth/util/RayTrace.java index 140117c..c9524aa 100644 --- a/src/main/java/net/torocraft/torohealth/util/RayTrace.java +++ b/src/main/java/net/torocraft/torohealth/util/RayTrace.java @@ -1,6 +1,5 @@ package net.torocraft.torohealth.util; -import java.util.function.Predicate; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.client.MinecraftClient; @@ -23,8 +22,9 @@ import net.minecraft.world.RaycastContext; import net.minecraft.world.RaycastContext.FluidHandling; +import java.util.function.Predicate; + public class RayTrace implements BlockView { - private static Predicate isVisible = entity -> !entity.isSpectator() && entity.collides(); private static MinecraftClient minecraft = MinecraftClient.getInstance(); @Override @@ -55,6 +55,7 @@ public LivingEntity getEntityInCrosshair (float partialTicks, double reachDistan Vec3d max = position.add(look.x * reachDistance, look.y * reachDistance, look.z * reachDistance); Box searchBox = viewer.getBoundingBox().stretch(look.multiply(reachDistance)).expand(1.0D, 1.0D, 1.0D); + Predicate isVisible = entity -> entity.collides() && EntityUtil.showHealthBar(entity, client); EntityHitResult result = ProjectileUtil.raycast(viewer, position, max, searchBox, isVisible, reachDistance * reachDistance); if (result == null || result.getEntity() == null) {