Fix entities desyncing in 1.21.3

This commit is contained in:
libraryaddict 2024-11-19 22:28:37 +13:00
parent 852ce71dbc
commit 1739ef51c0
5 changed files with 78 additions and 17 deletions

View File

@ -3,6 +3,7 @@ package me.libraryaddict.disguise.utilities;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketSendEvent;
import com.github.retrooper.packetevents.event.simple.PacketPlaySendEvent;
import com.github.retrooper.packetevents.protocol.entity.EntityPositionData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.item.enchantment.type.EnchantmentType;
@ -27,6 +28,7 @@ import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEn
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityHeadLook;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityMetadata;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityMovement;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityPositionSync;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityRelativeMove;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityRelativeMoveAndRotation;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityRotation;
@ -528,6 +530,14 @@ public class DisguiseUtilities {
packets.getPackets().addAll(newPackets);
}
public static EntityPositionData clone(EntityPositionData data) {
return new EntityPositionData(clone(data.getPosition()), clone(data.getDeltaMovement()), data.getYaw(), data.getPitch());
}
private static Vector3d clone(Vector3d vec) {
return new Vector3d(vec.getX(), vec.getY(), vec.getZ());
}
public static @Nullable List<PacketWrapper> adjustNamePositions(Disguise disguise, List<PacketWrapper> packets) {
int len = disguise.getMultiNameLength();
@ -551,7 +561,6 @@ public class DisguiseUtilities {
PacketWrapper cloned;
if (packet instanceof WrapperPlayServerEntityTeleport) {
// TODO Handle if this is a vehicle movement
WrapperPlayServerEntityTeleport tele = (WrapperPlayServerEntityTeleport) packet;
cloned = new WrapperPlayServerEntityTeleport(standId,
@ -565,6 +574,11 @@ public class DisguiseUtilities {
WrapperPlayServerEntityRelativeMove rot = (WrapperPlayServerEntityRelativeMove) packet;
cloned = new WrapperPlayServerEntityRelativeMove(standId, rot.getDeltaX(), rot.getDeltaY(), rot.getDeltaZ(),
rot.isOnGround());
} else if (packet instanceof WrapperPlayServerEntityPositionSync) {
WrapperPlayServerEntityPositionSync sync = (WrapperPlayServerEntityPositionSync) packet;
EntityPositionData data = clone(sync.getValues());
data.setPosition(data.getPosition().add(0, height + (DisguiseUtilities.getNameSpacing() * i), 0));
cloned = new WrapperPlayServerEntityPositionSync(standId, data, sync.isOnGround());
} else {
// It seems that EntityStatus packet was being added at some point, probably in some other transformation
continue; // throw new IllegalStateException("Unknown packet " + packet.getClass());
@ -3063,6 +3077,8 @@ public class DisguiseUtilities {
return new WrapperPlayServerCollectItem(event);
case DESTROY_ENTITIES:
return new WrapperPlayServerDestroyEntities(event);
case ENTITY_POSITION_SYNC:
return new WrapperPlayServerEntityPositionSync(event);
default:
throw new IllegalStateException(event.getPacketType() + " wasn't in the enums");
}
@ -3113,6 +3129,8 @@ public class DisguiseUtilities {
return ((WrapperPlayServerAttachEntity) wrapper).getAttachedId();
} else if (wrapper instanceof WrapperPlayServerCollectItem) {
return ((WrapperPlayServerCollectItem) wrapper).getCollectorEntityId();
} else if (wrapper instanceof WrapperPlayServerEntityPositionSync) {
return ((WrapperPlayServerEntityPositionSync) wrapper).getId();
} else {
throw new IllegalStateException("The packet " + wrapper.getClass() + " has no entity ID");
}
@ -3199,6 +3217,10 @@ public class DisguiseUtilities {
if (((WrapperPlayServerEntityEffect) wrapper).getEntityId() == playerId) {
((WrapperPlayServerEntityEffect) wrapper).setEntityId(DisguiseAPI.getSelfDisguiseId());
}
} else if (wrapper instanceof WrapperPlayServerEntityPositionSync) {
if (((WrapperPlayServerEntityPositionSync) wrapper).getId() == playerId) {
((WrapperPlayServerEntityPositionSync) wrapper).setId(DisguiseAPI.getSelfDisguiseId());
}
}
}

View File

@ -184,6 +184,7 @@ public class PacketsManager {
packetsToListen.add(Server.ENTITY_RELATIVE_MOVE);
packetsToListen.add(Server.ENTITY_VELOCITY);
packetsToListen.add(Server.ATTACH_ENTITY);
packetsToListen.add(Server.ENTITY_POSITION_SYNC);
}
// Add equipment packet

View File

@ -1,9 +1,11 @@
package me.libraryaddict.disguise.utilities.packets.packethandlers;
import com.github.retrooper.packetevents.protocol.entity.EntityPositionData;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.github.retrooper.packetevents.util.Vector3d;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityPositionSync;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityRelativeMove;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityRelativeMoveAndRotation;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityRotation;
@ -35,7 +37,8 @@ public class PacketHandlerMovement<T extends PacketWrapper<T>> implements IPacke
@Override
public PacketTypeCommon[] getHandledPackets() {
return new PacketTypeCommon[]{PacketType.Play.Server.ENTITY_RELATIVE_MOVE_AND_ROTATION, PacketType.Play.Server.ENTITY_ROTATION,
PacketType.Play.Server.ENTITY_TELEPORT, PacketType.Play.Server.ENTITY_RELATIVE_MOVE};
PacketType.Play.Server.ENTITY_TELEPORT, PacketType.Play.Server.ENTITY_RELATIVE_MOVE,
PacketType.Play.Server.ENTITY_POSITION_SYNC};
}
private double conRel(double oldCord, double newCord) {
@ -202,6 +205,13 @@ public class PacketHandlerMovement<T extends PacketWrapper<T>> implements IPacke
cloned = new WrapperPlayServerEntityRotation(rot.getEntityId(), yawValue = rot.getYaw(), pitchValue = rot.getPitch(),
rot.isOnGround());
} else if (sentPacket instanceof WrapperPlayServerEntityPositionSync) {
WrapperPlayServerEntityPositionSync sync = (WrapperPlayServerEntityPositionSync) sentPacket;
yawValue = sync.getValues().getYaw();
pitchValue = sync.getValues().getPitch();
cloned =
new WrapperPlayServerEntityPositionSync(sync.getId(), DisguiseUtilities.clone(sync.getValues()), sync.isOnGround());
} else {
throw new IllegalStateException("Unknown packet " + sentPacket.getClass());
}
@ -238,6 +248,11 @@ public class PacketHandlerMovement<T extends PacketWrapper<T>> implements IPacke
look.setYaw(yawValue);
look.setPitch(pitchValue);
} else if (cloned instanceof WrapperPlayServerEntityPositionSync) {
EntityPositionData data = ((WrapperPlayServerEntityPositionSync) cloned).getValues();
data.setYaw(yawValue);
data.setPitch(pitchValue);
}
if (entity == observer.getVehicle() && AbstractHorse.class.isAssignableFrom(disguise.getType().getEntityClass())) {
@ -246,6 +261,7 @@ public class PacketHandlerMovement<T extends PacketWrapper<T>> implements IPacke
packets.addPacket(packet);
} else if (cloned instanceof WrapperPlayServerEntityTeleport && disguise.getType().isArtDisplay()) {
// TODO Sync too
WrapperPlayServerEntityTeleport tele = (WrapperPlayServerEntityTeleport) cloned;
Location loc = entity.getLocation();
@ -270,6 +286,8 @@ public class PacketHandlerMovement<T extends PacketWrapper<T>> implements IPacke
((WrapperPlayServerEntityRelativeMoveAndRotation) cloned).setOnGround(false);
} else if (cloned instanceof WrapperPlayServerEntityRotation) {
((WrapperPlayServerEntityRotation) cloned).setOnGround(false);
} else if (cloned instanceof WrapperPlayServerEntityPositionSync) {
((WrapperPlayServerEntityPositionSync) cloned).setOnGround(false);
}
}
} else if (disguise.getType() == DisguiseType.DOLPHIN) {
@ -283,18 +301,32 @@ public class PacketHandlerMovement<T extends PacketWrapper<T>> implements IPacke
packets.addPacket(cloned);
}
if (yMod != 0 && sentPacket instanceof WrapperPlayServerEntityTeleport) {
PacketWrapper packet = packets.getPackets().get(0);
WrapperPlayServerEntityTeleport tele = (WrapperPlayServerEntityTeleport) packet;
if (yMod != 0) {
if (sentPacket instanceof WrapperPlayServerEntityTeleport) {
PacketWrapper packet = packets.getPackets().get(0);
WrapperPlayServerEntityTeleport tele = (WrapperPlayServerEntityTeleport) packet;
if (packet == sentPacket) {
packet = new WrapperPlayServerEntityTeleport(tele.getEntityId(), tele.getPosition().add(0, yMod, 0), tele.getYaw(),
tele.getPitch(), tele.isOnGround());
if (packet == sentPacket) {
packet = new WrapperPlayServerEntityTeleport(tele.getEntityId(), tele.getPosition().add(0, yMod, 0), tele.getYaw(),
tele.getPitch(), tele.isOnGround());
packets.clear();
packets.addPacket(packet);
} else {
tele.setPosition(tele.getPosition().add(0, yMod, 0));
packets.clear();
packets.addPacket(packet);
} else {
tele.setPosition(tele.getPosition().add(0, yMod, 0));
}
} else if (sentPacket instanceof WrapperPlayServerEntityPositionSync) {
WrapperPlayServerEntityPositionSync sync = (WrapperPlayServerEntityPositionSync) packets.getPackets().get(0);
if (sync == sentPacket) {
sync = new WrapperPlayServerEntityPositionSync(sync.getId(), DisguiseUtilities.clone(sync.getValues()),
sync.isOnGround());
packets.clear();
packets.addPacket(sync);
} else {
sync.getValues().setPosition(sync.getValues().getPosition().add(0, yMod, 0));
}
}
}
}

View File

@ -7,6 +7,7 @@ import com.github.retrooper.packetevents.event.simple.PacketPlaySendEvent;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerDestroyEntities;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityPositionSync;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityRelativeMove;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityRelativeMoveAndRotation;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityTeleport;
@ -26,6 +27,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import static com.github.retrooper.packetevents.protocol.packettype.PacketType.Play.Server.ENTITY_POSITION_SYNC;
import static com.github.retrooper.packetevents.protocol.packettype.PacketType.Play.Server.ENTITY_RELATIVE_MOVE;
import static com.github.retrooper.packetevents.protocol.packettype.PacketType.Play.Server.ENTITY_RELATIVE_MOVE_AND_ROTATION;
import static com.github.retrooper.packetevents.protocol.packettype.PacketType.Play.Server.ENTITY_TELEPORT;
@ -250,7 +252,7 @@ public class PacketListenerVehicleMovement extends SimplePacketListenerAbstract
refreshPosition(player, entry.getKey());
}
} else if (event.getPacketType() == ENTITY_TELEPORT || event.getPacketType() == ENTITY_RELATIVE_MOVE ||
event.getPacketType() == ENTITY_RELATIVE_MOVE_AND_ROTATION) {
event.getPacketType() == ENTITY_RELATIVE_MOVE_AND_ROTATION || event.getPacketType() == ENTITY_POSITION_SYNC) {
PlayerTracker tracker = trackerMap.get(player.getUniqueId());
if (tracker == null || tracker.vehicleAndPassengersId.isEmpty()) {
@ -267,9 +269,12 @@ public class PacketListenerVehicleMovement extends SimplePacketListenerAbstract
} else if (event.getPacketType() == ENTITY_RELATIVE_MOVE) {
wrapper = new WrapperPlayServerEntityRelativeMove(event);
entityId = ((WrapperPlayServerEntityRelativeMove) wrapper).getEntityId();
} else {
} else if (event.getPacketType() == ENTITY_RELATIVE_MOVE_AND_ROTATION) {
wrapper = new WrapperPlayServerEntityRelativeMoveAndRotation(event);
entityId = ((WrapperPlayServerEntityRelativeMoveAndRotation) wrapper).getEntityId();
} else {
wrapper = new WrapperPlayServerEntityPositionSync(event);
entityId = ((WrapperPlayServerEntityPositionSync) wrapper).getId();
}
} else {
entityId = DisguiseUtilities.getEntityId(wrapper);

View File

@ -34,7 +34,8 @@ public class PacketListenerViewSelfDisguise extends SimplePacketListenerAbstract
for (Server packet : new Server[]{NmsVersion.v1_20_R2.isSupported() ? Server.SPAWN_ENTITY : Server.SPAWN_PLAYER,
Server.ATTACH_ENTITY, Server.ENTITY_RELATIVE_MOVE_AND_ROTATION, Server.ENTITY_RELATIVE_MOVE, Server.ENTITY_HEAD_LOOK,
Server.ENTITY_ROTATION, Server.ENTITY_TELEPORT, Server.ENTITY_MOVEMENT, Server.ENTITY_METADATA, Server.ENTITY_EQUIPMENT,
Server.ENTITY_ANIMATION, Server.ENTITY_EFFECT, Server.ENTITY_VELOCITY, Server.UPDATE_ATTRIBUTES, Server.ENTITY_STATUS}) {
Server.ENTITY_ANIMATION, Server.ENTITY_EFFECT, Server.ENTITY_VELOCITY, Server.UPDATE_ATTRIBUTES, Server.ENTITY_STATUS,
Server.ENTITY_POSITION_SYNC}) {
listenedPackets[packet.ordinal()] = true;
}
}
@ -177,8 +178,8 @@ public class PacketListenerViewSelfDisguise extends SimplePacketListenerAbstract
}
} else if (event.getPacketType() == Server.ATTACH_ENTITY || event.getPacketType() == Server.ENTITY_RELATIVE_MOVE ||
event.getPacketType() == Server.ENTITY_RELATIVE_MOVE_AND_ROTATION || event.getPacketType() == Server.ENTITY_HEAD_LOOK ||
event.getPacketType() == Server.ENTITY_TELEPORT || event.getPacketType() == Server.ENTITY_ROTATION ||
event.getPacketType() == Server.ENTITY_EQUIPMENT) {
event.getPacketType() == Server.ENTITY_POSITION_SYNC || event.getPacketType() == Server.ENTITY_TELEPORT ||
event.getPacketType() == Server.ENTITY_ROTATION || event.getPacketType() == Server.ENTITY_EQUIPMENT) {
event.setCancelled(true);
} else if (event.getPacketType() == Server.ENTITY_STATUS) {
if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() &&