Make more async friendly

This commit is contained in:
fullwall 2023-10-15 20:53:01 +08:00
parent ea34a18ebe
commit ad89e9aa83
2 changed files with 13 additions and 6 deletions

View File

@ -125,18 +125,20 @@ public class ProtocolLibListener implements Listener {
uuid -> mirrorTraits.get(uuid)); uuid -> mirrorTraits.get(uuid));
return; return;
} }
List<PlayerInfoData> list = event.getPacket().getPlayerInfoDataLists().readSafely(0); List<PlayerInfoData> list = event.getPacket().getPlayerInfoDataLists().readSafely(0);
if (list == null) if (list == null)
return; return;
boolean changed = false; boolean changed = false;
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
PlayerInfoData npcInfo = list.get(i); PlayerInfoData npcInfo = list.get(i);
if (npcInfo == null) if (npcInfo == null)
continue; continue;
MirrorTrait trait = mirrorTraits.get(npcInfo.getProfile().getUUID()); MirrorTrait trait = mirrorTraits.get(npcInfo.getProfile().getUUID());
if (trait == null || !trait.isMirroring(event.getPlayer())) { if (trait == null || !trait.isMirroring(event.getPlayer()))
continue; continue;
}
GameProfile playerProfile = NMS.getProfile(event.getPlayer()); GameProfile playerProfile = NMS.getProfile(event.getPlayer());
if (trait.mirrorName()) { if (trait.mirrorName()) {
list.set(i, list.set(i,
@ -166,7 +168,7 @@ public class ProtocolLibListener implements Listener {
} }
}); });
manager.addPacketListener(new PacketAdapter( manager.addPacketListener(new PacketAdapter(
plugin, ListenerPriority.MONITOR, Arrays.asList(Server.ENTITY_HEAD_ROTATION, Server.ENTITY_LOOK, plugin, ListenerPriority.HIGHEST, Arrays.asList(Server.ENTITY_HEAD_ROTATION, Server.ENTITY_LOOK,
Server.REL_ENTITY_MOVE_LOOK, Server.ENTITY_MOVE_LOOK, Server.POSITION, Server.ENTITY_TELEPORT), Server.REL_ENTITY_MOVE_LOOK, Server.ENTITY_MOVE_LOOK, Server.POSITION, Server.ENTITY_TELEPORT),
ListenerOptions.ASYNC) { ListenerOptions.ASYNC) {
@Override @Override

View File

@ -33,7 +33,7 @@ public class RotationTrait extends Trait {
@Persist(reify = true) @Persist(reify = true)
private final RotationParams globalParameters = new RotationParams(); private final RotationParams globalParameters = new RotationParams();
private final RotationSession globalSession = new RotationSession(globalParameters); private final RotationSession globalSession = new RotationSession(globalParameters);
private final List<PacketRotationSession> packetSessions = Lists.newArrayList(); private final List<PacketRotationSession> packetSessions = Lists.newCopyOnWriteArrayList();
private final Map<UUID, PacketRotationSession> packetSessionsByUUID = Maps.newConcurrentMap(); private final Map<UUID, PacketRotationSession> packetSessionsByUUID = Maps.newConcurrentMap();
public RotationTrait() { public RotationTrait() {
@ -60,6 +60,7 @@ public class RotationTrait extends Trait {
} else { } else {
packetSessions.add(lrs); packetSessions.add(lrs);
} }
return lrs; return lrs;
} }
@ -79,11 +80,13 @@ public class RotationTrait extends Trait {
PacketRotationSession lrs = packetSessionsByUUID.get(player.getUniqueId()); PacketRotationSession lrs = packetSessionsByUUID.get(player.getUniqueId());
if (lrs != null && lrs.triple != null) if (lrs != null && lrs.triple != null)
return lrs; return lrs;
for (PacketRotationSession session : packetSessions) { for (PacketRotationSession session : packetSessions) {
if (session.accepts(player) && session.triple != null) { if (session.accepts(player) && session.triple != null) {
return session; return session;
} }
} }
return null; return null;
} }
@ -95,6 +98,7 @@ public class RotationTrait extends Trait {
public void run() { public void run() {
if (!npc.isSpawned()) if (!npc.isSpawned())
return; return;
if (npc.data().get(NPC.Metadata.RESET_PITCH_ON_TICK, false)) { if (npc.data().get(NPC.Metadata.RESET_PITCH_ON_TICK, false)) {
NMS.setPitch(npc.getEntity(), 0); NMS.setPitch(npc.getEntity(), 0);
} }
@ -180,6 +184,7 @@ public class RotationTrait extends Trait {
public void onPacketOverwritten() { public void onPacketOverwritten() {
if (triple == null) if (triple == null)
return; return;
triple.record(); triple.record();
} }
@ -224,7 +229,7 @@ public class RotationTrait extends Trait {
private boolean linkedBody; private boolean linkedBody;
private float maxPitchPerTick = 10; private float maxPitchPerTick = 10;
private float maxYawPerTick = 40; private float maxYawPerTick = 40;
private boolean persist = false; private volatile boolean persist = false;
private float[] pitchRange = { -180, 180 }; private float[] pitchRange = { -180, 180 };
private List<UUID> uuidFilter; private List<UUID> uuidFilter;
private float[] yawRange = { -180, 180 }; private float[] yawRange = { -180, 180 };
@ -383,7 +388,7 @@ public class RotationTrait extends Trait {
public class RotationSession { public class RotationSession {
private final RotationParams params; private final RotationParams params;
private int t = -1; private volatile int t = -1;
private Supplier<Float> targetPitch = () -> 0F; private Supplier<Float> targetPitch = () -> 0F;
private Supplier<Float> targetYaw = targetPitch; private Supplier<Float> targetYaw = targetPitch;