Skip to content

Commit

Permalink
Fix entities desyncing in 1.21.3
Browse files Browse the repository at this point in the history
  • Loading branch information
libraryaddict committed Nov 19, 2024
1 parent 852ce71 commit 1739ef5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand All @@ -27,6 +28,7 @@
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;
Expand Down Expand Up @@ -528,6 +530,14 @@ public static void adjustNamePositions(Disguise disguise, LibsPackets<? extends
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();

Expand All @@ -551,7 +561,6 @@ public static void adjustNamePositions(Disguise disguise, LibsPackets<? extends
PacketWrapper cloned;

if (packet instanceof WrapperPlayServerEntityTeleport) {
// TODO Handle if this is a vehicle movement
WrapperPlayServerEntityTeleport tele = (WrapperPlayServerEntityTeleport) packet;

cloned = new WrapperPlayServerEntityTeleport(standId,
Expand All @@ -565,6 +574,11 @@ public static void adjustNamePositions(Disguise disguise, LibsPackets<? extends
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());
Expand Down Expand Up @@ -3063,6 +3077,8 @@ public static PacketWrapper constructWrapper(PacketPlaySendEvent event) {
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");
}
Expand Down Expand Up @@ -3113,6 +3129,8 @@ public static Integer getEntityId(PacketWrapper wrapper) {
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");
}
Expand Down Expand Up @@ -3199,6 +3217,10 @@ public static void writeSelfDisguiseId(int playerId, PacketWrapper wrapper) {
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());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public static void setupMainPacketsListener() {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -202,6 +205,13 @@ private void handleMovement(Disguise disguise, LibsPackets<T> packets, Player ob

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());
}
Expand Down Expand Up @@ -238,6 +248,11 @@ private void handleMovement(Disguise disguise, LibsPackets<T> packets, Player ob

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())) {
Expand All @@ -246,6 +261,7 @@ private void handleMovement(Disguise disguise, LibsPackets<T> packets, Player ob

packets.addPacket(packet);
} else if (cloned instanceof WrapperPlayServerEntityTeleport && disguise.getType().isArtDisplay()) {
// TODO Sync too
WrapperPlayServerEntityTeleport tele = (WrapperPlayServerEntityTeleport) cloned;

Location loc = entity.getLocation();
Expand All @@ -270,6 +286,8 @@ private void handleMovement(Disguise disguise, LibsPackets<T> packets, Player ob
((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) {
Expand All @@ -283,18 +301,32 @@ private void handleMovement(Disguise disguise, LibsPackets<T> packets, Player ob
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));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand All @@ -26,6 +27,7 @@
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;
Expand Down Expand Up @@ -250,7 +252,7 @@ public void onPacketPlaySend(PacketPlaySendEvent event) {
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()) {
Expand All @@ -267,9 +269,12 @@ public void onPacketPlaySend(PacketPlaySendEvent event) {
} 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public PacketListenerViewSelfDisguise() {
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;
}
}
Expand Down Expand Up @@ -177,8 +178,8 @@ public void onPacketPlaySend(PacketPlaySendEvent event) {
}
} 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() &&
Expand Down

0 comments on commit 1739ef5

Please sign in to comment.