Skip to content

Commit 6c43ca3

Browse files
authored
Merge pull request #12 from Vislo/fix/bedrock-itemdisplay-rotation
Fix ItemDisplay rotation; preserve across move packets
2 parents eafd15a + 5141c36 commit 6c43ca3

2 files changed

Lines changed: 29 additions & 31 deletions

File tree

src/main/java/me/zimzaza4/geyserdisplayentity/entity/ItemDisplayEntity.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,6 @@ public void moveAbsolute(Vector3f position, float yaw, float pitch, float headYa
213213
position = position.clone().add(0, yOffset, 0);
214214

215215
setPosition(position);
216-
setYaw(yaw);
217-
setPitch(pitch);
218-
setHeadYaw(headYaw);
219216
setOnGround(isOnGround);
220217

221218
MoveEntityAbsolutePacket moveEntityPacket = new MoveEntityAbsolutePacket();

src/main/java/me/zimzaza4/geyserdisplayentity/entity/SlotDisplayEntity.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.cloudburstmc.math.vector.Vector3f;
99
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
1010
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
11+
import org.cloudburstmc.protocol.bedrock.packet.MoveEntityAbsolutePacket; // added
1112
import org.geysermc.geyser.entity.EntityDefinition;
1213
import org.geysermc.geyser.entity.type.Entity;
1314
import org.geysermc.geyser.session.GeyserSession;
@@ -28,11 +29,13 @@ public class SlotDisplayEntity extends Entity {
2829

2930
protected Settings.DisplayEntityOptions options = Settings.IMP.GENERAL;
3031

32+
protected Quaternionf lastLeft = Quaternionf.IDENTITY;
33+
protected Quaternionf lastRight = Quaternionf.IDENTITY;
34+
3135
public SlotDisplayEntity(GeyserSession session, int entityId, long geyserId, UUID uuid,
3236
EntityDefinition<?> definition,
3337
Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
3438
super(session, entityId, geyserId, uuid, definition, position, motion, yaw, pitch, headYaw);
35-
3639
}
3740

3841
public void updateMainHand(GeyserSession session) {
@@ -121,13 +124,19 @@ protected void applyScale() {
121124
}
122125

123126
public void setLeftRotation(EntityMetadata<Quaternionf, ?> entityMetadata) {
124-
setRotation(entityMetadata.getValue());
127+
Quaternionf quaternion = entityMetadata.getValue();
128+
this.lastLeft = quaternion;
129+
setRotation(quaternion);
125130
rotationUpdated = true;
131+
applyBedrockYawPitchFromCombined();
126132
}
127133

128134
public void setRightRotation(EntityMetadata<Quaternionf, ?> entityMetadata) {
129-
setRotation(entityMetadata.getValue());
135+
Quaternionf quaternion = entityMetadata.getValue();
136+
this.lastRight = quaternion;
137+
setRotation(quaternion);
130138
rotationUpdated = true;
139+
applyBedrockYawPitchFromCombined();
131140
}
132141

133142
protected void setRotation(Quaternionf q) {
@@ -149,16 +158,24 @@ protected void setRotation(Quaternionf q) {
149158
propertyManager.add("geyser:s_q", qScale);
150159
}
151160

152-
protected Vector3f getNonNormalScale(Quaternionf q) {
153-
Quaternionf qx = q.mul(0, 1, 0, 0).mul(q.conjugate());
154-
Quaternionf qy = q.mul(0, 0, 1, 0).mul(q.conjugate());
155-
Quaternionf qz = q.mul(0, 0, 0, 1).mul(q.conjugate());
161+
protected void applyBedrockYawPitchFromCombined() {
162+
Quaternionf combined = Quaternionf.from(lastLeft).mul(lastRight).normalize();
156163

157-
float x = (float) Math.sqrt(qx.getX() * qx.getX() + qx.getY() * qx.getY() + qx.getZ() * qx.getZ());
158-
float y = (float) Math.sqrt(qy.getX() * qy.getX() + qy.getY() * qy.getY() + qy.getZ() * qy.getZ());
159-
float z = (float) Math.sqrt(qz.getX() * qz.getX() + qz.getY() * qz.getY() + qz.getZ() * qz.getZ());
164+
Vector3f fwd = combined.rotate(0f, 0f, 1f);
165+
float yawDeg = (float) Math.toDegrees(Math.atan2(-fwd.getX(), fwd.getZ()));
166+
float pitchDeg = (float) Math.toDegrees(Math.asin(MathUtils.clamp(fwd.getY(), -1f, 1f)));
160167

161-
return Vector3f.from(x, y, z);
168+
yawDeg = MathUtils.wrapDegrees(yawDeg);
169+
setYaw(yawDeg);
170+
setHeadYaw(yawDeg);
171+
setPitch(pitchDeg);
172+
173+
MoveEntityAbsolutePacket rotPkt = new MoveEntityAbsolutePacket();
174+
rotPkt.setRuntimeEntityId(geyserId);
175+
rotPkt.setPosition(position);
176+
rotPkt.setRotation(getBedrockRotation());
177+
rotPkt.setTeleported(false);
178+
session.sendUpstreamPacket(rotPkt);
162179
}
163180

164181
protected Vector3f toEulerZYX(Quaternionf q) {
@@ -174,20 +191,6 @@ protected Vector3f toEulerZYX(Quaternionf q) {
174191
float r10 = m.get(1, 0);
175192
float r11 = m.get(1, 1);
176193

177-
// float w = qn.getW();
178-
// float x = qn.getX();
179-
// float y = qn.getY();
180-
// float z = qn.getZ();
181-
182-
// float yaw = (float) Math.atan2(2 * (y * w - x * z), 1 - 2 * (y * y + z * z));
183-
// float pitch = (float) Math.asin(2 * (x * y + z * w));
184-
// float roll = (float) Math.atan2(2 * (x * w - y * z), 1 - 2 * (x * x + z * z));
185-
186-
// float x = Math.abs(r20) < 0.9999999F ? (float) Math.atan2(r21, r22) : 0F;
187-
// float y = - MathUtils.clamp((float) Math.asin(r20), -1F, 1F);
188-
// float z = Math.abs(r20) < 0.9999999F ? (float) Math.atan2(r10, r00) : (float)
189-
// Math.atan2(- r01, r11);
190-
191194
float x, y, z;
192195

193196
if (Math.abs(r20) < 0.9999999F) {
@@ -214,6 +217,4 @@ protected void hackRotation(float x, float y, float z) {
214217
propertyManager.add("geyser:r_z", z);
215218
updateBedrockEntityProperties();
216219
}
217-
218-
219-
}
220+
}

0 commit comments

Comments
 (0)