This commit is contained in:
Leijurv 2023-07-16 18:17:15 -07:00
parent a83d1901f2
commit 0e567f2f90
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
1 changed files with 44 additions and 32 deletions

View File

@ -34,10 +34,7 @@ import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import java.lang.ref.SoftReference;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
/**
* @author Brady
@ -50,12 +47,15 @@ public final class NetherPathfinderContext {
// Visible for access in BlockStateOctreeInterface
final long context;
private final long seed;
private final ExecutorService executor;
private final ThreadPoolExecutor executor;
private final ExecutorService executorHighPriority;
public NetherPathfinderContext(long seed) {
this.context = NetherPathfinder.newContext(seed);
this.seed = seed;
this.executor = Executors.newSingleThreadExecutor();
this.executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
this.executorHighPriority = Executors.newSingleThreadExecutor();
}
public void queueCacheCulling(int chunkX, int chunkZ, int maxDistanceBlocks, BlockStateOctreeInterface boi) {
@ -68,8 +68,12 @@ public final class NetherPathfinderContext {
}
public void queueForPacking(final Chunk chunkIn) {
if (this.executor.getQueue().size() > 50000) {
return;
}
final SoftReference<Chunk> ref = new SoftReference<>(chunkIn);
this.executor.execute(() -> {
synchronized (this.cacheLock) {
// TODO: Prioritize packing recent chunks and/or ones that the path goes through,
// and prune the oldest chunks per chunkPackerQueueMaxSize
final Chunk chunk = ref.get();
@ -77,11 +81,16 @@ public final class NetherPathfinderContext {
long ptr = NetherPathfinder.getOrCreateChunk(this.context, chunk.x, chunk.z);
writeChunkData(chunk, ptr);
}
}
});
}
public void queueBlockUpdate(BlockChangeEvent event) {
if (this.executor.getQueue().size() > 50000) {
return;
}
this.executor.execute(() -> {
synchronized (this.cacheLock) {
ChunkPos chunkPos = event.getChunkPos();
long ptr = NetherPathfinder.getChunkPointer(this.context, chunkPos.x, chunkPos.z);
if (ptr == 0) return; // this shouldn't ever happen
@ -91,11 +100,13 @@ public final class NetherPathfinderContext {
boolean isSolid = pair.second() != AIR_BLOCK_STATE;
Octree.setBlock(ptr, pos.getX() & 15, pos.getY(), pos.getZ() & 15, isSolid);
});
}
});
}
public CompletableFuture<PathSegment> pathFindAsync(final BlockPos src, final BlockPos dst) {
return CompletableFuture.supplyAsync(() -> {
synchronized (this.cacheLock) {
final PathSegment segment = NetherPathfinder.pathFind(
this.context,
src.getX(), src.getY(), src.getZ(),
@ -107,7 +118,8 @@ public final class NetherPathfinderContext {
throw new PathCalculationException("Path calculation failed");
}
return segment;
}, this.executor);
}
}, this.executorHighPriority);
}
/**