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 Instance instance;
protected Chunk currentChunk; protected Chunk currentChunk;
protected final Position position; protected final Position position;
protected double lastX, lastY, lastZ; /**
protected double cacheX, cacheY, cacheZ; // Used to synchronize with #getPosition * Used to calculate delta movement
protected float lastYaw, lastPitch; */
protected float cacheYaw, cachePitch; 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; protected boolean onGround;
private BoundingBox boundingBox; private BoundingBox boundingBox;
@ -138,6 +143,8 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
this.entityType = entityType; this.entityType = entityType;
this.uuid = uuid; this.uuid = uuid;
this.position = new Position(); this.position = new Position();
this.lastPosition = new Position();
this.lastSyncedPosition = new Position();
setBoundingBox(entityType.getWidth(), entityType.getHeight(), entityType.getWidth()); 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) { public Entity(@NotNull EntityType entityType, @NotNull UUID uuid, @NotNull Position spawnPosition) {
this(entityType, uuid); this(entityType, uuid);
this.position.set(spawnPosition); this.position.set(spawnPosition);
this.lastX = spawnPosition.getX(); this.lastPosition.set(spawnPosition);
this.lastY = spawnPosition.getY();
this.lastZ = spawnPosition.getZ();
} }
@Deprecated @Deprecated
@ -492,12 +497,9 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
// Synchronization with updated fields in #getPosition() // Synchronization with updated fields in #getPosition()
{ {
final boolean positionChange = cacheX != position.getX() || final boolean positionChange = !position.isSimilar(lastSyncedPosition);
cacheY != position.getY() || final boolean viewChange = !position.hasSimilarView(lastSyncedPosition);
cacheZ != position.getZ(); final double distance = positionChange ? position.getDistance(lastSyncedPosition) : 0;
final boolean viewChange = cacheYaw != position.getYaw() ||
cachePitch != position.getPitch();
final double distance = positionChange ? position.getDistance(cacheX, cacheY, cacheZ) : 0;
if (distance >= 8 || (positionChange && isNettyClient)) { if (distance >= 8 || (positionChange && isNettyClient)) {
// Teleport has the priority over everything else // Teleport has the priority over everything else
@ -505,7 +507,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
} else if (positionChange && viewChange) { } else if (positionChange && viewChange) {
EntityPositionAndRotationPacket positionAndRotationPacket = EntityPositionAndRotationPacket positionAndRotationPacket =
EntityPositionAndRotationPacket.getPacket(getEntityId(), EntityPositionAndRotationPacket.getPacket(getEntityId(),
position, new Position(cacheX, cacheY, cacheZ), isOnGround()); position, lastSyncedPosition, isOnGround());
sendPacketToViewersAndSelf(positionAndRotationPacket); sendPacketToViewersAndSelf(positionAndRotationPacket);
@ -520,7 +522,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
} else if (positionChange) { } else if (positionChange) {
EntityPositionPacket entityPositionPacket = EntityPositionPacket.getPacket(getEntityId(), EntityPositionPacket entityPositionPacket = EntityPositionPacket.getPacket(getEntityId(),
position, new Position(cacheX, cacheY, cacheZ), isOnGround()); position, lastSyncedPosition, isOnGround());
sendPacketToViewersAndSelf(entityPositionPacket); sendPacketToViewersAndSelf(entityPositionPacket);
@ -869,11 +871,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
} }
this.position.set(spawnPosition); this.position.set(spawnPosition);
this.lastX = this.position.getX(); this.lastPosition.set(position);
this.lastY = this.position.getY();
this.lastZ = this.position.getZ();
this.lastYaw = this.position.getYaw();
this.lastPitch = this.position.getPitch();
this.isActive = true; this.isActive = true;
this.instance = instance; this.instance = instance;
@ -1320,9 +1318,9 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
position.setX(x); position.setX(x);
position.setY(y); position.setY(y);
position.setZ(z); position.setZ(z);
this.cacheX = x; lastSyncedPosition.setX(x);
this.cacheY = y; lastSyncedPosition.setY(y);
this.cacheZ = z; lastSyncedPosition.setZ(z);
if (hasPassenger()) { if (hasPassenger()) {
for (Entity passenger : getPassengers()) { for (Entity passenger : getPassengers()) {
@ -1355,9 +1353,9 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
} }
} }
this.lastX = position.getX(); this.lastPosition.setX(position.getX());
this.lastY = position.getY(); this.lastPosition.setY(position.getY());
this.lastZ = position.getZ(); this.lastPosition.setZ(position.getZ());
} }
/** /**
@ -1377,12 +1375,12 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
* @param pitch the pitch * @param pitch the pitch
*/ */
public void refreshView(float yaw, float pitch) { public void refreshView(float yaw, float pitch) {
this.lastYaw = position.getYaw(); this.lastPosition.setYaw(position.getYaw());
this.lastPitch = position.getPitch(); this.lastPosition.setPitch(position.getPitch());
position.setYaw(yaw); position.setYaw(yaw);
position.setPitch(pitch); position.setPitch(pitch);
this.cacheYaw = yaw; this.lastSyncedPosition.setYaw(yaw);
this.cachePitch = pitch; 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); private final Set<Player> targetBreakers = Collections.singleton(this);
// Position synchronization with viewers // Position synchronization with viewers
private double lastPlayerSyncX, lastPlayerSyncY, lastPlayerSyncZ; private final Position lastSyncedPlayerPosition;
private float lastPlayerSyncYaw, lastPlayerSyncPitch;
// Experience orb pickup // Experience orb pickup
protected Cooldown experiencePickupCooldown = new Cooldown(new UpdateOption(10, TimeUnit.TICK)); 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); setBoundingBox(0.6f, 1.8f, 0.6f);
setRespawnPoint(new Position(0, 0, 0)); setRespawnPoint(new Position(0, 0, 0));
this.lastSyncedPlayerPosition = new Position();
this.settings = new PlayerSettings(); this.settings = new PlayerSettings();
this.inventory = new PlayerInventory(this); this.inventory = new PlayerInventory(this);
@ -424,11 +424,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
// Multiplayer sync // Multiplayer sync
if (!viewers.isEmpty()) { if (!viewers.isEmpty()) {
final boolean positionChanged = position.getX() != lastPlayerSyncX || final boolean positionChanged = !position.isSimilar(lastSyncedPlayerPosition);
position.getY() != lastPlayerSyncY || final boolean viewChanged = !position.hasSimilarView(lastSyncedPlayerPosition);
position.getZ() != lastPlayerSyncZ;
final boolean viewChanged = position.getYaw() != lastPlayerSyncYaw ||
position.getPitch() != lastPlayerSyncPitch;
if (positionChanged || viewChanged) { if (positionChanged || viewChanged) {
// Player moved since last time // Player moved since last time
@ -437,10 +434,10 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
ServerPacket optionalUpdatePacket = null; ServerPacket optionalUpdatePacket = null;
if (positionChanged && viewChanged) { if (positionChanged && viewChanged) {
updatePacket = EntityPositionAndRotationPacket.getPacket(getEntityId(), updatePacket = EntityPositionAndRotationPacket.getPacket(getEntityId(),
position, new Position(lastPlayerSyncX, lastPlayerSyncY, lastPlayerSyncZ), onGround); position, lastSyncedPlayerPosition, onGround);
} else if (positionChanged) { } else if (positionChanged) {
updatePacket = EntityPositionPacket.getPacket(getEntityId(), updatePacket = EntityPositionPacket.getPacket(getEntityId(),
position, new Position(lastPlayerSyncX, lastPlayerSyncY, lastPlayerSyncZ), onGround); position, lastSyncedPlayerPosition, onGround);
} else { } else {
// View changed // View changed
updatePacket = EntityRotationPacket.getPacket(getEntityId(), updatePacket = EntityRotationPacket.getPacket(getEntityId(),
@ -465,15 +462,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
} }
// Update sync data // Update sync data
if (positionChanged) { lastSyncedPlayerPosition.set(position);
lastPlayerSyncX = position.getX();
lastPlayerSyncY = position.getY();
lastPlayerSyncZ = position.getZ();
}
if (viewChanged) {
lastPlayerSyncYaw = position.getYaw();
lastPlayerSyncPitch = position.getPitch();
}
} }
} }