Skip to content

Commit

Permalink
Fix ChunkEvent#Unload fired too late
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Nov 14, 2024
1 parent 89310cd commit 43f6ac3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,44 @@ abstract class ChunkHolderMixin extends GenerationChunkHolderMixin {
@Shadow public abstract CompletableFuture<ChunkResult<LevelChunk>> shadow$getEntityTickingChunkFuture();
// @formatter:on

private LevelChunk impl$loadedChunk;

/**
* After onFullChunkStatusChange
*/
@Inject(method = "lambda$scheduleFullChunkPromotion$4", at = @At("TAIL"))
private void impl$onScheduleFullChunkPromotion(final ChunkMap $$0x, final FullChunkStatus $$1x, final CallbackInfo ci) {
if ($$1x == FullChunkStatus.ENTITY_TICKING && ShouldFire.CHUNK_EVENT_LOAD) {
if ($$1x == FullChunkStatus.ENTITY_TICKING) {
this.shadow$getEntityTickingChunkFuture().getNow(ChunkHolder.UNLOADED_LEVEL_CHUNK).ifSuccess(chunk -> {
final Vector3i chunkPos = VecHelper.toVector3i(chunk.getPos());
final ChunkEvent.Load event = SpongeEventFactory.createChunkEventLoad(PhaseTracker.getInstance().currentCause(),
this.impl$loadedChunk = chunk;
if (ShouldFire.CHUNK_EVENT_LOAD) {
final Vector3i chunkPos = VecHelper.toVector3i(chunk.getPos());
final ChunkEvent.Load event = SpongeEventFactory.createChunkEventLoad(PhaseTracker.getInstance().currentCause(),
(WorldChunk) chunk, chunkPos, (ResourceKey) (Object) chunk.getLevel().dimension().location());
SpongeCommon.post(event);
SpongeCommon.post(event);
}
});
}
}

@Inject(method = "demoteFullChunk", at = @At("HEAD"))
private void impl$onDemoteFullChunkPre(final ChunkMap $$0x, final FullChunkStatus $$1x, final CallbackInfo ci) {
if (this.impl$loadedChunk != null && ShouldFire.CHUNK_EVENT_UNLOAD_PRE) {
final Vector3i chunkPos = VecHelper.toVector3i(this.impl$loadedChunk.getPos());
final ChunkEvent.Unload event = SpongeEventFactory.createChunkEventUnloadPre(PhaseTracker.getInstance().currentCause(),
(WorldChunk) this.impl$loadedChunk, chunkPos, (ResourceKey) (Object) this.impl$loadedChunk.getLevel().dimension().location());
SpongeCommon.post(event);
}
}

@Inject(method = "demoteFullChunk", at = @At("TAIL"))
private void impl$onDemoteFullChunkPost(final ChunkMap $$0x, final FullChunkStatus $$1x, final CallbackInfo ci) {
if (this.impl$loadedChunk != null && ShouldFire.CHUNK_EVENT_UNLOAD_POST) {
final Vector3i chunkPos = VecHelper.toVector3i(this.impl$loadedChunk.getPos());
final ChunkEvent.Unload event = SpongeEventFactory.createChunkEventUnloadPost(PhaseTracker.getInstance().currentCause(), chunkPos,
(ResourceKey) (Object) this.impl$loadedChunk.getLevel().dimension().location());
SpongeCommon.post(event);
}
this.impl$loadedChunk = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,6 @@ public abstract class ChunkMapMixin implements ChunkMapBridge {
)
)
private void impl$onSetUnloaded(final ServerLevel level, final LevelChunk chunk) {
final Vector3i chunkPos = VecHelper.toVector3i(chunk.getPos());

if (ShouldFire.CHUNK_EVENT_UNLOAD_PRE) {
final ChunkEvent.Unload event = SpongeEventFactory.createChunkEventUnloadPre(PhaseTracker.getInstance().currentCause(),
(WorldChunk) chunk, chunkPos, (ResourceKey) (Object) this.level.dimension().location());
SpongeCommon.post(event);
}

level.unload(chunk);

for (final Direction dir : Constants.Chunk.CARDINAL_DIRECTIONS) {
Expand All @@ -136,12 +128,6 @@ public abstract class ChunkMapMixin implements ChunkMapBridge {
((LevelChunkBridge) neighbor).bridge$setNeighborChunk(oppositeIndex, null);
}
}

if (ShouldFire.CHUNK_EVENT_UNLOAD_POST) {
final ChunkEvent.Unload event = SpongeEventFactory.createChunkEventUnloadPost(PhaseTracker.getInstance().currentCause(), chunkPos,
(ResourceKey) (Object) this.level.dimension().location());
SpongeCommon.post(event);
}
}

@Inject(method = "save", at = @At(value = "HEAD"), cancellable = true)
Expand Down

0 comments on commit 43f6ac3

Please sign in to comment.