| 1 | package net.minecraft.server.level;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.Collections;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.function.Consumer;
|
| 12 | import java.util.function.Predicate;
|
| 13 | import net.minecraft.core.component.DataComponents;
|
| 14 | import net.minecraft.network.protocol.Packet;
|
| 15 | import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
| 16 | import net.minecraft.network.protocol.game.ClientboundBundlePacket;
|
| 17 | import net.minecraft.network.protocol.game.ClientboundEntityPositionSyncPacket;
|
| 18 | import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket;
|
| 19 | import net.minecraft.network.protocol.game.ClientboundMoveMinecartPacket;
|
| 20 | import net.minecraft.network.protocol.game.ClientboundProjectilePowerPacket;
|
| 21 | import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
| 22 | import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
|
| 23 | import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
| 24 | import net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket;
|
| 25 | import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket;
|
| 26 | import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
|
| 27 | import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
| 28 | import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket;
|
| 29 | import net.minecraft.network.protocol.game.MovementPacket;
|
| 30 | import net.minecraft.network.protocol.game.VecDelta;
|
| 31 | import net.minecraft.network.protocol.game.VecDeltaCodec;
|
| 32 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 33 | import net.minecraft.util.Mth;
|
| 34 | import net.minecraft.world.entity.Entity;
|
| 35 | import net.minecraft.world.entity.EquipmentSlot;
|
| 36 | import net.minecraft.world.entity.InterpolationTracker;
|
| 37 | import net.minecraft.world.entity.Leashable;
|
| 38 | import net.minecraft.world.entity.LivingEntity;
|
| 39 | import net.minecraft.world.entity.PositionPath;
|
| 40 | import net.minecraft.world.entity.UpdateInterval;
|
| 41 | import net.minecraft.world.entity.ai.attributes.AttributeInstance;
|
| 42 | import net.minecraft.world.entity.decoration.ItemFrame;
|
| 43 | import net.minecraft.world.entity.item.ItemEntity;
|
| 44 | import net.minecraft.world.entity.projectile.arrow.AbstractArrow;
|
| 45 | import net.minecraft.world.entity.projectile.hurtingprojectile.AbstractHurtingProjectile;
|
| 46 | import net.minecraft.world.entity.vehicle.minecart.AbstractMinecart;
|
| 47 | import net.minecraft.world.entity.vehicle.minecart.NewMinecartBehavior;
|
| 48 | import net.minecraft.world.item.ItemStack;
|
| 49 | import net.minecraft.world.item.MapItem;
|
| 50 | import net.minecraft.world.level.saveddata.maps.MapId;
|
| 51 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 52 | import net.minecraft.world.phys.Vec3;
|
| 53 | import org.jspecify.annotations.Nullable;
|
| 54 | import org.slf4j.Logger;
|
| 55 |
|
| 56 | public class ServerEntity {
|
| 57 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 58 | private static final int TOLERANCE_LEVEL_ROTATION = 1;
|
| 59 | private static final double TOLERANCE_LEVEL_POSITION = 7.6293945E-6F;
|
| 60 | public static final int FORCED_POS_UPDATE_PERIOD = 60;
|
| 61 | private static final int FORCED_TELEPORT_PERIOD = 400;
|
| 62 | private final ServerLevel level;
|
| 63 | private final Entity entity;
|
| 64 | private final UpdateInterval updateInterval;
|
| 65 | private final boolean trackDelta;
|
| 66 | private final ServerEntity.Synchronizer synchronizer;
|
| 67 | private final InterpolationTracker interpolationTracker;
|
| 68 | private final VecDeltaCodec positionCodec = new VecDeltaCodec();
|
| 69 | private byte lastSentYRot;
|
| 70 | private byte lastSentXRot;
|
| 71 | private byte lastSentYHeadRot;
|
| 72 | private Vec3 lastSentMovement;
|
| 73 | private int tickCount;
|
| 74 | private int teleportDelay;
|
| 75 | private List<Entity> lastPassengers = Collections.emptyList();
|
| 76 | private boolean wasRiding;
|
| 77 | private boolean wasOnGround;
|
| 78 | private @Nullable List<SynchedEntityData.DataValue<?>> trackedDataValues;
|
| 79 |
|
| 80 | public ServerEntity(
|
| 81 | final ServerLevel level, final Entity entity, final UpdateInterval updateInterval, final boolean trackDelta, final ServerEntity.Synchronizer synchronizer
|
| 82 | ) {
|
| 83 | this.level = level;
|
| 84 | this.synchronizer = synchronizer;
|
| 85 | this.entity = entity;
|
| 86 | this.updateInterval = updateInterval;
|
| 87 | this.trackDelta = trackDelta;
|
| 88 | this.interpolationTracker = entity.getInterpolation().interpolationTracker();
|
| 89 | this.positionCodec.setBase(entity.trackingPosition());
|
| 90 | this.lastSentMovement = entity.getDeltaMovement();
|
| 91 | this.lastSentYRot = Mth.packDegrees(entity.getYRot());
|
| 92 | this.lastSentXRot = Mth.packDegrees(entity.getXRot());
|
| 93 | this.lastSentYHeadRot = Mth.packDegrees(entity.getYHeadRot());
|
| 94 | this.wasOnGround = entity.onGround();
|
| 95 | this.trackedDataValues = entity.getEntityData().getNonDefaultValues();
|
| 96 | }
|
| 97 |
|
| 98 | public void sendChanges() {
|
| 99 | this.entity.updateDataBeforeSync();
|
| 100 | List<Entity> passengers = this.entity.getPassengers();
|
| 101 | if (!passengers.equals(this.lastPassengers)) {
|
| 102 | this.synchronizer.sendToTrackingPlayers(new ClientboundSetPassengersPacket(this.entity));
|
| 103 | this.lastPassengers = passengers;
|
| 104 | }
|
| 105 |
|
| 106 | if (this.entity instanceof ItemFrame frame && this.tickCount % 10 == 0) {
|
| 107 | ItemStack itemStack = frame.getItem();
|
| 108 | if (itemStack.getItem() instanceof MapItem) {
|
| 109 | MapId id = itemStack.get(DataComponents.MAP_ID);
|
| 110 | MapItemSavedData data = MapItem.getSavedData(id, this.level);
|
| 111 | if (data != null) {
|
| 112 | for (ServerPlayer player : this.level.players()) {
|
| 113 | data.tickCarriedBy(player, itemStack, frame);
|
| 114 | Packet<?> packet = data.getUpdatePacket(id, player);
|
| 115 | if (packet != null) {
|
| 116 | player.connection.send(packet);
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | this.sendDirtyEntityData();
|
| 123 | }
|
| 124 |
|
| 125 | Vec3 currentPosition = this.entity.trackingPosition();
|
| 126 | this.interpolationTracker.updateTracking(currentPosition);
|
| 127 | if (this.entity.syncPosition) {
|
| 128 | this.tickCount = this.updateInterval.nextInterval(this.tickCount);
|
| 129 | this.entity.syncPosition = false;
|
| 130 | }
|
| 131 |
|
| 132 | if (this.entity.needsSync || this.updateInterval.test(this.tickCount) || this.entity.getEntityData().isDirty()) {
|
| 133 | byte yRotn = Mth.packDegrees(this.entity.getYRot());
|
| 134 | byte xRotn = Mth.packDegrees(this.entity.getXRot());
|
| 135 | boolean shouldSendRotation = Math.abs(yRotn - this.lastSentYRot) >= 1 || Math.abs(xRotn - this.lastSentXRot) >= 1;
|
| 136 | if (this.entity.isPassenger()) {
|
| 137 | if (shouldSendRotation) {
|
| 138 | this.synchronizer.sendToTrackingPlayers(new ClientboundMoveEntityPacket.Rot(this.entity.getId(), yRotn, xRotn, this.entity.onGround()));
|
| 139 | this.lastSentYRot = yRotn;
|
| 140 | this.lastSentXRot = xRotn;
|
| 141 | }
|
| 142 |
|
| 143 | this.interpolationTracker.clear();
|
| 144 | this.positionCodec.setBase(currentPosition);
|
| 145 | this.sendDirtyEntityData();
|
| 146 | this.wasRiding = true;
|
| 147 | } else if (this.entity instanceof AbstractMinecart minecart && minecart.getBehavior() instanceof NewMinecartBehavior newMinecartBehavior) {
|
| 148 | this.handleMinecartPosRot(newMinecartBehavior, yRotn, xRotn, shouldSendRotation);
|
| 149 | } else {
|
| 150 | this.teleportDelay++;
|
| 151 | PositionPath position = this.interpolationTracker.getPositionPath(currentPosition);
|
| 152 | this.interpolationTracker.clear();
|
| 153 | MovementPacket<ClientGamePacketListener> packet;
|
| 154 | if (this.entity instanceof AbstractArrow) {
|
| 155 | packet = this.createMovePacket(position, yRotn, xRotn, true, true);
|
| 156 | } else {
|
| 157 | boolean positionChanged = this.positionCodec.delta(currentPosition).lengthSqr() >= 7.6293945E-6F;
|
| 158 | boolean shouldSendPosition = positionChanged || this.tickCount % 60 == 0;
|
| 159 | packet = this.createMovePacket(position, yRotn, xRotn, shouldSendPosition, shouldSendRotation);
|
| 160 | }
|
| 161 |
|
| 162 | if (this.entity.needsSync || this.trackDelta || this.entity instanceof LivingEntity livingEntity && livingEntity.isFallFlying()) {
|
| 163 | Vec3 movement = this.entity.getDeltaMovement();
|
| 164 | double diff = movement.distanceToSqr(this.lastSentMovement);
|
| 165 | if (diff > 1.0E-7 || diff > 0.0 && movement.lengthSqr() == 0.0) {
|
| 166 | this.lastSentMovement = movement;
|
| 167 | if (this.entity instanceof AbstractHurtingProjectile projectile) {
|
| 168 | this.synchronizer
|
| 169 | .sendToTrackingPlayers(
|
| 170 | new ClientboundBundlePacket(
|
| 171 | List.of(
|
| 172 | new ClientboundSetEntityMotionPacket(this.entity.getId(), this.lastSentMovement),
|
| 173 | new ClientboundProjectilePowerPacket(projectile.getId(), projectile.accelerationPower)
|
| 174 | )
|
| 175 | )
|
| 176 | );
|
| 177 | } else {
|
| 178 | this.synchronizer.sendToTrackingPlayers(new ClientboundSetEntityMotionPacket(this.entity.getId(), this.lastSentMovement));
|
| 179 | }
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | if (packet != null) {
|
| 184 | this.synchronizer.sendToTrackingPlayers(packet);
|
| 185 | this.sendDirtyEntityData();
|
| 186 | if (packet.hasPosition()) {
|
| 187 | this.positionCodec.setBase(currentPosition);
|
| 188 | }
|
| 189 |
|
| 190 | if (packet.hasRotation()) {
|
| 191 | this.lastSentYRot = yRotn;
|
| 192 | this.lastSentXRot = xRotn;
|
| 193 | }
|
| 194 | } else {
|
| 195 | this.sendDirtyEntityData();
|
| 196 | }
|
| 197 |
|
| 198 | this.wasRiding = false;
|
| 199 | }
|
| 200 |
|
| 201 | byte yHeadRot = Mth.packDegrees(this.entity.getYHeadRot());
|
| 202 | if (Math.abs(yHeadRot - this.lastSentYHeadRot) >= 1) {
|
| 203 | this.synchronizer.sendToTrackingPlayers(new ClientboundRotateHeadPacket(this.entity, yHeadRot));
|
| 204 | this.lastSentYHeadRot = yHeadRot;
|
| 205 | }
|
| 206 |
|
| 207 | this.entity.needsSync = false;
|
| 208 | }
|
| 209 |
|
| 210 | this.tickCount++;
|
| 211 | if (this.entity.syncVelocity) {
|
| 212 | this.entity.syncVelocity = false;
|
| 213 | this.synchronizer.sendToTrackingPlayersAndSelf(new ClientboundSetEntityMotionPacket(this.entity));
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | private @Nullable MovementPacket<ClientGamePacketListener> createMovePacket(
|
| 218 | final PositionPath position, final byte yRotn, final byte xRotn, final boolean shouldSendPosition, final boolean shouldSendRotation
|
| 219 | ) {
|
| 220 | if (this.entity.getRequiresPrecisePosition() || this.teleportDelay > 400 || this.wasRiding || this.wasOnGround != this.entity.onGround()) {
|
| 221 | this.wasOnGround = this.entity.onGround();
|
| 222 | this.teleportDelay = 0;
|
| 223 | return ClientboundEntityPositionSyncPacket.of(this.entity, position);
|
| 224 | }
|
| 225 |
|
| 226 | if (shouldSendPosition) {
|
| 227 | VecDelta encodedDelta = this.positionCodec.tryEncode(position);
|
| 228 | if (encodedDelta == null || this.isFullPrecisionEncodingRequired(position.endPosition(), encodedDelta)) {
|
| 229 | return ClientboundEntityPositionSyncPacket.of(this.entity, position);
|
| 230 | } else {
|
| 231 | return shouldSendRotation
|
| 232 | ? new ClientboundMoveEntityPacket.PosRot(this.entity.getId(), encodedDelta, yRotn, xRotn, this.entity.onGround())
|
| 233 | : new ClientboundMoveEntityPacket.Pos(this.entity.getId(), encodedDelta, this.entity.onGround());
|
| 234 | }
|
| 235 | } else {
|
| 236 | return shouldSendRotation ? new ClientboundMoveEntityPacket.Rot(this.entity.getId(), yRotn, xRotn, this.entity.onGround()) : null;
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | private boolean isFullPrecisionEncodingRequired(final Vec3 pos, final VecDelta delta) {
|
| 241 | if (this.entity instanceof ItemEntity) {
|
| 242 | if (this.entity.verticalCollision
|
| 243 | && (delta.hasDeltaX() && VecDeltaCodec.encodingPrecisionLoss(pos.x) != 0.0 || delta.hasDeltaZ() && VecDeltaCodec.encodingPrecisionLoss(pos.z) != 0.0)) {
|
| 244 | return true;
|
| 245 | }
|
| 246 |
|
| 247 | if (this.entity.horizontalCollision
|
| 248 | && (delta.hasDeltaX() && VecDeltaCodec.encodingPrecisionLoss(pos.z) != 0.0 || delta.hasDeltaZ() && VecDeltaCodec.encodingPrecisionLoss(pos.x) != 0.0)) {
|
| 249 | return true;
|
| 250 | }
|
| 251 | }
|
| 252 |
|
| 253 | return false;
|
| 254 | }
|
| 255 |
|
| 256 | private void handleMinecartPosRot(final NewMinecartBehavior newMinecartBehavior, final byte yRotn, final byte xRotn, final boolean shouldSendRotation) {
|
| 257 | this.sendDirtyEntityData();
|
| 258 | if (newMinecartBehavior.lerpSteps.isEmpty()) {
|
| 259 | Vec3 movement = this.entity.getDeltaMovement();
|
| 260 | double diff = movement.distanceToSqr(this.lastSentMovement);
|
| 261 | Vec3 currentPosition = this.entity.trackingPosition();
|
| 262 | boolean positionChanged = this.positionCodec.delta(currentPosition).lengthSqr() >= 7.6293945E-6F;
|
| 263 | boolean shouldSendPosition = positionChanged || this.tickCount % 60 == 0;
|
| 264 | if (shouldSendPosition || shouldSendRotation || diff > 1.0E-7) {
|
| 265 | this.synchronizer
|
| 266 | .sendToTrackingPlayers(
|
| 267 | new ClientboundMoveMinecartPacket(
|
| 268 | this.entity.getId(),
|
| 269 | List.of(new NewMinecartBehavior.MinecartStep(this.entity.position(), this.entity.getDeltaMovement(), this.entity.getYRot(), this.entity.getXRot(), 1.0F))
|
| 270 | )
|
| 271 | );
|
| 272 | }
|
| 273 | } else {
|
| 274 | this.synchronizer.sendToTrackingPlayers(new ClientboundMoveMinecartPacket(this.entity.getId(), List.copyOf(newMinecartBehavior.lerpSteps)));
|
| 275 | newMinecartBehavior.lerpSteps.clear();
|
| 276 | }
|
| 277 |
|
| 278 | this.lastSentYRot = yRotn;
|
| 279 | this.lastSentXRot = xRotn;
|
| 280 | this.positionCodec.setBase(this.entity.position());
|
| 281 | }
|
| 282 |
|
| 283 | public void removePairing(final ServerPlayer player) {
|
| 284 | this.entity.stopSeenByPlayer(player);
|
| 285 | player.connection.send(new ClientboundRemoveEntitiesPacket(this.entity.getId()));
|
| 286 | }
|
| 287 |
|
| 288 | public void addPairing(final ServerPlayer player) {
|
| 289 | List<Packet<? super ClientGamePacketListener>> packets = new ArrayList<>();
|
| 290 | this.sendPairingData(player, packets::add);
|
| 291 | player.connection.send(new ClientboundBundlePacket(packets));
|
| 292 | this.entity.startSeenByPlayer(player);
|
| 293 | }
|
| 294 |
|
| 295 | public void sendPairingData(final ServerPlayer player, final Consumer<Packet<ClientGamePacketListener>> broadcast) {
|
| 296 | this.entity.updateDataBeforeSync();
|
| 297 | if (this.entity.isRemoved()) {
|
| 298 | LOGGER.warn("Fetching packet for removed entity {}", this.entity);
|
| 299 | }
|
| 300 |
|
| 301 | Packet<ClientGamePacketListener> packet = this.entity.getAddEntityPacket(this);
|
| 302 | broadcast.accept(packet);
|
| 303 | if (this.trackedDataValues != null) {
|
| 304 | broadcast.accept(new ClientboundSetEntityDataPacket(this.entity.getId(), this.trackedDataValues));
|
| 305 | }
|
| 306 |
|
| 307 | if (this.entity instanceof LivingEntity livingEntity) {
|
| 308 | Collection<AttributeInstance> attributes = livingEntity.getAttributes().getSyncableAttributes();
|
| 309 | if (!attributes.isEmpty()) {
|
| 310 | broadcast.accept(new ClientboundUpdateAttributesPacket(this.entity.getId(), attributes));
|
| 311 | }
|
| 312 | }
|
| 313 |
|
| 314 | if (this.entity instanceof LivingEntity livingEntity) {
|
| 315 | List<Pair<EquipmentSlot, ItemStack>> slots = Lists.newArrayList();
|
| 316 |
|
| 317 | for (EquipmentSlot slot : EquipmentSlot.VALUES) {
|
| 318 | ItemStack itemStack = livingEntity.getItemBySlot(slot);
|
| 319 | if (!itemStack.isEmpty()) {
|
| 320 | slots.add(Pair.of(slot, itemStack.copy()));
|
| 321 | }
|
| 322 | }
|
| 323 |
|
| 324 | if (!slots.isEmpty()) {
|
| 325 | broadcast.accept(new ClientboundSetEquipmentPacket(this.entity.getId(), slots));
|
| 326 | }
|
| 327 | }
|
| 328 |
|
| 329 | if (!this.entity.getPassengers().isEmpty()) {
|
| 330 | broadcast.accept(new ClientboundSetPassengersPacket(this.entity));
|
| 331 | }
|
| 332 |
|
| 333 | if (this.entity.isPassenger()) {
|
| 334 | broadcast.accept(new ClientboundSetPassengersPacket(this.entity.getVehicle()));
|
| 335 | }
|
| 336 |
|
| 337 | if (this.entity instanceof Leashable leashable && leashable.isLeashed()) {
|
| 338 | broadcast.accept(new ClientboundSetEntityLinkPacket(this.entity, leashable.getLeashHolder()));
|
| 339 | }
|
| 340 | }
|
| 341 |
|
| 342 | public Vec3 getPositionBase() {
|
| 343 | return this.positionCodec.getBase();
|
| 344 | }
|
| 345 |
|
| 346 | public Vec3 getLastSentMovement() {
|
| 347 | return this.lastSentMovement;
|
| 348 | }
|
| 349 |
|
| 350 | public float getLastSentXRot() {
|
| 351 | return Mth.unpackDegrees(this.lastSentXRot);
|
| 352 | }
|
| 353 |
|
| 354 | public float getLastSentYRot() {
|
| 355 | return Mth.unpackDegrees(this.lastSentYRot);
|
| 356 | }
|
| 357 |
|
| 358 | public float getLastSentYHeadRot() {
|
| 359 | return Mth.unpackDegrees(this.lastSentYHeadRot);
|
| 360 | }
|
| 361 |
|
| 362 | private void sendDirtyEntityData() {
|
| 363 | SynchedEntityData entityData = this.entity.getEntityData();
|
| 364 | List<SynchedEntityData.DataValue<?>> packedValues = entityData.packDirty();
|
| 365 | if (packedValues != null) {
|
| 366 | this.trackedDataValues = entityData.getNonDefaultValues();
|
| 367 | this.synchronizer.sendToTrackingPlayersAndSelf(new ClientboundSetEntityDataPacket(this.entity.getId(), packedValues));
|
| 368 | }
|
| 369 |
|
| 370 | if (this.entity instanceof LivingEntity livingEntity) {
|
| 371 | Set<AttributeInstance> attributes = livingEntity.getAttributes().getAttributesToSync();
|
| 372 | if (!attributes.isEmpty()) {
|
| 373 | this.synchronizer.sendToTrackingPlayersAndSelf(new ClientboundUpdateAttributesPacket(this.entity.getId(), attributes));
|
| 374 | }
|
| 375 |
|
| 376 | attributes.clear();
|
| 377 | }
|
| 378 | }
|
| 379 |
|
| 380 | public interface Synchronizer {
|
| 381 | void sendToTrackingPlayers(Packet<? super ClientGamePacketListener> packet);
|
| 382 |
|
| 383 | void sendToTrackingPlayersAndSelf(Packet<? super ClientGamePacketListener> packet);
|
| 384 |
|
| 385 | void sendToTrackingPlayersFiltered(Packet<? super ClientGamePacketListener> packet, Predicate<ServerPlayer> predicate);
|
| 386 | }
|
| 387 | }
|
| 388 |
|