From 65aaa074930289be839d2c25fe7eff7a8a8c1ffd Mon Sep 17 00:00:00 2001 From: David Choo Date: Thu, 23 Dec 2021 19:49:58 -0500 Subject: [PATCH] Fix sneaking speed adjustment and bounding box (#2728) * Fix sneaking speed adjustment and bounding box Also remove redundant session variable in SessionPlayerEntity * Add comment to valid field * Hopefully fix crawling after swimming in water --- .../geyser/entity/EntityDefinitions.java | 2 +- .../geysermc/geyser/entity/type/Entity.java | 11 +++++--- .../type/player/SessionPlayerEntity.java | 26 +++++++++++++------ .../geyser/session/GeyserSession.java | 18 +++++-------- .../BedrockAdventureSettingsTranslator.java | 5 ---- 5 files changed, 33 insertions(+), 29 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/entity/EntityDefinitions.java b/core/src/main/java/org/geysermc/geyser/entity/EntityDefinitions.java index 999ce4fa8..f35e106d2 100644 --- a/core/src/main/java/org/geysermc/geyser/entity/EntityDefinitions.java +++ b/core/src/main/java/org/geysermc/geyser/entity/EntityDefinitions.java @@ -181,7 +181,7 @@ public final class EntityDefinitions { .addTranslator(MetadataType.BOOLEAN, Entity::setDisplayNameVisible) .addTranslator(MetadataType.BOOLEAN, (entity, entityMetadata) -> entity.setFlag(EntityFlag.SILENT, ((BooleanEntityMetadata) entityMetadata).getPrimitiveValue())) .addTranslator(MetadataType.BOOLEAN, Entity::setGravity) - .addTranslator(MetadataType.POSE, Entity::setPose) + .addTranslator(MetadataType.POSE, (entity, entityMetadata) -> entity.setPose(entityMetadata.getValue())) .addTranslator(MetadataType.INT, Entity::setFreezing) .build(); diff --git a/core/src/main/java/org/geysermc/geyser/entity/type/Entity.java b/core/src/main/java/org/geysermc/geyser/entity/type/Entity.java index bb09490a5..5d26b25b0 100644 --- a/core/src/main/java/org/geysermc/geyser/entity/type/Entity.java +++ b/core/src/main/java/org/geysermc/geyser/entity/type/Entity.java @@ -81,6 +81,9 @@ public class Entity { protected EntityDefinition definition; + /** + * Indicates if the entity has been initialized and spawned + */ protected boolean valid; /* Metadata about this specific entity */ @@ -372,9 +375,7 @@ public class Entity { /** * Usually used for bounding box and not animation. */ - public void setPose(EntityMetadata entityMetadata) { - Pose pose = entityMetadata.getValue(); - + public void setPose(Pose pose) { setFlag(EntityFlag.SLEEPING, pose.equals(Pose.SLEEPING)); // Triggered when crawling setFlag(EntityFlag.SWIMMING, pose.equals(Pose.SWIMMING)); @@ -390,13 +391,15 @@ public class Entity { setBoundingBoxWidth(definition.width()); } - public void setBoundingBoxHeight(float height) { + public boolean setBoundingBoxHeight(float height) { if (height != boundingBoxHeight) { boundingBoxHeight = height; dirtyMetadata.put(EntityData.BOUNDING_BOX_HEIGHT, boundingBoxHeight); updatePassengerOffsets(); + return true; } + return false; } public void setBoundingBoxWidth(float width) { diff --git a/core/src/main/java/org/geysermc/geyser/entity/type/player/SessionPlayerEntity.java b/core/src/main/java/org/geysermc/geyser/entity/type/player/SessionPlayerEntity.java index 47a254fa8..b61110e6c 100644 --- a/core/src/main/java/org/geysermc/geyser/entity/type/player/SessionPlayerEntity.java +++ b/core/src/main/java/org/geysermc/geyser/entity/type/player/SessionPlayerEntity.java @@ -73,13 +73,10 @@ public class SessionPlayerEntity extends PlayerEntity { */ private int fakeTradeXp; - private final GeyserSession session; - public SessionPlayerEntity(GeyserSession session) { super(session, -1, 1, new GameProfile(UUID.randomUUID(), "unknown"), Vector3f.ZERO, Vector3f.ZERO, 0, 0, 0); valid = true; - this.session = session; } @Override @@ -95,7 +92,7 @@ public class SessionPlayerEntity extends PlayerEntity { @Override public void setPosition(Vector3f position) { - if (session != null) { // null during entity initialization + if (valid) { // Don't update during session init session.getCollisionManager().updatePlayerBoundingBox(position); } super.setPosition(position); @@ -117,15 +114,28 @@ public class SessionPlayerEntity extends PlayerEntity { super.setFlags(entityMetadata); // Swimming/crawling is controlled by the Java server boolean swimming = (entityMetadata.getPrimitiveValue() & 0x10) == 0x10; - session.setSwimming(swimming); + if (swimming) { + setPose(Pose.SWIMMING); + } session.setSwimmingInWater(swimming && getFlag(EntityFlag.SPRINTING)); refreshSpeed = true; } @Override - public void setPose(EntityMetadata entityMetadata) { - super.setPose(entityMetadata); - session.setPose(entityMetadata.getValue()); + public boolean setBoundingBoxHeight(float height) { + if (super.setBoundingBoxHeight(height)) { + if (valid) { // Don't update during session init + session.getCollisionManager().updatePlayerBoundingBox(); + } + return true; + } + return false; + } + + @Override + public void setPose(Pose pose) { + super.setPose(pose); + session.setPose(pose); refreshSpeed = true; } diff --git a/core/src/main/java/org/geysermc/geyser/session/GeyserSession.java b/core/src/main/java/org/geysermc/geyser/session/GeyserSession.java index 4cd9cd69f..02a0814d8 100644 --- a/core/src/main/java/org/geysermc/geyser/session/GeyserSession.java +++ b/core/src/main/java/org/geysermc/geyser/session/GeyserSession.java @@ -1059,18 +1059,14 @@ public class GeyserSession implements GeyserConnection, CommandSender { } private void setSneakingPose(boolean sneaking) { - this.pose = sneaking ? Pose.SNEAKING : Pose.STANDING; - playerEntity.setBoundingBoxHeight(sneaking ? 1.5f : playerEntity.getDefinition().height()); + if (this.pose == Pose.SNEAKING && !sneaking) { + this.pose = Pose.STANDING; + playerEntity.setBoundingBoxHeight(playerEntity.getDefinition().height()); + } else if (sneaking) { + this.pose = Pose.SNEAKING; + playerEntity.setBoundingBoxHeight(1.5f); + } playerEntity.setFlag(EntityFlag.SNEAKING, sneaking); - - collisionManager.updatePlayerBoundingBox(); - } - - public void setSwimming(boolean swimming) { - this.pose = swimming ? Pose.SWIMMING : Pose.STANDING; - playerEntity.setBoundingBoxHeight(swimming ? 0.6f : playerEntity.getDefinition().height()); - playerEntity.setFlag(EntityFlag.SWIMMING, swimming); - playerEntity.updateBedrockMetadata(); } public void setFlying(boolean flying) { diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockAdventureSettingsTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockAdventureSettingsTranslator.java index d4de9e22a..f2edb6fa0 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockAdventureSettingsTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockAdventureSettingsTranslator.java @@ -49,10 +49,5 @@ public class BedrockAdventureSettingsTranslator extends PacketTranslator