Skip to content

Commit

Permalink
rename per_player: stabler uuid based lookup
Browse files Browse the repository at this point in the history
to replace the eid lookup
  • Loading branch information
mcmonkey4eva committed Oct 18, 2020
1 parent d4cbf17 commit a3caf80
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
Expand Up @@ -137,7 +137,7 @@ public void execute(final ScriptEntry scriptEntry) {
continue;
}
if (name.asString().equals("cancel")) {
customNames.remove(bukkitEntity.getEntityId());
customNames.remove(bukkitEntity.getUniqueId());
if (bukkitEntity.isCustomNameVisible()) {
if (players == null) {
for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
Expand All @@ -157,10 +157,10 @@ public void execute(final ScriptEntry scriptEntry) {
}
else {
final BukkitTagContext originalContext = (BukkitTagContext) scriptEntry.context.clone();
HashMap<UUID, Function<Player, String>> playerToFuncMap = customNames.get(bukkitEntity.getEntityId());
HashMap<UUID, Function<Player, String>> playerToFuncMap = customNames.get(bukkitEntity.getUniqueId());
if (playerToFuncMap == null) {
playerToFuncMap = new HashMap<>();
customNames.put(bukkitEntity.getEntityId(), playerToFuncMap);
customNames.put(bukkitEntity.getUniqueId(), playerToFuncMap);
}
Function<Player, String> nameGetter = p -> {
originalContext.player = new PlayerTag(p);
Expand Down Expand Up @@ -227,21 +227,21 @@ else if (entity instanceof PlayerTag) {
}
else {
Entity bukkitEntity = entity.getDenizenEntity().getBukkitEntity();
customNames.remove(bukkitEntity.getEntityId());
customNames.remove(bukkitEntity.getUniqueId());
bukkitEntity.setCustomName(nameString);
bukkitEntity.setCustomNameVisible(true);
}
}
}

public static HashMap<Integer, HashMap<UUID, Function<Player, String>>> customNames = new HashMap<>();
public static HashMap<UUID, HashMap<UUID, Function<Player, String>>> customNames = new HashMap<>();

public static boolean hasAnyDynamicRenames() {
return !customNames.isEmpty();
}

public static String getCustomNameFor(int eid, Player player) {
HashMap<UUID, Function<Player, String>> map = customNames.get(eid);
public static String getCustomNameFor(UUID entityId, Player player) {
HashMap<UUID, Function<Player, String>> map = customNames.get(entityId);
if (map == null) {
return null;
}
Expand Down
Expand Up @@ -169,7 +169,11 @@ public void processCustomNameForPacket(Packet<?> packet) {
PacketPlayOutEntityMetadata metadataPacket = (PacketPlayOutEntityMetadata) packet;
try {
int eid = ENTITY_METADATA_EID.getInt(metadataPacket);
String nameToApply = RenameCommand.getCustomNameFor(eid, player.getBukkitEntity());
Entity ent = player.world.getEntity(eid);
if (ent == null) {
return; // If it doesn't exist on-server, it's definitely not relevant, so move on
}
String nameToApply = RenameCommand.getCustomNameFor(ent.getUniqueID(), player.getBukkitEntity());
if (nameToApply == null) {
return;
}
Expand Down
Expand Up @@ -169,7 +169,11 @@ public void processCustomNameForPacket(Packet<?> packet) {
PacketPlayOutEntityMetadata metadataPacket = (PacketPlayOutEntityMetadata) packet;
try {
int eid = ENTITY_METADATA_EID.getInt(metadataPacket);
String nameToApply = RenameCommand.getCustomNameFor(eid, player.getBukkitEntity());
Entity ent = player.world.getEntity(eid);
if (ent == null) {
return; // If it doesn't exist on-server, it's definitely not relevant, so move on
}
String nameToApply = RenameCommand.getCustomNameFor(ent.getUniqueID(), player.getBukkitEntity());
if (nameToApply == null) {
return;
}
Expand Down
Expand Up @@ -190,7 +190,11 @@ public void processCustomNameForPacket(Packet<?> packet) {
PacketPlayOutEntityMetadata metadataPacket = (PacketPlayOutEntityMetadata) packet;
try {
int eid = ENTITY_METADATA_EID.getInt(metadataPacket);
String nameToApply = RenameCommand.getCustomNameFor(eid, player.getBukkitEntity());
Entity ent = player.world.getEntity(eid);
if (ent == null) {
return; // If it doesn't exist on-server, it's definitely not relevant, so move on
}
String nameToApply = RenameCommand.getCustomNameFor(ent.getUniqueID(), player.getBukkitEntity());
if (nameToApply == null) {
return;
}
Expand Down

0 comments on commit a3caf80

Please sign in to comment.