11package org .yatopiamc .c2me .mixin .threading .chunkio ;
22
3+ import com .google .common .collect .Sets ;
34import com .ibm .asyncutil .locks .AsyncNamedLock ;
45import com .mojang .datafixers .DataFixer ;
56import com .mojang .datafixers .util .Either ;
4243import org .yatopiamc .c2me .common .util .SneakyThrow ;
4344
4445import java .io .File ;
46+ import java .util .HashMap ;
47+ import java .util .HashSet ;
48+ import java .util .Set ;
4549import java .util .concurrent .CompletableFuture ;
50+ import java .util .concurrent .ConcurrentHashMap ;
4651import java .util .concurrent .ConcurrentLinkedQueue ;
4752import java .util .function .Supplier ;
4853
@@ -93,16 +98,23 @@ private void onInit(CallbackInfo info) {
9398 chunkLock = AsyncNamedLock .createFair ();
9499 }
95100
101+ private Set <ChunkPos > scheduledChunks = new HashSet <>();
102+
96103 /**
97104 * @author ishland
98105 * @reason async io and deserialization
99106 */
100107 @ Overwrite
101108 private CompletableFuture <Either <Chunk , ChunkHolder .Unloaded >> loadChunk (ChunkPos pos ) {
109+ if (scheduledChunks == null ) scheduledChunks = new HashSet <>();
110+ synchronized (scheduledChunks ) {
111+ if (scheduledChunks .contains (pos )) throw new IllegalArgumentException ("Already scheduled" );
112+ scheduledChunks .add (pos );
113+ }
102114
103115 final CompletableFuture <CompoundTag > poiData = ((C2MECachedRegionStorage ) this .pointOfInterestStorage .worker ).getNbtAtAsync (pos );
104116
105- return getUpdatedChunkTagAtAsync (pos ).thenApplyAsync (compoundTag -> {
117+ final CompletableFuture < Either < Chunk , ChunkHolder . Unloaded >> future = getUpdatedChunkTagAtAsync (pos ).thenApplyAsync (compoundTag -> {
106118 if (compoundTag != null ) {
107119 try {
108120 if (compoundTag .contains ("Level" , 10 ) && compoundTag .getCompound ("Level" ).contains ("Status" , 8 )) {
@@ -127,6 +139,12 @@ private CompletableFuture<Either<Chunk, ChunkHolder.Unloaded>> loadChunk(ChunkPo
127139 return Either .left (new ProtoChunk (pos , UpgradeData .NO_UPGRADE_DATA ));
128140 }
129141 }, this .mainThreadExecutor );
142+ future .exceptionally (throwable -> null ).thenRun (() -> {
143+ synchronized (scheduledChunks ) {
144+ scheduledChunks .remove (pos );
145+ }
146+ });
147+ return future ;
130148
131149 // [VanillaCopy] - for reference
132150 /*
0 commit comments