Fix duplicate entity tracker creation

This commit is contained in:
fullwall 2024-07-14 22:22:51 +08:00
parent 6d18bc16c7
commit 7447351c22
2 changed files with 14 additions and 27 deletions

View File

@ -111,7 +111,6 @@ public class TraderLlamaController extends MobEntityController {
super.customServerAiStep(); super.customServerAiStep();
} }
setDespawnDelay(10); setDespawnDelay(10);
NMS.setStepHeight(getBukkitEntity(), 1);
npc.update(); npc.update();
} }
} }

View File

@ -626,23 +626,13 @@ public class NMSImpl implements NMSBridge {
@Override @Override
public EntityPacketTracker getPacketTracker(org.bukkit.entity.Entity entity) { public EntityPacketTracker getPacketTracker(org.bukkit.entity.Entity entity) {
ServerLevel server = (ServerLevel) getHandle(entity).level(); ServerLevel server = (ServerLevel) getHandle(entity).level();
TrackedEntity tracked = null; TrackedEntity tracked = server.getChunkSource().chunkMap.entityMap.get(entity.getEntityId());
if (TRACKED_ENTITY_GETTER != null) {
try {
tracked = (TrackedEntity) TRACKED_ENTITY_GETTER.invoke(getHandle(entity));
} catch (Throwable e) {
e.printStackTrace();
}
} else {
tracked = server.getChunkSource().chunkMap.entityMap.get(entity.getEntityId());
}
if (tracked == null) if (tracked == null)
return null; return null;
TrackedEntity entry = tracked;
return new EntityPacketTracker() { return new EntityPacketTracker() {
@Override @Override
public void link(Player player) { public void link(Player player) {
entry.updatePlayer((ServerPlayer) getHandle(player)); tracked.updatePlayer((ServerPlayer) getHandle(player));
} }
@Override @Override
@ -651,12 +641,12 @@ public class NMSImpl implements NMSBridge {
@Override @Override
public void unlink(Player player) { public void unlink(Player player) {
entry.removePlayer((ServerPlayer) getHandle(player)); tracked.removePlayer((ServerPlayer) getHandle(player));
} }
@Override @Override
public void unlinkAll(Consumer<Player> callback) { public void unlinkAll(Consumer<Player> callback) {
entry.broadcastRemoved(); tracked.broadcastRemoved();
} }
}; };
} }
@ -1326,8 +1316,8 @@ public class NMSImpl implements NMSBridge {
@Override @Override
public void removeFromWorld(org.bukkit.entity.Entity entity) { public void removeFromWorld(org.bukkit.entity.Entity entity) {
Preconditions.checkNotNull(entity); Preconditions.checkNotNull(entity);
Entity nmsEntity = ((CraftEntity) entity).getHandle(); Entity handle = getHandle(entity);
((ServerLevel) nmsEntity.level()).getChunkSource().removeEntity(nmsEntity); ((ServerLevel) handle.level()).getChunkSource().removeEntity(handle);
} }
@Override @Override
@ -1346,21 +1336,20 @@ public class NMSImpl implements NMSBridge {
@Override @Override
public void replaceTrackerEntry(org.bukkit.entity.Entity entity) { public void replaceTrackerEntry(org.bukkit.entity.Entity entity) {
Entity handle = getHandle(entity); Entity handle = getHandle(entity);
ServerLevel server = (ServerLevel) handle.level(); ChunkMap cm = ((ServerLevel) handle.level()).getChunkSource().chunkMap;
TrackedEntity entry = cm.entityMap.get(entity.getEntityId());
if (entry == null)
return;
entry.broadcastRemoved();
CitizensEntityTracker newTracker = new CitizensEntityTracker(cm, entry);
for (MethodHandle setter : TRACKED_ENTITY_SETTERS) { for (MethodHandle setter : TRACKED_ENTITY_SETTERS) {
try { try {
setter.invoke(handle, new CitizensEntityTracker(server.getChunkSource().chunkMap, setter.invoke(handle, newTracker);
(TrackedEntity) TRACKED_ENTITY_GETTER.invoke(handle)));
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
TrackedEntity entry = server.getChunkSource().chunkMap.entityMap.get(entity.getEntityId()); cm.entityMap.put(entity.getEntityId(), newTracker);
if (entry == null)
return;
entry.broadcastRemoved();
CitizensEntityTracker replace = new CitizensEntityTracker(server.getChunkSource().chunkMap, entry);
server.getChunkSource().chunkMap.entityMap.put(entity.getEntityId(), replace);
} }
@Override @Override
@ -2613,7 +2602,6 @@ public class NMSImpl implements NMSBridge {
private static final MethodHandle SIZE_FIELD_SETTER = NMS.getFirstSetter(Entity.class, EntityDimensions.class); private static final MethodHandle SIZE_FIELD_SETTER = NMS.getFirstSetter(Entity.class, EntityDimensions.class);
private static MethodHandle SKULL_META_PROFILE; private static MethodHandle SKULL_META_PROFILE;
private static MethodHandle TEAM_FIELD; private static MethodHandle TEAM_FIELD;
private static final MethodHandle TRACKED_ENTITY_GETTER = NMS.getFirstGetter(Entity.class, TrackedEntity.class);
private static final Collection<MethodHandle> TRACKED_ENTITY_SETTERS = NMS.getSettersOfType(Entity.class, private static final Collection<MethodHandle> TRACKED_ENTITY_SETTERS = NMS.getSettersOfType(Entity.class,
TrackedEntity.class); TrackedEntity.class);
static { static {