Skip to content

Commit

Permalink
fix: Fix crash with Ad Astra and other dimension mods
Browse files Browse the repository at this point in the history
This code is also slightly faster and more concise, since it's no longer creating an array, but instead using the chunk sections array provided by the chunk.
  • Loading branch information
Steveplays28 committed Apr 29, 2024
1 parent e9b7388 commit ba765fb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,11 @@ public NoiseChunkGeneratorMixin(BiomeSource biomeSource) {
return blockState;
}

@SuppressWarnings("ForLoopReplaceableByForEach")
@Inject(method = "populateNoise(Ljava/util/concurrent/Executor;Lnet/minecraft/world/gen/chunk/Blender;Lnet/minecraft/world/gen/noise/NoiseConfig;Lnet/minecraft/world/gen/StructureAccessor;Lnet/minecraft/world/chunk/Chunk;)Ljava/util/concurrent/CompletableFuture;", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/world/chunk/Chunk;getSectionIndex(I)I", ordinal = 1), cancellable = true)
private void noisium$populateNoiseInject(Executor executor, Blender blender, NoiseConfig noiseConfig, StructureAccessor structureAccessor, Chunk chunk, CallbackInfoReturnable<CompletableFuture<Chunk>> cir, @Local(ordinal = 1) int minimumYFloorDiv, @Local(ordinal = 2) int generationShapeHeightFloorDiv, @Local(ordinal = 3) int startingChunkSectionIndex, @Local(ordinal = 4) int minimumYChunkSectionIndex) {
ChunkSection[] chunkSections = new ChunkSection[startingChunkSectionIndex - minimumYChunkSectionIndex + 1];

var chunkSections = chunk.getSectionArray();
for (int chunkSectionIndex = startingChunkSectionIndex; chunkSectionIndex >= minimumYChunkSectionIndex; --chunkSectionIndex) {
ChunkSection chunkSection = chunk.getSection(chunkSectionIndex);

chunkSection.lock();
chunkSections[chunkSectionIndex] = chunkSection;
chunkSections[chunkSectionIndex].lock();
}

cir.setReturnValue(CompletableFuture.supplyAsync(
Expand All @@ -71,8 +66,8 @@ public NoiseChunkGeneratorMixin(BiomeSource biomeSource) {
)
), Util.getMainWorkerExecutor()).whenCompleteAsync((chunk2, throwable) -> {
// Replace an enhanced for loop with a fori loop
for (int i = 0; i < chunkSections.length; i++) {
chunkSections[i].unlock();
for (int chunkSectionIndex = startingChunkSectionIndex; chunkSectionIndex >= minimumYChunkSectionIndex; --chunkSectionIndex) {
chunkSections[chunkSectionIndex].unlock();
}
}, executor));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,11 @@ public LithiumNoiseChunkGeneratorMixin(BiomeSource biomeSource) {
return blockState;
}

@SuppressWarnings("ForLoopReplaceableByForEach")
@Inject(method = "populateNoise(Ljava/util/concurrent/Executor;Lnet/minecraft/world/gen/chunk/Blender;Lnet/minecraft/world/gen/noise/NoiseConfig;Lnet/minecraft/world/gen/StructureAccessor;Lnet/minecraft/world/chunk/Chunk;)Ljava/util/concurrent/CompletableFuture;", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/world/chunk/Chunk;getSectionIndex(I)I", ordinal = 1), cancellable = true)
private void noisium$populateNoiseInject(Executor executor, Blender blender, NoiseConfig noiseConfig, StructureAccessor structureAccessor, Chunk chunk, CallbackInfoReturnable<CompletableFuture<Chunk>> cir, @Local(ordinal = 1) int minimumYFloorDiv, @Local(ordinal = 2) int generationShapeHeightFloorDiv, @Local(ordinal = 3) int startingChunkSectionIndex, @Local(ordinal = 4) int minimumYChunkSectionIndex) {
ChunkSection[] chunkSections = new ChunkSection[startingChunkSectionIndex - minimumYChunkSectionIndex + 1];

var chunkSections = chunk.getSectionArray();
for (int chunkSectionIndex = startingChunkSectionIndex; chunkSectionIndex >= minimumYChunkSectionIndex; --chunkSectionIndex) {
ChunkSection chunkSection = chunk.getSection(chunkSectionIndex);

chunkSection.lock();
chunkSections[chunkSectionIndex] = chunkSection;
chunkSections[chunkSectionIndex].lock();
}

cir.setReturnValue(CompletableFuture.supplyAsync(
Expand All @@ -63,8 +58,8 @@ public LithiumNoiseChunkGeneratorMixin(BiomeSource biomeSource) {
), Util.getMainWorkerExecutor()).whenCompleteAsync((chunk2, throwable) -> {
// Replace an enhanced for loop with a fori loop
// Also run calculateCounts() on every chunk section to add Lithium compatibility
for (int i = 0; i < chunkSections.length; i++) {
var chunkSection = chunkSections[i];
for (int chunkSectionIndex = startingChunkSectionIndex; chunkSectionIndex >= minimumYChunkSectionIndex; --chunkSectionIndex) {
var chunkSection = chunkSections[chunkSectionIndex];

chunkSection.calculateCounts();
chunkSection.unlock();
Expand Down

0 comments on commit ba765fb

Please sign in to comment.