Skip to content

Commit

Permalink
Offer more control over LightingChunk invalidations (Minestom#2156)
Browse files Browse the repository at this point in the history
* Offer more control over LightingChunk invalidations

* Respect freezeInvalidation on this other invalidate method

* Rename to invalidateNeighborsSection and remove redundant null check
  • Loading branch information
Minikloon committed May 30, 2024
1 parent 85942b6 commit 43ed60d
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions src/main/java/net/minestom/server/instance/LightingChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class LightingChunk extends DynamicChunk {

boolean chunkLoaded = false;
private int highestBlock;
private boolean freezeInvalidation = false;

private final ReentrantLock packetGenerationLock = new ReentrantLock();
private final AtomicInteger resendTimer = new AtomicInteger(-1);
Expand Down Expand Up @@ -107,7 +108,15 @@ private boolean checkSkyOcclusion(Block block) {
return occludesBottom || occludesTop;
}

private void invalidateSection(int coordinate) {
public void setFreezeInvalidation(boolean freezeInvalidation) {
this.freezeInvalidation = freezeInvalidation;
}

public void invalidateNeighborsSection(int coordinate) {
if (freezeInvalidation) {
return;
}

for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
Chunk neighborChunk = instance.getChunk(chunkX + i, chunkZ + j);
Expand All @@ -126,6 +135,21 @@ private void invalidateSection(int coordinate) {
}
}

public void invalidateResendDelay() {
if (!doneInit || freezeInvalidation) {
return;
}

for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
Chunk neighborChunk = instance.getChunk(chunkX + i, chunkZ + j);
if (neighborChunk instanceof LightingChunk light) {
light.resendTimer.set(resendDelay);
}
}
}
}

@Override
public void setBlock(int x, int y, int z, @NotNull Block block,
@Nullable BlockHandler.Placement placement,
Expand All @@ -135,22 +159,10 @@ public void setBlock(int x, int y, int z, @NotNull Block block,

// Invalidate neighbor chunks, since they can be updated by this block change
int coordinate = ChunkUtils.getChunkCoordinate(y);
if (chunkLoaded) {
invalidateSection(coordinate);
if (chunkLoaded && !freezeInvalidation) {
invalidateNeighborsSection(coordinate);
invalidateResendDelay();
this.lightCache.invalidate();

if (doneInit) {
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
Chunk neighborChunk = instance.getChunk(chunkX + i, chunkZ + j);
if (neighborChunk == null) continue;

if (neighborChunk instanceof LightingChunk light) {
light.resendTimer.set(resendDelay);
}
}
}
}
}
}

Expand Down

0 comments on commit 43ed60d

Please sign in to comment.