Skip to content

Commit

Permalink
Apply Transformations on ArchetypeVolumes
Browse files Browse the repository at this point in the history
This presents the required objects to perform positional transformations
on all entities/blocks/etc. within a Volume. The key with these changes
is that certain objects have requirements to perform "rotations" and
"mirroring" as a middle step. This is all built to support that out of
the box using the new Transformation API.
Signed-off-by: Gabriel Harris-Rouquette <gabizou@me.com>

Signed-off-by: Gabriel Harris-Rouquette <gabizou@me.com>
  • Loading branch information
gabizou committed Jul 21, 2021
1 parent 259dfa8 commit 9e4bcb7
Show file tree
Hide file tree
Showing 41 changed files with 2,803 additions and 86 deletions.
2 changes: 1 addition & 1 deletion SpongeAPI
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ dependencies {
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")

testImplementation("org.mockito:mockito-core:$mockitoVersion")
testImplementation("org.mockito:mockito-junit-jupiter:$mockitoVersion")
testImplementation("org.mockito:mockito-inline:$mockitoVersion")
}

val organization: String by project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ protected ValidationType getValidationType() {
}

@Override
public org.spongepowered.api.block.entity.BlockEntityArchetype copy() {
public SpongeBlockEntityArchetype copy() {
final SpongeBlockEntityArchetypeBuilder builder = SpongeBlockEntityArchetypeBuilder.pooled();
builder.type = this.type;
builder.data = NBTTranslator.INSTANCE.translate(this.compound);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public BlockEntityArchetype.Builder blockEntityData(final DataView dataView) {
}

@Override
public BlockEntityArchetype build() {
public SpongeBlockEntityArchetype build() {
if (this.blockState == null) {
throw new IllegalStateException("BlockState cannot be null!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ protected ValidationType getValidationType() {
}

@Override
public EntityArchetype copy() {
public SpongeEntityArchetype copy() {
return new SpongeEntityArchetype(this.type, this.compound.copy());
}

Expand Down
148 changes: 147 additions & 1 deletion src/main/java/org/spongepowered/common/util/DirectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

import static com.google.common.base.Preconditions.checkNotNull;

import net.minecraft.world.level.block.state.properties.DirectionProperty;
import org.spongepowered.api.util.Direction;

import java.util.Objects;
import net.minecraft.world.level.block.state.properties.DirectionProperty;

public final class DirectionUtil {

Expand Down Expand Up @@ -81,6 +81,152 @@ public static net.minecraft.world.level.block.state.BlockState set(final net.min
return holder.setValue(property, direction);
}

public static Direction fromRotation(int i) {
switch (i) {
case 0:
return Direction.SOUTH;
case 1:
return Direction.SOUTH_SOUTHWEST;
case 2:
return Direction.SOUTHWEST;
case 3:
return Direction.WEST_SOUTHWEST;
case 4:
return Direction.WEST;
case 5:
return Direction.WEST_NORTHWEST;
case 6:
return Direction.NORTHWEST;
case 7:
return Direction.NORTH_NORTHWEST;
case 8:
return Direction.NORTH;
case 9:
return Direction.NORTH_NORTHEAST;
case 10:
return Direction.NORTHEAST;
case 11:
return Direction.EAST_NORTHEAST;
case 12:
return Direction.EAST;
case 13:
return Direction.EAST_SOUTHEAST;
case 14:
return Direction.SOUTHEAST;
case 15:
return Direction.SOUTH_SOUTHEAST;
default:
return Direction.NORTH;
}
}
public static int toRotation(final Direction direction) {
switch (direction) {
case SOUTH:
return 0;
case SOUTH_SOUTHWEST:
return 1;
case SOUTHWEST:
return 2;
case WEST_SOUTHWEST:
return 3;
case WEST:
return 4;
case WEST_NORTHWEST:
return 5;
case NORTHWEST:
return 6;
case NORTH_NORTHWEST:
return 7;
case NORTH:
return 8;
case NORTH_NORTHEAST:
return 9;
case NORTHEAST:
return 10;
case EAST_NORTHEAST:
return 11;
case EAST:
return 12;
case EAST_SOUTHEAST:
return 13;
case SOUTHEAST:
return 14;
case SOUTH_SOUTHEAST:
return 15;
default:
return 0;
}
}

public static Direction fromHorizontalHanging(int i) {
switch (i) {
case 0:
return Direction.SOUTH;
case 1:
return Direction.WEST;
case 2:
return Direction.NORTH;
case 3:
return Direction.EAST;
default:
return Direction.NORTH;
}
}

public static int toHorizontalHanging(Direction direction) {
switch (direction) {
case SOUTH:
return 0;
case WEST:
return 1;
case NORTH:
return 2;
case EAST:
return 3;
default:
return 0;
}
}


public static Direction fromHanging(int i) {
switch (i) {
case 0:
return Direction.DOWN;
case 1:
return Direction.UP;
case 2:
return Direction.NORTH;
case 3:
return Direction.SOUTH;
case 4:
return Direction.WEST;
case 5:
return Direction.EAST;
default:
return Direction.DOWN;
}
}

public static int toHanging(Direction direction) {
switch (direction) {
case DOWN:
return 0;
case UP:
return 1;
case NORTH:
return 2;
case SOUTH:
return 3;
case WEST:
return 4;
case EAST:
return 5;
default:
return 0;
}
}

public static int directionToIndex(final Direction direction) {
switch (direction) {
case NORTH:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@

public final class SpongeTransformation implements Transformation {

private final Vector3d origin;
private final Matrix4d transformation;
private final Matrix4d directionTransformation;
private final boolean performRounding;
private final Rotation rotation;
private final boolean flipx;
private final boolean flipz;
final Vector3d origin;
final Matrix4d transformation;
final Matrix4d directionTransformation;
final boolean performRounding;
final Rotation rotation;
final boolean flipx;
final boolean flipz;

public SpongeTransformation(final Vector3d origin, final Matrix4d transformation, final Matrix4d directionTransformation,
final boolean performRounding, final Rotation rotation, final boolean flipx, final boolean flipz) {
Expand Down Expand Up @@ -172,4 +172,9 @@ public boolean initialMirror(final @NonNull Axis axis) {
);
}

@Override
public Builder toBuilder() {
return new SpongeTransformationBuilder(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ public SpongeTransformationBuilder() {
this.reset();
}

SpongeTransformationBuilder(final SpongeTransformation transformation) {
this.reset();
this.origin = transformation.origin;
// undo the origin transformation
final Matrix4d partialRotation = this.transformation.translate(this.origin.mul(-1));
final Matrix4d invertedMatrix = Matrix4d.createTranslation(this.origin);
this.transformation = partialRotation.mul(invertedMatrix);
this.directionTransformation = transformation.directionTransformation;
this.flipx = transformation.flipx;
this.flipz = transformation.flipz;
this.rotation = transformation.rotation;
this.performRounding = transformation.performRounding;
}

@Override
public @NonNull SpongeTransformationBuilder reset() {
this.transformation = Matrix4d.IDENTITY;
Expand Down
Loading

0 comments on commit 9e4bcb7

Please sign in to comment.