io.github.dailystruggle.rtp.common.selection.region.ChunkSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of RTP Show documentation
Show all versions of RTP Show documentation
a random teleport plugin
package io.github.dailystruggle.rtp.common.selection.region;
import io.github.dailystruggle.rtp.common.serverSide.substitutions.RTPChunk;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
public final class ChunkSet {
public final List> chunks;
public final CompletableFuture complete;
public ChunkSet(List> chunks, CompletableFuture complete) {
this.chunks = chunks;
this.complete = complete;
AtomicLong count = new AtomicLong();
Semaphore countAccess = new Semaphore(1);
chunks.forEach(rtpChunkCompletableFuture -> rtpChunkCompletableFuture.thenAccept(rtpChunk -> {
try {
countAccess.acquire();
long i = count.incrementAndGet();
if (i == chunks.size()) {
this.complete.complete(true);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
countAccess.release();
}
}));
}
public void keep(boolean keep) {
chunks.forEach(chunk -> {
if (chunk.isDone()) {
try {
RTPChunk rtpChunk = chunk.get();
rtpChunk.keep(keep);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
} else {
chunk.thenAccept(chunk1 -> chunk1.keep(keep));
}
});
}
public void whenComplete(Consumer consumer) {
if (complete.isDone()) {
try {
consumer.accept(complete.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return;
}
complete.thenAccept(consumer);
}
}