make the packer queue super cute and deduplicated

This commit is contained in:
Leijurv 2020-08-17 15:19:11 -07:00
parent e6ba4ef309
commit 38d047dbd1
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A

View File

@ -26,6 +26,7 @@ import baritone.api.utils.Helper;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.chunk.Chunk;
import java.io.IOException;
@ -33,6 +34,8 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
/**
@ -56,7 +59,17 @@ public final class CachedWorld implements ICachedWorld, Helper {
*/
private final String directory;
private final LinkedBlockingQueue<Chunk> toPack = new LinkedBlockingQueue<>();
/**
* Queue of positions to pack. Refers to the toPackMap, in that every element of this queue will be a
* key in that map.
*/
private final LinkedBlockingQueue<ChunkPos> toPackQueue = new LinkedBlockingQueue<>();
/**
* All chunk positions pending packing. This map will be updated in-place if a new update to the chunk occurs
* while waiting in the queue for the packer thread to get to it.
*/
private final Map<ChunkPos, Chunk> toPackMap = new ConcurrentHashMap<>();
private final int dimension;
@ -89,10 +102,8 @@ public final class CachedWorld implements ICachedWorld, Helper {
@Override
public final void queueForPacking(Chunk chunk) {
try {
toPack.put(chunk);
} catch (InterruptedException e) {
e.printStackTrace();
if (toPackMap.put(chunk.getPos(), chunk) == null) {
toPackQueue.add(chunk.getPos());
}
}
@ -293,13 +304,9 @@ public final class CachedWorld implements ICachedWorld, Helper {
public void run() {
while (true) {
// TODO: Add CachedWorld unloading to remove the redundancy of having this
LinkedBlockingQueue<Chunk> queue = toPack;
if (queue == null) {
break;
}
try {
Chunk chunk = queue.take();
ChunkPos pos = toPackQueue.take();
Chunk chunk = toPackMap.remove(pos);
CachedChunk cached = ChunkPacker.pack(chunk);
CachedWorld.this.updateCachedChunk(cached);
//System.out.println("Processed chunk at " + chunk.x + "," + chunk.z);