Search in sources :

Example 6 with Vector3d

use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.

the class ServerLevelMixin method impl$constructPostEventForEntityAdd.

@Inject(method = "addEntity", at = @At("HEAD"))
private void impl$constructPostEventForEntityAdd(final Entity entity, final CallbackInfoReturnable<Boolean> cir) {
    if (!(entity instanceof EntityBridge)) {
        return;
    }
    if (!((EntityBridge) entity).bridge$isConstructing()) {
        return;
    }
    ((EntityBridge) entity).bridge$fireConstructors();
    final Vec3 position = entity.position();
    final ServerLocation location = ServerLocation.of((ServerWorld) this, position.x(), position.y(), position.z());
    final Vec2 rotationVector = entity.getRotationVector();
    final Vector3d rotation = new Vector3d(rotationVector.x, rotationVector.y, 0);
    try (final CauseStackManager.StackFrame frame = PhaseTracker.SERVER.pushCauseFrame()) {
        frame.pushCause(entity);
        final Event construct = SpongeEventFactory.createConstructEntityEventPost(frame.currentCause(), (org.spongepowered.api.entity.Entity) entity, location, rotation, ((EntityType<?>) entity.getType()));
        SpongeCommon.post(construct);
    }
}
Also used : EntityType(org.spongepowered.api.entity.EntityType) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Vector3d(org.spongepowered.math.vector.Vector3d) Vec2(net.minecraft.world.phys.Vec2) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Vec3(net.minecraft.world.phys.Vec3) ExplosionEvent(org.spongepowered.api.event.world.ExplosionEvent) PlaySoundEvent(org.spongepowered.api.event.sound.PlaySoundEvent) ChangeWeatherEvent(org.spongepowered.api.event.world.ChangeWeatherEvent) LightningEvent(org.spongepowered.api.event.action.LightningEvent) Event(org.spongepowered.api.event.Event) EntityBridge(org.spongepowered.common.bridge.world.entity.EntityBridge) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 7 with Vector3d

use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.

the class AbstractSpongeRayTrace method execute.

@Override
@NonNull
public Optional<RayTraceResult<@NonNull T>> execute() {
    this.setupEnd();
    // get the direction
    final Vector3d directionWithLength = this.end.sub(this.start);
    final double length = directionWithLength.length();
    final Vector3d direction = directionWithLength.normalize();
    if (direction.lengthSquared() == 0) {
        throw new IllegalStateException("The start and end must be two different vectors");
    }
    final ServerWorld serverWorld = Sponge.server().worldManager().world(this.world).orElseThrow(() -> new IllegalStateException("World with key " + this.world.formatted() + " is not loaded!"));
    Vector3i currentBlock = this.initialBlock(direction);
    final Vector3i steps = this.createSteps(direction);
    // The ray equation is, vec(u) + t vec(d). From a point (x, y), there is a t
    // that we need to traverse to get to a boundary. We work that out now...
    TData tData = this.createInitialTData(direction);
    Vector3d currentLocation = new Vector3d(this.start.x(), this.start.y(), this.start.z());
    final boolean requiresEntityTracking = this.requiresEntityTracking();
    boolean requireAdvancement = true;
    while (requireAdvancement) {
        final net.minecraft.world.phys.Vec3 vec3dstart = VecHelper.toVanillaVector3d(currentLocation);
        // As this iteration is for the CURRENT block location, we need to check where we are with the filter.
        if (this.continueWhileLocation != null && !this.continueWhileLocation.test(ServerLocation.of(serverWorld, currentBlock))) {
            return Optional.empty();
        }
        final Vector3d nextLocation;
        final net.minecraft.world.phys.Vec3 vec3dend;
        if (tData.getTotalTWithNextStep() > length) {
            // This is the last step, we break out of the loop after this set of checks.
            requireAdvancement = false;
            nextLocation = this.end;
            vec3dend = VecHelper.toVanillaVector3d(this.end);
        } else {
            nextLocation = currentLocation.add(direction.x() * tData.getNextStep(), direction.y() * tData.getNextStep(), direction.z() * tData.getNextStep());
            vec3dend = VecHelper.toVanillaVector3d(nextLocation);
        }
        // Get the selection result.
        final Optional<RayTraceResult<@NonNull T>> result = this.testSelectLocation(serverWorld, vec3dstart, vec3dend);
        if (result.isPresent() && !this.shouldCheckFailures()) {
            // that's blocking the view.
            return result;
        }
        // Ensure that the block can be travelled through.
        if (!this.shouldAdvanceThroughBlock(serverWorld, vec3dstart, vec3dend)) {
            return Optional.empty();
        }
        // Ensure that the entities in the block can be travelled through.
        if (requiresEntityTracking && this.continueWhileEntity != null) {
            final double resultDistance;
            if (result.isPresent()) {
                resultDistance = result.get().hitPosition().distanceSquared(currentLocation);
            } else {
                resultDistance = Double.MAX_VALUE;
            }
            final AABB targetAABB = this.getBlockAABB(currentBlock);
            for (final net.minecraft.world.entity.Entity entity : this.getFailingEntities(serverWorld, targetAABB)) {
                final Optional<net.minecraft.world.phys.Vec3> vec3d = entity.getBoundingBox().clip(vec3dstart, vec3dend);
                if (vec3d.isPresent()) {
                    final net.minecraft.world.phys.Vec3 hitPosition = vec3d.get();
                    final double sqdist = hitPosition.distanceToSqr(vec3dstart);
                    if (sqdist < resultDistance) {
                        // We have a failure, so at this point we just bail out and end the trace.
                        return Optional.empty();
                    }
                }
            }
        }
        // If we still have a result at this point, return it.
        if (result.isPresent()) {
            return result;
        }
        if (requireAdvancement) {
            currentLocation = nextLocation;
            currentBlock = this.getNextBlock(currentBlock, tData, steps);
            tData = this.advance(tData, steps, direction);
        }
    }
    return Optional.empty();
}
Also used : RayTraceResult(org.spongepowered.api.util.blockray.RayTraceResult) ServerWorld(org.spongepowered.api.world.server.ServerWorld) Vector3d(org.spongepowered.math.vector.Vector3d) NonNull(org.checkerframework.checker.nullness.qual.NonNull) Vector3i(org.spongepowered.math.vector.Vector3i) AABB(net.minecraft.world.phys.AABB) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 8 with Vector3d

use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.

the class SpongeExplosionBuilder method build.

@Override
public Explosion build() throws IllegalStateException {
    // TODO Check coordinates and if world is loaded here.
    Preconditions.checkState(this.location != null, "Location is null!");
    final World<?, ?> world = this.location.world();
    final Vector3d origin = this.location.position();
    final net.minecraft.world.level.Explosion explosion = new net.minecraft.world.level.Explosion((net.minecraft.world.level.Level) world, (Entity) this.sourceExplosive, null, null, origin.x(), origin.y(), origin.z(), this.radius, this.canCauseFire, this.shouldBreakBlocks ? net.minecraft.world.level.Explosion.BlockInteraction.DESTROY : net.minecraft.world.level.Explosion.BlockInteraction.NONE);
    ((ExplosionBridge) explosion).bridge$setShouldBreakBlocks(this.shouldBreakBlocks);
    ((ExplosionBridge) explosion).bridge$setShouldDamageEntities(this.shouldDamageEntities);
    ((ExplosionBridge) explosion).bridge$setShouldPlaySmoke(this.shouldSmoke);
    ((ExplosionBridge) explosion).bridge$setResolution(this.resolution);
    ((ExplosionBridge) explosion).bridge$setRandomness(this.randomness);
    ((ExplosionBridge) explosion).bridge$setKnockback(this.knockback);
    return (Explosion) explosion;
}
Also used : ExplosionBridge(org.spongepowered.common.bridge.world.level.ExplosionBridge) Explosion(org.spongepowered.api.world.explosion.Explosion) Vector3d(org.spongepowered.math.vector.Vector3d)

Example 9 with Vector3d

use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.

the class SpongeTransformation method transformDirection.

@Override
@NonNull
public Vector3d transformDirection(@NonNull final Vector3d original) {
    final Vector4d transformed = this.directionTransformation.transform(original.normalize().toVector4(1));
    final Vector3d result;
    if (this.performRounding) {
        result = new Vector3d(GenericMath.round(transformed.x(), 14), GenericMath.round(transformed.y(), 14), GenericMath.round(transformed.z(), 14));
    } else {
        result = transformed.toVector3();
    }
    return result.normalize();
}
Also used : Vector4d(org.spongepowered.math.vector.Vector4d) Vector3d(org.spongepowered.math.vector.Vector3d) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 10 with Vector3d

use of org.spongepowered.math.vector.Vector3d in project SpongeCommon by SpongePowered.

the class SpongeAABB method contains.

@Override
public boolean contains(final double x, final double y, final double z) {
    final Vector3d min = this.min;
    final Vector3d max = this.max;
    return min.x() <= x && max.x() >= x && min.y() <= y && max.y() >= y && min.z() <= z && max.z() >= z;
}
Also used : Vector3d(org.spongepowered.math.vector.Vector3d)

Aggregations

Vector3d (org.spongepowered.math.vector.Vector3d)71 Vector3i (org.spongepowered.math.vector.Vector3i)16 CauseStackManager (org.spongepowered.api.event.CauseStackManager)14 AABB (org.spongepowered.api.util.AABB)14 Test (org.junit.jupiter.api.Test)13 ServerWorld (org.spongepowered.api.world.server.ServerWorld)13 BlockPos (net.minecraft.core.BlockPos)8 ServerLevel (net.minecraft.server.level.ServerLevel)7 BlockState (org.spongepowered.api.block.BlockState)7 Transformation (org.spongepowered.api.util.transformation.Transformation)7 Function (java.util.function.Function)6 IntStream (java.util.stream.IntStream)6 Stream (java.util.stream.Stream)6 Entity (org.spongepowered.api.entity.Entity)6 MoveEntityEvent (org.spongepowered.api.event.entity.MoveEntityEvent)6 ServerLocation (org.spongepowered.api.world.server.ServerLocation)6 Nullable (org.checkerframework.checker.nullness.qual.Nullable)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 MethodSource (org.junit.jupiter.params.provider.MethodSource)5 BlockType (org.spongepowered.api.block.BlockType)5