Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.15.2] How to properly force load/unload chunks across dimensions?

Featured Replies

Posted

For every loaded chunk in the Overworld, I want to load the chunk which corresponds with it in my dimension (same x and z). I currently have:

@SubscribeEvent
public static void chunkLoad(final ChunkEvent.Load event) {
	if (event.getWorld() != null) if (!event.getWorld().isRemote()) {
		ServerWorld world = (ServerWorld) event.getWorld();
			
		if (world.dimension.getType() == DBMDimensions.realm_of_the_ancients) {
				
			// Force Loads Corresponding Chunks in the Overworld
			DBMChunkManager.getLoadedChunks(world).forEach((a, b) -> world.getServer().getWorld(DimensionType.OVERWORLD).forceChunk(b.getPosition().x, b.getPosition().z, true));
		} else if (world.dimension.getType() == DimensionType.OVERWORLD && DBMDimensions.realm_of_the_ancients != null) {
				
			// Force Loads Corresponding Chunks in the Realm of the Ancients
			DBMChunkManager.getLoadedChunks(world).forEach((a, b) -> world.getServer().getWorld(DBMDimensions.realm_of_the_ancients).forceChunk(b.getPosition().x, b.getPosition().z, true));
		}
	}
}
		
@SubscribeEvent
public static void chunkUnload(final ChunkEvent.Unload event) {
	if (event.getWorld() != null) if (!event.getWorld().isRemote()) {
		ServerWorld world = (ServerWorld) event.getWorld();
			
		if (world.dimension.getType() == DBMDimensions.realm_of_the_ancients) {
				
			// Force Unloads Corresponding Chunks in the Overworld
			world.getServer().getWorld(DimensionType.OVERWORLD).forceChunk(event.getChunk().getPos().x, event.getChunk().getPos().z, false);
		} else if (world.dimension.getType() == DimensionType.OVERWORLD && DBMDimensions.realm_of_the_ancients != null) {
				
			// Force Unloads Corresponding Chunks in the Realm of the Ancients
			world.getServer().getWorld(DBMDimensions.realm_of_the_ancients).forceChunk(event.getChunk().getPos().x, event.getChunk().getPos().z, false);
		}
	}
}

And here is the class I made to get the loaded chunks:

public class DBMChunkManager {

	private static final Field LOADED_CHUNKS_FIELD = ObfuscationReflectionHelper.findField(ChunkManager.class, "immutableLoadedChunks");
	
	static {
		LOADED_CHUNKS_FIELD.setAccessible(true);
	}

	@SuppressWarnings("unchecked") @Nullable
	public static Long2ObjectLinkedOpenHashMap<ChunkHolder> getLoadedChunks(ServerWorld world) {
		try {
			return (Long2ObjectLinkedOpenHashMap<ChunkHolder>) LOADED_CHUNKS_FIELD.get(world.getChunkProvider().chunkManager);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

 

However, any time I try to create a new world, the game gets stuck at 0%.
The last console message to show up is:

[01May2020 23:18:46.645] [Server thread/INFO] [net.minecraftforge.common.DimensionManager/DIMS]: Registered dimension daboismod:the_realm_of_the_ancients of type daboismod:the_realm_of_the_ancients and id 3

The game doesn't crash or suspend, it just does not do anything from this point onward. Any help is greatly appreciated.

  • Author
8 hours ago, diesieben07 said:

You have a deadlock, because while a chunk is being loaded the loaded chunk map is being blocked.

How would I go about fixing the deadlock? I removed the DBMChunkManager class as I realized I didn't need it. Here is the new code:
 

@SubscribeEvent
public static void chunkLoad(final ChunkEvent.Load event) {
		
	if (event.getWorld() != null) if (event.getWorld().chunkExists(event.getChunk().getPos().x, event.getChunk().getPos().z)) if (!event.getWorld().isRemote()) {
		ServerWorld world = (ServerWorld) event.getWorld();
			
		if (world.dimension.getType() == DBMDimensions.realm_of_the_ancients) {

			// Force Loads Corresponding Chunks in the Overworld
			world.getServer().getWorld(DimensionType.OVERWORLD).forceChunk(event.getChunk().getPos().x, event.getChunk().getPos().z, true);
		} else if (world.dimension.getType() == DimensionType.OVERWORLD && DBMDimensions.realm_of_the_ancients != null) {
				
			// Force Loads Corresponding Chunks in the Realm of the Ancients
			world.getServer().getWorld(DBMDimensions.realm_of_the_ancients).forceChunk(event.getChunk().getPos().x, event.getChunk().getPos().z, true);
		}
	}
}
		
@SubscribeEvent
public static void chunkUnload(final ChunkEvent.Unload event) {
		
	if (event.getWorld() != null) if (event.getWorld().chunkExists(event.getChunk().getPos().x, event.getChunk().getPos().z)) if (!event.getWorld().isRemote()) {
			
		ServerWorld world = (ServerWorld) event.getWorld();
			
		if (world.dimension.getType() == DBMDimensions.realm_of_the_ancients) {
				
			// Force Unloads Corresponding Chunks in the Overworld
			world.getServer().getWorld(DimensionType.OVERWORLD).forceChunk(event.getChunk().getPos().x, event.getChunk().getPos().z, false);
		} else if (world.dimension.getType() == DimensionType.OVERWORLD && DBMDimensions.realm_of_the_ancients != null) {
				
			// Force Unloads Corresponding Chunks in the Realm of the Ancients
			world.getServer().getWorld(DBMDimensions.realm_of_the_ancients).forceChunk(event.getChunk().getPos().x, event.getChunk().getPos().z, false);
		}
	}
}

Still gets stuck at 0%.

Edited by kaydogz
fixed wrong thing at the bottom

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.