From d7eff518058c6f0697d3d79ada8ba3de52d24091 Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Sat, 27 Apr 2019 16:23:24 -0300 Subject: [PATCH] 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); + } }