From c606119c1d1c4de372a069ff060aecb0812f5321 Mon Sep 17 00:00:00 2001 From: Redned Date: Sat, 14 Sep 2019 23:19:13 -0500 Subject: [PATCH] Fix entities glitching out and teleporting to the wrong location --- README.md | 2 +- .../org/geysermc/connector/entity/Entity.java | 11 +++- .../network/translators/TranslatorsInit.java | 3 + .../entity/JavaEntityAnimationTranslator.java | 64 +++++++++++++++++++ .../entity/JavaEntityDestroyTranslator.java | 1 - 5 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityAnimationTranslator.java diff --git a/README.md b/README.md index 5949f8b50..c51d73b28 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Links: - [x] NBT data - [ ] Inventory movement (transactions) - [x] Player movement support -- [x] Entity support (Experimental) +- [x] Entity support - [ ] Chunks (Experimental) - [x] Block translation (may be missing mappings) - [ ] Block updates diff --git a/connector/src/main/java/org/geysermc/connector/entity/Entity.java b/connector/src/main/java/org/geysermc/connector/entity/Entity.java index b93cd79fc..5da206992 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/Entity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/Entity.java @@ -106,6 +106,8 @@ public class Entity { RemoveEntityPacket removeEntityPacket = new RemoveEntityPacket(); removeEntityPacket.setUniqueEntityId(geyserId); session.getUpstream().sendPacket(removeEntityPacket); + + valid = false; } public void moveRelative(double relX, double relY, double relZ, float pitch, float yaw) { @@ -113,11 +115,11 @@ public class Entity { } public void moveRelative(double relX, double relY, double relZ, Vector3f rotation) { - if (relX == 0 && relY != 0 && relZ != 0 && position.getX() == 0 && position.getY() == 0) + if (relX == 0 && relY == 0 && relZ == 0 && position.getX() == 0 && position.getY() == 0) return; this.rotation = rotation; - this.position = new Vector3f(position.getX() + relX, position.getX() + relY, position.getX() + relZ); + this.position = new Vector3f(position.getX() + relX, position.getY() + relY, position.getZ() + relZ); this.movePending = true; } @@ -126,7 +128,7 @@ public class Entity { } public void moveAbsolute(Vector3f position, Vector3f rotation) { - if (position.getX() == 0 && position.getX() != 0 && position.getX() != 0 && rotation.getX() == 0 && rotation.getY() == 0) + if (position.getX() == 0 && position.getX() == 0 && position.getX() == 0 && rotation.getX() == 0 && rotation.getY() == 0) return; this.position = position; @@ -138,6 +140,9 @@ public class Entity { public EntityDataDictionary getMetadata() { EntityFlags flags = new EntityFlags(); flags.setFlag(EntityFlag.HAS_GRAVITY, true); + flags.setFlag(EntityFlag.HAS_COLLISION, true); + flags.setFlag(EntityFlag.CAN_SHOW_NAME, true); + flags.setFlag(EntityFlag.NO_AI, false); EntityDataDictionary dictionary = new EntityDataDictionary(); dictionary.put(EntityData.NAMETAG, ""); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java b/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java index 448acd6aa..d068a8291 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java @@ -30,6 +30,7 @@ import com.github.steveice10.mc.protocol.packet.ingame.server.ServerChatPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.ServerJoinGamePacket; import com.github.steveice10.mc.protocol.packet.ingame.server.ServerRespawnPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.ServerTitlePacket; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityAnimationPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityDestroyPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityHeadLookPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityMetadataPacket; @@ -78,6 +79,7 @@ import org.geysermc.connector.network.translators.item.ItemTranslator; import org.geysermc.connector.network.translators.java.JavaChatTranslator; import org.geysermc.connector.network.translators.java.JavaJoinGameTranslator; import org.geysermc.connector.network.translators.java.JavaRespawnTranslator; +import org.geysermc.connector.network.translators.java.entity.JavaEntityAnimationTranslator; import org.geysermc.connector.network.translators.java.entity.JavaEntityDestroyTranslator; import org.geysermc.connector.network.translators.java.entity.JavaEntityHeadLookTranslator; import org.geysermc.connector.network.translators.java.entity.JavaEntityMetadataTranslator; @@ -143,6 +145,7 @@ public class TranslatorsInit { Registry.registerJava(ServerRespawnPacket.class, new JavaRespawnTranslator()); Registry.registerJava(ServerSpawnPositionPacket.class, new JavaSpawnPositionTranslator()); + Registry.registerJava(ServerEntityAnimationPacket.class, new JavaEntityAnimationTranslator()); Registry.registerJava(ServerEntityPositionPacket.class, new JavaEntityPositionTranslator()); Registry.registerJava(ServerEntityPositionRotationPacket.class, new JavaEntityPositionRotationTranslator()); Registry.registerJava(ServerEntityTeleportPacket.class, new JavaEntityTeleportTranslator()); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityAnimationTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityAnimationTranslator.java new file mode 100644 index 000000000..021a925da --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityAnimationTranslator.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2019 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.connector.network.translators.java.entity; + +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityAnimationPacket; +import com.nukkitx.protocol.bedrock.packet.AnimatePacket; +import org.geysermc.connector.entity.Entity; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.PacketTranslator; + +public class JavaEntityAnimationTranslator extends PacketTranslator { + + @Override + public void translate(ServerEntityAnimationPacket packet, GeyserSession session) { + Entity entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId()); + if (packet.getEntityId() == session.getPlayerEntity().getEntityId()) { + entity = session.getPlayerEntity(); + } + if (entity == null) + return; + + AnimatePacket animatePacket = new AnimatePacket(); + animatePacket.setRuntimeEntityId(entity.getGeyserId()); + switch (packet.getAnimation()) { + case SWING_ARM: + animatePacket.setAction(AnimatePacket.Action.SWING_ARM); + break; + case CRITICAL_HIT: + animatePacket.setAction(AnimatePacket.Action.CRITICAL_HIT); + break; + case ENCHANTMENT_CRITICAL_HIT: + animatePacket.setAction(AnimatePacket.Action.MAGIC_CRITICAL_HIT); + break; + case LEAVE_BED: + animatePacket.setAction(AnimatePacket.Action.WAKE_UP); + break; + } + + session.getUpstream().sendPacket(animatePacket); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityDestroyTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityDestroyTranslator.java index ed741f95a..55da3c733 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityDestroyTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityDestroyTranslator.java @@ -26,7 +26,6 @@ package org.geysermc.connector.network.translators.java.entity; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityDestroyPacket; -import com.nukkitx.protocol.bedrock.packet.RemoveEntityPacket; import org.geysermc.connector.entity.Entity; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator;