Skip to content

Commit

Permalink
Add Keys.DISPLAY_TRANSFORM
Browse files Browse the repository at this point in the history
  • Loading branch information
avaruus1 committed Aug 4, 2024
1 parent 2901a2b commit d505aea
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SpongeAPI
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public static void register(final DataProviderRegistrator registrator) {
.create(Keys.TRANSFORM)
.get(DisplayEntityData::getTransform)
.set(DisplayEntityData::setTransform)
.create(Keys.DISPLAY_TRANSFORM)
.get(DisplayEntityData::getTransformMatrix)
.set(DisplayEntityData::setTransformMatrix)
.asMutable(DisplayAccessor.class)
.create(Keys.BILLBOARD_TYPE)
.get(h -> (BillboardType) (Object) h.invoker$getBillboardConstraints())
Expand Down Expand Up @@ -231,4 +234,28 @@ private static void setTransform(final Display h, final Transform transform) {
var vanillaTransform = new Transformation(vMatrix);
((DisplayAccessor) h).invoker$setTransformation(vanillaTransform);
}

private static Matrix4d getTransformMatrix(final Display display) {
final Transformation transform = DisplayAccessor.invoker$createTransformation(display.getEntityData());
final var vMatrix = transform.getMatrix();

return new Matrix4d(
vMatrix.m00(), vMatrix.m01(), vMatrix.m02(), vMatrix.m03(),
vMatrix.m10(), vMatrix.m11(), vMatrix.m12(), vMatrix.m13(),
vMatrix.m20(), vMatrix.m21(), vMatrix.m22(), vMatrix.m23(),
vMatrix.m30(), vMatrix.m31(), vMatrix.m32(), vMatrix.m33()
);
}

private static void setTransformMatrix(final Display h, final Matrix4d matrix) {
var vMatrix = new org.joml.Matrix4f(
(float) matrix.get(0, 0), (float) matrix.get(0, 1), (float) matrix.get(0, 2), (float) matrix.get(0, 3),
(float) matrix.get(1, 0), (float) matrix.get(1, 1), (float) matrix.get(1, 2), (float) matrix.get(1, 3),
(float) matrix.get(2, 0), (float) matrix.get(2, 1), (float) matrix.get(2, 2), (float) matrix.get(2, 3),
(float) matrix.get(3, 0), (float) matrix.get(3, 1), (float) matrix.get(3, 2), (float) matrix.get(3, 3)
);
var vanillaTransform = new Transformation(vMatrix);
((DisplayAccessor) h).invoker$setTransformation(vanillaTransform);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.block.transaction.BlockTransaction;
import org.spongepowered.api.block.transaction.Operations;
import org.spongepowered.api.command.Command;
import org.spongepowered.api.command.Command.Parameterized;
import org.spongepowered.api.command.CommandResult;
Expand All @@ -40,6 +42,8 @@
import org.spongepowered.api.entity.display.TextAlignments;
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.lifecycle.RegisterCommandEvent;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
Expand All @@ -50,6 +54,7 @@
import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.math.imaginary.Quaterniond;
import org.spongepowered.math.matrix.Matrix4d;
import org.spongepowered.math.vector.Vector3d;
import org.spongepowered.plugin.PluginContainer;
import org.spongepowered.plugin.builtin.jvm.Plugin;
Expand All @@ -64,6 +69,13 @@ public DisplayEntityTest(final PluginContainer plugin) {
this.plugin = plugin;
}

@Listener
public void onBlockChange(final ChangeBlockEvent.All event, @First final ServerPlayer player) {
for (BlockTransaction transaction : event.transactions()) {
System.out.println(BlockTypes.registry().valueKey(transaction.original().state().type()) + " :: " + Operations.registry().valueKey(transaction.operation()));
}
}

@Listener
public void onRegisterCommand(final RegisterCommandEvent<Parameterized> event) {
event.register(this.plugin, Command.builder()
Expand All @@ -83,6 +95,7 @@ public void onRegisterCommand(final RegisterCommandEvent<Parameterized> event) {
final int col5 = 3;
final int col6 = 5;
final int col7 = 6;
final int col8 = 7;
var textDisplay = spawnEntity(player.world(), EntityTypes.TEXT_DISPLAY, centerPos, forwardDir, -4, -1);
textDisplay.offer(Keys.DISPLAY_NAME, Component.text("DisplayEntityTest").color(NamedTextColor.GOLD));
textDisplay.offer(Keys.SEE_THROUGH_BLOCKS, true);
Expand Down Expand Up @@ -249,6 +262,19 @@ public void onRegisterCommand(final RegisterCommandEvent<Parameterized> event) {

// TODO interpolate text opacity?


blockDisplay = createEntity(player.world(), EntityTypes.BLOCK_DISPLAY, centerPos, forwardDir, col8, 0);
blockDisplay.offer(Keys.BLOCK_STATE, BlockTypes.BEDROCK.get().defaultState());
player.world().spawnEntity(blockDisplay);
blockDisplay.offer(Keys.DISPLAY_TRANSFORM, Matrix4d.from(
0.5, 0.3, -0.071, 0.8,
1, 1, 0.69, 0,
0.279, 0.873, -0.400, 0.019,
0, 0, 0, 0
));
blockDisplay.offer(Keys.INTERPOLATION_DURATION, Ticks.of(100));
blockDisplay.offer(Keys.INTERPOLATION_DELAY, Ticks.of(100));

});
return CommandResult.success();
})
Expand Down

0 comments on commit d505aea

Please sign in to comment.