Clean up some regen code (#2405)

This commit is contained in:
Hannes Greule 2023-09-09 16:07:29 +02:00 committed by GitHub
parent 7288393a39
commit 60a3994d62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 114 deletions

View File

@ -184,9 +184,6 @@ public class PaperweightRegen extends Regenerator<ChunkAccess, ProtoChunk, Level
protected boolean prepare() { protected boolean prepare() {
this.originalServerWorld = ((CraftWorld) originalBukkitWorld).getHandle(); this.originalServerWorld = ((CraftWorld) originalBukkitWorld).getHandle();
originalChunkProvider = originalServerWorld.getChunkSource(); originalChunkProvider = originalServerWorld.getChunkSource();
if (!(originalChunkProvider instanceof ServerChunkCache)) {
return false;
}
//flat bedrock? (only on paper) //flat bedrock? (only on paper)
if (paperConfigField != null) { if (paperConfigField != null) {
@ -197,7 +194,7 @@ public class PaperweightRegen extends Regenerator<ChunkAccess, ProtoChunk, Level
} }
seed = options.getSeed().orElse(originalServerWorld.getSeed()); seed = options.getSeed().orElse(originalServerWorld.getSeed());
chunkStati.forEach((s, c) -> super.chunkStati.put(new ChunkStatusWrap(s), c)); chunkStati.forEach((s, c) -> super.chunkStatuses.put(new ChunkStatusWrap(s), c));
return true; return true;
} }
@ -680,7 +677,7 @@ public class PaperweightRegen extends Regenerator<ChunkAccess, ProtoChunk, Level
} }
@Override @Override
public CompletableFuture<?> processChunk(Long xz, List<ChunkAccess> accessibleChunks) { public CompletableFuture<?> processChunk(List<ChunkAccess> accessibleChunks) {
return chunkStatus.generate( return chunkStatus.generate(
Runnable::run, // TODO revisit, we might profit from this somehow? Runnable::run, // TODO revisit, we might profit from this somehow?
freshWorld, freshWorld,

View File

@ -178,9 +178,6 @@ public class PaperweightRegen extends Regenerator<ChunkAccess, ProtoChunk, Level
protected boolean prepare() { protected boolean prepare() {
this.originalServerWorld = ((CraftWorld) originalBukkitWorld).getHandle(); this.originalServerWorld = ((CraftWorld) originalBukkitWorld).getHandle();
originalChunkProvider = originalServerWorld.getChunkSource(); originalChunkProvider = originalServerWorld.getChunkSource();
if (!(originalChunkProvider instanceof ServerChunkCache)) {
return false;
}
//flat bedrock? (only on paper) //flat bedrock? (only on paper)
if (paperConfigField != null) { if (paperConfigField != null) {
@ -191,7 +188,7 @@ public class PaperweightRegen extends Regenerator<ChunkAccess, ProtoChunk, Level
} }
seed = options.getSeed().orElse(originalServerWorld.getSeed()); seed = options.getSeed().orElse(originalServerWorld.getSeed());
chunkStati.forEach((s, c) -> super.chunkStati.put(new ChunkStatusWrap(s), c)); chunkStati.forEach((s, c) -> super.chunkStatuses.put(new ChunkStatusWrap(s), c));
return true; return true;
} }
@ -523,7 +520,7 @@ public class PaperweightRegen extends Regenerator<ChunkAccess, ProtoChunk, Level
} }
@Override @Override
public CompletableFuture<?> processChunk(Long xz, List<ChunkAccess> accessibleChunks) { public CompletableFuture<?> processChunk(List<ChunkAccess> accessibleChunks) {
return chunkStatus.generate( return chunkStatus.generate(
Runnable::run, // TODO revisit, we might profit from this somehow? Runnable::run, // TODO revisit, we might profit from this somehow?
freshWorld, freshWorld,

View File

@ -192,9 +192,6 @@ public class PaperweightRegen extends Regenerator<ChunkAccess, ProtoChunk, Level
protected boolean prepare() { protected boolean prepare() {
this.originalServerWorld = ((CraftWorld) originalBukkitWorld).getHandle(); this.originalServerWorld = ((CraftWorld) originalBukkitWorld).getHandle();
originalChunkProvider = originalServerWorld.getChunkSource(); originalChunkProvider = originalServerWorld.getChunkSource();
if (!(originalChunkProvider instanceof ServerChunkCache)) {
return false;
}
//flat bedrock? (only on paper) //flat bedrock? (only on paper)
if (paperConfigField != null) { if (paperConfigField != null) {
@ -205,7 +202,7 @@ public class PaperweightRegen extends Regenerator<ChunkAccess, ProtoChunk, Level
} }
seed = options.getSeed().orElse(originalServerWorld.getSeed()); seed = options.getSeed().orElse(originalServerWorld.getSeed());
chunkStati.forEach((s, c) -> super.chunkStati.put(new ChunkStatusWrap(s), c)); chunkStati.forEach((s, c) -> super.chunkStatuses.put(new ChunkStatusWrap(s), c));
return true; return true;
} }
@ -554,7 +551,7 @@ public class PaperweightRegen extends Regenerator<ChunkAccess, ProtoChunk, Level
} }
@Override @Override
public CompletableFuture<?> processChunk(Long xz, List<ChunkAccess> accessibleChunks) { public CompletableFuture<?> processChunk(List<ChunkAccess> accessibleChunks) {
return chunkStatus.generate( return chunkStatus.generate(
Runnable::run, // TODO revisit, we might profit from this somehow? Runnable::run, // TODO revisit, we might profit from this somehow?
freshWorld, freshWorld,

View File

@ -192,9 +192,6 @@ public class PaperweightRegen extends Regenerator<ChunkAccess, ProtoChunk, Level
protected boolean prepare() { protected boolean prepare() {
this.originalServerWorld = ((CraftWorld) originalBukkitWorld).getHandle(); this.originalServerWorld = ((CraftWorld) originalBukkitWorld).getHandle();
originalChunkProvider = originalServerWorld.getChunkSource(); originalChunkProvider = originalServerWorld.getChunkSource();
if (!(originalChunkProvider instanceof ServerChunkCache)) {
return false;
}
//flat bedrock? (only on paper) //flat bedrock? (only on paper)
if (paperConfigField != null) { if (paperConfigField != null) {
@ -205,7 +202,7 @@ public class PaperweightRegen extends Regenerator<ChunkAccess, ProtoChunk, Level
} }
seed = options.getSeed().orElse(originalServerWorld.getSeed()); seed = options.getSeed().orElse(originalServerWorld.getSeed());
chunkStati.forEach((s, c) -> super.chunkStati.put(new ChunkStatusWrap(s), c)); chunkStati.forEach((s, c) -> super.chunkStatuses.put(new ChunkStatusWrap(s), c));
return true; return true;
} }
@ -555,7 +552,7 @@ public class PaperweightRegen extends Regenerator<ChunkAccess, ProtoChunk, Level
} }
@Override @Override
public CompletableFuture<?> processChunk(Long xz, List<ChunkAccess> accessibleChunks) { public CompletableFuture<?> processChunk(List<ChunkAccess> accessibleChunks) {
return chunkStatus.generate( return chunkStatus.generate(
Runnable::run, // TODO revisit, we might profit from this somehow? Runnable::run, // TODO revisit, we might profit from this somehow?
freshWorld, freshWorld,

View File

@ -22,14 +22,14 @@ import com.sk89q.worldedit.world.block.BaseBlock;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap; import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.longs.LongList;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.bukkit.World;
import org.bukkit.generator.BiomeProvider; import org.bukkit.generator.BiomeProvider;
import org.bukkit.generator.BlockPopulator; import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.WorldInfo; import org.bukkit.generator.WorldInfo;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
@ -42,7 +42,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors;
/** /**
* Represents an abstract regeneration handler. * Represents an abstract regeneration handler.
@ -62,7 +61,7 @@ public abstract class Regenerator<IChunkAccess, ProtoChunk extends IChunkAccess,
protected final RegenOptions options; protected final RegenOptions options;
//runtime //runtime
protected final Map<ChunkStatus, Concurrency> chunkStati = new LinkedHashMap<>(); protected final Map<ChunkStatus, Concurrency> chunkStatuses = new LinkedHashMap<>();
private final Long2ObjectLinkedOpenHashMap<ProtoChunk> protoChunks = new Long2ObjectLinkedOpenHashMap<>(); private final Long2ObjectLinkedOpenHashMap<ProtoChunk> protoChunks = new Long2ObjectLinkedOpenHashMap<>();
private final Long2ObjectOpenHashMap<Chunk> chunks = new Long2ObjectOpenHashMap<>(); private final Long2ObjectOpenHashMap<Chunk> chunks = new Long2ObjectOpenHashMap<>();
protected boolean generateConcurrent = true; protected boolean generateConcurrent = true;
@ -85,19 +84,19 @@ public abstract class Regenerator<IChunkAccess, ProtoChunk extends IChunkAccess,
this.options = options; this.options = options;
} }
private static Random getChunkRandom(long worldseed, int x, int z) { private static Random getChunkRandom(long worldSeed, int x, int z) {
Random random = new Random(); Random random = new Random();
random.setSeed(worldseed); random.setSeed(worldSeed);
long xRand = random.nextLong() / 2L * 2L + 1L; long xRand = random.nextLong() / 2L * 2L + 1L;
long zRand = random.nextLong() / 2L * 2L + 1L; long zRand = random.nextLong() / 2L * 2L + 1L;
random.setSeed((long) x * xRand + (long) z * zRand ^ worldseed); random.setSeed((long) x * xRand + (long) z * zRand ^ worldSeed);
return random; return random;
} }
/** /**
* Regenerates the selected {@code Region}. * Regenerates the selected {@code Region}.
* *
* @return whether or not the regeneration process was successful * @return whether the regeneration process was successful
* @throws Exception when something goes terribly wrong * @throws Exception when something goes terribly wrong
*/ */
public boolean regenerate() throws Exception { public boolean regenerate() throws Exception {
@ -175,8 +174,8 @@ public abstract class Regenerator<IChunkAccess, ProtoChunk extends IChunkAccess,
//for now it is working well and fast, if we are bored in the future we could do the research (a lot of it) to reduce the border radius //for now it is working well and fast, if we are bored in the future we could do the research (a lot of it) to reduce the border radius
//generate chunk coords lists with a certain radius //generate chunk coords lists with a certain radius
Int2ObjectOpenHashMap<List<Long>> chunkCoordsForRadius = new Int2ObjectOpenHashMap<>(); Int2ObjectOpenHashMap<long[]> chunkCoordsForRadius = new Int2ObjectOpenHashMap<>();
chunkStati.keySet().stream().map(ChunkStatusWrapper::requiredNeighborChunkRadius0).distinct().forEach(radius -> { chunkStatuses.keySet().stream().mapToInt(ChunkStatusWrapper::requiredNeighborChunkRadius0).distinct().forEach(radius -> {
if (radius == -1) { //ignore ChunkStatus.EMPTY if (radius == -1) { //ignore ChunkStatus.EMPTY
return; return;
} }
@ -186,19 +185,19 @@ public abstract class Regenerator<IChunkAccess, ProtoChunk extends IChunkAccess,
}); });
//create chunks //create chunks
for (Long xz : chunkCoordsForRadius.get(0)) { for (long xz : chunkCoordsForRadius.get(0)) {
ProtoChunk chunk = createProtoChunk(MathMan.unpairIntX(xz), MathMan.unpairIntY(xz)); ProtoChunk chunk = createProtoChunk(MathMan.unpairIntX(xz), MathMan.unpairIntY(xz));
protoChunks.put(xz, chunk); protoChunks.put(xz, chunk);
} }
//generate lists for RegionLimitedWorldAccess, need to be square with odd length (e.g. 17x17), 17 = 1 middle chunk + 8 border chunks * 2 //generate lists for RegionLimitedWorldAccess, need to be square with odd length (e.g. 17x17), 17 = 1 middle chunk + 8 border chunks * 2
Int2ObjectOpenHashMap<Long2ObjectOpenHashMap<List<IChunkAccess>>> worldlimits = new Int2ObjectOpenHashMap<>(); Int2ObjectOpenHashMap<Long2ObjectOpenHashMap<List<IChunkAccess>>> worldLimits = new Int2ObjectOpenHashMap<>();
chunkStati.keySet().stream().map(ChunkStatusWrapper::requiredNeighborChunkRadius0).distinct().forEach(radius -> { chunkStatuses.keySet().stream().mapToInt(ChunkStatusWrapper::requiredNeighborChunkRadius0).distinct().forEach(radius -> {
if (radius == -1) { //ignore ChunkStatus.EMPTY if (radius == -1) { //ignore ChunkStatus.EMPTY
return; return;
} }
Long2ObjectOpenHashMap<List<IChunkAccess>> map = new Long2ObjectOpenHashMap<>(); Long2ObjectOpenHashMap<List<IChunkAccess>> map = new Long2ObjectOpenHashMap<>();
for (Long xz : chunkCoordsForRadius.get(radius)) { for (long xz : chunkCoordsForRadius.get(radius)) {
int x = MathMan.unpairIntX(xz); int x = MathMan.unpairIntX(xz);
int z = MathMan.unpairIntY(xz); int z = MathMan.unpairIntY(xz);
List<IChunkAccess> l = new ArrayList<>((radius + 1 + radius) * (radius + 1 + radius)); List<IChunkAccess> l = new ArrayList<>((radius + 1 + radius) * (radius + 1 + radius));
@ -209,80 +208,63 @@ public abstract class Regenerator<IChunkAccess, ProtoChunk extends IChunkAccess,
} }
map.put(xz, l); map.put(xz, l);
} }
worldlimits.put(radius, map); worldLimits.put(radius, map);
}); });
//run generation tasks excluding FULL chunk status //run generation tasks excluding FULL chunk status
for (Map.Entry<ChunkStatus, Concurrency> entry : chunkStati.entrySet()) { for (Map.Entry<ChunkStatus, Concurrency> entry : chunkStatuses.entrySet()) {
ChunkStatus chunkStatus = entry.getKey(); ChunkStatus chunkStatus = entry.getKey();
int radius = chunkStatus.requiredNeighborChunkRadius0(); int radius = chunkStatus.requiredNeighborChunkRadius0();
List<Long> coords = chunkCoordsForRadius.get(radius); long[] coords = chunkCoordsForRadius.get(radius);
Long2ObjectOpenHashMap<List<IChunkAccess>> limitsForRadius = worldLimits.get(radius);
if (this.generateConcurrent && entry.getValue() == Concurrency.RADIUS) { if (this.generateConcurrent && entry.getValue() == Concurrency.RADIUS) {
SequentialTasks<ConcurrentTasks<SequentialTasks<Long>>> tasks = getChunkStatusTaskRows(coords, radius); SequentialTasks<ConcurrentTasks<LongList>> tasks = getChunkStatusTaskRows(coords, radius);
for (ConcurrentTasks<SequentialTasks<Long>> para : tasks) { for (ConcurrentTasks<LongList> para : tasks) {
List<Runnable> scheduled = new ArrayList<>(tasks.size()); List<Runnable> scheduled = new ArrayList<>(tasks.size());
for (SequentialTasks<Long> row : para) { for (LongList row : para) {
scheduled.add(() -> { scheduled.add(() -> {
for (Long xz : row) { for (long xz : row) {
chunkStatus.processChunkSave(xz, worldlimits.get(radius).get(xz)); chunkStatus.processChunkSave(xz, limitsForRadius.get(xz));
} }
}); });
} }
try { runAndWait(scheduled);
List<Future<?>> futures = new ArrayList<>();
scheduled.forEach(task -> futures.add(executor.submit(task)));
for (Future<?> future : futures) {
future.get();
}
} catch (Exception e) {
e.printStackTrace();
}
} }
} else if (this.generateConcurrent && entry.getValue() == Concurrency.FULL) { } else if (this.generateConcurrent && entry.getValue() == Concurrency.FULL) {
// every chunk can be processed individually // every chunk can be processed individually
List<Runnable> scheduled = new ArrayList<>(coords.size()); List<Runnable> scheduled = new ArrayList<>(coords.length);
for (long xz : coords) { for (long xz : coords) {
scheduled.add(() -> { scheduled.add(() -> chunkStatus.processChunkSave(xz, limitsForRadius.get(xz)));
chunkStatus.processChunkSave(xz, worldlimits.get(radius).get(xz));
});
}
try {
List<Future<?>> futures = new ArrayList<>();
scheduled.forEach(task -> futures.add(executor.submit(task)));
for (Future<?> future : futures) {
future.get();
}
} catch (Exception e) {
e.printStackTrace();
} }
runAndWait(scheduled);
} else { // Concurrency.NONE or generateConcurrent == false } else { // Concurrency.NONE or generateConcurrent == false
// run sequential but submit to different thread // run sequential but submit to different thread
// running regen on the main thread otherwise triggers async-only events on the main thread // running regen on the main thread otherwise triggers async-only events on the main thread
executor.submit(() -> { executor.submit(() -> {
for (long xz : coords) { for (long xz : coords) {
chunkStatus.processChunkSave(xz, worldlimits.get(radius).get(xz)); chunkStatus.processChunkSave(xz, limitsForRadius.get(xz));
} }
}).get(); // wait until finished this step }).get(); // wait until finished this step
} }
} }
//convert to proper chunks //convert to proper chunks
for (Long xz : chunkCoordsForRadius.get(0)) { for (long xz : chunkCoordsForRadius.get(0)) {
ProtoChunk proto = protoChunks.get(xz); ProtoChunk proto = protoChunks.get(xz);
chunks.put(xz, createChunk(proto)); chunks.put(xz, createChunk(proto));
} }
//final chunkstatus //final chunkstatus
ChunkStatus FULL = getFullChunkStatus(); ChunkStatus FULL = getFullChunkStatus();
for (Long xz : chunkCoordsForRadius.get(0)) { //FULL.requiredNeighbourChunkRadius() == 0! for (long xz : chunkCoordsForRadius.get(0)) { //FULL.requiredNeighbourChunkRadius() == 0!
Chunk chunk = chunks.get(xz); Chunk chunk = chunks.get(xz);
FULL.processChunkSave(xz, Arrays.asList(chunk)); FULL.processChunkSave(xz, List.of(chunk));
} }
//populate //populate
List<BlockPopulator> populators = getBlockPopulators(); List<BlockPopulator> populators = getBlockPopulators();
for (Long xz : chunkCoordsForRadius.get(0)) { for (long xz : chunkCoordsForRadius.get(0)) {
int x = MathMan.unpairIntX(xz); int x = MathMan.unpairIntX(xz);
int z = MathMan.unpairIntY(xz); int z = MathMan.unpairIntY(xz);
@ -302,6 +284,18 @@ public abstract class Regenerator<IChunkAccess, ProtoChunk extends IChunkAccess,
return true; return true;
} }
private void runAndWait(final List<Runnable> tasks) {
try {
List<Future<?>> futures = new ArrayList<>();
tasks.forEach(task -> futures.add(executor.submit(task)));
for (Future<?> future : futures) {
future.get();
}
} catch (Exception e) {
LOGGER.catching(e);
}
}
private void copyToWorld() { private void copyToWorld() {
//Setting Blocks //Setting Blocks
boolean genbiomes = options.shouldRegenBiomes(); boolean genbiomes = options.shouldRegenBiomes();
@ -437,7 +431,7 @@ public abstract class Regenerator<IChunkAccess, ProtoChunk extends IChunkAccess,
protected abstract IChunkCache<IChunkGet> initSourceQueueCache(); protected abstract IChunkCache<IChunkGet> initSourceQueueCache();
//algorithms //algorithms
private List<Long> getChunkCoordsRegen(Region region, int border) { //needs to be square num of chunks private long[] getChunkCoordsRegen(Region region, int border) { //needs to be square num of chunks
BlockVector3 oldMin = region.getMinimumPoint(); BlockVector3 oldMin = region.getMinimumPoint();
BlockVector3 newMin = BlockVector3.at( BlockVector3 newMin = BlockVector3.at(
(oldMin.getX() >> 4 << 4) - border * 16, (oldMin.getX() >> 4 << 4) - border * 16,
@ -455,76 +449,79 @@ public abstract class Regenerator<IChunkAccess, ProtoChunk extends IChunkAccess,
.sorted(Comparator .sorted(Comparator
.comparingInt(BlockVector2::getZ) .comparingInt(BlockVector2::getZ)
.thenComparingInt(BlockVector2::getX)) //needed for RegionLimitedWorldAccess .thenComparingInt(BlockVector2::getX)) //needed for RegionLimitedWorldAccess
.map(c -> MathMan.pairInt(c.getX(), c.getZ())) .mapToLong(c -> MathMan.pairInt(c.getX(), c.getZ()))
.collect(Collectors.toList()); .toArray();
} }
/** /**
* Creates a list of chunkcoord rows that may be executed concurrently * Creates a list of chunkcoord rows that may be executed concurrently
* *
* @param allcoords the coords that should be sorted into rows, must be sorted by z and x * @param allCoords the coords that should be sorted into rows, must be sorted by z and x
* @param requiredNeighborChunkRadius the radius of neighbor chunks that may not be written to concurrently (ChunkStatus * @param requiredNeighborChunkRadius the radius of neighbor chunks that may not be written to concurrently (ChunkStatus
* .requiredNeighborRadius) * .requiredNeighborRadius)
* @return a list of chunkcoords rows that may be executed concurrently * @return a list of chunkcoords rows that may be executed concurrently
*/ */
private SequentialTasks<ConcurrentTasks<SequentialTasks<Long>>> getChunkStatusTaskRows( private SequentialTasks<ConcurrentTasks<LongList>> getChunkStatusTaskRows(
List<Long> allcoords, long[] allCoords,
int requiredNeighborChunkRadius int requiredNeighborChunkRadius
) { ) {
int requiredneighbors = Math.max(0, requiredNeighborChunkRadius); int requiredNeighbors = Math.max(0, requiredNeighborChunkRadius);
int minx = allcoords.isEmpty() ? 0 : MathMan.unpairIntX(allcoords.get(0)); final int coordsCount = allCoords.length;
int maxx = allcoords.isEmpty() ? 0 : MathMan.unpairIntX(allcoords.get(allcoords.size() - 1)); long first = coordsCount == 0 ? 0 : allCoords[0];
int minz = allcoords.isEmpty() ? 0 : MathMan.unpairIntY(allcoords.get(0)); long last = coordsCount == 0 ? 0 : allCoords[coordsCount - 1];
int maxz = allcoords.isEmpty() ? 0 : MathMan.unpairIntY(allcoords.get(allcoords.size() - 1)); int minX = MathMan.unpairIntX(first);
SequentialTasks<ConcurrentTasks<SequentialTasks<Long>>> tasks; int maxX = MathMan.unpairIntX(last);
if (maxz - minz > maxx - minx) { int minZ = MathMan.unpairIntY(first);
int numlists = Math.min(requiredneighbors * 2 + 1, maxx - minx + 1); int maxZ = MathMan.unpairIntY(last);
SequentialTasks<ConcurrentTasks<LongList>> tasks;
if (maxZ - minZ > maxX - minX) {
int numlists = Math.min(requiredNeighbors * 2 + 1, maxX - minX + 1);
Int2ObjectOpenHashMap<SequentialTasks<Long>> byx = new Int2ObjectOpenHashMap(); Int2ObjectOpenHashMap<LongList> byX = new Int2ObjectOpenHashMap<>();
int expectedListLength = (allcoords.size() + 1) / (maxx - minx); int expectedListLength = (coordsCount + 1) / (maxX - minX);
//init lists //init lists
for (int i = minx; i <= maxx; i++) { for (int i = minX; i <= maxX; i++) {
byx.put(i, new SequentialTasks(expectedListLength)); byX.put(i, new LongArrayList(expectedListLength));
} }
//sort into lists by x coord //sort into lists by x coord
for (Long xz : allcoords) { for (long allCoord : allCoords) {
byx.get(MathMan.unpairIntX(xz)).add(xz); byX.get(MathMan.unpairIntX(allCoord)).add(allCoord);
} }
//create parallel tasks //create parallel tasks
tasks = new SequentialTasks(numlists); tasks = new SequentialTasks<>(numlists);
for (int offset = 0; offset < numlists; offset++) { for (int offset = 0; offset < numlists; offset++) {
ConcurrentTasks<SequentialTasks<Long>> para = new ConcurrentTasks((maxz - minz + 1) / numlists + 1); ConcurrentTasks<LongList> para = new ConcurrentTasks<>((maxZ - minZ + 1) / numlists + 1);
for (int i = 0; minx + i * numlists + offset <= maxx; i++) { for (int i = 0; minX + i * numlists + offset <= maxX; i++) {
para.add(byx.get(minx + i * numlists + offset)); para.add(byX.get(minX + i * numlists + offset));
} }
tasks.add(para); tasks.add(para);
} }
} else { } else {
int numlists = Math.min(requiredneighbors * 2 + 1, maxz - minz + 1); int numlists = Math.min(requiredNeighbors * 2 + 1, maxZ - minZ + 1);
Int2ObjectOpenHashMap<SequentialTasks<Long>> byz = new Int2ObjectOpenHashMap(); Int2ObjectOpenHashMap<LongList> byZ = new Int2ObjectOpenHashMap<>();
int expectedListLength = (allcoords.size() + 1) / (maxz - minz); int expectedListLength = (coordsCount + 1) / (maxZ - minZ);
//init lists //init lists
for (int i = minz; i <= maxz; i++) { for (int i = minZ; i <= maxZ; i++) {
byz.put(i, new SequentialTasks(expectedListLength)); byZ.put(i, new LongArrayList(expectedListLength));
} }
//sort into lists by x coord //sort into lists by x coord
for (Long xz : allcoords) { for (long allCoord : allCoords) {
byz.get(MathMan.unpairIntY(xz)).add(xz); byZ.get(MathMan.unpairIntY(allCoord)).add(allCoord);
} }
//create parallel tasks //create parallel tasks
tasks = new SequentialTasks(numlists); tasks = new SequentialTasks<>(numlists);
for (int offset = 0; offset < numlists; offset++) { for (int offset = 0; offset < numlists; offset++) {
ConcurrentTasks<SequentialTasks<Long>> para = new ConcurrentTasks((maxx - minx + 1) / numlists + 1); ConcurrentTasks<LongList> para = new ConcurrentTasks<>((maxX - minX + 1) / numlists + 1);
for (int i = 0; minz + i * numlists + offset <= maxz; i++) { for (int i = 0; minZ + i * numlists + offset <= maxZ; i++) {
para.add(byz.get(minz + i * numlists + offset)); para.add(byZ.get(minZ + i * numlists + offset));
} }
tasks.add(para); tasks.add(para);
} }
@ -576,15 +573,14 @@ public abstract class Regenerator<IChunkAccess, ProtoChunk extends IChunkAccess,
/** /**
* Return the name of the wrapped {@code ChunkStatus}. * Return the name of the wrapped {@code ChunkStatus}.
* *
* @param xz represents the chunk coordinates of the chunk to process as denoted by {@code MathMan}
* @param accessibleChunks a list of chunks that will be used during the execution of the wrapped {@code ChunkStatus}. * @param accessibleChunks a list of chunks that will be used during the execution of the wrapped {@code ChunkStatus}.
* This list is order in the correct order required by the {@code ChunkStatus}, unless Mojang suddenly decides to do things differently. * This list is order in the correct order required by the {@code ChunkStatus}, unless Mojang suddenly decides to do things differently.
*/ */
public abstract CompletableFuture<?> processChunk(Long xz, List<IChunkAccess> accessibleChunks); public abstract CompletableFuture<?> processChunk(List<IChunkAccess> accessibleChunks);
void processChunkSave(Long xz, List<IChunkAccess> accessibleChunks) { void processChunkSave(long xz, List<IChunkAccess> accessibleChunks) {
try { try {
processChunk(xz, accessibleChunks).get(); processChunk(accessibleChunks).get();
} catch (Exception e) { } catch (Exception e) {
LOGGER.error( LOGGER.error(
"Error while running " + name() + " on chunk " + MathMan.unpairIntX(xz) + "/" + MathMan.unpairIntY(xz), "Error while running " + name() + " on chunk " + MathMan.unpairIntX(xz) + "/" + MathMan.unpairIntY(xz),
@ -597,16 +593,16 @@ public abstract class Regenerator<IChunkAccess, ProtoChunk extends IChunkAccess,
public static class SequentialTasks<T> extends Tasks<T> { public static class SequentialTasks<T> extends Tasks<T> {
public SequentialTasks(int expectedsize) { public SequentialTasks(int expectedSize) {
super(expectedsize); super(expectedSize);
} }
} }
public static class ConcurrentTasks<T> extends Tasks<T> { public static class ConcurrentTasks<T> extends Tasks<T> {
public ConcurrentTasks(int expectedsize) { public ConcurrentTasks(int expectedSize) {
super(expectedsize); super(expectedSize);
} }
} }
@ -615,8 +611,8 @@ public abstract class Regenerator<IChunkAccess, ProtoChunk extends IChunkAccess,
private final List<T> tasks; private final List<T> tasks;
public Tasks(int expectedsize) { public Tasks(int expectedSize) {
tasks = new ArrayList(expectedsize); tasks = new ArrayList<>(expectedSize);
} }
public void add(T task) { public void add(T task) {