Skip to content

Commit 06dc981

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 06dc981

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

Lines changed: 18 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,6 +33,18 @@ 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+
EntityData<Optional<Component>> nameData = new EntityData<>(
38+
2,
39+
EntityDataTypes.OPTIONAL_ADV_COMPONENT,
40+
Optional.of(Component.text(entity.getProperty(propertyRegistry.getByName("display_name", String.class)))
41+
));
42+
43+
sendPacket(player, new WrapperPlayServerEntityMetadata(
44+
entity.getEntityId(),
45+
Collections.singletonList(nameData)
46+
));
47+
3048
sendAllMetadata(player, entity, properties);
3149
if (EntityTypes.isTypeInstanceOf(entity.getType(), EntityTypes.LIVINGENTITY)) sendAllAttributes(player, entity, properties);
3250
createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedColor.class)));

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

Lines changed: 18 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.util.Vector3d;
57
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityHeadLook;
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,13 +14,16 @@
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
import java.util.concurrent.CompletableFuture;
2025

26+
2127
public class V1_20_2PacketFactory extends V1_19_3PacketFactory {
2228

2329
protected ConfigManager configManager;
@@ -34,6 +40,18 @@ public CompletableFuture<Void> spawnPlayer(Player player, PacketEntity entity, P
3440
NpcLocation location = entity.getLocation();
3541
sendPacket(player, new WrapperPlayServerSpawnEntity(entity.getEntityId(), Optional.of(entity.getUuid()), entity.getType(),
3642
npcLocationToVector(location), location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.of(new Vector3d())));
43+
44+
EntityData<Optional<Component>> nameData = new EntityData<>(
45+
2,
46+
EntityDataTypes.OPTIONAL_ADV_COMPONENT,
47+
Optional.of(Component.text(entity.getProperty(propertyRegistry.getByName("display_name", String.class)))
48+
));
49+
50+
sendPacket(player, new WrapperPlayServerEntityMetadata(
51+
entity.getEntityId(),
52+
Collections.singletonList(nameData)
53+
));
54+
3755
sendPacket(player, new WrapperPlayServerEntityHeadLook(entity.getEntityId(), location.getYaw()));
3856
sendAllMetadata(player, entity, properties);
3957
sendAllAttributes(player, entity, properties);

0 commit comments

Comments
 (0)