Skip to content

Commit b97f237

Browse files
committed
Some model rendering modules or plug-ins may use the entity's creature name to select and replace the NPC model. The previous writing method will cause the player-type NPC to always use a random numeric ID as the entity name. Therefore, in order to solve this problem, I allow NPCs to have custom names.
1 parent a030ae3 commit b97f237

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_17PacketFactory.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public void spawnEntity(Player player, PacketEntity entity, PropertyHolder prope
3434
sendPacket(player, new WrapperPlayServerSpawnEntity(entity.getEntityId(), Optional.of(entity.getUuid()), entity.getType(),
3535
npcLocationToVector(location), location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.of(new Vector3d())));
3636

37+
// 设置除玩家以外的实体昵称 若display_name属性为空,则用默认实体名称
38+
// Sets the nickname of an entity other than the player. If the display_name attribute is empty, the default entity name is used.
3739
String displayName = entity.getProperty(propertyRegistry.getByName("display_name", String.class));
3840
if (displayName != null) {
3941
EntityData<Optional<Component>> nameData = new EntityData<>(
@@ -46,17 +48,7 @@ public void spawnEntity(Player player, PacketEntity entity, PropertyHolder prope
4648
Collections.singletonList(nameData)
4749
));
4850
}
49-
5051
sendAllMetadata(player, entity, properties);
51-
EntityData<Boolean> hideNameDisplay = new EntityData<>(
52-
3,
53-
EntityDataTypes.BOOLEAN,
54-
false
55-
);
56-
sendPacket(player, new WrapperPlayServerEntityMetadata(
57-
entity.getEntityId(),
58-
Collections.singletonList(hideNameDisplay)
59-
));
6052
if (EntityTypes.isTypeInstanceOf(entity.getType(), EntityTypes.LIVINGENTITY)) sendAllAttributes(player, entity, properties);
6153
createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedColor.class)));
6254
}

plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_19_3PacketFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public CompletableFuture<Void> addTabPlayer(Player player, PacketEntity entity,
3030

3131
CompletableFuture<Void> future = new CompletableFuture<>();
3232

33+
// 设置玩家类型NPC的昵称 若display_name属性为空,则用默认虚拟实体ID作为名称
34+
// Set the nickname of the player type NPC. If the display_name attribute is empty, the default virtual entity ID is used as the name.
3335
String displayName = entity.getProperty(propertyRegistry.getByName("display_name", String.class)) == null ? entity.getEntityId()+"" : entity.getProperty(propertyRegistry.getByName("display_name", String.class));
34-
3536
skinned(player, properties, new UserProfile(entity.getUuid(), displayName)).thenAccept(profile -> {
3637
WrapperPlayServerPlayerInfoUpdate.PlayerInfo info = new WrapperPlayServerPlayerInfoUpdate.PlayerInfo(
3738
profile, false, 1, GameMode.CREATIVE,

plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,14 @@ public void createTeam(Player player, PacketEntity entity, NamedColor namedColor
124124
namedColor == null ? NamedTextColor.WHITE : NamedTextColor.NAMES.value(namedColor.name().toLowerCase()),
125125
WrapperPlayServerTeams.OptionData.NONE
126126
)));
127+
128+
String displayName = entity.getProperty(propertyRegistry.getByName("display_name", String.class)) == null ? entity.getEntityId()+"" : entity.getProperty(propertyRegistry.getByName("display_name", String.class));
129+
// 这个displayName替换掉原来的变量,是为了
130+
// '当NPC实体为玩家类型时,隐藏掉NPC头顶上的名称显示 不然会和hologram一起显示 导致有些混淆与冲突'
131+
// This displayName replaces the original variable in order to
132+
// 'hide the name display above the NPC's head when the NPC entity is a player type, otherwise it will be displayed together with the hologram, causing some confusion and conflict'
127133
sendPacket(player, new WrapperPlayServerTeams("npc_team_" + entity.getEntityId(), WrapperPlayServerTeams.TeamMode.ADD_ENTITIES, (WrapperPlayServerTeams.ScoreBoardTeamInfo) null,
128-
entity.getType() == EntityTypes.PLAYER ? Integer.toString(entity.getEntityId()) : entity.getUuid().toString()));
134+
entity.getType() == EntityTypes.PLAYER ? displayName : entity.getUuid().toString()));
129135
}
130136

131137
@Override

0 commit comments

Comments
 (0)