From a40cf8f2dc4215d6c1fae069532854330823f872 Mon Sep 17 00:00:00 2001 From: Daniel Orr Date: Thu, 25 Jul 2024 22:20:04 +0100 Subject: [PATCH] Blueprint#flattenFutures --- gradle.properties | 2 +- .../mcbrawls/blueprint/structure/Blueprint.kt | 26 +++++++++++++++++-- .../blueprint/structure/ProgressiveFuture.kt | 15 +++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/net/mcbrawls/blueprint/structure/ProgressiveFuture.kt diff --git a/gradle.properties b/gradle.properties index c5d2012..4655a5c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ yarn_build=7 loader_version=0.15.11 # Mod Properties -mod_version=1.6.0 +mod_version=1.6.1 maven_group=net.mcbrawls mod_id=blueprint diff --git a/src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt b/src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt index 28863f5..835e923 100644 --- a/src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt +++ b/src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt @@ -64,7 +64,7 @@ data class Blueprint( * Launches a completable future placing this blueprint in the world at the given position. * @return a placed blueprint future and a progress provider */ - fun placeWithProgress(world: ServerWorld, position: BlockPos, processor: BlockStateProcessor? = null): Pair, ProgressProvider> { + fun placeWithProgress(world: ServerWorld, position: BlockPos, processor: BlockStateProcessor? = null): ProgressiveFuture { val progress = AtomicReference(0.0f) val future: CompletableFuture = CompletableFuture.supplyAsync { @@ -79,7 +79,7 @@ data class Blueprint( PlacedBlueprint(this, position) } - return future to ProgressProvider(progress::get) + return ProgressiveFuture(future, ProgressProvider(progress::get)) } /** @@ -141,5 +141,27 @@ data class Blueprint( * An entirely empty blueprint. */ val EMPTY = Blueprint(emptyList(), emptyList(), emptyMap(), Vec3i.ZERO, emptyMap()) + + /** + * Flattens a set of placed blueprint futures into one progressive future. + * @return a progressive future of combined futures and progress provider + */ + fun flattenFutures(futures: Set>): ProgressiveFuture<*> { + // create compounded future + val future = CompletableFuture.runAsync { + val completableFutures = futures.map(ProgressiveFuture<*>::future) + completableFutures.forEach(CompletableFuture<*>::join) + } + + // provide average progress + val provider = ProgressProvider { + val providers = futures.map(ProgressiveFuture<*>::progressProvider) + val progresses = providers.map(ProgressProvider::getProgress) + val average = progresses.average() + average.toFloat() + } + + return ProgressiveFuture(future, provider) + } } } diff --git a/src/main/kotlin/net/mcbrawls/blueprint/structure/ProgressiveFuture.kt b/src/main/kotlin/net/mcbrawls/blueprint/structure/ProgressiveFuture.kt new file mode 100644 index 0000000..a5eeac4 --- /dev/null +++ b/src/main/kotlin/net/mcbrawls/blueprint/structure/ProgressiveFuture.kt @@ -0,0 +1,15 @@ +package net.mcbrawls.blueprint.structure + +import java.util.concurrent.CompletableFuture + +data class ProgressiveFuture( + /** + * The future. + */ + val future: CompletableFuture, + + /** + * The progress provider of the future. + */ + val progressProvider: ProgressProvider +)