Merged Entity#sendSynchronization and Player#updatePlayerPosition to #sendTeleportPacket and increased scheduled sync delay

This commit is contained in:
Németh Noel 2021-05-01 00:05:49 +02:00
parent 5af6ebfeb6
commit ea368876af
2 changed files with 27 additions and 34 deletions

View File

@ -111,7 +111,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
protected EntityType entityType; // UNSAFE to change, modify at your own risk
// Network synchronization, send the absolute position of the entity each X milliseconds
private static final UpdateOption SYNCHRONIZATION_COOLDOWN = new UpdateOption(1500, TimeUnit.MILLISECOND);
private static final UpdateOption SYNCHRONIZATION_COOLDOWN = new UpdateOption(1, TimeUnit.MINUTE);
private UpdateOption customSynchronizationCooldown;
private long lastAbsoluteSynchronizationTime;
@ -269,7 +269,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
refreshPosition(teleportPosition);
refreshView(teleportPosition.getYaw(), teleportPosition.getPitch());
sendSynchronization();
sendTeleportPacket(teleportPosition);
OptionalCallback.execute(callback);
};
@ -634,7 +634,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
// Synchronization and packets...
if (!isNettyClient) {
sendSynchronization();
sendTeleportPacket(position.clone());
}
// Verify if velocity packet has to be sent
if (hasVelocity() || (!isNettyClient && gravityTickCount > 0)) {
@ -705,8 +705,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
// Scheduled synchronization
if (!Cooldown.hasCooldown(time, lastAbsoluteSynchronizationTime, getSynchronizationCooldown())) {
this.lastAbsoluteSynchronizationTime = time;
sendSynchronization();
sendTeleportPacket(position.clone());
}
if (shouldRemove() && !MinecraftServer.isStopping()) {
@ -1539,12 +1538,21 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
return metaDataPacket;
}
protected void sendSynchronization() {
EntityTeleportPacket entityTeleportPacket = new EntityTeleportPacket();
/**
* Used to synchronize entity position with viewers
*
* @param pos Should be {@link Entity#position#clone()}
*/
@ApiStatus.Internal
protected void sendTeleportPacket(final Position pos) {
final EntityTeleportPacket entityTeleportPacket = new EntityTeleportPacket();
entityTeleportPacket.entityId = getEntityId();
entityTeleportPacket.position = getPosition().clone();
entityTeleportPacket.position = pos;
entityTeleportPacket.onGround = isOnGround();
sendPacketToViewers(entityTeleportPacket);
this.lastAbsoluteSynchronizationTime = System.currentTimeMillis();
this.lastSyncedPosition.set(pos);
}
/**

View File

@ -74,6 +74,7 @@ import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.time.UpdateOption;
import net.minestom.server.utils.validate.Check;
import net.minestom.server.world.DimensionType;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -714,7 +715,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
}
if (dimensionChange || firstSpawn) {
updatePlayerPosition(); // So the player doesn't get stuck
sendTeleportPacket(position.clone()); // So the player doesn't get stuck
this.inventory.update();
}
@ -1665,27 +1666,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
}
@Override
public void teleport(@NotNull Position position, @Nullable long[] chunks, @Nullable Runnable callback) {
super.teleport(position, chunks, () -> {
updatePlayerPosition();
OptionalCallback.execute(callback);
});
}
@Override
public void teleport(@NotNull Position position, @Nullable Runnable callback) {
final boolean sameChunk = getPosition().inSameChunk(position);
final long[] chunks = sameChunk ? null :
ChunkUtils.getChunksInRange(position, getChunkRange());
teleport(position, chunks, callback);
}
@Override
public void teleport(@NotNull Position position) {
teleport(position, null);
}
/**
* Gets the player connection.
* <p>
@ -2041,14 +2021,19 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
}
/**
* Used for synchronization purpose, mainly for teleportation
* @see Entity#sendTeleportPacket(Position)
* @param pos Should be {@link Entity#position#clone()}
*/
protected void updatePlayerPosition() {
PlayerPositionAndLookPacket positionAndLookPacket = new PlayerPositionAndLookPacket();
positionAndLookPacket.position = position.clone(); // clone needed to prevent synchronization issue
@Override
@ApiStatus.Internal
protected void sendTeleportPacket(final Position pos) {
final PlayerPositionAndLookPacket positionAndLookPacket = new PlayerPositionAndLookPacket();
positionAndLookPacket.position = pos;
positionAndLookPacket.flags = 0x00;
positionAndLookPacket.teleportId = teleportId.incrementAndGet();
playerConnection.sendPacket(positionAndLookPacket);
super.sendTeleportPacket(pos);
}
/**