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);
}
}
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();
}
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;
}
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();
}
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;
}
Aggregations