From ee85abf34c18df9ea70e5b45e57fd3b75f798d54 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Sat, 15 Feb 2020 17:39:34 -0600 Subject: [PATCH] Fix player movement not being visible, movement bugs and laggy entities (Addresses #74, #109) --- .../org/geysermc/connector/entity/Entity.java | 44 +++++++++++-------- .../connector/entity/PlayerEntity.java | 35 +++++++++++++++ .../network/session/cache/EntityCache.java | 1 - .../bedrock/BedrockMovePlayerTranslator.java | 9 ++-- .../entity/JavaEntityHeadLookTranslator.java | 23 +++++++--- .../JavaEntityPositionRotationTranslator.java | 14 +----- .../entity/JavaEntityPositionTranslator.java | 16 +------ .../entity/JavaEntityRotationTranslator.java | 14 ++++-- .../entity/JavaEntityTeleportTranslator.java | 16 +------ .../JavaPlayerPositionRotationTranslator.java | 16 +------ 10 files changed, 97 insertions(+), 91 deletions(-) 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 80b477d5..7c72368f 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/Entity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/Entity.java @@ -65,7 +65,6 @@ public class Entity { protected Vector3f rotation; protected float scale = 1; - protected boolean movePending; protected EntityType entityType; @@ -79,14 +78,14 @@ public class Entity { this.entityId = entityId; this.geyserId = geyserId; this.entityType = entityType; - this.position = position; this.motion = motion; this.rotation = rotation; this.valid = false; - this.movePending = false; this.dimension = 0; + setPosition(position); + metadata.put(EntityData.SCALE, 1f); metadata.put(EntityData.MAX_AIR, (short) 400); metadata.put(EntityData.AIR, (short) 0); @@ -132,25 +131,40 @@ public class Entity { return true; } - public void moveRelative(double relX, double relY, double relZ, float yaw, float pitch) { - moveRelative(relX, relY, relZ, Vector3f.from(yaw, pitch, yaw)); + public void moveRelative(GeyserSession session, double relX, double relY, double relZ, float yaw, float pitch, boolean isOnGround) { + moveRelative(session, relX, relY, relZ, Vector3f.from(yaw, pitch, yaw), isOnGround); } - public void moveRelative(double relX, double relY, double relZ, Vector3f rotation) { + public void moveRelative(GeyserSession session, double relX, double relY, double relZ, Vector3f rotation, boolean isOnGround) { setRotation(rotation); this.position = Vector3f.from(position.getX() + relX, position.getY() + relY, position.getZ() + relZ); - this.movePending = true; + + MoveEntityAbsolutePacket moveEntityPacket = new MoveEntityAbsolutePacket(); + moveEntityPacket.setRuntimeEntityId(geyserId); + moveEntityPacket.setPosition(position); + moveEntityPacket.setRotation(getBedrockRotation()); + moveEntityPacket.setOnGround(isOnGround); + moveEntityPacket.setTeleported(false); + + session.getUpstream().sendPacket(moveEntityPacket); } - public void moveAbsolute(Vector3f position, float yaw, float pitch) { - moveAbsolute(position, Vector3f.from(yaw, pitch, yaw)); + public void moveAbsolute(GeyserSession session, Vector3f position, float yaw, float pitch, boolean isOnGround) { + moveAbsolute(session, position, Vector3f.from(yaw, pitch, yaw), isOnGround); } - public void moveAbsolute(Vector3f position, Vector3f rotation) { + public void moveAbsolute(GeyserSession session, Vector3f position, Vector3f rotation, boolean isOnGround) { setPosition(position); setRotation(rotation); - this.movePending = true; + MoveEntityAbsolutePacket moveEntityPacket = new MoveEntityAbsolutePacket(); + moveEntityPacket.setRuntimeEntityId(geyserId); + moveEntityPacket.setPosition(position); + moveEntityPacket.setRotation(getBedrockRotation()); + moveEntityPacket.setOnGround(isOnGround); + moveEntityPacket.setTeleported(false); + + session.getUpstream().sendPacket(moveEntityPacket); } public void updateBedrockAttributes(GeyserSession session) { @@ -216,14 +230,6 @@ public class Entity { session.getUpstream().sendPacket(entityDataPacket); } - public void setPosition(Vector3f position) { - if (is(PlayerEntity.class)) { - this.position = position.add(0, entityType.getOffset(), 0); - return; - } - this.position = position; - } - /** * x = Pitch, y = HeadYaw, z = Yaw */ diff --git a/connector/src/main/java/org/geysermc/connector/entity/PlayerEntity.java b/connector/src/main/java/org/geysermc/connector/entity/PlayerEntity.java index 120b49ef..49af6554 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/PlayerEntity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/PlayerEntity.java @@ -30,6 +30,7 @@ import com.nukkitx.math.vector.Vector3f; import com.nukkitx.protocol.bedrock.data.CommandPermission; import com.nukkitx.protocol.bedrock.data.PlayerPermission; import com.nukkitx.protocol.bedrock.packet.AddPlayerPacket; +import com.nukkitx.protocol.bedrock.packet.MovePlayerPacket; import com.nukkitx.protocol.bedrock.packet.PlayerListPacket; import lombok.Getter; import lombok.Setter; @@ -115,4 +116,38 @@ public class PlayerEntity extends LivingEntity { }); } } + + @Override + public void moveAbsolute(GeyserSession session, Vector3f position, Vector3f rotation, boolean isOnGround) { + setPosition(position); + setRotation(rotation); + + MovePlayerPacket movePlayerPacket = new MovePlayerPacket(); + movePlayerPacket.setRuntimeEntityId(geyserId); + movePlayerPacket.setPosition(this.position); + movePlayerPacket.setRotation(getBedrockRotation()); + movePlayerPacket.setOnGround(isOnGround); + movePlayerPacket.setMode(MovePlayerPacket.Mode.NORMAL); + + session.getUpstream().sendPacket(movePlayerPacket); + } + + @Override + public void moveRelative(GeyserSession session, double relX, double relY, double relZ, Vector3f rotation, boolean isOnGround) { + setRotation(rotation); + this.position = Vector3f.from(position.getX() + relX, position.getY() + relY, position.getZ() + relZ); + + MovePlayerPacket movePlayerPacket = new MovePlayerPacket(); + movePlayerPacket.setRuntimeEntityId(geyserId); + movePlayerPacket.setPosition(position); + movePlayerPacket.setRotation(getBedrockRotation()); + movePlayerPacket.setOnGround(isOnGround); + movePlayerPacket.setMode(MovePlayerPacket.Mode.NORMAL); + session.getUpstream().sendPacket(movePlayerPacket); + } + + @Override + public void setPosition(Vector3f position) { + this.position = position.add(0, entityType.getOffset(), 0); + } } diff --git a/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java b/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java index 2834cb90..bbd714e2 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java @@ -55,7 +55,6 @@ public class EntityCache { } public void spawnEntity(Entity entity) { - entity.moveAbsolute(entity.getPosition(), entity.getRotation().getX(), entity.getRotation().getY()); cacheEntity(entity); entity.spawnEntity(session); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockMovePlayerTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockMovePlayerTranslator.java index 37b517d3..d89060f6 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockMovePlayerTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockMovePlayerTranslator.java @@ -61,8 +61,7 @@ public class BedrockMovePlayerTranslator extends PacketTranslator