| 1 | package net.minecraft.client.multiplayer;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
| 5 | import java.io.FileOutputStream;
|
| 6 | import java.io.IOException;
|
| 7 | import java.nio.charset.StandardCharsets;
|
| 8 | import java.util.concurrent.atomic.AtomicReferenceArray;
|
| 9 | import java.util.function.BooleanSupplier;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.Minecraft;
|
| 13 | import net.minecraft.core.SectionPos;
|
| 14 | import net.minecraft.core.registries.Registries;
|
| 15 | import net.minecraft.network.FriendlyByteBuf;
|
| 16 | import net.minecraft.network.protocol.game.ClientboundLevelChunkPacketData;
|
| 17 | import net.minecraft.world.level.BlockGetter;
|
| 18 | import net.minecraft.world.level.ChunkPos;
|
| 19 | import net.minecraft.world.level.LightLayer;
|
| 20 | import net.minecraft.world.level.biome.Biomes;
|
| 21 | import net.minecraft.world.level.chunk.ChunkSource;
|
| 22 | import net.minecraft.world.level.chunk.EmptyLevelChunk;
|
| 23 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 24 | import net.minecraft.world.level.chunk.LevelChunkSection;
|
| 25 | import net.minecraft.world.level.chunk.status.ChunkStatus;
|
| 26 | import net.minecraft.world.level.lighting.LevelLightEngine;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 | import org.slf4j.Logger;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public class ClientChunkCache extends ChunkSource {
|
| 32 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 33 | private final LevelChunk emptyChunk;
|
| 34 | private final LevelLightEngine lightEngine;
|
| 35 | private volatile ClientChunkCache.Storage storage;
|
| 36 | private final ClientLevel level;
|
| 37 |
|
| 38 | public ClientChunkCache(final ClientLevel level, final int serverChunkRadius) {
|
| 39 | this.level = level;
|
| 40 | this.emptyChunk = new EmptyLevelChunk(level, new ChunkPos(0, 0), level.registryAccess().lookupOrThrow(Registries.BIOME).getOrThrow(Biomes.PLAINS));
|
| 41 | this.lightEngine = new LevelLightEngine(this, true, level.dimensionType().hasSkyLight());
|
| 42 | this.storage = new ClientChunkCache.Storage(calculateStorageRange(serverChunkRadius));
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public LevelLightEngine getLightEngine() {
|
| 47 | return this.lightEngine;
|
| 48 | }
|
| 49 |
|
| 50 | private static boolean isValidChunk(final @Nullable LevelChunk chunk, final int x, final int z) {
|
| 51 | if (chunk == null) {
|
| 52 | return false;
|
| 53 | }
|
| 54 |
|
| 55 | ChunkPos pos = chunk.getPos();
|
| 56 | return pos.x() == x && pos.z() == z;
|
| 57 | }
|
| 58 |
|
| 59 | public void drop(final ChunkPos pos) {
|
| 60 | if (this.storage.inRange(pos.x(), pos.z())) {
|
| 61 | int index = this.storage.getIndex(pos.x(), pos.z());
|
| 62 | LevelChunk currentChunk = this.storage.getChunk(index);
|
| 63 | if (isValidChunk(currentChunk, pos.x(), pos.z())) {
|
| 64 | this.storage.drop(index, currentChunk);
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | public @Nullable LevelChunk getChunk(final int x, final int z, final ChunkStatus targetStatus, final boolean loadOrGenerate) {
|
| 70 | if (this.storage.inRange(x, z)) {
|
| 71 | LevelChunk chunk = this.storage.getChunk(this.storage.getIndex(x, z));
|
| 72 | if (isValidChunk(chunk, x, z)) {
|
| 73 | return chunk;
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | return loadOrGenerate ? this.emptyChunk : null;
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public BlockGetter getLevel() {
|
| 82 | return this.level;
|
| 83 | }
|
| 84 |
|
| 85 | public void replaceBiomes(final int chunkX, final int chunkZ, final FriendlyByteBuf readBuffer) {
|
| 86 | if (!this.storage.inRange(chunkX, chunkZ)) {
|
| 87 | LOGGER.warn("Ignoring chunk since it's not in the view range: {}, {}", chunkX, chunkZ);
|
| 88 | } else {
|
| 89 | int index = this.storage.getIndex(chunkX, chunkZ);
|
| 90 | LevelChunk chunk = this.storage.chunks.get(index);
|
| 91 | if (!isValidChunk(chunk, chunkX, chunkZ)) {
|
| 92 | LOGGER.warn("Ignoring chunk since it's not present: {}, {}", chunkX, chunkZ);
|
| 93 | } else {
|
| 94 | chunk.replaceBiomes(readBuffer);
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | public @Nullable LevelChunk replaceWithPacketData(final int chunkX, final int chunkZ, final ClientboundLevelChunkPacketData chunkData) {
|
| 100 | if (!this.storage.inRange(chunkX, chunkZ)) {
|
| 101 | LOGGER.warn("Ignoring chunk since it's not in the view range: {}, {}", chunkX, chunkZ);
|
| 102 | return null;
|
| 103 | }
|
| 104 |
|
| 105 | int index = this.storage.getIndex(chunkX, chunkZ);
|
| 106 | LevelChunk chunk = this.storage.chunks.get(index);
|
| 107 | ChunkPos pos = new ChunkPos(chunkX, chunkZ);
|
| 108 | if (!isValidChunk(chunk, chunkX, chunkZ)) {
|
| 109 | chunk = new LevelChunk(this.level, pos);
|
| 110 | chunk.replaceWithPacketData(chunkX, chunkZ, chunkData);
|
| 111 | this.storage.replace(index, chunk);
|
| 112 | } else {
|
| 113 | chunk.replaceWithPacketData(chunkX, chunkZ, chunkData);
|
| 114 | this.storage.refreshEmptySections(chunk);
|
| 115 | }
|
| 116 |
|
| 117 | this.level.onChunkLoaded(pos);
|
| 118 | return chunk;
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public void tick(final BooleanSupplier haveTime, final boolean tickChunks) {
|
| 123 | }
|
| 124 |
|
| 125 | public void updateViewCenter(final int x, final int z) {
|
| 126 | this.storage.viewCenterX = x;
|
| 127 | this.storage.viewCenterZ = z;
|
| 128 | }
|
| 129 |
|
| 130 | public void updateViewRadius(final int viewRange) {
|
| 131 | int chunkRadius = this.storage.chunkRadius;
|
| 132 | int newChunkRadius = calculateStorageRange(viewRange);
|
| 133 | if (chunkRadius != newChunkRadius) {
|
| 134 | ClientChunkCache.Storage newStorage = new ClientChunkCache.Storage(newChunkRadius);
|
| 135 | newStorage.viewCenterX = this.storage.viewCenterX;
|
| 136 | newStorage.viewCenterZ = this.storage.viewCenterZ;
|
| 137 |
|
| 138 | for (int i = 0; i < this.storage.chunks.length(); i++) {
|
| 139 | LevelChunk chunk = this.storage.chunks.get(i);
|
| 140 | if (chunk != null) {
|
| 141 | ChunkPos pos = chunk.getPos();
|
| 142 | if (newStorage.inRange(pos.x(), pos.z())) {
|
| 143 | newStorage.replace(newStorage.getIndex(pos.x(), pos.z()), chunk);
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | this.storage = newStorage;
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | private static int calculateStorageRange(final int viewRange) {
|
| 153 | return Math.max(2, viewRange) + 3;
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | public String gatherStats() {
|
| 158 | return this.storage.chunks.length() + ", " + this.getLoadedChunksCount();
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | public int getLoadedChunksCount() {
|
| 163 | return this.storage.chunkCount;
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public void onLightUpdate(final LightLayer layer, final SectionPos pos) {
|
| 168 | Minecraft.getInstance().levelExtractor.setSectionDirty(pos.x(), pos.y(), pos.z());
|
| 169 | }
|
| 170 |
|
| 171 | public LongOpenHashSet addedEmptySections() {
|
| 172 | return this.storage.addedEmptySections[this.storage.updatingSetsIndex];
|
| 173 | }
|
| 174 |
|
| 175 | public LongOpenHashSet removedEmptySections() {
|
| 176 | return this.storage.removedEmptySections[this.storage.updatingSetsIndex];
|
| 177 | }
|
| 178 |
|
| 179 | public LongOpenHashSet addedLoadedChunks() {
|
| 180 | return this.storage.addedLoadedChunks[this.storage.updatingSetsIndex];
|
| 181 | }
|
| 182 |
|
| 183 | public LongOpenHashSet removedLoadedChunks() {
|
| 184 | return this.storage.removedLoadedChunks[this.storage.updatingSetsIndex];
|
| 185 | }
|
| 186 |
|
| 187 | public void flipUpdateTrackingSets() {
|
| 188 | this.storage.updatingSetsIndex = (this.storage.updatingSetsIndex + 1) % 2;
|
| 189 | this.storage.addedEmptySections[this.storage.updatingSetsIndex].clear();
|
| 190 | this.storage.removedEmptySections[this.storage.updatingSetsIndex].clear();
|
| 191 | this.storage.addedLoadedChunks[this.storage.updatingSetsIndex].clear();
|
| 192 | this.storage.removedLoadedChunks[this.storage.updatingSetsIndex].clear();
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public void onSectionEmptinessChanged(final int sectionX, final int sectionY, final int sectionZ, final boolean empty) {
|
| 197 | this.storage.onSectionEmptinessChanged(sectionX, sectionY, sectionZ, empty);
|
| 198 | }
|
| 199 |
|
| 200 | @Environment(EnvType.CLIENT)
|
| 201 | private final class Storage {
|
| 202 | private static final int UPDATE_TRACKING_BUFFERS = 2;
|
| 203 | private final AtomicReferenceArray<@Nullable LevelChunk> chunks;
|
| 204 | private final LongOpenHashSet[] addedEmptySections = new LongOpenHashSet[2];
|
| 205 | private final LongOpenHashSet[] removedEmptySections = new LongOpenHashSet[2];
|
| 206 | private final LongOpenHashSet[] addedLoadedChunks = new LongOpenHashSet[2];
|
| 207 | private final LongOpenHashSet[] removedLoadedChunks = new LongOpenHashSet[2];
|
| 208 | private int updatingSetsIndex;
|
| 209 | private final int chunkRadius;
|
| 210 | private final int viewRange;
|
| 211 | private volatile int viewCenterX;
|
| 212 | private volatile int viewCenterZ;
|
| 213 | private int chunkCount;
|
| 214 |
|
| 215 | private Storage(final int chunkRadius) {
|
| 216 | this.chunkRadius = chunkRadius;
|
| 217 | this.viewRange = chunkRadius * 2 + 1;
|
| 218 | this.chunks = new AtomicReferenceArray<>(this.viewRange * this.viewRange);
|
| 219 |
|
| 220 | for (int i = 0; i < 2; i++) {
|
| 221 | this.addedEmptySections[i] = new LongOpenHashSet();
|
| 222 | this.removedEmptySections[i] = new LongOpenHashSet();
|
| 223 | this.addedLoadedChunks[i] = new LongOpenHashSet();
|
| 224 | this.removedLoadedChunks[i] = new LongOpenHashSet();
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | private int getIndex(final int chunkX, final int chunkZ) {
|
| 229 | return Math.floorMod(chunkZ, this.viewRange) * this.viewRange + Math.floorMod(chunkX, this.viewRange);
|
| 230 | }
|
| 231 |
|
| 232 | private void replace(final int index, final @Nullable LevelChunk newChunk) {
|
| 233 | LevelChunk removedChunk = this.chunks.getAndSet(index, newChunk);
|
| 234 | if (removedChunk != null) {
|
| 235 | this.chunkCount--;
|
| 236 | this.onChunkRemoved(removedChunk);
|
| 237 | ClientChunkCache.this.level.unload(removedChunk);
|
| 238 | }
|
| 239 |
|
| 240 | if (newChunk != null) {
|
| 241 | this.chunkCount++;
|
| 242 | this.onChunkAdded(newChunk);
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | private void drop(final int index, final LevelChunk oldChunk) {
|
| 247 | if (this.chunks.compareAndSet(index, oldChunk, null)) {
|
| 248 | this.chunkCount--;
|
| 249 | this.onChunkRemoved(oldChunk);
|
| 250 | }
|
| 251 |
|
| 252 | ClientChunkCache.this.level.unload(oldChunk);
|
| 253 | }
|
| 254 |
|
| 255 | public void onSectionEmptinessChanged(final int sectionX, final int sectionY, final int sectionZ, final boolean empty) {
|
| 256 | if (this.inRange(sectionX, sectionZ)) {
|
| 257 | long sectionNode = SectionPos.asLong(sectionX, sectionY, sectionZ);
|
| 258 | if (empty) {
|
| 259 | this.markSectionEmpty(sectionNode);
|
| 260 | } else {
|
| 261 | this.markSectionNotEmpty(sectionNode);
|
| 262 | }
|
| 263 | }
|
| 264 | }
|
| 265 |
|
| 266 | private void onChunkRemoved(final LevelChunk chunk) {
|
| 267 | ChunkPos chunkPos = chunk.getPos();
|
| 268 | long chunkNode = chunkPos.pack();
|
| 269 | this.addedLoadedChunks[this.updatingSetsIndex].remove(chunkNode);
|
| 270 | this.removedLoadedChunks[this.updatingSetsIndex].add(chunkNode);
|
| 271 | LevelChunkSection[] sections = chunk.getSections();
|
| 272 |
|
| 273 | for (int sectionIndex = 0; sectionIndex < sections.length; sectionIndex++) {
|
| 274 | this.markSectionEmpty(SectionPos.asLong(chunkPos.x(), chunk.getSectionYFromSectionIndex(sectionIndex), chunkPos.z()));
|
| 275 | }
|
| 276 | }
|
| 277 |
|
| 278 | private void onChunkAdded(final LevelChunk chunk) {
|
| 279 | ChunkPos chunkPos = chunk.getPos();
|
| 280 | long chunkNode = chunkPos.pack();
|
| 281 | this.removedLoadedChunks[this.updatingSetsIndex].remove(chunkNode);
|
| 282 | this.addedLoadedChunks[this.updatingSetsIndex].add(chunkNode);
|
| 283 | LevelChunkSection[] sections = chunk.getSections();
|
| 284 |
|
| 285 | for (int sectionIndex = 0; sectionIndex < sections.length; sectionIndex++) {
|
| 286 | LevelChunkSection section = sections[sectionIndex];
|
| 287 | long sectionNode = SectionPos.asLong(chunkPos.x(), chunk.getSectionYFromSectionIndex(sectionIndex), chunkPos.z());
|
| 288 | if (section.hasOnlyAir()) {
|
| 289 | this.markSectionEmpty(sectionNode);
|
| 290 | } else {
|
| 291 | this.markSectionNotEmpty(sectionNode);
|
| 292 | }
|
| 293 | }
|
| 294 | }
|
| 295 |
|
| 296 | private void refreshEmptySections(final LevelChunk chunk) {
|
| 297 | ChunkPos chunkPos = chunk.getPos();
|
| 298 | LevelChunkSection[] sections = chunk.getSections();
|
| 299 |
|
| 300 | for (int sectionIndex = 0; sectionIndex < sections.length; sectionIndex++) {
|
| 301 | LevelChunkSection section = sections[sectionIndex];
|
| 302 | long sectionNode = SectionPos.asLong(chunkPos.x(), chunk.getSectionYFromSectionIndex(sectionIndex), chunkPos.z());
|
| 303 | if (section.hasOnlyAir()) {
|
| 304 | this.markSectionEmpty(sectionNode);
|
| 305 | } else {
|
| 306 | this.markSectionNotEmpty(sectionNode);
|
| 307 | }
|
| 308 | }
|
| 309 | }
|
| 310 |
|
| 311 | private void markSectionEmpty(final long sectionNode) {
|
| 312 | this.removedEmptySections[this.updatingSetsIndex].remove(sectionNode);
|
| 313 | this.addedEmptySections[this.updatingSetsIndex].add(sectionNode);
|
| 314 | }
|
| 315 |
|
| 316 | private void markSectionNotEmpty(final long sectionNode) {
|
| 317 | this.addedEmptySections[this.updatingSetsIndex].remove(sectionNode);
|
| 318 | this.removedEmptySections[this.updatingSetsIndex].add(sectionNode);
|
| 319 | }
|
| 320 |
|
| 321 | private boolean inRange(final int chunkX, final int chunkZ) {
|
| 322 | return Math.abs(chunkX - this.viewCenterX) <= this.chunkRadius && Math.abs(chunkZ - this.viewCenterZ) <= this.chunkRadius;
|
| 323 | }
|
| 324 |
|
| 325 | public @Nullable LevelChunk getChunk(final int index) {
|
| 326 | return this.chunks.get(index);
|
| 327 | }
|
| 328 |
|
| 329 | private void dumpChunks(final String file) {
|
| 330 | try (FileOutputStream stream = new FileOutputStream(file)) {
|
| 331 | int chunkRadius = ClientChunkCache.this.storage.chunkRadius;
|
| 332 |
|
| 333 | for (int z = this.viewCenterZ - chunkRadius; z <= this.viewCenterZ + chunkRadius; z++) {
|
| 334 | for (int x = this.viewCenterX - chunkRadius; x <= this.viewCenterX + chunkRadius; x++) {
|
| 335 | LevelChunk chunk = ClientChunkCache.this.storage.chunks.get(ClientChunkCache.this.storage.getIndex(x, z));
|
| 336 | if (chunk != null) {
|
| 337 | ChunkPos pos = chunk.getPos();
|
| 338 | stream.write((pos.x() + "\t" + pos.z() + "\t" + chunk.isEmpty() + "\n").getBytes(StandardCharsets.UTF_8));
|
| 339 | }
|
| 340 | }
|
| 341 | }
|
| 342 | } catch (IOException e) {
|
| 343 | ClientChunkCache.LOGGER.error("Failed to dump chunks to file {}", file, e);
|
| 344 | }
|
| 345 | }
|
| 346 | }
|
| 347 | }
|
| 348 |
|