Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide health bars for armor stands and invisible entities that aren't glowing #116

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/torocraft/torohealth/util/EntityUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
}
}
5 changes: 3 additions & 2 deletions src/main/java/net/torocraft/torohealth/util/RayTrace.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Entity> isVisible = entity -> !entity.isSpectator() && entity.collides();
private static MinecraftClient minecraft = MinecraftClient.getInstance();

@Override
Expand Down Expand Up @@ -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<Entity> 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) {
Expand Down