Skip to content

Commit

Permalink
Fix display entities being upside down on 1.20.x
Browse files Browse the repository at this point in the history
This was due to Mojang flipping them 180 on the y-axis in the 1.20 update
  • Loading branch information
jpenilla committed Aug 3, 2023
1 parent 712e9f6 commit fbd31c1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
52 changes: 42 additions & 10 deletions src/main/java/xyz/jpenilla/chesscraft/PieceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.bukkit.profile.PlayerTextures;
import org.bukkit.util.Transformation;
import org.joml.AxisAngle4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import xyz.jpenilla.chesscraft.config.PieceOptions;
import xyz.jpenilla.chesscraft.data.CardinalDirection;
Expand Down Expand Up @@ -70,16 +71,7 @@ public void applyToWorld(final ChessBoard board, final BoardStateHolder game, fi
return;
}
world.spawn(pos.toLocation(world), ItemDisplay.class, itemDisplay -> {
itemDisplay.setTransformation(new Transformation(
// center
new Vector3f(0.5f * board.scale(), 0, 0.5f * board.scale()),
// flip upwards
new AxisAngle4f((float) Math.toRadians(90.0D), 1, 0, 0),
// scale
new Vector3f(0.5f * board.scale()),
// rotate
new AxisAngle4f((float) Math.toRadians(rotation(board.facing(), piece)), 0, 0, 1)
));
itemDisplay.setTransformation(transformationFor(board, piece));
itemDisplay.setItemDisplayTransform(ItemDisplay.ItemDisplayTransform.FIXED);
itemDisplay.setItemStack(this.options.item(piece));
itemDisplay.setInvulnerable(true);
Expand All @@ -95,6 +87,46 @@ public void applyToWorld(final ChessBoard board, final BoardStateHolder game, fi
});
}

private static Transformation transformationFor(final ChessBoard board, final Piece piece) {
// flip upwards
final Quaternionf left = new Quaternionf(new AxisAngle4f((float) Math.toRadians(90.0D), 1, 0, 0));
transformForVersion(left);
// rotate
final Quaternionf right = new Quaternionf(new AxisAngle4f((float) Math.toRadians(rotation(board.facing(), piece)), 0, 0, 1));

return new Transformation(
// center
new Vector3f(0.5f * board.scale(), 0, 0.5f * board.scale()),
left,
// scale
new Vector3f(0.5f * board.scale()),
right
);
}

private static void transformForVersion(final Quaternionf left) {
if (Bukkit.getServer().getMinecraftVersion().startsWith("1.19")) {
// 1.19.x - nothing to do
return;
}
// 1.20+ - account for 180 flip on y-axis
rotateYFlip(left);
}

/**
* Transforms the given quaternion in-place to account for the
* changes Mojang made to item displays in 1.20.
*
* @param q quaternion
*/
private static void rotateYFlip(final Quaternionf q) {
final float x = q.x, y = q.y, z = q.z, w = q.w;
q.x = z;
q.y = w;
q.z = x;
q.w = -y;
}

private static float rotation(final CardinalDirection facing, final Piece piece) {
final double deg = facing.radians() * 180 / Math.PI;
if (piece.color() == PieceColor.WHITE) {
Expand Down

0 comments on commit fbd31c1

Please sign in to comment.