From d7eff518058c6f0697d3d79ada8ba3de52d24091 Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Sat, 27 Apr 2019 16:23:24 -0300 Subject: [PATCH 1/3] Not tested pose handling --- .../blockconnections/ConnectionData.java | 1 - .../MetadataRewriter.java | 48 +++++++++++++++++-- .../packets/EntityPackets.java | 6 +++ .../storage/EntityTracker.java | 44 +++++++++++++++++ 4 files changed, 95 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java index 287ed9105..574b071b6 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java @@ -192,7 +192,6 @@ public class ConnectionData { } } } - updateChunkSectionNeighbours(user, chunk.getX(), chunk.getZ(), i); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java index c136c02f6..a2d37e355 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java @@ -47,13 +47,24 @@ public class MetadataRewriter { if (type.isOrHasParent(Entity1_14Types.EntityType.ABSTRACT_INSENTIENT)) { if (metadata.getId() == 13) { - tracker.setInsentientData(entityId, (byte) ((((Number)metadata.getValue()).byteValue() & ~0x4) + tracker.setInsentientData(entityId, (byte) ((((Number) metadata.getValue()).byteValue() & ~0x4) | (tracker.getInsentientData(entityId) & 0x4))); // New attacking metadata metadata.setValue(tracker.getInsentientData(entityId)); } } - if (type.isOrHasParent(Entity1_14Types.EntityType.ZOMBIE)) { + if (type.isOrHasParent(Entity1_14Types.EntityType.PLAYER)) { + if (metadata.getId() == 0) { + byte flags = ((Number) metadata.getValue()).byteValue(); + // Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize + tracker.setEntityFlags(entityId, flags); + } else if (metadata.getId() == 7) { + tracker.setRiptide(entityId, (((Number) metadata.getValue()).byteValue() & 0x4) != 0); + } + if (metadata.getId() == 0 || metadata.getId() == 7) { + metadatas.add(new Metadata(6, MetaType1_14.Pose, recalculatePlayerPose(entityId, tracker))); + } + } else if (type.isOrHasParent(Entity1_14Types.EntityType.ZOMBIE)) { if (metadata.getId() == 16) { tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | ((boolean) metadata.getValue() ? 0x4 : 0))); // New attacking @@ -107,7 +118,8 @@ public class MetadataRewriter { } } else if (type.is(Entity1_14Types.EntityType.FIREWORKS_ROCKET)) { if (metadata.getId() == 8) { - if (metadata.getValue().equals(0)) metadata.setValue(null); // https://bugs.mojang.com/browse/MC-111480 + if (metadata.getValue().equals(0)) + metadata.setValue(null); // https://bugs.mojang.com/browse/MC-111480 metadata.setMetaType(MetaType1_14.OptVarInt); } } else if (type.isOrHasParent(Entity1_14Types.EntityType.ABSTRACT_SKELETON)) { @@ -150,6 +162,14 @@ public class MetadataRewriter { } } + private static boolean isSneaking(byte flags) { + return (flags & 0x2) != 0; + } + + private static boolean isSwimming(byte flags) { + return (flags & 0x10) != 0; + } + private static int getNewProfessionId(int old) { // profession -> career switch (old) { @@ -170,6 +190,28 @@ public class MetadataRewriter { } } + private static boolean isFallFlying(int entityFlags) { + return (entityFlags & 0x80) != 0; + } + + private static int recalculatePlayerPose(int entityId, EntityTracker tracker) { + byte flags = tracker.getEntityFlags(entityId); + // Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize + int pose = 0; // standing + if (isFallFlying(flags)) { + pose = 1; + } else if (tracker.isSleeping(entityId)) { + pose = 2; + } else if (isSwimming(flags)) { + pose = 3; + } else if (tracker.isRiptide(entityId)) { + pose = 4; + } else if (isSneaking(flags)) { + pose = 5; + } + return pose; + } + public static int getNewParticleId(int id) { if (id >= 10) { id += 2; // new lava drips 10, 11 diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java index 72a1a959a..a63eb8b21 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java @@ -195,6 +195,9 @@ public class EntityPackets { public void handle(PacketWrapper wrapper) throws Exception { short animation = wrapper.passthrough(Type.UNSIGNED_BYTE); if (animation == 2) { //Leave bed + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + tracker.setSleeping(wrapper.get(Type.VAR_INT, 0), false); + PacketWrapper metadataPacket = wrapper.create(0x43); metadataPacket.write(Type.VAR_INT, wrapper.get(Type.VAR_INT, 0)); List metadataList = new LinkedList<>(); @@ -215,6 +218,9 @@ public class EntityPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + tracker.setSleeping(wrapper.get(Type.VAR_INT, 0), true); + Position position = wrapper.read(Type.POSITION); List metadataList = new LinkedList<>(); metadataList.add(new Metadata(12, MetaType1_14.OptPosition, position)); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker.java index d6ae09aa6..ace6ba6e7 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker.java @@ -14,6 +14,9 @@ import java.util.concurrent.ConcurrentHashMap; public class EntityTracker extends StoredObject implements ExternalJoinGameListener { private final Map clientEntityTypes = new ConcurrentHashMap<>(); private final Map insentientData = new ConcurrentHashMap<>(); + // 0x1 = sleeping, 0x2 = riptide + private final Map sleepingAndRiptideData = new ConcurrentHashMap<>(); + private final Map playerEntityFlags = new ConcurrentHashMap<>(); @Getter @Setter private int latestTradeWindowId; @@ -31,6 +34,8 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe public void removeEntity(int entityId) { clientEntityTypes.remove(entityId); insentientData.remove(entityId); + sleepingAndRiptideData.remove(entityId); + playerEntityFlags.remove(entityId); } public void addEntity(int entityId, Entity1_14Types.EntityType type) { @@ -46,6 +51,37 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe insentientData.put(entity, value); } + private static byte zeroIfNull(Byte val) { + if (val == null) return 0; + return val; + } + + public boolean isSleeping(int player) { + return (zeroIfNull(sleepingAndRiptideData.get(player)) & 1) != 0; + } + + public void setSleeping(int player, boolean value) { + byte newValue = (byte) ((zeroIfNull(sleepingAndRiptideData.get(player)) & ~1) | (value ? 1 : 0)); + if (newValue == 0) { + sleepingAndRiptideData.remove(player); + } else { + sleepingAndRiptideData.put(player, newValue); + } + } + + public boolean isRiptide(int player) { + return (zeroIfNull(sleepingAndRiptideData.get(player)) & 2) != 0; + } + + public void setRiptide(int player, boolean value) { + byte newValue = (byte) ((zeroIfNull(sleepingAndRiptideData.get(player)) & ~2) | (value ? 2 : 0)); + if (newValue == 0) { + sleepingAndRiptideData.remove(player); + } else { + sleepingAndRiptideData.put(player, newValue); + } + } + public boolean has(int entityId) { return clientEntityTypes.containsKey(entityId); } @@ -58,4 +94,12 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe public void onExternalJoinGame(int playerEntityId) { clientEntityTypes.put(playerEntityId, Entity1_14Types.EntityType.PLAYER); } + + public byte getEntityFlags(int player) { + return zeroIfNull(playerEntityFlags.get(player)); + } + + public void setEntityFlags(int player, byte data) { + playerEntityFlags.put(player, data); + } } From 61b5c90aa7ef8acc88dec4b467eb535cee48c943 Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Sat, 27 Apr 2019 17:21:01 -0300 Subject: [PATCH 2/3] Recalculate pose when using or leaving bed --- .../protocol1_14to1_13_2/MetadataRewriter.java | 2 +- .../protocol1_14to1_13_2/packets/EntityPackets.java | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java index a2d37e355..a66cc1ab4 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java @@ -194,7 +194,7 @@ public class MetadataRewriter { return (entityFlags & 0x80) != 0; } - private static int recalculatePlayerPose(int entityId, EntityTracker tracker) { + public static int recalculatePlayerPose(int entityId, EntityTracker tracker) { byte flags = tracker.getEntityFlags(entityId); // Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize int pose = 0; // standing diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java index a63eb8b21..b47710a93 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java @@ -196,11 +196,13 @@ public class EntityPackets { short animation = wrapper.passthrough(Type.UNSIGNED_BYTE); if (animation == 2) { //Leave bed EntityTracker tracker = wrapper.user().get(EntityTracker.class); - tracker.setSleeping(wrapper.get(Type.VAR_INT, 0), false); + int entityId = wrapper.get(Type.VAR_INT, 0); + tracker.setSleeping(entityId, false); PacketWrapper metadataPacket = wrapper.create(0x43); - metadataPacket.write(Type.VAR_INT, wrapper.get(Type.VAR_INT, 0)); + metadataPacket.write(Type.VAR_INT, entityId); List metadataList = new LinkedList<>(); + metadataList.add(new Metadata(6, MetaType1_14.Pose, MetadataRewriter.recalculatePlayerPose(entityId, tracker))); metadataList.add(new Metadata(12, MetaType1_14.OptPosition, null)); metadataPacket.write(Types1_14.METADATA_LIST, metadataList); metadataPacket.send(Protocol1_14To1_13_2.class); @@ -219,11 +221,13 @@ public class EntityPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { EntityTracker tracker = wrapper.user().get(EntityTracker.class); - tracker.setSleeping(wrapper.get(Type.VAR_INT, 0), true); + int entityId = wrapper.get(Type.VAR_INT, 0); + tracker.setSleeping(entityId, true); Position position = wrapper.read(Type.POSITION); List metadataList = new LinkedList<>(); metadataList.add(new Metadata(12, MetaType1_14.OptPosition, position)); + metadataList.add(new Metadata(6, MetaType1_14.Pose, MetadataRewriter.recalculatePlayerPose(entityId, tracker))); wrapper.write(Types1_14.METADATA_LIST, metadataList); } }); From 6d8a763f4c3205e415e7f854ac610e89668e9c8c Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Sat, 27 Apr 2019 17:39:45 -0300 Subject: [PATCH 3/3] Don't send pose to the client entity --- .../MetadataRewriter.java | 20 ++++++++++--------- .../packets/EntityPackets.java | 8 ++++++-- .../packets/WorldPackets.java | 4 +++- .../storage/EntityTracker.java | 4 ++++ 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java index a66cc1ab4..52899f59b 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java @@ -54,15 +54,17 @@ public class MetadataRewriter { } if (type.isOrHasParent(Entity1_14Types.EntityType.PLAYER)) { - if (metadata.getId() == 0) { - byte flags = ((Number) metadata.getValue()).byteValue(); - // Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize - tracker.setEntityFlags(entityId, flags); - } else if (metadata.getId() == 7) { - tracker.setRiptide(entityId, (((Number) metadata.getValue()).byteValue() & 0x4) != 0); - } - if (metadata.getId() == 0 || metadata.getId() == 7) { - metadatas.add(new Metadata(6, MetaType1_14.Pose, recalculatePlayerPose(entityId, tracker))); + if (entityId != tracker.getClientEntityId()) { + if (metadata.getId() == 0) { + byte flags = ((Number) metadata.getValue()).byteValue(); + // Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize + tracker.setEntityFlags(entityId, flags); + } else if (metadata.getId() == 7) { + tracker.setRiptide(entityId, (((Number) metadata.getValue()).byteValue() & 0x4) != 0); + } + if (metadata.getId() == 0 || metadata.getId() == 7) { + metadatas.add(new Metadata(6, MetaType1_14.Pose, recalculatePlayerPose(entityId, tracker))); + } } } else if (type.isOrHasParent(Entity1_14Types.EntityType.ZOMBIE)) { if (metadata.getId() == 16) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java index b47710a93..537455bdb 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java @@ -202,7 +202,9 @@ public class EntityPackets { PacketWrapper metadataPacket = wrapper.create(0x43); metadataPacket.write(Type.VAR_INT, entityId); List metadataList = new LinkedList<>(); - metadataList.add(new Metadata(6, MetaType1_14.Pose, MetadataRewriter.recalculatePlayerPose(entityId, tracker))); + if (tracker.getClientEntityId() != entityId) { + metadataList.add(new Metadata(6, MetaType1_14.Pose, MetadataRewriter.recalculatePlayerPose(entityId, tracker))); + } metadataList.add(new Metadata(12, MetaType1_14.OptPosition, null)); metadataPacket.write(Types1_14.METADATA_LIST, metadataList); metadataPacket.send(Protocol1_14To1_13_2.class); @@ -227,7 +229,9 @@ public class EntityPackets { Position position = wrapper.read(Type.POSITION); List metadataList = new LinkedList<>(); metadataList.add(new Metadata(12, MetaType1_14.OptPosition, position)); - metadataList.add(new Metadata(6, MetaType1_14.Pose, MetadataRewriter.recalculatePlayerPose(entityId, tracker))); + if (tracker.getClientEntityId() != entityId) { + metadataList.add(new Metadata(6, MetaType1_14.Pose, MetadataRewriter.recalculatePlayerPose(entityId, tracker))); + } wrapper.write(Types1_14.METADATA_LIST, metadataList); } }); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java index 731d171b6..9095651bf 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java @@ -302,7 +302,9 @@ public class WorldPackets { Entity1_14Types.EntityType entType = Entity1_14Types.EntityType.PLAYER; // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + tracker.addEntity(entityId, entType); + tracker.setClientEntityId(entityId); } }); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker.java index ace6ba6e7..0c8768098 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker.java @@ -22,6 +22,9 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe private int latestTradeWindowId; @Getter @Setter + private int clientEntityId; + @Getter + @Setter private boolean forceSendCenterChunk = true; @Getter @Setter @@ -92,6 +95,7 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe @Override public void onExternalJoinGame(int playerEntityId) { + clientEntityId = playerEntityId; clientEntityTypes.put(playerEntityId, Entity1_14Types.EntityType.PLAYER); }