More fixes

This commit is contained in:
TheMode 2021-07-07 01:32:30 +02:00
parent b00131c525
commit 8a5147c994
5 changed files with 31 additions and 34 deletions

View File

@ -527,9 +527,9 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
} else {
newVelocity = deltaPos;
newPosition = new Pos(
position.x() + velocity.x() / tps,
position.y() + velocity.y() / tps,
position.z() + velocity.z() / tps
position.x() + velocity.x() / tps,
position.y() + velocity.y() / tps,
position.z() + velocity.z() / tps
);
}
@ -1296,7 +1296,6 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
* Updates internal fields and sends updates
*
* @param position the new position
* @see #refreshCoordinate(Pos)
* @see #sendPositionUpdate(boolean)
*/
@ApiStatus.Internal
@ -1353,7 +1352,6 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
refreshCurrentChunk(newChunk);
}
}
this.lastPosition = position;
}
/**

View File

@ -26,8 +26,8 @@ import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.scoreboard.Team;
import net.minestom.server.sound.SoundEvent;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Vector;
import net.minestom.server.utils.block.BlockIterator;
import net.minestom.server.utils.coordinate.Point;
import net.minestom.server.utils.coordinate.Vec;
import net.minestom.server.utils.time.Cooldown;
import net.minestom.server.utils.time.TimeUnit;
@ -723,11 +723,11 @@ public class LivingEntity extends Entity implements EquipmentHandler {
* @param maxDistance The max distance to scan
* @return A list of {@link BlockPosition} in this entities line of sight
*/
public List<BlockPosition> getLineOfSight(int maxDistance) {
List<BlockPosition> blocks = new ArrayList<>();
Iterator<BlockPosition> it = new BlockIterator(this, maxDistance);
public List<Point> getLineOfSight(int maxDistance) {
List<Point> blocks = new ArrayList<>();
Iterator<Point> it = new BlockIterator(this, maxDistance);
while (it.hasNext()) {
BlockPosition position = it.next();
final Point position = it.next();
if (!getInstance().getBlock(position).isAir()) blocks.add(position);
}
return blocks;
@ -742,12 +742,12 @@ public class LivingEntity extends Entity implements EquipmentHandler {
* @return if the current entity has line of sight to the given one.
*/
public boolean hasLineOfSight(Entity entity) {
Vector start = getPosition().toVector().add(0D, getEyeHeight(), 0D);
Vector end = entity.getPosition().toVector().add(0D, getEyeHeight(), 0D);
Vector direction = end.subtract(start);
int maxDistance = (int) Math.ceil(direction.length());
final var start = getPosition().asVec().add(0D, getEyeHeight(), 0D);
final var end = entity.getPosition().asVec().add(0D, getEyeHeight(), 0D);
final var direction = end.sub(start);
final int maxDistance = (int) Math.ceil(direction.length());
Iterator<BlockPosition> it = new BlockIterator(start, direction.normalize(), 0D, maxDistance);
Iterator<Point> it = new BlockIterator(start, direction.normalize(), 0D, maxDistance);
while (it.hasNext()) {
Block block = getInstance().getBlock(it.next());
if (!block.isAir() && !block.isLiquid()) {
@ -761,13 +761,13 @@ public class LivingEntity extends Entity implements EquipmentHandler {
* Gets the target (not-air) {@link BlockPosition} of the entity.
*
* @param maxDistance The max distance to scan before returning null
* @return The {@link BlockPosition} targeted by this entity, null if non are found
* @return The block position targeted by this entity, null if non are found
*/
public BlockPosition getTargetBlockPosition(int maxDistance) {
Iterator<BlockPosition> it = new BlockIterator(this, maxDistance);
public Point getTargetBlockPosition(int maxDistance) {
Iterator<Point> it = new BlockIterator(this, maxDistance);
while (it.hasNext()) {
BlockPosition position = it.next();
if (getInstance().getBlock(position) != Block.AIR) return position;
final Point position = it.next();
if (!getInstance().getBlock(position).isAir()) return position;
}
return null;
}

View File

@ -71,10 +71,11 @@ public class Navigator {
final double speedZ = Math.sin(radians) * speed;
// Update 'position' view
PositionUtils.lookAlong(position, dx, direction.y(), dz);
final var view = PositionUtils.lookAlong(position, dx, direction.y(), dz);
entity.setView(view.yaw(), view.pitch());
// Prevent ghosting
final CollisionUtils.PhysicsResult physicsResult = CollisionUtils.handlePhysics(entity, new Vec(speedX, speedY, speedZ));
final var physicsResult = CollisionUtils.handlePhysics(entity, new Vec(speedX, speedY, speedZ));
// Will move the entity during Entity#tick
entity.teleport(physicsResult.newPosition());

View File

@ -5,7 +5,9 @@ import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.Vector;
import net.minestom.server.utils.coordinate.Point;
import net.minestom.server.utils.coordinate.Pos;
import net.minestom.server.utils.coordinate.Vec;
import org.jetbrains.annotations.NotNull;
import java.util.Iterator;
@ -14,7 +16,7 @@ import java.util.NoSuchElementException;
/**
* This class performs ray tracing and iterates along blocks on a line
*/
public class BlockIterator implements Iterator<BlockPosition> {
public class BlockIterator implements Iterator<Point> {
private final int maxDistance;
@ -22,7 +24,7 @@ public class BlockIterator implements Iterator<BlockPosition> {
private boolean end = false;
private BlockPosition[] blockQueue = new BlockPosition[3];
private Point[] blockQueue = new Point[3];
private int currentBlock = 0;
private int currentDistance = 0;
private int maxDistanceInt;
@ -50,7 +52,7 @@ public class BlockIterator implements Iterator<BlockPosition> {
* trace. Setting this value above 140 may lead to problems with
* unloaded chunks. A value of 0 indicates no limit
*/
public BlockIterator(@NotNull Vector start, @NotNull Vector direction, double yOffset, int maxDistance) {
public BlockIterator(@NotNull Vec start, @NotNull Vec direction, double yOffset, int maxDistance) {
this.maxDistance = maxDistance;
Vector startClone = start.clone();
@ -173,8 +175,8 @@ public class BlockIterator implements Iterator<BlockPosition> {
}
private boolean blockEquals(@NotNull BlockPosition a, @NotNull BlockPosition b) {
return a.getX() == b.getX() && a.getY() == b.getY() && a.getZ() == b.getZ();
private boolean blockEquals(@NotNull Point a, @NotNull Point b) {
return a.x() == b.x() && a.y() == b.y() && a.z() == b.z();
}
private BlockFace getXFace(@NotNull Vector direction) {

View File

@ -1,17 +1,13 @@
package net.minestom.server.utils.position;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.coordinate.Pos;
import org.jetbrains.annotations.NotNull;
public final class PositionUtils {
public static void lookAlong(@NotNull Position position, double dx, double dy, double dz) {
public static Pos lookAlong(@NotNull Pos position, double dx, double dy, double dz) {
final double horizontalAngle = Math.atan2(dz, dx);
final float yaw = (float) (horizontalAngle * (180.0 / Math.PI)) - 90;
final float pitch = (float) Math.atan2(dy, Math.max(Math.abs(dx), Math.abs(dz)));
position.setYaw(yaw);
position.setPitch(pitch);
return position.withView(yaw, pitch);
}
}