Reduce NPCSeenByPlayerEvent allocations

This commit is contained in:
fullwall 2023-06-17 22:00:26 +08:00
parent c2e4f93416
commit 3571c05d9d
12 changed files with 52 additions and 16 deletions

View File

@ -31,7 +31,7 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry {
if (entityplayer instanceof EntityHumanNPC)
return;
Entity tracker = getTracker(this);
if (tracker instanceof NPCHolder) {
if (!trackedPlayers.contains(entityplayer) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
NPCSeenByPlayerEvent event = new NPCSeenByPlayerEvent(npc, entityplayer.getBukkitEntity());
Bukkit.getPluginManager().callEvent(event);

View File

@ -16,7 +16,6 @@ import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCSeenByPlayerEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.nms.v1_11_R1.entity.EntityHumanNPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_11_R1.Entity;
@ -26,12 +25,15 @@ import net.minecraft.server.v1_11_R1.EntityTrackerEntry;
public class PlayerlistTrackerEntry extends EntityTrackerEntry {
private Entity tracker;
private Map<EntityPlayer, Boolean> trackingMap;
public PlayerlistTrackerEntry(Entity entity, int i, int j, int k, boolean flag) {
super(entity, i, j, k, flag);
tracker = getTracker(this);
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
trackingMap = delegate;
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
@ -78,6 +80,10 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry {
this(getTracker(entry), getE(entry), getF(entry), getG(entry), getU(entry));
}
private boolean isTracked(EntityPlayer player) {
return trackingMap != null ? trackingMap.containsKey(player) : trackedPlayers.contains(player);
}
public void updateLastPlayer(EntityPlayer lastUpdatedPlayer) {
if (lastUpdatedPlayer == null || tracker.dead || tracker.getBukkitEntity().getType() != EntityType.PLAYER)
return;
@ -93,16 +99,15 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry {
@Override
public void updatePlayer(final EntityPlayer entityplayer) {
// prevent updates to NPC "viewers"
if (entityplayer instanceof EntityHumanNPC)
return;
if (tracker instanceof NPCHolder) {
if (!tracker.dead && !isTracked(entityplayer) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
NPCSeenByPlayerEvent event = new NPCSeenByPlayerEvent(npc, entityplayer.getBukkitEntity());
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
}
if (entityplayer instanceof NPCHolder)
return;
super.updatePlayer(entityplayer);
}

View File

@ -26,12 +26,15 @@ import net.minecraft.server.v1_12_R1.EntityTrackerEntry;
public class PlayerlistTrackerEntry extends EntityTrackerEntry {
private final Entity tracker;
private Map<EntityPlayer, Boolean> trackingMap;
public PlayerlistTrackerEntry(Entity entity, int i, int j, int k, boolean flag) {
super(entity, i, j, k, flag);
this.tracker = getTracker(this);
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
trackingMap = delegate;
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
@ -78,6 +81,10 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry {
this(getTracker(entry), getE(entry), getF(entry), getG(entry), getU(entry));
}
private boolean isTracked(EntityPlayer player) {
return trackingMap != null ? trackingMap.containsKey(player) : trackedPlayers.contains(player);
}
public void updateLastPlayer(EntityPlayer lastUpdatedPlayer) {
if (lastUpdatedPlayer == null || tracker.dead || tracker.getBukkitEntity().getType() != EntityType.PLAYER)
return;
@ -93,7 +100,7 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry {
@Override
public void updatePlayer(final EntityPlayer entityplayer) {
if (tracker instanceof NPCHolder) {
if (!tracker.dead && !isTracked(entityplayer) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
NPCSeenByPlayerEvent event = new NPCSeenByPlayerEvent(npc, entityplayer.getBukkitEntity());
Bukkit.getPluginManager().callEvent(event);

View File

@ -26,6 +26,7 @@ import net.minecraft.server.v1_13_R2.PacketPlayOutAnimation;
public class PlayerlistTrackerEntry extends EntityTrackerEntry {
private final Entity tracker;
private Map<EntityPlayer, Boolean> trackingMap;
public PlayerlistTrackerEntry(Entity entity, int i, int j, int k, boolean flag) {
super(entity, i, j, k, flag);
@ -33,6 +34,7 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry {
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
trackingMap = delegate;
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
@ -79,6 +81,10 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry {
this(getTracker(entry), getE(entry), getF(entry), getG(entry), getU(entry));
}
private boolean isTracked(EntityPlayer player) {
return trackingMap != null ? trackingMap.containsKey(player) : trackedPlayers.contains(player);
}
public void updateLastPlayer(EntityPlayer lastUpdatedPlayer) {
if (tracker.dead || lastUpdatedPlayer == null || tracker.getBukkitEntity().getType() != EntityType.PLAYER)
return;
@ -97,7 +103,7 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry {
@Override
public void updatePlayer(final EntityPlayer entityplayer) {
if (tracker instanceof NPCHolder) {
if (!tracker.dead && !isTracked(entityplayer) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
NPCSeenByPlayerEvent event = new NPCSeenByPlayerEvent(npc, entityplayer.getBukkitEntity());
Bukkit.getPluginManager().callEvent(event);

View File

@ -27,6 +27,7 @@ import net.minecraft.server.v1_14_R1.PlayerChunkMap.EntityTracker;
public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
private final Entity tracker;
private Map<EntityPlayer, Boolean> trackingMap;
public PlayerlistTracker(PlayerChunkMap map, Entity entity, int i, int j, boolean flag) {
map.super(entity, i, j, flag);
@ -34,6 +35,7 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
trackingMap = delegate;
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
@ -80,6 +82,10 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
this(map, getTracker(entry), getTrackingDistance(entry), getD(entry), getE(entry));
}
private boolean isTracked(EntityPlayer player) {
return trackingMap != null ? trackingMap.containsKey(player) : trackedPlayers.contains(player);
}
public void updateLastPlayer(EntityPlayer lastUpdatedPlayer) {
if (tracker.dead || lastUpdatedPlayer == null || tracker.getBukkitEntity().getType() != EntityType.PLAYER)
return;
@ -97,7 +103,7 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
@Override
public void updatePlayer(final EntityPlayer entityplayer) {
if (tracker instanceof NPCHolder) {
if (!tracker.dead && !isTracked(entityplayer) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
NPCSeenByPlayerEvent event = new NPCSeenByPlayerEvent(npc, entityplayer.getBukkitEntity());
Bukkit.getPluginManager().callEvent(event);

View File

@ -27,6 +27,7 @@ import net.minecraft.server.v1_15_R1.PlayerChunkMap.EntityTracker;
public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
private final Entity tracker;
private Map<EntityPlayer, Boolean> trackingMap;
public PlayerlistTracker(PlayerChunkMap map, Entity entity, int i, int j, boolean flag) {
map.super(entity, i, j, flag);
@ -34,6 +35,7 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
trackingMap = delegate;
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
@ -80,6 +82,10 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
this(map, getTracker(entry), getTrackingDistance(entry), getD(entry), getE(entry));
}
private boolean isTracked(EntityPlayer player) {
return trackingMap != null ? trackingMap.containsKey(player) : trackedPlayers.contains(player);
}
public void updateLastPlayer(EntityPlayer lastUpdatedPlayer) {
if (tracker.dead || lastUpdatedPlayer == null || tracker.getBukkitEntity().getType() != EntityType.PLAYER)
return;
@ -97,7 +103,7 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
@Override
public void updatePlayer(final EntityPlayer entityplayer) {
if (tracker instanceof NPCHolder) {
if (!tracker.dead && !isTracked(entityplayer) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
NPCSeenByPlayerEvent event = new NPCSeenByPlayerEvent(npc, entityplayer.getBukkitEntity());
Bukkit.getPluginManager().callEvent(event);

View File

@ -27,6 +27,7 @@ import net.minecraft.server.v1_16_R3.PlayerChunkMap.EntityTracker;
public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
private final Entity tracker;
private Map<EntityPlayer, Boolean> trackingMap;
public PlayerlistTracker(PlayerChunkMap map, Entity entity, int i, int j, boolean flag) {
map.super(entity, i, j, flag);
@ -34,6 +35,7 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
trackingMap = delegate;
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
@ -80,6 +82,10 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
this(map, getTracker(entry), getTrackingDistance(entry), getD(entry), getE(entry));
}
private boolean isTracked(EntityPlayer player) {
return trackingMap != null ? trackingMap.containsKey(player) : trackedPlayers.contains(player);
}
public void updateLastPlayer(EntityPlayer lastUpdatedPlayer) {
if (tracker.dead || lastUpdatedPlayer == null || tracker.getBukkitEntity().getType() != EntityType.PLAYER)
return;
@ -97,7 +103,7 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
@Override
public void updatePlayer(final EntityPlayer entityplayer) {
if (tracker instanceof NPCHolder) {
if (!tracker.dead && !isTracked(entityplayer) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
NPCSeenByPlayerEvent event = new NPCSeenByPlayerEvent(npc, entityplayer.getBukkitEntity());
Bukkit.getPluginManager().callEvent(event);

View File

@ -52,7 +52,7 @@ public class PlayerlistTracker extends ChunkMap.TrackedEntity {
@Override
public void updatePlayer(final ServerPlayer entityplayer) {
if (tracker instanceof NPCHolder) {
if (!tracker.isRemoved() && !seenBy.contains(entityplayer.connection) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
NPCSeenByPlayerEvent event = new NPCSeenByPlayerEvent(npc, entityplayer.getBukkitEntity());
REQUIRES_SYNC = Util.callEventPossiblySync(event, REQUIRES_SYNC);

View File

@ -52,7 +52,7 @@ public class PlayerlistTracker extends ChunkMap.TrackedEntity {
@Override
public void updatePlayer(final ServerPlayer entityplayer) {
if (tracker instanceof NPCHolder) {
if (!tracker.isRemoved() && !seenBy.contains(entityplayer.connection) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
NPCSeenByPlayerEvent event = new NPCSeenByPlayerEvent(npc, entityplayer.getBukkitEntity());
REQUIRES_SYNC = Util.callEventPossiblySync(event, REQUIRES_SYNC);

View File

@ -79,7 +79,7 @@ public class CitizensEntityTracker extends ChunkMap.TrackedEntity {
if (entityplayer instanceof EntityHumanNPC)
return;
if (tracker instanceof NPCHolder) {
if (!tracker.isRemoved() && !seenBy.contains(entityplayer.connection) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
NPCSeenByPlayerEvent event = new NPCSeenByPlayerEvent(npc, entityplayer.getBukkitEntity());
REQUIRES_SYNC = Util.callEventPossiblySync(event, REQUIRES_SYNC);

View File

@ -79,7 +79,7 @@ public class CitizensEntityTracker extends ChunkMap.TrackedEntity {
if (entityplayer instanceof EntityHumanNPC)
return;
if (tracker instanceof NPCHolder) {
if (!tracker.isRemoved() && !seenBy.contains(entityplayer.connection) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
if (REQUIRES_SYNC == null) {
REQUIRES_SYNC = !Bukkit.isPrimaryThread();

View File

@ -55,7 +55,7 @@ public class PlayerlistTrackerEntry extends EntityTrackerEntry {
// prevent updates to NPC "viewers"
if (entityplayer instanceof EntityHumanNPC)
return;
if (tracker instanceof NPCHolder) {
if (!trackedPlayers.contains(entityplayer) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
NPCSeenByPlayerEvent event = new NPCSeenByPlayerEvent(npc, entityplayer.getBukkitEntity());
Bukkit.getPluginManager().callEvent(event);