Skip to content

Commit

Permalink
refactor: extract the discardNametagReason()
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurawald committed Jan 3, 2025
1 parent 754f9cf commit ee6f1b4
Showing 1 changed file with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.github.sakurawald.module.initializer.nametag.job.UpdateNametagJob;
import net.minecraft.entity.EntityPose;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.decoration.Brightness;
import net.minecraft.entity.decoration.DisplayEntity;
Expand Down Expand Up @@ -55,14 +56,9 @@ public void tick() {
}

/* discard nametag if the vehicle is sneaking */
if (this.getVehicle().isSneaking()) {
LogUtil.debug("discard nametag entity {}: its vehicle is sneaking", this);
this.discardNametag();
}

/* discard nametag if the vehicle is invisible */
if (this.getVehicle().isInvisible()) {
LogUtil.debug("discard nametag entity {}: its vehicle is in-visible", this);
String discardNametagReason = getNametagDiscardReason((LivingEntity) this.getVehicle());
if (discardNametagReason != null) {
LogUtil.debug("discard nametag entity {}: {}", this, discardNametagReason);
this.discardNametag();
}

Expand Down Expand Up @@ -159,14 +155,15 @@ private static void renderNametag(DisplayEntity.TextDisplayEntity nametag, Serve
}
}

private static boolean shouldCreateNametagForPlayer(ServerPlayerEntity player) {
if (player.isDead()) return false;
if (player.isSneaking()) return false;
private static String getNametagDiscardReason(LivingEntity entity) {
if (entity.isDead()) return "The entity is dead.";
if (entity.isSneaking()) return "The entity is sneaking.";

// when the player jumps into the ender portal in the end, its world is minecraft:overworld, its removal reason is `CHANGED_DIMENSION`
if (player.getRemovalReason() != null) return false;
if (entity.getRemovalReason() != null) return "The entity is removed.";
if (entity.isInvisible()) return "The entity is invisible.";

return true;
return null;
}

public static void processNametagsForOnlinePlayers() {
Expand All @@ -179,7 +176,7 @@ public static void processNametagsForOnlinePlayers() {
// update
ServerHelper.getPlayers().forEach(player -> {
// should we create the nametag for this player?
if (!shouldCreateNametagForPlayer(player)) return;
if (getNametagDiscardReason(player) != null) return;

// make if not exists
if (!player2nametag.containsKey(player)) {
Expand Down

0 comments on commit ee6f1b4

Please sign in to comment.