| 1 | package net.minecraft.server.network;
|
| 2 |
|
| 3 | import com.google.common.collect.Comparators;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
| 6 | import it.unimi.dsi.fastutil.longs.LongSet;
|
| 7 | import java.util.Comparator;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Objects;
|
| 10 | import net.minecraft.SharedConstants;
|
| 11 | import net.minecraft.network.protocol.game.ClientboundChunkBatchFinishedPacket;
|
| 12 | import net.minecraft.network.protocol.game.ClientboundChunkBatchStartPacket;
|
| 13 | import net.minecraft.network.protocol.game.ClientboundForgetLevelChunkPacket;
|
| 14 | import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket;
|
| 15 | import net.minecraft.server.level.ChunkMap;
|
| 16 | import net.minecraft.server.level.ServerLevel;
|
| 17 | import net.minecraft.server.level.ServerPlayer;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.world.level.ChunkPos;
|
| 20 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | public class PlayerChunkSender {
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 | public static final float MIN_CHUNKS_PER_TICK = 0.01F;
|
| 26 | public static final float MAX_CHUNKS_PER_TICK = 64.0F;
|
| 27 | private static final float START_CHUNKS_PER_TICK = 9.0F;
|
| 28 | private static final int MAX_UNACKNOWLEDGED_BATCHES = 10;
|
| 29 | private final LongSet pendingChunks = new LongOpenHashSet();
|
| 30 | private final boolean memoryConnection;
|
| 31 | private float desiredChunksPerTick = 9.0F;
|
| 32 | private float batchQuota;
|
| 33 | private int unacknowledgedBatches;
|
| 34 | private int maxUnacknowledgedBatches = 1;
|
| 35 |
|
| 36 | public PlayerChunkSender(final boolean memoryConnection) {
|
| 37 | this.memoryConnection = memoryConnection;
|
| 38 | }
|
| 39 |
|
| 40 | public void markChunkPendingToSend(final LevelChunk chunk) {
|
| 41 | this.pendingChunks.add(chunk.getPos().pack());
|
| 42 | }
|
| 43 |
|
| 44 | public void dropChunk(final ServerPlayer player, final ChunkPos pos) {
|
| 45 | if (!this.pendingChunks.remove(pos.pack()) && player.isAlive()) {
|
| 46 | player.connection.send(new ClientboundForgetLevelChunkPacket(pos));
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | public void sendNextChunks(final ServerPlayer player) {
|
| 51 | if (this.unacknowledgedBatches < this.maxUnacknowledgedBatches) {
|
| 52 | float maxBatchSize = Math.max(1.0F, this.desiredChunksPerTick);
|
| 53 | this.batchQuota = Math.min(this.batchQuota + this.desiredChunksPerTick, maxBatchSize);
|
| 54 | if (!(this.batchQuota < 1.0F)) {
|
| 55 | if (!this.pendingChunks.isEmpty()) {
|
| 56 | ServerLevel level = player.level();
|
| 57 | ChunkMap chunkMap = level.getChunkSource().chunkMap;
|
| 58 | List<LevelChunk> chunksToSend = this.collectChunksToSend(chunkMap, player.chunkPosition());
|
| 59 | if (!chunksToSend.isEmpty()) {
|
| 60 | ServerGamePacketListenerImpl connection = player.connection;
|
| 61 | this.unacknowledgedBatches++;
|
| 62 | connection.send(ClientboundChunkBatchStartPacket.INSTANCE);
|
| 63 |
|
| 64 | for (LevelChunk chunk : chunksToSend) {
|
| 65 | sendChunk(connection, level, chunk);
|
| 66 | }
|
| 67 |
|
| 68 | connection.send(new ClientboundChunkBatchFinishedPacket(chunksToSend.size()));
|
| 69 | this.batchQuota = this.batchQuota - chunksToSend.size();
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | private static void sendChunk(final ServerGamePacketListenerImpl connection, final ServerLevel level, final LevelChunk chunk) {
|
| 77 | connection.send(new ClientboundLevelChunkWithLightPacket(chunk, level.getLightEngine(), null, null));
|
| 78 | ChunkPos pos = chunk.getPos();
|
| 79 | if (SharedConstants.DEBUG_VERBOSE_SERVER_EVENTS) {
|
| 80 | LOGGER.debug("SEN {}", pos);
|
| 81 | }
|
| 82 |
|
| 83 | level.debugSynchronizers().startTrackingChunk(connection.player, chunk.getPos());
|
| 84 | }
|
| 85 |
|
| 86 | private List<LevelChunk> collectChunksToSend(final ChunkMap chunkMap, final ChunkPos playerPos) {
|
| 87 | int maxBatchSize = Mth.floor(this.batchQuota);
|
| 88 | List<LevelChunk> chunks;
|
| 89 | if (!this.memoryConnection && this.pendingChunks.size() > maxBatchSize) {
|
| 90 | chunks = this.pendingChunks
|
| 91 | .stream()
|
| 92 | .collect(Comparators.least(maxBatchSize, Comparator.comparingInt(playerPos::distanceSquared)))
|
| 93 | .stream()
|
| 94 | .mapToLong(Long::longValue)
|
| 95 | .mapToObj(chunkMap::getChunkToSend)
|
| 96 | .filter(Objects::nonNull)
|
| 97 | .toList();
|
| 98 | } else {
|
| 99 | chunks = this.pendingChunks
|
| 100 | .longStream()
|
| 101 | .mapToObj(chunkMap::getChunkToSend)
|
| 102 | .filter(Objects::nonNull)
|
| 103 | .sorted(Comparator.comparingInt(chunkx -> playerPos.distanceSquared(chunkx.getPos())))
|
| 104 | .toList();
|
| 105 | }
|
| 106 |
|
| 107 | for (LevelChunk chunk : chunks) {
|
| 108 | this.pendingChunks.remove(chunk.getPos().pack());
|
| 109 | }
|
| 110 |
|
| 111 | return chunks;
|
| 112 | }
|
| 113 |
|
| 114 | public void onChunkBatchReceivedByClient(final float desiredChunksPerTick) {
|
| 115 | this.unacknowledgedBatches--;
|
| 116 | if (this.unacknowledgedBatches < 0) {
|
| 117 | this.unacknowledgedBatches = 0;
|
| 118 | }
|
| 119 |
|
| 120 | this.desiredChunksPerTick = Double.isNaN(desiredChunksPerTick) ? 0.01F : Mth.clamp(desiredChunksPerTick, 0.01F, 64.0F);
|
| 121 | if (this.unacknowledgedBatches == 0) {
|
| 122 | this.batchQuota = 1.0F;
|
| 123 | }
|
| 124 |
|
| 125 | this.maxUnacknowledgedBatches = 10;
|
| 126 | }
|
| 127 |
|
| 128 | public boolean isPending(final long pos) {
|
| 129 | return this.pendingChunks.contains(pos);
|
| 130 | }
|
| 131 | }
|
| 132 |
|