Skip to content

Commit a030ae3

Browse files
committed
"Hi there! I've added a 'Custom Entity Name' feature to replace the previous 'Random Number ID or Default Entity Name'.
I thought this might be useful for users who want more control over naming their entities, and I hope it adds value to the project. If you don't mind, could you take a look at the changes? I'd love to contribute to making ZNPCsPlus even better! ❤ Thanks for your hard work on this project!"
1 parent f0a03ec commit a030ae3

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package lol.pyr.znpcsplus.packets;
22

33
import com.github.retrooper.packetevents.PacketEventsAPI;
4+
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
5+
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
46
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
57
import com.github.retrooper.packetevents.util.Vector3d;
8+
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityMetadata;
69
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnEntity;
710
import lol.pyr.znpcsplus.api.entity.PropertyHolder;
811
import lol.pyr.znpcsplus.config.ConfigManager;
@@ -11,12 +14,15 @@
1114
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
1215
import lol.pyr.znpcsplus.util.NamedColor;
1316
import lol.pyr.znpcsplus.util.NpcLocation;
17+
import net.kyori.adventure.text.Component;
1418
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
1519
import org.bukkit.entity.Player;
1620
import org.bukkit.plugin.Plugin;
1721

22+
import java.util.Collections;
1823
import java.util.Optional;
1924

25+
2026
public class V1_17PacketFactory extends V1_8PacketFactory {
2127
public V1_17PacketFactory(TaskScheduler scheduler, PacketEventsAPI<Plugin> packetEvents, EntityPropertyRegistryImpl propertyRegistry, LegacyComponentSerializer textSerializer, ConfigManager configManager) {
2228
super(scheduler, packetEvents, propertyRegistry, textSerializer, configManager);
@@ -27,7 +33,30 @@ public void spawnEntity(Player player, PacketEntity entity, PropertyHolder prope
2733
NpcLocation location = entity.getLocation();
2834
sendPacket(player, new WrapperPlayServerSpawnEntity(entity.getEntityId(), Optional.of(entity.getUuid()), entity.getType(),
2935
npcLocationToVector(location), location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.of(new Vector3d())));
36+
37+
String displayName = entity.getProperty(propertyRegistry.getByName("display_name", String.class));
38+
if (displayName != null) {
39+
EntityData<Optional<Component>> nameData = new EntityData<>(
40+
2,
41+
EntityDataTypes.OPTIONAL_ADV_COMPONENT,
42+
Optional.of(Component.text(displayName))
43+
);
44+
sendPacket(player, new WrapperPlayServerEntityMetadata(
45+
entity.getEntityId(),
46+
Collections.singletonList(nameData)
47+
));
48+
}
49+
3050
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+
));
3160
if (EntityTypes.isTypeInstanceOf(entity.getType(), EntityTypes.LIVINGENTITY)) sendAllAttributes(player, entity, properties);
3261
createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedColor.class)));
3362
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ public V1_19_3PacketFactory(TaskScheduler scheduler, PacketEventsAPI<Plugin> pac
2727
@Override
2828
public CompletableFuture<Void> addTabPlayer(Player player, PacketEntity entity, PropertyHolder properties) {
2929
if (entity.getType() != EntityTypes.PLAYER) return CompletableFuture.completedFuture(null);
30+
3031
CompletableFuture<Void> future = new CompletableFuture<>();
31-
skinned(player, properties, new UserProfile(entity.getUuid(), Integer.toString(entity.getEntityId()))).thenAccept(profile -> {
32+
33+
String displayName = entity.getProperty(propertyRegistry.getByName("display_name", String.class)) == null ? entity.getEntityId()+"" : entity.getProperty(propertyRegistry.getByName("display_name", String.class));
34+
35+
skinned(player, properties, new UserProfile(entity.getUuid(), displayName)).thenAccept(profile -> {
3236
WrapperPlayServerPlayerInfoUpdate.PlayerInfo info = new WrapperPlayServerPlayerInfoUpdate.PlayerInfo(
3337
profile, false, 1, GameMode.CREATIVE,
34-
Component.text(configManager.getConfig().tabDisplayName().replace("{id}", Integer.toString(entity.getEntityId()))), null);
38+
Component.text(configManager.getConfig().tabDisplayName().replace("{id}", displayName)), null);
3539
sendPacket(player, new WrapperPlayServerPlayerInfoUpdate(EnumSet.of(WrapperPlayServerPlayerInfoUpdate.Action.ADD_PLAYER,
3640
WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_LISTED), info, info));
3741
future.complete(null);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Optional;
1919
import java.util.concurrent.CompletableFuture;
2020

21+
2122
public class V1_20_2PacketFactory extends V1_19_3PacketFactory {
2223

2324
protected ConfigManager configManager;

0 commit comments

Comments
 (0)