From 765d6057da0bb9882ce31973e6f78be32f340c4d Mon Sep 17 00:00:00 2001 From: TheMode Date: Sun, 24 Apr 2022 05:37:47 +0200 Subject: [PATCH] Avoid entity field lookup Signed-off-by: TheMode --- .../java/net/minestom/server/entity/Metadata.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Metadata.java b/src/main/java/net/minestom/server/entity/Metadata.java index 2e58d0dfa..009647b6c 100644 --- a/src/main/java/net/minestom/server/entity/Metadata.java +++ b/src/main/java/net/minestom/server/entity/Metadata.java @@ -174,25 +174,27 @@ public final class Metadata { public void setIndex(int index, @NotNull Entry entry) { this.metadataMap.put(index, entry); // Send metadata packet to update viewers and self - if (this.entity != null && this.entity.isActive()) { + final Entity entity = this.entity; + if (entity != null && entity.isActive()) { if (!this.notifyAboutChanges) { synchronized (this.notNotifiedChanges) { this.notNotifiedChanges.put(index, entry); } } else { - this.entity.sendPacketToViewersAndSelf(new EntityMetaDataPacket(entity.getEntityId(), Map.of(index, entry))); + entity.sendPacketToViewersAndSelf(new EntityMetaDataPacket(entity.getEntityId(), Map.of(index, entry))); } } } public void setNotifyAboutChanges(boolean notifyAboutChanges) { - if (!NOTIFIED_CHANGES.compareAndSet(this, !notifyAboutChanges, notifyAboutChanges)) { + if (!NOTIFIED_CHANGES.compareAndSet(this, !notifyAboutChanges, notifyAboutChanges)) return; - } if (!notifyAboutChanges) { // Ask future metadata changes to be cached return; } + final Entity entity = this.entity; + if (entity == null || !entity.isActive()) return; Map> entries; synchronized (this.notNotifiedChanges) { Map> awaitingChanges = this.notNotifiedChanges; @@ -200,10 +202,7 @@ public final class Metadata { entries = Map.copyOf(awaitingChanges); awaitingChanges.clear(); } - if (entries == null || this.entity == null || !this.entity.isActive()) { - return; - } - this.entity.sendPacketToViewersAndSelf(new EntityMetaDataPacket(entity.getEntityId(), entries)); + entity.sendPacketToViewersAndSelf(new EntityMetaDataPacket(entity.getEntityId(), entries)); } public @NotNull Map> getEntries() {