Skip to content

Commit

Permalink
fix: hide the nametag if the player is in-visible. (nametag module)
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurawald committed Jan 3, 2025
1 parent bfea7a4 commit 754f9cf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public void tick() {
this.discardNametag();
}

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

}
};

Expand Down Expand Up @@ -153,6 +159,16 @@ private static void renderNametag(DisplayEntity.TextDisplayEntity nametag, Serve
}
}

private static boolean shouldCreateNametagForPlayer(ServerPlayerEntity player) {
if (player.isDead()) return false;
if (player.isSneaking()) return false;

// 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;

return true;
}

public static void processNametagsForOnlinePlayers() {
// since the virtual entity is not added into the server, so we should call tick() ourselves.
player2nametag.values().forEach(DisplayEntity::tick);
Expand All @@ -162,10 +178,8 @@ public static void processNametagsForOnlinePlayers() {

// update
ServerHelper.getPlayers().forEach(player -> {
if (player.isDead()) return;
if (player.isSneaking()) return;
// 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;
// should we create the nametag for this player?
if (!shouldCreateNametagForPlayer(player)) return;

// make if not exists
if (!player2nametag.containsKey(player)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public class TesterInitializer extends ModuleInitializer {
return 1;
}

@CommandNode("visible")
private static int testInvisible(@CommandSource ServerPlayerEntity player) {

player.sendMessage(Text.of(String.valueOf(player.isInvisible())));

return 1;
}

private static void testTextReplacement(ServerPlayerEntity player) {
/* make */
MutableText root = Text.empty();
Expand Down

0 comments on commit 754f9cf

Please sign in to comment.