Fix per-player lookclose

This commit is contained in:
fullwall 2023-01-01 18:43:41 +08:00
parent b4280103c1
commit 9103823ebb
3 changed files with 23 additions and 3 deletions

View File

@ -108,7 +108,6 @@ public class ProtocolLibListener {
PacketRotationSession session = trait.getPacketSession(event.getPlayer());
if (session == null || !session.isActive())
return;
PacketContainer packet = event.getPacket();
PacketType type = event.getPacketType();
if (type == Server.ENTITY_HEAD_ROTATION) {
@ -127,6 +126,8 @@ public class ProtocolLibListener {
packet.getFloat().write(0, session.getBodyYaw());
packet.getFloat().write(1, session.getPitch());
}
session.onPacketOverwritten();
}
});
}

View File

@ -106,6 +106,7 @@ public class LookClose extends Trait implements Toggleable {
for (UUID uuid : Sets.newHashSet(Sets.difference(sessions.keySet(), seen))) {
sessions.remove(uuid).end();
}
return;
} else if (sessions.size() > 0) {
for (PacketRotationSession session : sessions.values()) {
session.end();

View File

@ -145,7 +145,7 @@ public class RotationTrait extends Trait {
public static class PacketRotationSession {
private boolean ended;
private final RotationSession session;
private RotationTriple triple;
private PacketRotationTriple triple;
public PacketRotationSession(RotationSession session) {
this.session = session;
@ -180,6 +180,12 @@ public class RotationTrait extends Trait {
return !ended && session.isActive();
}
public void onPacketOverwritten() {
if (triple == null)
return;
triple.record();
}
public void run(Entity entity) {
if (triple == null) {
triple = new PacketRotationTriple(entity);
@ -192,13 +198,25 @@ public class RotationTrait extends Trait {
}
private static class PacketRotationTriple extends EntityRotation {
private float lastBodyYaw;
private float lastHeadYaw;
private float lastPitch;
public PacketRotationTriple(Entity entity) {
super(entity);
}
@Override
public void apply() {
// NMS.sendRotationNearby(entity, bodyYaw, headYaw, pitch);
if (Math.abs(lastBodyYaw - bodyYaw) + Math.abs(lastHeadYaw - headYaw) + Math.abs(pitch - lastPitch) > 1) {
NMS.sendRotationNearby(entity, bodyYaw, headYaw, pitch);
}
}
public void record() {
lastBodyYaw = bodyYaw;
lastHeadYaw = headYaw;
lastPitch = pitch;
}
}