mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
More fixes
This commit is contained in:
parent
b00131c525
commit
8a5147c994
@ -527,9 +527,9 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
|
|||||||
} else {
|
} else {
|
||||||
newVelocity = deltaPos;
|
newVelocity = deltaPos;
|
||||||
newPosition = new Pos(
|
newPosition = new Pos(
|
||||||
position.x() + velocity.x() / tps,
|
position.x() + velocity.x() / tps,
|
||||||
position.y() + velocity.y() / tps,
|
position.y() + velocity.y() / tps,
|
||||||
position.z() + velocity.z() / 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
|
* Updates internal fields and sends updates
|
||||||
*
|
*
|
||||||
* @param position the new position
|
* @param position the new position
|
||||||
* @see #refreshCoordinate(Pos)
|
|
||||||
* @see #sendPositionUpdate(boolean)
|
* @see #sendPositionUpdate(boolean)
|
||||||
*/
|
*/
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
@ -1353,7 +1352,6 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
|
|||||||
refreshCurrentChunk(newChunk);
|
refreshCurrentChunk(newChunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.lastPosition = position;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,8 +26,8 @@ import net.minestom.server.network.player.PlayerConnection;
|
|||||||
import net.minestom.server.scoreboard.Team;
|
import net.minestom.server.scoreboard.Team;
|
||||||
import net.minestom.server.sound.SoundEvent;
|
import net.minestom.server.sound.SoundEvent;
|
||||||
import net.minestom.server.utils.BlockPosition;
|
import net.minestom.server.utils.BlockPosition;
|
||||||
import net.minestom.server.utils.Vector;
|
|
||||||
import net.minestom.server.utils.block.BlockIterator;
|
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.coordinate.Vec;
|
||||||
import net.minestom.server.utils.time.Cooldown;
|
import net.minestom.server.utils.time.Cooldown;
|
||||||
import net.minestom.server.utils.time.TimeUnit;
|
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
|
* @param maxDistance The max distance to scan
|
||||||
* @return A list of {@link BlockPosition} in this entities line of sight
|
* @return A list of {@link BlockPosition} in this entities line of sight
|
||||||
*/
|
*/
|
||||||
public List<BlockPosition> getLineOfSight(int maxDistance) {
|
public List<Point> getLineOfSight(int maxDistance) {
|
||||||
List<BlockPosition> blocks = new ArrayList<>();
|
List<Point> blocks = new ArrayList<>();
|
||||||
Iterator<BlockPosition> it = new BlockIterator(this, maxDistance);
|
Iterator<Point> it = new BlockIterator(this, maxDistance);
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
BlockPosition position = it.next();
|
final Point position = it.next();
|
||||||
if (!getInstance().getBlock(position).isAir()) blocks.add(position);
|
if (!getInstance().getBlock(position).isAir()) blocks.add(position);
|
||||||
}
|
}
|
||||||
return blocks;
|
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.
|
* @return if the current entity has line of sight to the given one.
|
||||||
*/
|
*/
|
||||||
public boolean hasLineOfSight(Entity entity) {
|
public boolean hasLineOfSight(Entity entity) {
|
||||||
Vector start = getPosition().toVector().add(0D, getEyeHeight(), 0D);
|
final var start = getPosition().asVec().add(0D, getEyeHeight(), 0D);
|
||||||
Vector end = entity.getPosition().toVector().add(0D, getEyeHeight(), 0D);
|
final var end = entity.getPosition().asVec().add(0D, getEyeHeight(), 0D);
|
||||||
Vector direction = end.subtract(start);
|
final var direction = end.sub(start);
|
||||||
int maxDistance = (int) Math.ceil(direction.length());
|
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()) {
|
while (it.hasNext()) {
|
||||||
Block block = getInstance().getBlock(it.next());
|
Block block = getInstance().getBlock(it.next());
|
||||||
if (!block.isAir() && !block.isLiquid()) {
|
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.
|
* Gets the target (not-air) {@link BlockPosition} of the entity.
|
||||||
*
|
*
|
||||||
* @param maxDistance The max distance to scan before returning null
|
* @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) {
|
public Point getTargetBlockPosition(int maxDistance) {
|
||||||
Iterator<BlockPosition> it = new BlockIterator(this, maxDistance);
|
Iterator<Point> it = new BlockIterator(this, maxDistance);
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
BlockPosition position = it.next();
|
final Point position = it.next();
|
||||||
if (getInstance().getBlock(position) != Block.AIR) return position;
|
if (!getInstance().getBlock(position).isAir()) return position;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -71,10 +71,11 @@ public class Navigator {
|
|||||||
final double speedZ = Math.sin(radians) * speed;
|
final double speedZ = Math.sin(radians) * speed;
|
||||||
|
|
||||||
// Update 'position' view
|
// 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
|
// 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
|
// Will move the entity during Entity#tick
|
||||||
entity.teleport(physicsResult.newPosition());
|
entity.teleport(physicsResult.newPosition());
|
||||||
|
@ -5,7 +5,9 @@ import net.minestom.server.instance.block.BlockFace;
|
|||||||
import net.minestom.server.utils.BlockPosition;
|
import net.minestom.server.utils.BlockPosition;
|
||||||
import net.minestom.server.utils.Position;
|
import net.minestom.server.utils.Position;
|
||||||
import net.minestom.server.utils.Vector;
|
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.Pos;
|
||||||
|
import net.minestom.server.utils.coordinate.Vec;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
@ -14,7 +16,7 @@ import java.util.NoSuchElementException;
|
|||||||
/**
|
/**
|
||||||
* This class performs ray tracing and iterates along blocks on a line
|
* 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;
|
private final int maxDistance;
|
||||||
|
|
||||||
@ -22,7 +24,7 @@ public class BlockIterator implements Iterator<BlockPosition> {
|
|||||||
|
|
||||||
private boolean end = false;
|
private boolean end = false;
|
||||||
|
|
||||||
private BlockPosition[] blockQueue = new BlockPosition[3];
|
private Point[] blockQueue = new Point[3];
|
||||||
private int currentBlock = 0;
|
private int currentBlock = 0;
|
||||||
private int currentDistance = 0;
|
private int currentDistance = 0;
|
||||||
private int maxDistanceInt;
|
private int maxDistanceInt;
|
||||||
@ -50,7 +52,7 @@ public class BlockIterator implements Iterator<BlockPosition> {
|
|||||||
* trace. Setting this value above 140 may lead to problems with
|
* trace. Setting this value above 140 may lead to problems with
|
||||||
* unloaded chunks. A value of 0 indicates no limit
|
* 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;
|
this.maxDistance = maxDistance;
|
||||||
|
|
||||||
Vector startClone = start.clone();
|
Vector startClone = start.clone();
|
||||||
@ -173,8 +175,8 @@ public class BlockIterator implements Iterator<BlockPosition> {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean blockEquals(@NotNull BlockPosition a, @NotNull BlockPosition b) {
|
private boolean blockEquals(@NotNull Point a, @NotNull Point b) {
|
||||||
return a.getX() == b.getX() && a.getY() == b.getY() && a.getZ() == b.getZ();
|
return a.x() == b.x() && a.y() == b.y() && a.z() == b.z();
|
||||||
}
|
}
|
||||||
|
|
||||||
private BlockFace getXFace(@NotNull Vector direction) {
|
private BlockFace getXFace(@NotNull Vector direction) {
|
||||||
|
@ -1,17 +1,13 @@
|
|||||||
package net.minestom.server.utils.position;
|
package net.minestom.server.utils.position;
|
||||||
|
|
||||||
import net.minestom.server.utils.Position;
|
import net.minestom.server.utils.coordinate.Pos;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public final class PositionUtils {
|
public final class PositionUtils {
|
||||||
|
public static Pos lookAlong(@NotNull Pos position, double dx, double dy, double dz) {
|
||||||
public static void lookAlong(@NotNull Position position, double dx, double dy, double dz) {
|
|
||||||
final double horizontalAngle = Math.atan2(dz, dx);
|
final double horizontalAngle = Math.atan2(dz, dx);
|
||||||
final float yaw = (float) (horizontalAngle * (180.0 / Math.PI)) - 90;
|
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)));
|
final float pitch = (float) Math.atan2(dy, Math.max(Math.abs(dx), Math.abs(dz)));
|
||||||
|
return position.withView(yaw, pitch);
|
||||||
position.setYaw(yaw);
|
|
||||||
position.setPitch(pitch);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user