Merged individual position values to position fields

This commit is contained in:
Németh Noel 2021-04-30 23:01:43 +02:00
parent 20ed4926c8
commit fa8a499321
2 changed files with 35 additions and 48 deletions

View File

@ -70,10 +70,15 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
protected Instance instance;
protected Chunk currentChunk;
protected final Position position;
protected double lastX, lastY, lastZ;
protected double cacheX, cacheY, cacheZ; // Used to synchronize with #getPosition
protected float lastYaw, lastPitch;
protected float cacheYaw, cachePitch;
/**
* Used to calculate delta movement
*/
protected final Position lastPosition;
/**
* Used to check if any change made to the {@link Entity#position} field since
* the last packets sent
*/
protected final Position lastSyncedPosition;
protected boolean onGround;
private BoundingBox boundingBox;
@ -138,6 +143,8 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
this.entityType = entityType;
this.uuid = uuid;
this.position = new Position();
this.lastPosition = new Position();
this.lastSyncedPosition = new Position();
setBoundingBox(entityType.getWidth(), entityType.getHeight(), entityType.getWidth());
@ -157,9 +164,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
public Entity(@NotNull EntityType entityType, @NotNull UUID uuid, @NotNull Position spawnPosition) {
this(entityType, uuid);
this.position.set(spawnPosition);
this.lastX = spawnPosition.getX();
this.lastY = spawnPosition.getY();
this.lastZ = spawnPosition.getZ();
this.lastPosition.set(spawnPosition);
}
@Deprecated
@ -492,12 +497,9 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
// Synchronization with updated fields in #getPosition()
{
final boolean positionChange = cacheX != position.getX() ||
cacheY != position.getY() ||
cacheZ != position.getZ();
final boolean viewChange = cacheYaw != position.getYaw() ||
cachePitch != position.getPitch();
final double distance = positionChange ? position.getDistance(cacheX, cacheY, cacheZ) : 0;
final boolean positionChange = !position.isSimilar(lastSyncedPosition);
final boolean viewChange = !position.hasSimilarView(lastSyncedPosition);
final double distance = positionChange ? position.getDistance(lastSyncedPosition) : 0;
if (distance >= 8 || (positionChange && isNettyClient)) {
// Teleport has the priority over everything else
@ -505,7 +507,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
} else if (positionChange && viewChange) {
EntityPositionAndRotationPacket positionAndRotationPacket =
EntityPositionAndRotationPacket.getPacket(getEntityId(),
position, new Position(cacheX, cacheY, cacheZ), isOnGround());
position, lastSyncedPosition, isOnGround());
sendPacketToViewersAndSelf(positionAndRotationPacket);
@ -520,7 +522,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
} else if (positionChange) {
EntityPositionPacket entityPositionPacket = EntityPositionPacket.getPacket(getEntityId(),
position, new Position(cacheX, cacheY, cacheZ), isOnGround());
position, lastSyncedPosition, isOnGround());
sendPacketToViewersAndSelf(entityPositionPacket);
@ -869,11 +871,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
}
this.position.set(spawnPosition);
this.lastX = this.position.getX();
this.lastY = this.position.getY();
this.lastZ = this.position.getZ();
this.lastYaw = this.position.getYaw();
this.lastPitch = this.position.getPitch();
this.lastPosition.set(position);
this.isActive = true;
this.instance = instance;
@ -1320,9 +1318,9 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
position.setX(x);
position.setY(y);
position.setZ(z);
this.cacheX = x;
this.cacheY = y;
this.cacheZ = z;
lastSyncedPosition.setX(x);
lastSyncedPosition.setY(y);
lastSyncedPosition.setZ(z);
if (hasPassenger()) {
for (Entity passenger : getPassengers()) {
@ -1355,9 +1353,9 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
}
}
this.lastX = position.getX();
this.lastY = position.getY();
this.lastZ = position.getZ();
this.lastPosition.setX(position.getX());
this.lastPosition.setY(position.getY());
this.lastPosition.setZ(position.getZ());
}
/**
@ -1377,12 +1375,12 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
* @param pitch the pitch
*/
public void refreshView(float yaw, float pitch) {
this.lastYaw = position.getYaw();
this.lastPitch = position.getPitch();
this.lastPosition.setYaw(position.getYaw());
this.lastPosition.setPitch(position.getPitch());
position.setYaw(yaw);
position.setPitch(pitch);
this.cacheYaw = yaw;
this.cachePitch = pitch;
this.lastSyncedPosition.setYaw(yaw);
this.lastSyncedPosition.setPitch(pitch);
}
/**

View File

@ -149,8 +149,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
private final Set<Player> targetBreakers = Collections.singleton(this);
// Position synchronization with viewers
private double lastPlayerSyncX, lastPlayerSyncY, lastPlayerSyncZ;
private float lastPlayerSyncYaw, lastPlayerSyncPitch;
private final Position lastSyncedPlayerPosition;
// Experience orb pickup
protected Cooldown experiencePickupCooldown = new Cooldown(new UpdateOption(10, TimeUnit.TICK));
@ -188,6 +187,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
setBoundingBox(0.6f, 1.8f, 0.6f);
setRespawnPoint(new Position(0, 0, 0));
this.lastSyncedPlayerPosition = new Position();
this.settings = new PlayerSettings();
this.inventory = new PlayerInventory(this);
@ -424,11 +424,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
// Multiplayer sync
if (!viewers.isEmpty()) {
final boolean positionChanged = position.getX() != lastPlayerSyncX ||
position.getY() != lastPlayerSyncY ||
position.getZ() != lastPlayerSyncZ;
final boolean viewChanged = position.getYaw() != lastPlayerSyncYaw ||
position.getPitch() != lastPlayerSyncPitch;
final boolean positionChanged = !position.isSimilar(lastSyncedPlayerPosition);
final boolean viewChanged = !position.hasSimilarView(lastSyncedPlayerPosition);
if (positionChanged || viewChanged) {
// Player moved since last time
@ -437,10 +434,10 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
ServerPacket optionalUpdatePacket = null;
if (positionChanged && viewChanged) {
updatePacket = EntityPositionAndRotationPacket.getPacket(getEntityId(),
position, new Position(lastPlayerSyncX, lastPlayerSyncY, lastPlayerSyncZ), onGround);
position, lastSyncedPlayerPosition, onGround);
} else if (positionChanged) {
updatePacket = EntityPositionPacket.getPacket(getEntityId(),
position, new Position(lastPlayerSyncX, lastPlayerSyncY, lastPlayerSyncZ), onGround);
position, lastSyncedPlayerPosition, onGround);
} else {
// View changed
updatePacket = EntityRotationPacket.getPacket(getEntityId(),
@ -465,15 +462,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
}
// Update sync data
if (positionChanged) {
lastPlayerSyncX = position.getX();
lastPlayerSyncY = position.getY();
lastPlayerSyncZ = position.getZ();
}
if (viewChanged) {
lastPlayerSyncYaw = position.getYaw();
lastPlayerSyncPitch = position.getPitch();
}
lastSyncedPlayerPosition.set(position);
}
}