Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package net.fabricmc.fabric.impl.event.lifecycle;

import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.level.ChunkEvent;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.chunk.LevelChunk;
Expand All @@ -29,6 +33,24 @@
public final class LifecycleEventsImpl implements ModInitializer {
@Override
public void onInitialize() {
// Neo: fire chunk load/unload from NeoForge's chunk events instead of vanilla injections,
// so they also fire when a mod replaces vanilla's chunk system and posts NeoForge's events itself.
NeoForge.EVENT_BUS.addListener(ChunkEvent.Load.class, event -> {
if (event.getLevel() instanceof ServerLevel level) {
ServerChunkEvents.CHUNK_LOAD.invoker().onChunkLoad(level, event.getChunk(), event.isNewChunk());

if (event.isNewChunk()) {
ServerChunkEvents.CHUNK_GENERATE.invoker().onChunkGenerate(level, event.getChunk());
}
}
});

NeoForge.EVENT_BUS.addListener(ChunkEvent.Unload.class, event -> {
if (event.getLevel() instanceof ServerLevel level) {
ServerChunkEvents.CHUNK_UNLOAD.invoker().onChunkUnload(level, event.getChunk());
}
});

// Part of impl for block entity events
ServerChunkEvents.CHUNK_LOAD.register((level, chunk, generated) -> {
((LoadedChunksCache) level).fabric_markLoaded(chunk);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import net.minecraft.server.level.FullChunkStatus;
import net.minecraft.server.level.GenerationChunkHolder;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ImposterProtoChunk;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.status.ChunkStatusTasks;
import net.minecraft.world.level.chunk.status.WorldGenContext;
Expand All @@ -38,19 +37,11 @@ abstract class ChunkStatusTasksMixin {
@Unique
private static final FullChunkStatus[] fabric_FULL_CHUNK_STATUSES = FullChunkStatus.values(); // values() clones the internal array each call, so cache the return

// Neo: CHUNK_LOAD and CHUNK_GENERATE are fired from NeoForge's ChunkEvent.Load (see LifecycleEventsImpl), which is posted at the end of this lambda.
@Inject(method = "lambda$full$0", at = @At("TAIL"))
private static void onChunkLoad(ChunkAccess chunk, WorldGenContext worldGenContext, GenerationChunkHolder chunkHolder, CallbackInfoReturnable<ChunkAccess> callbackInfoReturnable) {
LevelChunk levelChunk = (LevelChunk) callbackInfoReturnable.getReturnValue();

boolean generated = !(chunk instanceof ImposterProtoChunk);

// We fire the event at TAIL since the chunk is guaranteed to be a LevelChunk then.
ServerChunkEvents.CHUNK_LOAD.invoker().onChunkLoad(worldGenContext.level(), levelChunk, generated);

if (generated) {
ServerChunkEvents.CHUNK_GENERATE.invoker().onChunkGenerate(worldGenContext.level(), levelChunk);
}

// Handles the case where the chunk becomes accessible from being completely unloaded, only fires if chunkHolder has been set to at least that full chunk status
FullChunkStatusEventTracker chunkStatusTracker = (FullChunkStatusEventTracker) chunkHolder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"compatibilityLevel": "JAVA_25",
"mixins": [
"ChunkHolderMixin",
"ChunkMapMixin",
"ChunkStatusTasksMixin",
"EntityMixin",
"EntityTypeMixin",
Expand Down
Loading