Remove world gen step duplicate code

This commit is contained in:
James Seibel
2025-11-22 11:27:01 -06:00
parent a709ab6071
commit ab4a9cbb55
7 changed files with 80 additions and 120 deletions
@@ -27,26 +27,26 @@ public abstract class AbstractWorldGenStep
/** @return the list of chunks that have an earlier status and can be generated */
protected ArrayList<ChunkAccess> getChunksToGenerate(List<ChunkWrapper> chunkWrappers)
protected ArrayList<ChunkWrapper> getChunkWrappersToGenerate(List<ChunkWrapper> chunkWrappers)
{
ArrayList<ChunkAccess> chunksToGenerate = new ArrayList<>();
ArrayList<ChunkWrapper> chunkWrappersToGenerate = new ArrayList<>(chunkWrappers.size());
for (ChunkWrapper chunkWrapper : chunkWrappers)
{
ChunkAccess chunk = chunkWrapper.getChunk();
if (chunkWrapper.getStatus().isOrAfter(this.getChunkStatus()))
{
// this chunk has already generated this step
// this chunk has already been generated up to this step
continue;
}
else if (chunk instanceof ProtoChunk)
{
chunkWrapper.trySetStatus(this.getChunkStatus());
chunksToGenerate.add(chunk);
chunkWrappersToGenerate.add(chunkWrapper);
}
}
return chunksToGenerate;
return chunkWrappersToGenerate;
}
@@ -67,9 +67,12 @@ public final class StepBiomes extends AbstractWorldGenStep
ThreadWorldGenParams tParams, DhLitWorldGenRegion worldGenRegion,
ArrayGridList<ChunkWrapper> chunkWrappers)
{
ArrayList<ChunkAccess> chunksToDo = this.getChunksToGenerate(chunkWrappers);
for (ChunkAccess chunk : chunksToDo)
ArrayList<ChunkWrapper> chunksToDo = this.getChunkWrappersToGenerate(chunkWrappers);
for (ChunkWrapper chunkWrapper : chunksToDo)
{
ChunkAccess chunk = chunkWrapper.getChunk();
#if MC_VER < MC_1_18_2
this.environment.params.generator.createBiomes(this.environment.params.biomes, chunk);
#elif MC_VER < MC_1_19_2
@@ -37,6 +37,7 @@ import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.chunk.status.ChunkStatus;
#endif
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
@@ -70,18 +71,10 @@ public final class StepFeatures extends AbstractWorldGenStep
ThreadWorldGenParams tParams, DhLitWorldGenRegion worldGenRegion,
ArrayGridList<ChunkWrapper> chunkWrappers)
{
for (ChunkWrapper chunkWrapper : chunkWrappers)
ArrayList<ChunkWrapper> chunksToDo = this.getChunkWrappersToGenerate(chunkWrappers);
for (ChunkWrapper chunkWrapper : chunksToDo)
{
ChunkAccess chunk = chunkWrapper.getChunk();
if (chunkWrapper.getStatus().isOrAfter(STATUS))
{
// this chunk has already generated this step
continue;
}
else if (chunk instanceof ProtoChunk)
{
chunkWrapper.trySetStatus(STATUS);
}
try
@@ -68,21 +68,11 @@ public final class StepNoise extends AbstractWorldGenStep
ThreadWorldGenParams tParams, DhLitWorldGenRegion worldGenRegion,
ArrayGridList<ChunkWrapper> chunkWrappers)
{
ArrayList<ChunkAccess> chunksToDo = new ArrayList<>();
for (ChunkWrapper chunkWrapper : chunkWrappers)
ArrayList<ChunkWrapper> chunksToDo = this.getChunkWrappersToGenerate(chunkWrappers);
for (ChunkWrapper chunkWrapper : chunksToDo)
{
ChunkAccess chunk = chunkWrapper.getChunk();
if (chunkWrapper.getStatus().isOrAfter(STATUS))
{
continue;
}
chunkWrapper.trySetStatus(STATUS);
chunksToDo.add(chunk);
}
for (ChunkAccess chunk : chunksToDo)
{
#if MC_VER < MC_1_17_1
this.environment.params.generator.fillFromNoise(worldGenRegion, tParams.structFeat, chunk);
#elif MC_VER < MC_1_18_2
@@ -114,7 +104,6 @@ public final class StepNoise extends AbstractWorldGenStep
tParams.structFeat.forWorldGenRegion(worldGenRegion),
chunk));
#endif
UncheckedInterruptedException.throwIfInterrupted();
}
}
@@ -65,26 +65,10 @@ public final class StepStructureReference extends AbstractWorldGenStep
ThreadWorldGenParams tParams, DhLitWorldGenRegion worldGenRegion,
ArrayGridList<ChunkWrapper> chunkWrappers)
{
ArrayList<ChunkAccess> chunksToDo = new ArrayList<ChunkAccess>();
for (ChunkWrapper chunkWrapper : chunkWrappers)
ArrayList<ChunkWrapper> chunksToDo = this.getChunkWrappersToGenerate(chunkWrappers);
for (ChunkWrapper chunkWrapper : chunksToDo)
{
ChunkAccess chunk = chunkWrapper.getChunk();
if (chunkWrapper.getStatus().isOrAfter(STATUS))
{
// this chunk has already generated this step
continue;
}
else if (chunk instanceof ProtoChunk)
{
chunkWrapper.trySetStatus(STATUS);
chunksToDo.add(chunk);
}
}
for (ChunkAccess chunk : chunksToDo)
{
// System.out.println("StepStructureReference: "+chunk.getPos());
this.environment.params.generator.createReferences(worldGenRegion, tParams.structFeat.forWorldGenRegion(worldGenRegion), chunk);
}
}
@@ -69,71 +69,75 @@ public final class StepStructureStart extends AbstractWorldGenStep
ThreadWorldGenParams tParams, DhLitWorldGenRegion worldGenRegion,
ArrayGridList<ChunkWrapper> chunkWrappers)
{
ArrayList<ChunkAccess> chunksToDo = this.getChunksToGenerate(chunkWrappers);
ArrayList<ChunkWrapper> chunksToDo = this.getChunkWrappersToGenerate(chunkWrappers);
#if MC_VER < MC_1_19_2
if (this.environment.params.worldGenSettings.generateFeatures())
{
if (!this.environment.params.worldGenSettings.generateFeatures())
#elif MC_VER < MC_1_19_4
if (this.environment.params.worldGenSettings.generateStructures())
{
if (!this.environment.params.worldGenSettings.generateStructures())
#else
if (this.environment.params.worldOptions.generateStructures())
{
if (!this.environment.params.worldOptions.generateStructures())
#endif
for (ChunkAccess chunk : chunksToDo)
{
return;
}
for (ChunkWrapper chunkWrapper : chunksToDo)
{
ChunkAccess chunk = chunkWrapper.getChunk();
// hopefully this shouldn't cause any performance issues (this step is generally quite quick so hopefully it should be fine)
// and should prevent some concurrency issues
STRUCTURE_PLACEMENT_LOCK.lock();
#if MC_VER < MC_1_19_2
this.environment.params.generator.createStructures(this.environment.params.registry, tParams.structFeat, chunk, this.environment.params.structures,
this.environment.params.worldSeed);
#elif MC_VER < MC_1_19_4
this.environment.params.generator.createStructures(this.environment.params.registry, this.environment.params.randomState, tParams.structFeat, chunk, this.environment.params.structures,
this.environment.params.worldSeed);
#elif MC_VER <= MC_1_21_3
this.environment.params.generator.createStructures(this.environment.params.registry,
this.environment.params.level.getChunkSource().getGeneratorState(),
tParams.structFeat, chunk, this.environment.params.structures);
#else
this.environment.params.generator.createStructures(this.environment.params.registry,
this.environment.params.level.getChunkSource().getGeneratorState(),
tParams.structFeat, chunk, this.environment.params.structures,
this.environment.params.level.dimension());
#endif
#if MC_VER >= MC_1_18_2
try
{
// hopefully this shouldn't cause any performance issues (this step is generally quite quick so hopefully it should be fine)
// and should prevent some concurrency issues
STRUCTURE_PLACEMENT_LOCK.lock();
tParams.structCheck.onStructureLoad(chunk.getPos(), chunk.getAllStarts());
}
catch (ArrayIndexOutOfBoundsException firstEx)
{
// There's a rare issue with StructStart where it throws ArrayIndexOutOfBounds
// This means the structFeat is corrupted (For some reason) and I need to reset it.
// TODO: Figure out in the future why this happens even though I am using new structFeat - OLD
#if MC_VER < MC_1_19_2
this.environment.params.generator.createStructures(this.environment.params.registry, tParams.structFeat, chunk, this.environment.params.structures,
this.environment.params.worldSeed);
#elif MC_VER < MC_1_19_4
this.environment.params.generator.createStructures(this.environment.params.registry, this.environment.params.randomState, tParams.structFeat, chunk, this.environment.params.structures,
this.environment.params.worldSeed);
#elif MC_VER <= MC_1_21_3
this.environment.params.generator.createStructures(this.environment.params.registry,
this.environment.params.level.getChunkSource().getGeneratorState(),
tParams.structFeat, chunk, this.environment.params.structures);
#else
this.environment.params.generator.createStructures(this.environment.params.registry,
this.environment.params.level.getChunkSource().getGeneratorState(),
tParams.structFeat, chunk, this.environment.params.structures,
this.environment.params.level.dimension());
#endif
// reset the structureStart
tParams.recreateStructureCheck();
#if MC_VER >= MC_1_18_2
try
{
// try running the structure logic again
tParams.structCheck.onStructureLoad(chunk.getPos(), chunk.getAllStarts());
}
catch (ArrayIndexOutOfBoundsException firstEx)
catch (ArrayIndexOutOfBoundsException secondEx)
{
// There's a rare issue with StructStart where it throws ArrayIndexOutOfBounds
// This means the structFeat is corrupted (For some reason) and I need to reset it.
// TODO: Figure out in the future why this happens even though I am using new structFeat - OLD
// reset the structureStart
tParams.recreateStructureCheck();
try
{
// try running the structure logic again
tParams.structCheck.onStructureLoad(chunk.getPos(), chunk.getAllStarts());
}
catch (ArrayIndexOutOfBoundsException secondEx)
{
// the structure logic failed again, log it and move on
LOGGER.error("Unable to create structure starts for " + chunk.getPos() + ". This is an error with MC's world generation. Ignoring and continuing generation. Error: " + secondEx.getMessage()); // don't log the full stack trace since it is long and will generally end up in MC's code
}
// the structure logic failed again, log it and move on
LOGGER.error("Unable to create structure starts for " + chunk.getPos() + ". This is an error with MC's world generation. Ignoring and continuing generation. Error: " + secondEx.getMessage()); // don't log the full stack trace since it is long and will generally end up in MC's code
}
#endif
STRUCTURE_PLACEMENT_LOCK.unlock();
}
#endif
STRUCTURE_PLACEMENT_LOCK.unlock();
}
}
@@ -65,34 +65,21 @@ public final class StepSurface extends AbstractWorldGenStep
ThreadWorldGenParams tParams, DhLitWorldGenRegion worldGenRegion,
ArrayGridList<ChunkWrapper> chunkWrappers)
{
ArrayList<ChunkAccess> chunksToDo = new ArrayList<>();
for (ChunkWrapper chunkWrapper : chunkWrappers)
ArrayList<ChunkWrapper> chunksToDo = this.getChunkWrappersToGenerate(chunkWrappers);
for (ChunkWrapper chunkWrapper : chunksToDo)
{
ChunkAccess chunk = chunkWrapper.getChunk();
if (chunkWrapper.getStatus().isOrAfter(STATUS))
{
// this chunk has already generated this step
continue;
}
else if (chunk instanceof ProtoChunk)
{
chunkWrapper.trySetStatus(STATUS);
chunksToDo.add(chunk);
}
}
for (ChunkAccess chunk : chunksToDo)
{
// System.out.println("StepSurface: "+chunk.getPos());
#if MC_VER < MC_1_18_2
environment.params.generator.buildSurfaceAndBedrock(worldGenRegion, chunk);
this.environment.params.generator.buildSurfaceAndBedrock(worldGenRegion, chunk);
#elif MC_VER < MC_1_19_2
environment.params.generator.buildSurface(worldGenRegion, tParams.structFeat.forWorldGenRegion(worldGenRegion), chunk);
this.environment.params.generator.buildSurface(worldGenRegion, tParams.structFeat.forWorldGenRegion(worldGenRegion), chunk);
#else
environment.params.generator.buildSurface(worldGenRegion, tParams.structFeat.forWorldGenRegion(worldGenRegion), environment.params.randomState, chunk);
this.environment.params.generator.buildSurface(worldGenRegion, tParams.structFeat.forWorldGenRegion(worldGenRegion), this.environment.params.randomState, chunk);
#endif
}
}
}