Skip to content

Commit e6c95db

Browse files
committed
Fix custom dynamic light sources sometimes not updating previously lit chunks.
1 parent 8793e92 commit e6c95db

File tree

6 files changed

+45
-64
lines changed

6 files changed

+45
-64
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@
280280

281281
- Added Malay and Malay (Jawi) translations ([#256](https://github.com/LambdAurora/LambDynamicLights/pull/256)).
282282
- Fixed Upside-down English translations ([#257](https://github.com/LambdAurora/LambDynamicLights/pull/257)).
283+
- Fixed custom dynamic light sources sometimes not updating previously lit chunks.
283284

284285
[SpruceUI]: https://github.com/LambdAurora/SpruceUI "SpruceUI page"
285286
[pridelib]: https://github.com/Queerbric/pridelib "Pridelib page"

api/src/main/java/dev/lambdaurora/lambdynlights/api/behavior/DynamicLightBehavior.java

+10-52
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Each dynamic lighting behavior have a way to give a light level at a given position, a bounding box, and a way to check for changes.
2020
*
2121
* @author LambdAurora, Akarys
22-
* @version 4.0.0
22+
* @version 4.0.1
2323
* @since 4.0.0
2424
*/
2525
public interface DynamicLightBehavior {
@@ -38,7 +38,7 @@ public interface DynamicLightBehavior {
3838
* double dz = pos.getZ() - this.z + 0.5;
3939
*
4040
* double distanceSquared = dx * dx + dy * dy + dz * dz;
41-
* return luminance - Math.sqrt(distanceSquared) * falloffRatio;
41+
* return Math.max(luminance - Math.sqrt(distanceSquared) * falloffRatio, 0.0);
4242
* }
4343
* </code></pre>
4444
*
@@ -81,15 +81,15 @@ default boolean isRemoved() {
8181

8282
/**
8383
* Represents the bounding box of a dynamic lighting behavior.
84+
*
85+
* @param startX the starting X-coordinate of this bounding box
86+
* @param startY the starting Y-coordinate of this bounding box
87+
* @param startZ the starting Z-coordinate of this bounding box
88+
* @param endX the ending X-coordinate of this bounding box
89+
* @param endY the ending Y-coordinate of this bounding box
90+
* @param endZ the ending Z-coordinate of this bounding box
8491
*/
85-
class BoundingBox {
86-
int startX;
87-
int startY;
88-
int startZ;
89-
int endX;
90-
int endY;
91-
int endZ;
92-
92+
record BoundingBox(int startX, int startY, int startZ, int endX, int endY, int endZ) {
9393
public BoundingBox(
9494
int startX,
9595
int startY,
@@ -105,47 +105,5 @@ public BoundingBox(
105105
this.endY = Math.max(startY, endY);
106106
this.endZ = Math.max(startZ, endZ);
107107
}
108-
109-
/**
110-
* {@return the starting X-coordinate of this bounding box}
111-
*/
112-
public int startX() {
113-
return this.startX;
114-
}
115-
116-
/**
117-
* {@return the starting Y-coordinate of this bounding box}
118-
*/
119-
public int startY() {
120-
return this.startY;
121-
}
122-
123-
/**
124-
* {@return the starting Z-coordinate of this bounding box}
125-
*/
126-
public int startZ() {
127-
return this.startZ;
128-
}
129-
130-
/**
131-
* {@return the ending X-coordinate of this bounding box}
132-
*/
133-
public int endX() {
134-
return this.endX;
135-
}
136-
137-
/**
138-
* {@return the ending Y-coordinate of this bounding box}
139-
*/
140-
public int endY() {
141-
return this.endY;
142-
}
143-
144-
/**
145-
* {@return the ending Z-coordinate of this bounding box}
146-
*/
147-
public int endZ() {
148-
return this.endZ;
149-
}
150108
}
151109
}

build_logic/src/main/kotlin/lambdynamiclights/Constants.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Constants {
77
const val NAME = "lambdynamiclights"
88
const val NAMESPACE = "lambdynlights"
99
const val PRETTY_NAME = "LambDynamicLights"
10-
const val VERSION = "4.0.0"
10+
const val VERSION = "4.0.1"
1111
const val JAVA_VERSION = 21
1212

1313
const val DESCRIPTION = "The most feature-complete dynamic lighting mod for Fabric."

src/main/java/dev/lambdaurora/lambdynlights/LambDynLights.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import dev.lambdaurora.lambdynlights.accessor.WorldRendererAccessor;
1313
import dev.lambdaurora.lambdynlights.api.DynamicLightsContext;
1414
import dev.lambdaurora.lambdynlights.api.DynamicLightsInitializer;
15+
import dev.lambdaurora.lambdynlights.api.behavior.DynamicLightBehavior;
1516
import dev.lambdaurora.lambdynlights.api.behavior.DynamicLightBehaviorManager;
1617
import dev.lambdaurora.lambdynlights.api.entity.EntityLightSourceManager;
1718
import dev.lambdaurora.lambdynlights.api.item.ItemLightSourceManager;
@@ -67,7 +68,7 @@
6768
* Represents the LambDynamicLights mod.
6869
*
6970
* @author LambdAurora
70-
* @version 4.0.0
71+
* @version 4.0.1
7172
* @since 1.0.0
7273
*/
7374
@ApiStatus.Internal
@@ -149,7 +150,9 @@ public void onInitializeClient() {
149150
var lightSource = it.next();
150151

151152
// In case of light sources controlled by a DynamicLightBehavior, they might require polling to be removed.
152-
if (lightSource instanceof DeferredDynamicLightSource(var behavior)) {
153+
if (lightSource instanceof DeferredDynamicLightSource deferred) {
154+
DynamicLightBehavior behavior = deferred.behavior();
155+
153156
if (behavior.isRemoved()) {
154157
this.toClear.add(lightSource);
155158
it.remove();

src/main/java/dev/lambdaurora/lambdynlights/engine/DynamicLightBehaviorSources.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Represents the dynamic lighting behavior manager implementation.
2020
*
2121
* @author LambdAurora, Akarys
22-
* @version 4.0.0
22+
* @version 4.0.1
2323
* @since 4.0.0
2424
*/
2525
public final class DynamicLightBehaviorSources implements DynamicLightBehaviorManager {
@@ -38,8 +38,6 @@ public void add(@NotNull DynamicLightBehavior source) {
3838
public boolean remove(DynamicLightBehavior source) {
3939
if (source == null) return false;
4040

41-
return this.mod.removeLightSources(other -> other instanceof DeferredDynamicLightSource(DynamicLightBehavior otherBehavior)
42-
&& otherBehavior == source
43-
);
41+
return this.mod.removeLightSources(other -> other instanceof DeferredDynamicLightSource deferred && deferred.behavior() == source);
4442
}
4543
}

src/main/java/dev/lambdaurora/lambdynlights/engine/source/DeferredDynamicLightSource.java

+26-5
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,22 @@
2525
* Represents a dynamic light source which is deferred to a {@link DynamicLightBehavior}.
2626
*
2727
* @author LambdAurora, Akarys
28-
* @version 4.0.0
28+
* @version 4.0.1
2929
* @since 4.0.0
3030
*/
31-
public record DeferredDynamicLightSource(DynamicLightBehavior behavior) implements DynamicLightSource {
31+
public final class DeferredDynamicLightSource implements DynamicLightSource {
32+
private final DynamicLightBehavior behavior;
33+
private DynamicLightBehavior.BoundingBox previousBoundingBox;
34+
35+
public DeferredDynamicLightSource(DynamicLightBehavior behavior) {
36+
this.behavior = behavior;
37+
this.previousBoundingBox = null;
38+
}
39+
40+
public DynamicLightBehavior behavior() {
41+
return this.behavior;
42+
}
43+
3244
@Override
3345
public Stream<SpatialLookupEntry> splitIntoDynamicLightEntries() {
3446
DynamicLightBehavior.BoundingBox boundingBox = this.behavior.getBoundingBox();
@@ -60,6 +72,17 @@ public LongSet getDynamicLightChunksToRebuild(boolean forced) {
6072

6173
var chunks = new LongOpenHashSet();
6274

75+
addBoundingBoxToChunksSet(chunks, boundingBox);
76+
if (this.previousBoundingBox != null) {
77+
addBoundingBoxToChunksSet(chunks, this.previousBoundingBox);
78+
}
79+
80+
this.previousBoundingBox = boundingBox;
81+
82+
return chunks;
83+
}
84+
85+
private static void addBoundingBoxToChunksSet(LongSet set, DynamicLightBehavior.BoundingBox boundingBox) {
6386
int chunkStartX = getStartChunk(boundingBox.startX());
6487
int chunkStartY = getStartChunk(boundingBox.startY());
6588
int chunkStartZ = getStartChunk(boundingBox.startZ());
@@ -70,12 +93,10 @@ public LongSet getDynamicLightChunksToRebuild(boolean forced) {
7093
for (int x = chunkStartX; x <= chunkEndX; x++) {
7194
for (int y = chunkStartY; y <= chunkEndY; y++) {
7295
for (int z = chunkStartZ; z <= chunkEndZ; z++) {
73-
chunks.add(ChunkSectionPos.asLong(x, y, z));
96+
set.add(ChunkSectionPos.asLong(x, y, z));
7497
}
7598
}
7699
}
77-
78-
return chunks;
79100
}
80101

81102
private static int getStartChunk(int blockPos) {

0 commit comments

Comments
 (0)