Merge pull request #267 from Kebab11noel/position-cleanup

Position cleanup
This commit is contained in:
TheMode 2021-05-01 00:55:54 +02:00 committed by GitHub
commit c027d649cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 35 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();
synchronizePosition();
OptionalCallback.execute(callback);
};
@ -634,7 +634,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
// Synchronization and packets...
if (!isNettyClient) {
sendSynchronization();
synchronizePosition();
}
// 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();
synchronizePosition();
}
if (shouldRemove() && !MinecraftServer.isStopping()) {
@ -1539,12 +1538,23 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
return metaDataPacket;
}
protected void sendSynchronization() {
EntityTeleportPacket entityTeleportPacket = new EntityTeleportPacket();
/**
* Used to synchronize entity position with viewers by sending an
* {@link EntityTeleportPacket} to viewers, in case of a player this is
* overridden in order to send an additional {@link PlayerPositionAndLookPacket}
* to itself.
*/
@ApiStatus.Internal
protected void synchronizePosition() {
final Position pos = position.clone();
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

@ -63,7 +63,6 @@ import net.minestom.server.sound.SoundCategory;
import net.minestom.server.sound.SoundEvent;
import net.minestom.server.stat.PlayerStatistic;
import net.minestom.server.utils.*;
import net.minestom.server.utils.callback.OptionalCallback;
import net.minestom.server.utils.chunk.ChunkCallback;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.entity.EntityUtils;
@ -74,6 +73,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 +714,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
}
if (dimensionChange || firstSpawn) {
updatePlayerPosition(); // So the player doesn't get stuck
synchronizePosition(); // So the player doesn't get stuck
this.inventory.update();
}
@ -1665,27 +1665,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 +2020,18 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
}
/**
* Used for synchronization purpose, mainly for teleportation
* @see Entity#synchronizePosition()
*/
protected void updatePlayerPosition() {
PlayerPositionAndLookPacket positionAndLookPacket = new PlayerPositionAndLookPacket();
positionAndLookPacket.position = position.clone(); // clone needed to prevent synchronization issue
@Override
@ApiStatus.Internal
protected void synchronizePosition() {
final PlayerPositionAndLookPacket positionAndLookPacket = new PlayerPositionAndLookPacket();
positionAndLookPacket.position = position.clone();
positionAndLookPacket.flags = 0x00;
positionAndLookPacket.teleportId = teleportId.incrementAndGet();
playerConnection.sendPacket(positionAndLookPacket);
super.synchronizePosition();
}
/**