mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-29 23:17:59 +02:00
Synchronize properly the view from Entity#getPosition
This commit is contained in:
parent
d91b1e9966
commit
966bbfc0cc
@ -57,6 +57,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
protected float lastX, lastY, lastZ;
|
protected float lastX, lastY, lastZ;
|
||||||
protected float cacheX, cacheY, cacheZ; // Used to synchronize with #getPosition
|
protected float cacheX, cacheY, cacheZ; // Used to synchronize with #getPosition
|
||||||
protected float lastYaw, lastPitch;
|
protected float lastYaw, lastPitch;
|
||||||
|
protected float cacheYaw, cachePitch;
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private BoundingBox boundingBox;
|
private BoundingBox boundingBox;
|
||||||
@ -206,6 +207,38 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
teleport(position, null);
|
teleport(position, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change the view of the entity
|
||||||
|
*
|
||||||
|
* @param yaw the new yaw
|
||||||
|
* @param pitch the new pitch
|
||||||
|
*/
|
||||||
|
public void setView(float yaw, float pitch) {
|
||||||
|
refreshView(yaw, pitch);
|
||||||
|
|
||||||
|
EntityRotationPacket entityRotationPacket = new EntityRotationPacket();
|
||||||
|
entityRotationPacket.entityId = getEntityId();
|
||||||
|
entityRotationPacket.yaw = yaw;
|
||||||
|
entityRotationPacket.pitch = pitch;
|
||||||
|
entityRotationPacket.onGround = onGround;
|
||||||
|
|
||||||
|
EntityHeadLookPacket entityHeadLookPacket = new EntityHeadLookPacket();
|
||||||
|
entityHeadLookPacket.entityId = getEntityId();
|
||||||
|
entityHeadLookPacket.yaw = yaw;
|
||||||
|
sendPacketToViewers(entityHeadLookPacket);
|
||||||
|
sendPacketToViewersAndSelf(entityRotationPacket);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change the view of the entity
|
||||||
|
* Only the yaw and pitch is used
|
||||||
|
*
|
||||||
|
* @param position the new view
|
||||||
|
*/
|
||||||
|
public void setView(Position position) {
|
||||||
|
setView(position.getYaw(), position.getPitch());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When set to true, the entity will automatically get new viewers when they come too close
|
* When set to true, the entity will automatically get new viewers when they come too close
|
||||||
* This can be use to complete control over which player can see it, without having to deal with
|
* This can be use to complete control over which player can see it, without having to deal with
|
||||||
@ -288,11 +321,19 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Synchronization with updated fields in #getPosition()
|
// Synchronization with updated fields in #getPosition()
|
||||||
|
{
|
||||||
|
// X/Y/Z axis
|
||||||
if (cacheX != position.getX() ||
|
if (cacheX != position.getX() ||
|
||||||
cacheY != position.getY() ||
|
cacheY != position.getY() ||
|
||||||
cacheZ != position.getZ()) {
|
cacheZ != position.getZ()) {
|
||||||
teleport(position);
|
teleport(position);
|
||||||
}
|
}
|
||||||
|
// Yaw/Pitch
|
||||||
|
if (cacheYaw != position.getYaw() ||
|
||||||
|
cachePitch != position.getPitch()) {
|
||||||
|
setView(position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (shouldUpdate(time)) {
|
if (shouldUpdate(time)) {
|
||||||
this.lastUpdate = time;
|
this.lastUpdate = time;
|
||||||
@ -779,7 +820,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
/**
|
/**
|
||||||
* Update the entity view internally
|
* Update the entity view internally
|
||||||
* <p>
|
* <p>
|
||||||
* Warning: you probably want to use {@link EntityCreature#setView(float, float)}
|
* Warning: you probably want to use {@link #setView(float, float)}
|
||||||
*
|
*
|
||||||
* @param yaw the yaw
|
* @param yaw the yaw
|
||||||
* @param pitch the pitch
|
* @param pitch the pitch
|
||||||
@ -789,6 +830,8 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
this.lastPitch = position.getPitch();
|
this.lastPitch = position.getPitch();
|
||||||
position.setYaw(yaw);
|
position.setYaw(yaw);
|
||||||
position.setPitch(pitch);
|
position.setPitch(pitch);
|
||||||
|
this.cacheYaw = yaw;
|
||||||
|
this.cachePitch = pitch;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void refreshSneaking(boolean sneaking) {
|
public void refreshSneaking(boolean sneaking) {
|
||||||
|
@ -125,14 +125,6 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
refreshPosition(newX, newY, newZ);
|
refreshPosition(newX, newY, newZ);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setView(float yaw, float pitch) {
|
|
||||||
EntityHeadLookPacket entityHeadLookPacket = new EntityHeadLookPacket();
|
|
||||||
entityHeadLookPacket.entityId = getEntityId();
|
|
||||||
entityHeadLookPacket.yaw = yaw;
|
|
||||||
sendPacketToViewers(entityHeadLookPacket);
|
|
||||||
refreshView(yaw, pitch);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void spawn() {
|
public void spawn() {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user