Skip to content

Commit 9f8a98e

Browse files
committed
Fix ticking entity startup and async texture lighting
1 parent af416af commit 9f8a98e

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

nms/src/main/kotlin/io/github/pylonmc/rebar/nms/entity/BlockTextureEntityImpl.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ class BlockTextureEntityImpl : BlockTextureEntity, SyncedDataHolder {
365365
return stateUpdated
366366
}
367367

368+
private fun CraftBlock.isChunkLoadedSafe(): Boolean =
369+
world.isChunkLoaded(x shr 4, z shr 4)
370+
368371
private fun calculateLightingOffset(): Vector3f {
369372
if (directLighting) {
370373
return ZERO_VECTOR
@@ -376,7 +379,7 @@ class BlockTextureEntityImpl : BlockTextureEntity, SyncedDataHolder {
376379
var brightestLight = 0.toByte()
377380
if (brightest !== ZERO_VECTOR) {
378381
val delegateBlock = block.block.getRelative(brightest.x.toInt(), brightest.y.toInt(), brightest.z.toInt()) as? CraftBlock
379-
if (delegateBlock?.blockState?.hasLightingData ?: false) {
382+
if (delegateBlock != null && delegateBlock.isChunkLoadedSafe() && delegateBlock.blockState.hasLightingData) {
380383
brightestLight = delegateBlock.lightLevel
381384
if (brightestLight >= 15) return brightest
382385
brightestFace = IMMEDIATE_FACES.firstOrNull { it.modX == brightest.x.toInt() && it.modY == brightest.y.toInt() && it.modZ == brightest.z.toInt() }
@@ -386,7 +389,7 @@ class BlockTextureEntityImpl : BlockTextureEntity, SyncedDataHolder {
386389
for (face in IMMEDIATE_FACES) {
387390
if (brightestFace == face) continue
388391
val delegateBlock = block.block.getRelative(face) as? CraftBlock ?: continue
389-
if (delegateBlock.blockState.hasLightingData) {
392+
if (delegateBlock.isChunkLoadedSafe() && delegateBlock.blockState.hasLightingData) {
390393
val light = delegateBlock.lightLevel
391394
if (light > brightestLight) {
392395
brightestLight = light
@@ -471,4 +474,4 @@ class BlockTextureEntityImpl : BlockTextureEntity, SyncedDataHolder {
471474
return dx * dx + dy * dy + dz * dz
472475
}
473476
}
474-
}
477+
}

rebar/src/main/kotlin/io/github/pylonmc/rebar/entity/interfaces/TickingRebarEntity.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,17 @@ interface TickingRebarEntity {
121121
private val dispatchers = mutableMapOf<RebarEntitySchema, CoroutineDispatcher>()
122122

123123
private fun startTicker(tickingEntity: TickingRebarEntity) {
124+
// Ensure the entity is present before assigning its ticking job. After
125+
// deserialization, the dispatcher cache can skip isAsync for later
126+
// entities with the same schema, leaving them absent from the map.
127+
val data = tickingEntities.getOrPut(tickingEntity) {
128+
TickingEntityData(RebarConfig.DEFAULT_TICK_INTERVAL, false, null)
129+
}
124130
val dispatcher = dispatchers.getOrPut((tickingEntity as RebarEntity<*>).schema) {
125131
if (tickingEntity.isAsync) Dispatchers.Default
126132
else Rebar.mainThreadDispatcher
127133
}
128-
tickingEntities[tickingEntity]?.job = Rebar.scope.launch(dispatcher) {
134+
data.job = Rebar.scope.launch(dispatcher) {
129135
while (true) {
130136
delayTicks(tickingEntity.tickInterval.toLong())
131137
try {
@@ -142,4 +148,4 @@ interface TickingRebarEntity {
142148
}
143149
}
144150
}
145-
}
151+
}

0 commit comments

Comments
 (0)