use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.
the class LevelMixin method bridge$createEntity.
@SuppressWarnings("unchecked")
public <E extends org.spongepowered.api.entity.Entity> E bridge$createEntity(final DataContainer dataContainer, @Nullable final Vector3d position, @Nullable final Predicate<Vector3d> positionCheck) throws IllegalArgumentException, IllegalStateException {
final EntityType<@NonNull ?> type = dataContainer.getRegistryValue(Constants.Entity.TYPE, RegistryTypes.ENTITY_TYPE).orElseThrow(() -> new IllegalArgumentException("DataContainer does not contain a valid entity type."));
final Vector3d proposedPosition;
if (position == null) {
proposedPosition = DataUtil.getPosition3d(dataContainer, Constants.Sponge.SNAPSHOT_WORLD_POSITION);
} else {
proposedPosition = position;
}
if (positionCheck != null && !positionCheck.test(proposedPosition)) {
throw new IllegalArgumentException(String.format("Position (%.2f, %.2f, %.2f) is not a valid position in this context.", proposedPosition.x(), proposedPosition.y(), proposedPosition.z()));
}
@Nullable final Vector3d rotation;
if (dataContainer.contains(Constants.Entity.ROTATION)) {
rotation = DataUtil.getPosition3d(dataContainer, Constants.Entity.ROTATION);
} else {
rotation = null;
}
@Nullable final Vector3d scale;
if (dataContainer.contains(Constants.Entity.SCALE)) {
scale = DataUtil.getPosition3d(dataContainer, Constants.Entity.SCALE);
} else {
scale = null;
}
final Entity createdEntity = this.bridge$createEntity(type, position, false);
dataContainer.getView(Constants.Sponge.UNSAFE_NBT).map(NBTTranslator.INSTANCE::translate).ifPresent(x -> {
final net.minecraft.world.entity.Entity e = ((net.minecraft.world.entity.Entity) createdEntity);
// mimicing Entity#restoreFrom
x.remove("Dimension");
e.load(x);
// position needs a reset
e.moveTo(proposedPosition.x(), proposedPosition.y(), proposedPosition.z());
});
if (rotation != null) {
createdEntity.setRotation(rotation);
}
if (scale != null) {
createdEntity.setScale(scale);
}
return (E) createdEntity;
}
use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.
the class ThrownEnderpearlMixin method impl$callMoveEntityEventForThrower.
@Inject(method = "onHit", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;teleportTo(DDD)V"), cancellable = true)
private void impl$callMoveEntityEventForThrower(final HitResult result, final CallbackInfo ci) {
if (this.shadow$getCommandSenderWorld().isClientSide || !ShouldFire.MOVE_ENTITY_EVENT) {
return;
}
final Entity entity = this.shadow$getOwner();
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.pushCause(entity);
frame.pushCause(this);
frame.addContext(EventContextKeys.MOVEMENT_TYPE, MovementTypes.ENDER_PEARL);
final MoveEntityEvent event = SpongeEventFactory.createMoveEntityEvent(frame.currentCause(), (org.spongepowered.api.entity.Entity) entity, VecHelper.toVector3d(entity.position()), VecHelper.toVector3d(this.shadow$position()), VecHelper.toVector3d(this.shadow$position()));
if (SpongeCommon.post(event)) {
// Eventhough the event is made, the pearl was still created so remove it anyways
this.shadow$remove();
return;
}
// This seems odd but we move the pearl so that the pearl's logic will move the living entity later in the impact method
final Vector3d destinationPosition = event.destinationPosition();
this.shadow$setPos(destinationPosition.x(), destinationPosition.y(), destinationPosition.z());
}
}
use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.
the class SpongeEntityArchetypeBuilder method from.
@SuppressWarnings({ "rawtypes", "EqualsBetweenInconvertibleTypes" })
@Override
public EntityArchetype.Builder from(final Entity entity) {
final EntityType<@NonNull ?> entityType = Objects.requireNonNull(Objects.requireNonNull(entity, "Cannot build an EntityArchetype for a null entity!").type(), "Entity is returning a null EntityType!");
if (!((net.minecraft.world.entity.EntityType) entityType).canSerialize() && entityType != HumanEntity.TYPE) {
throw new IllegalArgumentException("Attempting to archetype a non-serializable entity: " + entity);
}
this.entityType = entityType;
final CompoundTag compound = new CompoundTag();
net.minecraft.world.entity.Entity mcEntity = (net.minecraft.world.entity.Entity) entity;
if (entityType == HumanEntity.TYPE) {
mcEntity.saveWithoutId(compound);
} else {
mcEntity.saveAsPassenger(compound);
}
this.position = new Vector3d(mcEntity.getX(), mcEntity.getY(), mcEntity.getZ());
SpongeEntityArchetypeBuilder.stripCompound(compound);
compound.putBoolean(Constants.Sponge.EntityArchetype.REQUIRES_EXTRA_INITIAL_SPAWN, true);
this.position = entity.position();
this.compound = compound;
return this;
}
use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.
the class VolumeTransformationTest method testTransformationsOfPositions.
@MethodSource("testTransformationsOfPositions")
@ParameterizedTest
void testTransformationsOfPositions(final Vector3i min, final Vector3i max, final Vector3i origin, final Vector3i testForRoundTrip, final int rotationCount, final StubRotations wanted) {
final SpongeArchetypeVolume volume = VolumeTransformationTest.fillVolume(min, max, origin);
final Vector3i size = volume.size();
final Vector3i relativeMin = volume.min();
final Vector3d center = volume.logicalCenter();
ArchetypeVolume intermediary = volume;
for (int i = 0; i < rotationCount; i++) {
intermediary = intermediary.transform(Transformation.builder().origin(center).rotate(wanted).build());
}
Rotation expected = Rotations.NONE.get();
for (int i = 0; i < rotationCount; i++) {
expected = expected.and(wanted);
}
final Transformation expectedTransform = Transformation.builder().origin(center).rotate(expected).build();
final Transformation inverse = expectedTransform.inverse();
final ArchetypeVolume rotated = intermediary;
if (rotationCount > 0) {
final Vector3d preliminaryTransformed = expectedTransform.transformPosition(testForRoundTrip.toDouble());
Vector3i unTransformed = preliminaryTransformed.round().toInt();
for (int i = 0; i < rotationCount; i++) {
unTransformed = ((AbstractReferentArchetypeVolume) rotated).inverseTransform(unTransformed.x(), unTransformed.y(), unTransformed.z());
}
Assertions.assertEquals(testForRoundTrip, unTransformed);
}
for (int x = 0; x < size.x(); x++) {
for (int y = 0; y < size.y(); y++) {
for (int z = 0; z < size.z(); z++) {
final int relativeX = x + relativeMin.x();
final int relativeY = y + relativeMin.y();
final int relativeZ = z + relativeMin.z();
final Vector3d rawRelativePosition = new Vector3d(relativeX, relativeY, relativeZ);
final BlockState untransformedState = volume.block(relativeX, relativeY, relativeZ);
final Vector3i transformedPosition = expectedTransform.transformPosition(rawRelativePosition).toInt();
final BlockState transformedState = rotated.block(transformedPosition.x(), transformedPosition.y(), transformedPosition.z());
Assertions.assertEquals(untransformedState, transformedState, () -> String.format("Block Check Failed!\nOriginal(%d, %d, %d): %s\nTransformed(%d, %d, %d): %s\n", relativeX, relativeY, relativeZ, untransformedState, transformedPosition.x(), transformedPosition.y(), transformedPosition.z(), transformedState));
}
}
}
if (rotationCount < 0) {
return;
}
// At this point, we should have an abstract referent volume at least
rotated.blockStateStream(rotated.min(), rotated.max(), StreamOptions.lazily()).forEach((rotatedRef, type, x, y, z) -> {
final Vector3d transformedPos = new Vector3d(x, y, z);
// We have this offset in the stream, so we have to undo it here.
final Vector3d invertedTransformedPos = inverse.transformPosition(transformedPos.add(VolumePositionTranslators.BLOCK_OFFSET)).sub(VolumePositionTranslators.BLOCK_OFFSET);
final Vector3i invertedBlockPos = invertedTransformedPos.toInt();
final Vector3i expectedPos;
Assertions.assertTrue(type instanceof StubState, () -> String.format("expected state to be a stub state for pos: [%f, %f, %f] but got %s", x, y, z, type));
Assertions.assertNotEquals(((StubState) type).deducedPos, VolumeTransformationTest.INVALID_STUB_POSITION, () -> String.format("expected to have a positioned stub state: [%f, %f, %f] but got %s", x, y, z, type));
expectedPos = ((StubState) type).deducedPos;
Assertions.assertEquals(expectedPos, invertedBlockPos, () -> String.format("expected untransformed position %s for state %s does not match reverse transformed position: %s", expectedPos, type, invertedBlockPos));
final BlockState block = volume.block(expectedPos.x(), expectedPos.y(), expectedPos.z());
Assertions.assertEquals(type, block, () -> String.format("Expected deduced state to be equal from the original target volume but had a mismatch: Original target %s does not match %s", block, type));
});
}
use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.
the class SpongeAABBTest method testIntersectsRay.
@Test
void testIntersectsRay() {
final AABB aabb = new SpongeAABB(new Vector3d(0, 0, 0), new Vector3d(2, 2, 2));
Assertions.assertEquals(new Tuple<>(new Vector3d(2, 1, 1), new Vector3d(1, 0, 0)), aabb.intersects(new Vector3d(1, 1, 1), new Vector3d(1, 0, 0)).get());
Assertions.assertEquals(new Tuple<>(new Vector3d(1, 2, 1), new Vector3d(0, 1, 0)), aabb.intersects(new Vector3d(1, 1, 1), new Vector3d(0, 1, 0)).get());
Assertions.assertEquals(new Tuple<>(new Vector3d(1, 1, 2), new Vector3d(0, 0, 1)), aabb.intersects(new Vector3d(1, 1, 1), new Vector3d(0, 0, 1)).get());
Assertions.assertEquals(new Tuple<>(new Vector3d(0, 0, 0), new Vector3d(-1, -1, -1).normalize()), aabb.intersects(new Vector3d(-1, -1, -1), new Vector3d(1, 1, 1)).get());
Assertions.assertEquals(new Tuple<>(new Vector3d(0, 0, 1), new Vector3d(-1, -1, -0.0).normalize()), aabb.intersects(new Vector3d(-1, -1, 1), new Vector3d(1, 1, 0)).get());
Assertions.assertEquals(new Tuple<>(new Vector3d(0, 1, 1), new Vector3d(-1, -0.0, -0.0)), aabb.intersects(new Vector3d(-1, 1, 1), new Vector3d(1, 0, 0)).get());
Assertions.assertEquals(new Tuple<>(new Vector3d(2, 1, 1), new Vector3d(1, 0, 0)), aabb.intersects(new Vector3d(3, 1, 1), new Vector3d(-1, 0, 0)).get());
Assertions.assertEquals(new Tuple<>(new Vector3d(1, 0, 1), new Vector3d(-0.0, -1, -0.0)), aabb.intersects(new Vector3d(1, -1, 1), new Vector3d(0, 1, 0)).get());
Assertions.assertEquals(new Tuple<>(new Vector3d(1, 2, 1), new Vector3d(0, 1, 0)), aabb.intersects(new Vector3d(1, 3, 1), new Vector3d(0, -1, 0)).get());
Assertions.assertEquals(new Tuple<>(new Vector3d(1, 1, 0), new Vector3d(-0.0, -0.0, -1)), aabb.intersects(new Vector3d(1, 1, -1), new Vector3d(0, 0, 1)).get());
Assertions.assertEquals(new Tuple<>(new Vector3d(1, 1, 2), new Vector3d(0, 0, 1)), aabb.intersects(new Vector3d(1, 1, 3), new Vector3d(0, 0, -1)).get());
Assertions.assertFalse(aabb.intersects(new Vector3d(-1, -1, -1), new Vector3d(0, 1, 0)).isPresent());
}
Aggregations