| 1 | package net.minecraft.client;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.SectionPos;
|
| 8 | import net.minecraft.world.level.LevelHeightAccessor;
|
| 9 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 10 | import net.minecraft.world.level.chunk.status.ChunkStatus;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public class SectionUpdateTracker {
|
| 15 | private final RotatingSectionStorage<SectionUpdateTracker.SectionDirtyState> storage;
|
| 16 |
|
| 17 | public SectionUpdateTracker(final LevelHeightAccessor levelHeightAccessor, final int renderDistance) {
|
| 18 | this.storage = new RotatingSectionStorage<>(
|
| 19 | renderDistance,
|
| 20 | levelHeightAccessor.getMinSectionY(),
|
| 21 | levelHeightAccessor.getMaxSectionY(),
|
| 22 | (index, sectionNode) -> new SectionUpdateTracker.SectionDirtyState(true, false, sectionNode)
|
| 23 | );
|
| 24 | }
|
| 25 |
|
| 26 | public void setDirty(final int sectionX, final int sectionY, final int sectionZ, final boolean playerChanged) {
|
| 27 | SectionUpdateTracker.SectionDirtyState section = this.storage.getValue(sectionX, sectionY, sectionZ);
|
| 28 | if (section != null) {
|
| 29 | section.setDirty(playerChanged);
|
| 30 | }
|
| 31 | }
|
| 32 |
|
| 33 | public void repositionCamera(final SectionPos cameraSectionPos) {
|
| 34 | this.storage.repositionCenter(cameraSectionPos);
|
| 35 | }
|
| 36 |
|
| 37 | public int size() {
|
| 38 | return this.storage.size();
|
| 39 | }
|
| 40 |
|
| 41 | public SectionUpdateTracker.@Nullable SectionDirtyState getDirtyState(final long sectionNode) {
|
| 42 | return this.storage.getValue(sectionNode);
|
| 43 | }
|
| 44 |
|
| 45 | public boolean hasAllNeighbors(final ClientLevel level, final long sectionNode) {
|
| 46 | return this.doesChunkExistAt(level, SectionPos.offset(sectionNode, Direction.WEST))
|
| 47 | && this.doesChunkExistAt(level, SectionPos.offset(sectionNode, Direction.NORTH))
|
| 48 | && this.doesChunkExistAt(level, SectionPos.offset(sectionNode, Direction.EAST))
|
| 49 | && this.doesChunkExistAt(level, SectionPos.offset(sectionNode, Direction.SOUTH))
|
| 50 | && this.doesChunkExistAt(level, SectionPos.offset(sectionNode, -1, 0, -1))
|
| 51 | && this.doesChunkExistAt(level, SectionPos.offset(sectionNode, -1, 0, 1))
|
| 52 | && this.doesChunkExistAt(level, SectionPos.offset(sectionNode, 1, 0, -1))
|
| 53 | && this.doesChunkExistAt(level, SectionPos.offset(sectionNode, 1, 0, 1));
|
| 54 | }
|
| 55 |
|
| 56 | private boolean doesChunkExistAt(final ClientLevel level, final long sectionNode) {
|
| 57 | ChunkAccess chunk = level.getChunk(SectionPos.x(sectionNode), SectionPos.z(sectionNode), ChunkStatus.FULL, false);
|
| 58 | return chunk != null && level.getLightEngine().lightOnInColumn(SectionPos.getZeroNode(sectionNode));
|
| 59 | }
|
| 60 |
|
| 61 | @Environment(EnvType.CLIENT)
|
| 62 | public static class SectionDirtyState implements RotatingSectionStorage.Value {
|
| 63 | private boolean isDirty;
|
| 64 | private boolean isDirtyFromPlayer;
|
| 65 | private long sectionNode;
|
| 66 |
|
| 67 | private SectionDirtyState(final boolean isDirty, final boolean isDirtyFromPlayer, final long sectionNode) {
|
| 68 | this.isDirty = isDirty;
|
| 69 | this.isDirtyFromPlayer = isDirtyFromPlayer;
|
| 70 | this.sectionNode = sectionNode;
|
| 71 | }
|
| 72 |
|
| 73 | public void setDirty(final boolean fromPlayer) {
|
| 74 | boolean wasDirty = this.isDirty;
|
| 75 | this.isDirty = true;
|
| 76 | this.isDirtyFromPlayer = fromPlayer | (wasDirty && this.isDirtyFromPlayer);
|
| 77 | }
|
| 78 |
|
| 79 | public void setNotDirty() {
|
| 80 | this.isDirty = false;
|
| 81 | this.isDirtyFromPlayer = false;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void setSectionNode(final long sectionNode) {
|
| 86 | if (this.sectionNode != sectionNode) {
|
| 87 | this.sectionNode = sectionNode;
|
| 88 | this.isDirty = true;
|
| 89 | this.isDirtyFromPlayer = false;
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public long getSectionNode() {
|
| 95 | return this.sectionNode;
|
| 96 | }
|
| 97 |
|
| 98 | public boolean isDirty() {
|
| 99 | return this.isDirty;
|
| 100 | }
|
| 101 |
|
| 102 | public boolean isDirtyFromPlayer() {
|
| 103 | return this.isDirtyFromPlayer;
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 |
|