Created
March 11, 2024 06:37
-
-
Save Mimickal/43aa3a75a52c2b55ab9358f8c9acd1f9 to your computer and use it in GitHub Desktop.
Minecraft Forge 1.18 getLoadedChunks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** For some silly reason {@link ChunkMap#getChunks} is protected, so this is the workaround. */ | |
public static Iterable<ChunkHolder> getLoadedChunks(ServerLevel world) { | |
// Ironically, Reflection is probably the most portable way to do this. | |
try { | |
ChunkMap chunkMap = world.getChunkSource().chunkMap; | |
Method getChunks = chunkMap.getClass().getDeclaredMethod("getChunks"); | |
getChunks.setAccessible(true); | |
// AFAIK there's no way to do this cast that Java thinks is safe. | |
// ChunkMap.getChunks() only ever returns this type, so it's safe enough. | |
@SuppressWarnings("unchecked") | |
Iterable<ChunkHolder> chunkIterator = (Iterable<ChunkHolder>) getChunks.invoke(chunkMap); | |
return chunkIterator; | |
// Any of these exceptions being thrown means we messed something up above, so just explode. | |
} catch (NoSuchMethodException e) { | |
throw new RuntimeException("ServerChunkCache.getChunks() isn't a method, apparently.", e); | |
} catch (IllegalAccessException | InvocationTargetException e) { | |
throw new RuntimeException("Failed to invoke ServerChunkCache.getChunks()", e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment