Remove legacy position objects

This commit is contained in:
TheMode 2021-07-25 06:30:49 +02:00
parent ad964a0a39
commit 25d645a5cb
9 changed files with 10 additions and 1563 deletions

View File

@ -68,7 +68,10 @@ import net.minestom.server.scoreboard.Team;
import net.minestom.server.sound.SoundCategory;
import net.minestom.server.sound.SoundEvent;
import net.minestom.server.stat.PlayerStatistic;
import net.minestom.server.utils.*;
import net.minestom.server.utils.ArrayUtils;
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.PacketUtils;
import net.minestom.server.utils.TickUtils;
import net.minestom.server.utils.async.AsyncUtils;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.entity.EntityUtils;
@ -1234,7 +1237,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
* @param facePoint the point from where the player should aim
* @param targetPosition the target position to face
*/
public void facePosition(@NotNull FacePoint facePoint, @NotNull Position targetPosition) {
public void facePosition(@NotNull FacePoint facePoint, @NotNull Point targetPosition) {
facePosition(facePoint, targetPosition, null, null);
}

View File

@ -2,11 +2,10 @@ package net.minestom.server.entity.ai.goal;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.EntityProjectile;
import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.ai.GoalSelector;
import net.minestom.server.entity.pathfinding.Navigator;
import net.minestom.server.entity.EntityProjectile;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.time.Cooldown;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.validate.Check;
@ -17,7 +16,6 @@ import java.time.temporal.TemporalUnit;
import java.util.function.Function;
public class RangedAttackGoal extends GoalSelector {
private final Cooldown cooldown = new Cooldown(Duration.of(5, TimeUnit.SERVER_TICK));
private long lastShot;
@ -121,7 +119,7 @@ public class RangedAttackGoal extends GoalSelector {
}
}
Navigator navigator = this.entityCreature.getNavigator();
final var pathPosition = navigator.getPathPosition();
final var pathPosition = navigator.getPathPosition();
if (!comeClose && distanceSquared <= this.desirableRangeSquared) {
if (pathPosition != null) {
navigator.setPathTo(null);

View File

@ -1,11 +1,10 @@
package net.minestom.server.event.entity;
import net.minestom.server.coordinate.Point;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityProjectile;
import net.minestom.server.event.trait.CancellableEvent;
import net.minestom.server.event.trait.EntityEvent;
import net.minestom.server.utils.Position;
import net.minestom.server.coordinate.Point;
import org.jetbrains.annotations.NotNull;
/**

View File

@ -1,14 +1,13 @@
package net.minestom.server.instance.block.rule.vanilla;
import it.unimi.dsi.fastutil.Pair;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

View File

@ -3,7 +3,6 @@ package net.minestom.server.network.packet.server.play;
import net.minestom.server.coordinate.Point;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;

View File

@ -1,393 +0,0 @@
package net.minestom.server.utils;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
// TODO: pool block positions?
/**
* Represents the position of a block, so with integers instead of floating numbers.
*
* @deprecated use {@link net.minestom.server.coordinate.Vec} instead
*/
@Deprecated
public class BlockPosition implements Point {
private int x, y, z;
/**
* Creates a new {@link BlockPosition}.
*
* @param x the block X
* @param y the block Y
* @param z the block Z
*/
public BlockPosition(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Creates a new {@link BlockPosition}.
* <p>
* Float positions are converted to block position, notably used by {@link Position#toBlockPosition()}.
*
* @param x the block X
* @param y the block Y
* @param z the block Z
*/
public BlockPosition(double x, double y, double z) {
final int castedY = (int) y;
this.x = (int) Math.floor(x);
this.y = (y == castedY) ? castedY : castedY + 1;
this.z = (int) Math.floor(z);
}
/**
* Creates a new {@link BlockPosition} from a {@link Vector}.
*
* @param position the position vector
* @see #BlockPosition(double, double, double)
*/
public BlockPosition(@NotNull Vector position) {
this(position.getX(), position.getY(), position.getZ());
}
/**
* Adds offsets to this block position.
*
* @param x the X offset
* @param y the Y offset
* @param z the Z offset
* @return the instance of this block position
*/
@NotNull
public BlockPosition add(int x, int y, int z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
/**
* Subtracts offsets to this block position.
*
* @param x the X offset
* @param y the Y offset
* @param z the Z offset
* @return the instance of this block position
*/
@NotNull
public BlockPosition subtract(int x, int y, int z) {
this.x -= x;
this.y -= y;
this.z -= z;
return this;
}
/**
* Adds offsets to this block position.
*
* @param pos the pos to add
* @return the instance of this block position
*/
@NotNull
public BlockPosition add(@NotNull BlockPosition pos) {
this.x += pos.getX();
this.y += pos.getY();
this.z += pos.getZ();
return this;
}
/**
* Subtracts offsets to this block position.
*
* @param pos the pos to subtract
* @return the instance of this block position
*/
@NotNull
public BlockPosition subtract(@NotNull BlockPosition pos) {
this.x -= pos.getX();
this.y -= pos.getY();
this.z -= pos.getZ();
return this;
}
/**
* Gets the block X.
*
* @return the block X
*/
public int getX() {
return x;
}
/**
* Changes the X field.
* <p>
* WARNING: this will not change the block position.
*
* @param x the new X field
*/
public void setX(int x) {
this.x = x;
}
/**
* Gets the block Y.
*
* @return the block Y
*/
public int getY() {
return y;
}
/**
* Changes the Y field.
* <p>
* WARNING: this will not change the block position.
*
* @param y the new Y field
*/
public void setY(int y) {
this.y = y;
}
/**
* Gets the block Z.
*
* @return the block Z
*/
public int getZ() {
return z;
}
/**
* Changes the Z field.
* <p>
* WARNING: this will not change the block position.
*
* @param z the new Z field
*/
public void setZ(int z) {
this.z = z;
}
/**
* Gets the manhattan distance to another block position.
*
* @param blockPosition the block position to check the distance
* @return the distance between 'this' and {@code blockPosition}
*/
public int getManhattanDistance(@NotNull BlockPosition blockPosition) {
return Math.abs(getX() - blockPosition.getX()) +
Math.abs(getY() - blockPosition.getY()) +
Math.abs(getZ() - blockPosition.getZ());
}
/**
* Gets the distance to another block position.
* In cases where performance matters, {@link #getDistanceSquared(BlockPosition)} should be used
* as it does not perform the expensive Math.sqrt method.
*
* @param blockPosition the block position to check the distance
* @return the distance between 'this' and {@code blockPosition}
*/
public double getDistance(@NotNull BlockPosition blockPosition) {
return Math.sqrt(getDistanceSquared(blockPosition));
}
/**
* Gets the square distance to another block position.
*
* @param blockPosition the block position to check the distance
* @return the distance between 'this' and {@code blockPosition}
*/
public int getDistanceSquared(@NotNull BlockPosition blockPosition) {
return MathUtils.square(getX() - blockPosition.getX()) +
MathUtils.square(getY() - blockPosition.getY()) +
MathUtils.square(getZ() - blockPosition.getZ());
}
@Override
public double x() {
return x;
}
@Override
public double y() {
return y;
}
@Override
public double z() {
return z;
}
@Override
public @NotNull Point withX(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withX(double x) {
return null;
}
@Override
public @NotNull Point withY(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withY(double y) {
return null;
}
@Override
public @NotNull Point withZ(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withZ(double z) {
return null;
}
@Override
public @NotNull Point add(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point add(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point add(double value) {
return null;
}
@Override
public @NotNull Point sub(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point sub(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point sub(double value) {
return null;
}
@Override
public @NotNull Point mul(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point mul(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point mul(double value) {
return null;
}
@Override
public @NotNull Point div(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point div(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point div(double value) {
return null;
}
@Override
public @NotNull BlockPosition clone() {
return new BlockPosition(x, y, z);
}
/**
* Sets the x/y/z fields of this block position to the value of {@code block position}.
*
* @param blockPosition the block position to copy the values from
*/
public void copyCoordinates(@NotNull BlockPosition blockPosition) {
this.x = blockPosition.getX();
this.y = blockPosition.getY();
this.z = blockPosition.getZ();
}
/**
* Converts this block position to a {@link Position}.
*
* @return the converted {@link Position}
*/
@NotNull
public Position toPosition() {
return new Position(x, y, z);
}
/**
* Gets BlockPosition relative to a {@link BlockFace}
*
* @param face The blockface touching the relative block
* @return The BlockPositon touching the provided blockface
*/
@NotNull
public BlockPosition getRelative(BlockFace face) {
switch (face) {
case BOTTOM:
return new BlockPosition(getX(), getY() - 1, getZ());
case TOP:
return new BlockPosition(getX(), getY() + 1, getZ());
case NORTH:
return new BlockPosition(getX(), getY(), getZ() - 1);
case SOUTH:
return new BlockPosition(getX(), getY(), getZ() + 1);
case WEST:
return new BlockPosition(getX() - 1, getY(), getZ());
case EAST:
return new BlockPosition(getX() + 1, getY(), getZ());
}
return new BlockPosition(getX(), getY(), getZ()); // should never be called
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BlockPosition that = (BlockPosition) o;
return x == that.x &&
y == that.y &&
z == that.z;
}
@Override
public int hashCode() {
return Objects.hash(x, y, z);
}
@Override
public String toString() {
return "BlockPosition[" + x + ":" + y + ":" + z + "]";
}
}

View File

@ -1,518 +0,0 @@
package net.minestom.server.utils;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
/**
* Represents a position.
* The instance is not contained.
*
* @deprecated use {@link net.minestom.server.coordinate.Pos} instead
*/
@Deprecated
public class Position implements Point {
private double x, y, z;
private float yaw, pitch;
public Position(double x, double y, double z, float yaw, float pitch) {
this.x = x;
this.y = y;
this.z = z;
this.yaw = fixYaw(yaw);
this.pitch = pitch;
}
public Position(double x, double y, double z) {
this(x, y, z, 0, 0);
}
public Position() {
this(0, 0, 0);
}
/**
* Adds offsets to the current position.
*
* @param x the X offset
* @param y the Y offset
* @param z the Z offset
* @return the same object position
*/
@NotNull
public Position add(double x, double y, double z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
@Override
public @NotNull Point add(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point add(double value) {
return null;
}
@Override
public @NotNull Point sub(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point sub(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point sub(double value) {
return null;
}
@Override
public @NotNull Point mul(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point mul(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point mul(double value) {
return null;
}
@Override
public @NotNull Point div(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point div(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point div(double value) {
return null;
}
/**
* Adds a position to the current position.
*
* @param position the position to add to this
* @return the same object position
*/
@NotNull
public Position add(@NotNull Position position) {
this.x += position.x;
this.y += position.y;
this.z += position.z;
return this;
}
/**
* Removes offsets to the current position.
*
* @param x the X offset
* @param y the Y offset
* @param z the Z offset
* @return the same object position
*/
@NotNull
public Position subtract(double x, double y, double z) {
this.x -= x;
this.y -= y;
this.z -= z;
return this;
}
public double getDistance(double x, double y, double z) {
return Math.sqrt(MathUtils.square(x - getX()) +
MathUtils.square(y - getY()) +
MathUtils.square(z - getZ()));
}
/**
* Gets the distance between 2 positions.
* In cases where performance matters, {@link #getDistanceSquared(Position)} should be used
* as it does not perform the expensive Math.sqrt method.
*
* @param position the second position
* @return the distance between {@code this} and {@code position}
*/
public double getDistance(@NotNull Position position) {
return getDistance(position.getX(), position.getY(), position.getZ());
}
/**
* Gets the square distance to another position.
*
* @param position the second position
* @return the squared distance between {@code this} and {@code position}
*/
public double getDistanceSquared(@NotNull Position position) {
return MathUtils.square(getX() - position.getX()) +
MathUtils.square(getY() - position.getY()) +
MathUtils.square(getZ() - position.getZ());
}
/**
* Gets a unit-vector pointing in the direction that this Location is
* facing.
*
* @return a vector pointing the direction of this location's {@link
* #getPitch() pitch} and {@link #getYaw() yaw}
*/
@NotNull
public Vector getDirection() {
Vector vector = new Vector();
final float rotX = this.getYaw();
final float rotY = this.getPitch();
vector.setY((float) -Math.sin(Math.toRadians(rotY)));
final double xz = Math.cos(Math.toRadians(rotY));
vector.setX((-xz * Math.sin(Math.toRadians(rotX))));
vector.setZ((xz * Math.cos(Math.toRadians(rotX))));
return vector;
}
/**
* Sets the {@link #getYaw() yaw} and {@link #getPitch() pitch} to point
* in the direction of the vector.
*/
@NotNull
public Position setDirection(@NotNull Vector vector) {
/*
* Sin = Opp / Hyp
* Cos = Adj / Hyp
* Tan = Opp / Adj
*
* x = -Opp
* z = Adj
*/
final double _2PI = 2 * Math.PI;
final double x = vector.getX();
final double z = vector.getZ();
if (x == 0 && z == 0) {
pitch = vector.getY() > 0 ? -90 : 90;
return this;
}
final double theta = Math.atan2(-x, z);
yaw = (float) Math.toDegrees((theta + _2PI) % _2PI);
final double x2 = MathUtils.square(x);
final double z2 = MathUtils.square(z);
final double xz = Math.sqrt(x2 + z2);
pitch = (float) Math.toDegrees(Math.atan(-vector.getY() / xz));
return this;
}
/**
* Sets the x/y/z field of this position to the value of {@code position}.
*
* @param position the vector to copy the values from
*/
public void copy(@NotNull Vector position) {
this.x = position.getX();
this.y = position.getY();
this.z = position.getZ();
}
/**
* Sets the x/y/z/yaw/pitch fields of this position to the value of {@code position}.
*
* @param position the position to copy the values from
*/
public void copy(@NotNull Position position) {
this.x = position.getX();
this.y = position.getY();
this.z = position.getZ();
this.yaw = position.getYaw();
this.pitch = position.getPitch();
}
/**
* Sets the x/y/z fields of this position to the value of {@code position}.
*
* @param position the position to copy the values from
*/
public void copyCoordinates(@NotNull Position position) {
this.x = position.getX();
this.y = position.getY();
this.z = position.getZ();
}
@Override
public double x() {
return x;
}
@Override
public double y() {
return y;
}
@Override
public double z() {
return z;
}
@Override
public @NotNull Point withX(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withX(double x) {
return null;
}
@Override
public @NotNull Point withY(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withY(double y) {
return null;
}
@Override
public @NotNull Point withZ(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withZ(double z) {
return null;
}
@Override
public @NotNull Position clone() {
return new Position(x, y, z, yaw, pitch);
}
/**
* Gets if the two objects are position and have the same values.
*
* @param o the position to check the equality
* @return true if the two objects are position with the same values, false otherwise
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position = (Position) o;
return Double.compare(position.x, x) == 0 &&
Double.compare(position.y, y) == 0 &&
Double.compare(position.z, z) == 0 &&
Double.compare(position.yaw, yaw) == 0 &&
Double.compare(position.pitch, pitch) == 0;
}
/**
* Checks it two positions are similar (x/y/z).
*
* @param position the position to compare
* @return true if the two positions are similar
*/
public boolean isSimilar(@NotNull Position position) {
return Double.compare(position.x, x) == 0 &&
Double.compare(position.y, y) == 0 &&
Double.compare(position.z, z) == 0;
}
/**
* Checks if two positions have a similar view (yaw/pitch).
*
* @param position the position to compare
* @return true if the two positions have the same view
*/
public boolean hasSimilarView(@NotNull Position position) {
return Float.compare(position.yaw, yaw) == 0 &&
Float.compare(position.pitch, pitch) == 0;
}
/**
* Gets if two positions are in the same chunk.
*
* @param position the checked position chunk
* @return true if 'this' is in the same chunk as {@code position}
*/
public boolean inSameChunk(@NotNull Position position) {
final int chunkX1 = ChunkUtils.getChunkCoordinate(getX());
final int chunkZ1 = ChunkUtils.getChunkCoordinate(getZ());
final int chunkX2 = ChunkUtils.getChunkCoordinate(position.getX());
final int chunkZ2 = ChunkUtils.getChunkCoordinate(position.getZ());
return chunkX1 == chunkX2 && chunkZ1 == chunkZ2;
}
@Override
public int hashCode() {
return Objects.hash(x, y, z, yaw, pitch);
}
/**
* Gets the position X.
*
* @return the position X
*/
public double getX() {
return x;
}
/**
* Changes the position X.
*
* @param x the new position X
*/
public void setX(double x) {
this.x = x;
}
/**
* Gets the position Y.
*
* @return the position Y
*/
public double getY() {
return y;
}
/**
* Changes the position Y.
*
* @param y the new position Y
*/
public void setY(double y) {
this.y = y;
}
/**
* Gets the position Z.
*
* @return the position Z
*/
public double getZ() {
return z;
}
/**
* Changes the position Z.
*
* @param z the new position Z
*/
public void setZ(double z) {
this.z = z;
}
/**
* Changes the position to the given one.
*
* @param position the new position.
*/
public void set(Position position) {
this.x = position.x;
this.y = position.y;
this.z = position.z;
this.yaw = position.yaw;
this.pitch = position.pitch;
}
/**
* Gets the position yaw.
*
* @return the yaw
*/
public float getYaw() {
return yaw;
}
/**
* Changes the position yaw.
*
* @param yaw the new yaw
*/
public void setYaw(float yaw) {
this.yaw = fixYaw(yaw);
}
/**
* Fixes a yaw value that is not between -180.0F and 180.0F
* So for example -1355.0F becomes 85.0F and 225.0F becomes -135.0F
*
* @param yaw The possible "wrong" yaw
* @return a fixed yaw
*/
private float fixYaw(float yaw) {
yaw = yaw % 360;
if (yaw < -180.0F) {
yaw += 360.0F;
} else if (yaw > 180.0F) {
yaw -= 360.0F;
}
return yaw;
}
/**
* Gets the position pitch.
*
* @return the pitch
*/
public float getPitch() {
return pitch;
}
/**
* Changes the position pitch.
*
* @param pitch the new pitch
*/
public void setPitch(float pitch) {
this.pitch = pitch;
}
/**
* Converts this position to a {@link BlockPosition}.
*
* @return the converted {@link BlockPosition}
*/
@NotNull
public BlockPosition toBlockPosition() {
return new BlockPosition(x, y, z);
}
/**
* Converts this position to a {@link Vector}.
*
* @return the converted {@link Vector}
*/
@NotNull
public Vector toVector() {
return new Vector(x, y, z);
}
@Override
public String toString() {
return "Position[" + x + ":" + y + ":" + z + "] (" + yaw + "/" + pitch + ")";
}
}

View File

@ -1,639 +0,0 @@
package net.minestom.server.utils;
import net.minestom.server.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import java.util.function.DoubleUnaryOperator;
/**
* @deprecated use {@link net.minestom.server.coordinate.Vec} instead
*/
@Deprecated
public class Vector implements Point {
private static final double epsilon = 0.000001;
protected double x, y, z;
public Vector() {
this.x = 0;
this.y = 0;
this.z = 0;
}
public Vector(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
@NotNull
public Vector add(@NotNull Vector vec) {
x += vec.x;
y += vec.y;
z += vec.z;
return this;
}
@NotNull
public Vector add(double x, double y, double z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
@Override
public @NotNull Point add(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point add(double value) {
return null;
}
@Override
public @NotNull Point sub(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point sub(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point sub(double value) {
return null;
}
@Override
public @NotNull Point mul(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point mul(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point mul(double value) {
return null;
}
@Override
public @NotNull Point div(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point div(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point div(double value) {
return null;
}
/**
* Subtracts a vector from this one.
*
* @param vec The other vector
* @return the same vector
*/
@NotNull
public Vector subtract(@NotNull Vector vec) {
x -= vec.x;
y -= vec.y;
z -= vec.z;
return this;
}
@NotNull
public Vector subtract(double x, double y, double z) {
this.x -= x;
this.y -= y;
this.z -= z;
return this;
}
/**
* Multiplies the vector by another.
*
* @param vec The other vector
* @return the same vector
*/
@NotNull
public Vector multiply(@NotNull Vector vec) {
x *= vec.x;
y *= vec.y;
z *= vec.z;
return this;
}
/**
* Divides the vector by another.
*
* @param vec The other vector
* @return the same vector
*/
@NotNull
public Vector divide(@NotNull Vector vec) {
x /= vec.x;
y /= vec.y;
z /= vec.z;
return this;
}
/**
* Copies another vector
*
* @param vec The other vector
* @return the same vector
*/
@NotNull
public Vector copy(@NotNull Vector vec) {
x = vec.x;
y = vec.y;
z = vec.z;
return this;
}
/**
* Sets the x/y/z fields of this vector to the value of {@code vector}.
*
* @param vector the vector to copy the values from
*/
public void copyCoordinates(@NotNull Vector vector) {
this.x = vector.getX();
this.y = vector.getY();
this.z = vector.getZ();
}
/**
* Gets the magnitude of the vector, defined as sqrt(x^2+y^2+z^2). The
* value of this method is not cached and uses a costly square-root
* function, so do not repeatedly call this method to get the vector's
* magnitude. NaN will be returned if the inner result of the sqrt()
* function overflows, which will be caused if the length is too long.
*
* @return the magnitude
*/
public double length() {
return Math.sqrt(MathUtils.square(x) + MathUtils.square(y) + MathUtils.square(z));
}
/**
* Gets the magnitude of the vector squared.
*
* @return the magnitude
*/
public double lengthSquared() {
return MathUtils.square(x) + MathUtils.square(y) + MathUtils.square(z);
}
/**
* Gets the distance between this vector and another. The value of this
* method is not cached and uses a costly square-root function, so do not
* repeatedly call this method to get the vector's magnitude. NaN will be
* returned if the inner result of the sqrt() function overflows, which
* will be caused if the distance is too long.
*
* @param o The other vector
* @return the distance
*/
public double distance(@NotNull Vector o) {
return Math.sqrt(MathUtils.square(x - o.x) + MathUtils.square(y - o.y) + MathUtils.square(z - o.z));
}
/**
* Gets the squared distance between this vector and another.
*
* @param o The other vector
* @return the squared distance
*/
public double distanceSquared(@NotNull Vector o) {
return MathUtils.square(x - o.x) + MathUtils.square(y - o.y) + MathUtils.square(z - o.z);
}
/**
* Gets the angle between this vector and another in radians.
*
* @param other The other vector
* @return angle in radians
*/
public float angle(@NotNull Vector other) {
double dot = MathUtils.clamp(dot(other) / (length() * other.length()), -1.0, 1.0);
return (float) Math.acos(dot);
}
/**
* Performs scalar multiplication, multiplying all components with a
* scalar.
*
* @param m The factor
* @return the same vector
*/
@NotNull
public Vector multiply(int m) {
x *= m;
y *= m;
z *= m;
return this;
}
/**
* Performs scalar multiplication, multiplying all components with a
* scalar.
*
* @param m The factor
* @return the same vector
*/
@NotNull
public Vector multiply(double m) {
x *= m;
y *= m;
z *= m;
return this;
}
/**
* Performs scalar multiplication, multiplying all components with a
* scalar.
*
* @param m The factor
* @return the same vector
*/
@NotNull
public Vector multiply(float m) {
x *= m;
y *= m;
z *= m;
return this;
}
/**
* Calculates the dot product of this vector with another. The dot product
* is defined as x1*x2+y1*y2+z1*z2. The returned value is a scalar.
*
* @param other The other vector
* @return dot product
*/
public double dot(@NotNull Vector other) {
return x * other.x + y * other.y + z * other.z;
}
/**
* Calculates the cross product of this vector with another. The cross
* product is defined as:
* <ul>
* <li>x = y1 * z2 - y2 * z1
* <li>y = z1 * x2 - z2 * x1
* <li>z = x1 * y2 - x2 * y1
* </ul>
*
* @param o The other vector
* @return the same vector
*/
@NotNull
public Vector crossProduct(@NotNull Vector o) {
this.x = y * o.z - o.y * z;
this.y = z * o.x - o.z * x;
this.z = x * o.y - o.x * y;
return this;
}
/**
* Calculates the cross product of this vector with another without mutating
* the original. The cross product is defined as:
* <ul>
* <li>x = y1 * z2 - y2 * z1
* <li>y = z1 * x2 - z2 * x1
* <li>z = x1 * y2 - x2 * y1
* </ul>
*
* @param o The other vector
* @return a new vector
*/
@NotNull
public Vector getCrossProduct(@NotNull Vector o) {
final double x = this.y * o.z - o.y * this.z;
final double y = this.z * o.x - o.z * this.x;
final double z = this.x * o.y - o.x * this.y;
return new Vector(x, y, z);
}
/**
* Converts this vector to a unit vector (a vector with length of 1).
*
* @return the same vector
*/
public Vector normalize() {
double length = length();
x /= length;
y /= length;
z /= length;
return this;
}
/**
* Zero this vector's components.
*
* @return the same vector
*/
@NotNull
public Vector zero() {
x = 0;
y = 0;
z = 0;
return this;
}
public boolean isZero() {
return getX() == 0 &&
getY() == 0 &&
getZ() == 0;
}
/**
* Returns if a vector is normalized
*
* @return whether the vector is normalised
*/
public boolean isNormalized() {
return Math.abs(this.lengthSquared() - 1) < getEpsilon();
}
/**
* Rotates the vector around the x axis.
* <p>
* This piece of math is based on the standard rotation matrix for vectors
* in three dimensional space. This matrix can be found here:
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
* Matrix</a>.
*
* @param angle the angle to rotate the vector about. This angle is passed
* in radians
* @return the same vector
*/
@NotNull
public Vector rotateAroundX(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double oldY = getY();
double oldZ = getZ();
this.y = angleCos * oldY - angleSin * oldZ;
this.z = angleSin * oldY + angleCos * oldZ;
return this;
}
/**
* Rotates the vector around the y axis.
* <p>
* This piece of math is based on the standard rotation matrix for vectors
* in three dimensional space. This matrix can be found here:
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
* Matrix</a>.
*
* @param angle the angle to rotate the vector about. This angle is passed
* in radians
* @return the same vector
*/
@NotNull
public Vector rotateAroundY(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double oldX = getX();
double oldZ = getZ();
this.x = angleCos * oldX + angleSin * oldZ;
this.z = -angleSin * oldX + angleCos * oldZ;
return this;
}
/**
* Rotates the vector around the z axis
* <p>
* This piece of math is based on the standard rotation matrix for vectors
* in three dimensional space. This matrix can be found here:
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
* Matrix</a>.
*
* @param angle the angle to rotate the vector about. This angle is passed
* in radians
* @return the same vector
*/
@NotNull
public Vector rotateAroundZ(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double oldX = getX();
double oldY = getY();
this.x = angleCos * oldX - angleSin * oldY;
this.y = angleSin * oldX + angleCos * oldY;
return this;
}
/**
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
*
* <p>
* Rotation will follow the general Right-Hand-Rule, which means rotation
* will be counterclockwise when the axis is pointing towards the observer.
* <p>
* This method will always make sure the provided axis is a unit vector, to
* not modify the length of the vector when rotating. If you are experienced
* with the scaling of a non-unit axis vector, you can use
* {@link Vector#rotateAroundNonUnitAxis(Vector, double)}.
*
* @param axis the axis to rotate the vector around. If the passed vector is
* not of length 1, it gets copied and normalized before using it for the
* rotation. Please use {@link Vector#normalize()} on the instance before
* passing it to this method
* @param angle the angle to rotate the vector around the axis
* @return the same vector
*/
@NotNull
public Vector rotateAroundAxis(@NotNull Vector axis, double angle) throws IllegalArgumentException {
return rotateAroundNonUnitAxis(axis.isNormalized() ? axis : axis.clone().normalize(), angle);
}
/**
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
*
* <p>
* Rotation will follow the general Right-Hand-Rule, which means rotation
* will be counterclockwise when the axis is pointing towards the observer.
* <p>
* Note that the vector length will change accordingly to the axis vector
* length. If the provided axis is not a unit vector, the rotated vector
* will not have its previous length. The scaled length of the resulting
* vector will be related to the axis vector. If you are not perfectly sure
* about the scaling of the vector, use
* {@link Vector#rotateAroundAxis(Vector, double)}
*
* @param axis the axis to rotate the vector around.
* @param angle the angle to rotate the vector around the axis
* @return the same vector
*/
@NotNull
public Vector rotateAroundNonUnitAxis(@NotNull Vector axis, double angle) throws IllegalArgumentException {
double x = getX(), y = getY(), z = getZ();
double x2 = axis.getX(), y2 = axis.getY(), z2 = axis.getZ();
double cosTheta = Math.cos(angle);
double sinTheta = Math.sin(angle);
double dotProduct = this.dot(axis);
this.x = x2 * dotProduct * (1d - cosTheta)
+ x * cosTheta
+ (-z2 * y + y2 * z) * sinTheta;
this.y = y2 * dotProduct * (1d - cosTheta)
+ y * cosTheta
+ (z2 * x - x2 * z) * sinTheta;
this.z = z2 * dotProduct * (1d - cosTheta)
+ z * cosTheta
+ (-y2 * x + x2 * y) * sinTheta;
return this;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Vector)) {
return false;
}
Vector other = (Vector) obj;
return Math.abs(x - other.x) < epsilon && Math.abs(y - other.y) < epsilon && Math.abs(z - other.z) < epsilon && (this.getClass().equals(obj.getClass()));
}
/**
* Returns a hash code for this vector
*
* @return hash code
*/
@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
hash = 79 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
hash = 79 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
return hash;
}
@Override
public String toString() {
return "Vector{" +
"x=" + x +
", y=" + y +
", z=" + z +
'}';
}
@Override
public double x() {
return x;
}
@Override
public double y() {
return y;
}
@Override
public double z() {
return z;
}
@Override
public @NotNull Point withX(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withX(double x) {
return null;
}
@Override
public @NotNull Point withY(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withY(double y) {
return null;
}
@Override
public @NotNull Point withZ(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withZ(double z) {
return null;
}
@Override
public @NotNull Vector clone() {
return new Vector(x,y,z);
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
/**
* Gets a new {@link Position} from this vector.
*
* @return this vector as a position
*/
@NotNull
public Position toPosition() {
return new Position(x, y, z);
}
/**
* Get the threshold used for equals().
*
* @return The epsilon.
*/
public static double getEpsilon() {
return epsilon;
}
}

View File

@ -12,7 +12,6 @@ import net.minestom.server.entity.GameMode;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.math.IntRange;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
@ -62,7 +61,7 @@ public class EntityFinder {
return this;
}
public EntityFinder setStartPosition(@NotNull Position startPosition) {
public EntityFinder setStartPosition(@NotNull Point startPosition) {
this.startPosition = startPosition;
return this;
}