Skip to content

Commit d9e0c6b

Browse files
committed
Improve height / width handling
This will be later reworked to allow setting a height + width combo that accounts for the entity pose. For now, it's a general override
1 parent 0f9ad38 commit d9e0c6b

15 files changed

Lines changed: 87 additions & 65 deletions

File tree

core/src/main/java/org/geysermc/geyser/entity/spawn/EntitySpawnContext.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ public class EntitySpawnContext {
6666
private float yaw;
6767
private float pitch;
6868
private float headYaw;
69-
private float height;
70-
private float width;
7169
private float offset;
7270
private @Nullable Long geyserId;
7371
private @Nullable Collection<Consumer<GeyserEntity>> consumers;
@@ -77,28 +75,27 @@ public class EntitySpawnContext {
7775
});
7876

7977
public EntitySpawnContext(GeyserSession session, EntityTypeDefinition<?> type, int javaId, UUID uuid) {
80-
this(session, type, javaId, uuid, type.defaultBedrockDefinition(), Vector3f.ZERO, Vector3f.ZERO, 0, 0, 0,
81-
type.height(), type.width(), type.offset(), null);
78+
this(session, type, javaId, uuid, type.defaultBedrockDefinition(), Vector3f.ZERO, Vector3f.ZERO, 0, 0, 0, type.offset(), null);
8279
}
8380

84-
public EntitySpawnContext(GeyserSession session, EntityTypeDefinition<?> type, int entityId, float height, float width, long geyserId) {
85-
this(session, type, entityId, null, type.defaultBedrockDefinition(), Vector3f.ZERO, Vector3f.ZERO, 0, 0, 0, height, width, 0, geyserId);
81+
public EntitySpawnContext(GeyserSession session, EntityTypeDefinition<?> type, int entityId, long geyserId) {
82+
this(session, type, entityId, null, type.defaultBedrockDefinition(), Vector3f.ZERO, Vector3f.ZERO, 0, 0, 0, 0, geyserId);
8683
}
8784

8885
public static EntitySpawnContext fromPacket(GeyserSession session, EntityTypeDefinition<?> definition, ClientboundAddEntityPacket packet) {
8986
Vector3f position = Vector3f.from(packet.getX(), packet.getY(), packet.getZ());
9087
Vector3f motion = packet.getMovement().toFloat();
9188
return new EntitySpawnContext(session, definition, packet.getEntityId(), packet.getUuid(), definition.defaultBedrockDefinition(),
92-
position, motion, packet.getYaw(), packet.getPitch(), packet.getHeadYaw(), definition.height(), definition.width(), definition.offset(), null);
89+
position, motion, packet.getYaw(), packet.getPitch(), packet.getHeadYaw(), definition.offset(), null);
9390
}
9491

9592
public static EntitySpawnContext inherited(GeyserSession session, EntityTypeDefinition<?> definition, Entity base, Vector3f position) {
9693
return new EntitySpawnContext(session, definition, 0, null, definition.defaultBedrockDefinition(), position, base.getMotion(), base.getYaw(),
97-
base.getPitch(), base.getHeadYaw(), definition.height(), definition.width(), definition.offset(), null);
94+
base.getPitch(), base.getHeadYaw(), definition.offset(), null);
9895
}
9996

10097
public EntitySpawnContext(GeyserSession session, EntityTypeDefinition<?> definition, int javaId, UUID uuid, BedrockEntityDefinition bedrockEntityDefinition, Vector3f position,
101-
Vector3f motion, float yaw, float pitch, float headYaw, float height, float width, float offset, @Nullable Long geyserId) {
98+
Vector3f motion, float yaw, float pitch, float headYaw, float offset, @Nullable Long geyserId) {
10299
this.session = session;
103100
this.entityTypeDefinition = definition;
104101
this.javaId = javaId;
@@ -109,8 +106,6 @@ public EntitySpawnContext(GeyserSession session, EntityTypeDefinition<?> definit
109106
this.yaw = yaw;
110107
this.pitch = pitch;
111108
this.headYaw = headYaw;
112-
this.height = height;
113-
this.width = width;
114109
this.offset = offset;
115110
this.geyserId = geyserId;
116111
this.consumers = null;

core/src/main/java/org/geysermc/geyser/entity/type/Entity.java

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ public class Entity implements GeyserEntity {
148148
protected boolean silent = false;
149149
/* Metadata end */
150150

151+
// hacky temp solution till i can think of something better
152+
// this would be the height / width of the bounding box regardless of pose etc
153+
private float customBoundingBoxHeight;
154+
private float customBoundingBoxWidth;
155+
151156
protected List<Entity> passengers = Collections.emptyList();
152157
protected Entity vehicle;
153158
/**
@@ -174,11 +179,8 @@ public class Entity implements GeyserEntity {
174179
@Setter(AccessLevel.PROTECTED) // For players
175180
private boolean flagsDirty = false;
176181

177-
@Accessors(fluent = true)
178-
protected float width;
179-
@Accessors(fluent = true)
180-
protected float height;
181182
protected float offset;
183+
protected float scale = 1.0F;
182184

183185
protected final @Nullable GeyserEntityPropertyManager propertyManager;
184186

@@ -194,8 +196,6 @@ public Entity(EntitySpawnContext context) {
194196
this.yaw = context.yaw();
195197
this.pitch = context.pitch();
196198
this.headYaw = context.headYaw();
197-
this.width = context.width();
198-
this.height = context.height();
199199
this.offset = context.offset();
200200
this.valid = false;
201201
this.propertyManager = bedrockDefinition.registeredProperties().isEmpty() ? null : new GeyserEntityPropertyManager(bedrockDefinition.registeredProperties());
@@ -210,12 +210,12 @@ public Entity(EntitySpawnContext context) {
210210
* Called on entity spawn. Used to populate the entity metadata and flags with default values.
211211
*/
212212
protected void initializeMetadata() {
213-
dirtyMetadata.put(EntityDataTypes.WIDTH, width);
214-
dirtyMetadata.put(EntityDataTypes.HEIGHT, height);
215-
dirtyMetadata.put(EntityDataTypes.SCALE, 1f);
213+
dirtyMetadata.put(EntityDataTypes.SCALE, scale);
216214
dirtyMetadata.put(EntityDataTypes.COLOR, (byte) 0);
217215
dirtyMetadata.put(EntityDataTypes.AIR_SUPPLY_MAX, getMaxAir());
218216
setDimensionsFromPose(Pose.STANDING);
217+
dirtyMetadata.put(EntityDataTypes.WIDTH, getBoundingBoxWidth());
218+
dirtyMetadata.put(EntityDataTypes.HEIGHT, getBoundingBoxHeight());
219219
setFlag(EntityFlag.HAS_GRAVITY, true);
220220
setFlag(EntityFlag.HAS_COLLISION, true);
221221
setFlag(EntityFlag.CAN_SHOW_NAME, true);
@@ -656,14 +656,14 @@ public void setPose(Pose pose) {
656656
*/
657657
protected void setDimensionsFromPose(Pose pose) {
658658
// No flexibility options for basic entities
659-
setBoundingBoxHeight(height);
660-
setBoundingBoxWidth(width);
659+
setBoundingBoxHeight(javaTypeDefinition.height());
660+
setBoundingBoxWidth(javaTypeDefinition.width());
661661
}
662662

663663
public boolean setBoundingBoxHeight(float height) {
664664
if (height != boundingBoxHeight) {
665665
boundingBoxHeight = height;
666-
dirtyMetadata.put(EntityDataTypes.HEIGHT, boundingBoxHeight);
666+
dirtyMetadata.put(EntityDataTypes.HEIGHT, getBoundingBoxHeight());
667667

668668
updatePassengerOffsets();
669669
return true;
@@ -674,8 +674,41 @@ public boolean setBoundingBoxHeight(float height) {
674674
public void setBoundingBoxWidth(float width) {
675675
if (width != boundingBoxWidth) {
676676
boundingBoxWidth = width;
677-
dirtyMetadata.put(EntityDataTypes.WIDTH, boundingBoxWidth);
677+
dirtyMetadata.put(EntityDataTypes.WIDTH, getBoundingBoxWidth());
678+
}
679+
}
680+
681+
public float getBoundingBoxHeight() {
682+
if (customBoundingBoxHeight != 0) {
683+
return customBoundingBoxHeight;
684+
}
685+
return boundingBoxHeight;
686+
}
687+
688+
public float getBoundingBoxWidth() {
689+
if (customBoundingBoxWidth != 0) {
690+
return customBoundingBoxWidth;
678691
}
692+
return boundingBoxWidth;
693+
}
694+
695+
public void setCustomBoundingBoxWidth(float width) {
696+
this.customBoundingBoxWidth = width;
697+
dirtyMetadata.put(EntityDataTypes.WIDTH, customBoundingBoxWidth);
698+
}
699+
700+
public void setCustomBoundingBoxHeight(float height) {
701+
this.customBoundingBoxHeight = height;
702+
dirtyMetadata.put(EntityDataTypes.HEIGHT, customBoundingBoxHeight);
703+
}
704+
705+
public void setScale(float scale) {
706+
this.scale = scale;
707+
applyScale();
708+
}
709+
710+
protected void applyScale() {
711+
dirtyMetadata.put(EntityDataTypes.SCALE, scale);
679712
}
680713

681714
/**

core/src/main/java/org/geysermc/geyser/entity/type/LivingEntity.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,6 @@ public class LivingEntity extends Entity implements Tickable {
9494
*/
9595
private boolean isMaxFrozenState = false;
9696

97-
/**
98-
* The base scale entity data, without attributes applied. Used for such cases as baby variants.
99-
*/
100-
@Getter(AccessLevel.NONE)
101-
@Setter(AccessLevel.NONE)
102-
private float scale;
10397
/**
10498
* The scale sent through the Java attributes packet
10599
*/
@@ -340,17 +334,13 @@ public float setFreezing(IntEntityMetadata entityMetadata) {
340334
return freezingPercentage;
341335
}
342336

343-
protected void setScale(float scale) {
344-
this.scale = scale;
345-
applyScale();
346-
}
347-
348337
protected void setAttributeScale(float scale) {
349338
this.attributeScale = MathUtils.clamp(scale, GeyserAttributeType.SCALE.getMinimum(), GeyserAttributeType.SCALE.getMaximum());
350339
applyScale();
351340
}
352341

353-
private void applyScale() {
342+
@Override
343+
protected void applyScale() {
354344
// Take any adjustments Bedrock requires, and compute it alongside the attribute's additional changes
355345
this.dirtyMetadata.put(EntityDataTypes.SCALE, scale * attributeScale);
356346
}

core/src/main/java/org/geysermc/geyser/entity/type/living/AgeableEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public void setBaby(BooleanEntityMetadata entityMetadata) {
4747
setScale(isBaby ? getBabySize() : getAdultSize());
4848
setFlag(EntityFlag.BABY, isBaby);
4949

50-
setBoundingBoxHeight(height * (isBaby ? getBabySize() : getAdultSize()));
51-
setBoundingBoxWidth(width * (isBaby ? getBabySize() : getAdultSize()));
50+
setBoundingBoxHeight(javaTypeDefinition.height() * (isBaby ? getBabySize() : getAdultSize()));
51+
setBoundingBoxWidth(javaTypeDefinition.width() * (isBaby ? getBabySize() : getAdultSize()));
5252
}
5353

5454
/**

core/src/main/java/org/geysermc/geyser/entity/type/living/ArmorStandEntity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ public void setArmorStandFlags(ByteEntityMetadata entityMetadata) {
150150
setBoundingBoxWidth(0.0f);
151151
setBoundingBoxHeight(0.0f);
152152
} else {
153-
setBoundingBoxWidth(width);
154-
setBoundingBoxHeight(height);
153+
setBoundingBoxWidth(javaTypeDefinition.width());
154+
setBoundingBoxHeight(javaTypeDefinition.height());
155155
}
156156

157157
updateMountOffset();
@@ -412,13 +412,13 @@ public float getYOffset() {
412412
if (!positionRequiresOffset || isMarker || secondEntity != null) {
413413
return 0;
414414
}
415-
return height * getScale();
415+
return getBoundingBoxHeight() * getScale();
416416
}
417417

418418
/**
419419
* @return the scale according to Java
420420
*/
421-
private float getScale() {
421+
public float getScale() {
422422
return isSmall ? 0.5f : 1f;
423423
}
424424

core/src/main/java/org/geysermc/geyser/entity/type/living/animal/SnifferEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class SnifferEntity extends AnimalEntity implements Tickable {
5050

5151
public SnifferEntity(EntitySpawnContext context) {
5252
super(context);
53-
diggingHeight = height - 0.4f;
53+
diggingHeight = javaTypeDefinition.height() - 0.4f;
5454
}
5555

5656
@Override
@@ -63,7 +63,7 @@ public void setPose(Pose pose) {
6363
protected void setDimensionsFromPose(Pose pose) {
6464
if (getFlag(EntityFlag.DIGGING)) {
6565
setBoundingBoxHeight(diggingHeight);
66-
setBoundingBoxWidth(width);
66+
setBoundingBoxWidth(javaTypeDefinition.width());
6767
} else {
6868
super.setDimensionsFromPose(pose);
6969
}

core/src/main/java/org/geysermc/geyser/entity/type/living/animal/horse/CamelEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ public void setPose(Pose pose) {
108108
@Override
109109
protected void setDimensionsFromPose(Pose pose) {
110110
if (pose == Pose.SITTING) {
111-
setBoundingBoxHeight(height - SITTING_HEIGHT_DIFFERENCE);
112-
setBoundingBoxWidth(width);
111+
setBoundingBoxHeight(javaTypeDefinition.height() - SITTING_HEIGHT_DIFFERENCE);
112+
setBoundingBoxWidth(javaTypeDefinition.width());
113113
} else {
114114
super.setDimensionsFromPose(pose);
115115
}

core/src/main/java/org/geysermc/geyser/entity/type/living/monster/EnderDragonPartEntity.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package org.geysermc.geyser.entity.type.living.monster;
2727

2828
import org.cloudburstmc.math.vector.Vector3f;
29+
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
2930
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
3031
import org.cloudburstmc.protocol.bedrock.packet.MoveEntityAbsolutePacket;
3132
import org.geysermc.geyser.entity.VanillaEntities;
@@ -36,7 +37,10 @@
3637
public class EnderDragonPartEntity extends Entity {
3738

3839
public EnderDragonPartEntity(GeyserSession session, int entityId, long geyserId, float width, float height) {
39-
super(dragonPartSpawnContext(session, entityId, width, height, geyserId));
40+
super(dragonPartSpawnContext(session, entityId, geyserId));
41+
42+
dirtyMetadata.put(EntityDataTypes.WIDTH, width);
43+
dirtyMetadata.put(EntityDataTypes.HEIGHT, height);
4044

4145
setFlag(EntityFlag.INVISIBLE, true);
4246
setFlag(EntityFlag.FIRE_IMMUNE, true);
@@ -61,7 +65,7 @@ public void moveAbsoluteRaw(Vector3f position, float yaw, float pitch, float hea
6165
session.getQueuedImmediatelyPackets().add(moveEntityPacket);
6266
}
6367

64-
public static EntitySpawnContext dragonPartSpawnContext(GeyserSession session, int entityId, float width, float height, long geyserId) {
65-
return new EntitySpawnContext(session, VanillaEntities.ENDER_DRAGON_PART, entityId, height, width, geyserId);
68+
public static EntitySpawnContext dragonPartSpawnContext(GeyserSession session, int entityId, long geyserId) {
69+
return new EntitySpawnContext(session, VanillaEntities.ENDER_DRAGON_PART, entityId, geyserId);
6670
}
6771
}

core/src/main/java/org/geysermc/geyser/entity/type/living/monster/PhantomEntity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public PhantomEntity(EntitySpawnContext context) {
3737
public void setPhantomScale(IntEntityMetadata entityMetadata) {
3838
int size = entityMetadata.getPrimitiveValue();
3939
float modelScale = 1f + 0.15f * size;
40-
float boundsScale = (1f + (0.2f * size) / width) / modelScale;
40+
float boundsScale = (1f + (0.2f * size) / javaTypeDefinition.width()) / modelScale;
4141

42-
setBoundingBoxWidth(boundsScale * width);
43-
setBoundingBoxHeight(boundsScale * height);
42+
setBoundingBoxWidth(boundsScale * javaTypeDefinition.width());
43+
setBoundingBoxHeight(boundsScale * javaTypeDefinition.height());
4444
setScale(modelScale);
4545
}
4646

core/src/main/java/org/geysermc/geyser/entity/type/living/monster/ZoglinEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void setBaby(BooleanEntityMetadata entityMetadata) {
5050
@Override
5151
public float getBoundingBoxHeight() {
5252
float scale = getFlag(EntityFlag.BABY) ? 0.55f : 1f;
53-
return scale * height;
53+
return scale * javaTypeDefinition.height();
5454
}
5555

5656
@Override

0 commit comments

Comments
 (0)