diff --git a/src/mixins/java/org/spongepowered/common/mixin/core/server/level/ChunkHolderMixin.java b/src/mixins/java/org/spongepowered/common/mixin/core/server/level/ChunkHolderMixin.java index a58cb5b70d4..6b3535cd498 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/core/server/level/ChunkHolderMixin.java +++ b/src/mixins/java/org/spongepowered/common/mixin/core/server/level/ChunkHolderMixin.java @@ -53,18 +53,44 @@ abstract class ChunkHolderMixin extends GenerationChunkHolderMixin { @Shadow public abstract CompletableFuture> 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; + } } diff --git a/src/mixins/java/org/spongepowered/common/mixin/core/server/level/ChunkMapMixin.java b/src/mixins/java/org/spongepowered/common/mixin/core/server/level/ChunkMapMixin.java index 2df47d7beea..d3b06d5f6c9 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/core/server/level/ChunkMapMixin.java +++ b/src/mixins/java/org/spongepowered/common/mixin/core/server/level/ChunkMapMixin.java @@ -128,14 +128,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) { @@ -147,12 +139,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)