Jump to content

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


kaydogz

Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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