Skip to content

Commit

Permalink
Blueprint#flattenFutures
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Jul 25, 2024
1 parent a6e8c3f commit a40cf8f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 24 additions & 2 deletions src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompletableFuture<PlacedBlueprint>, ProgressProvider> {
fun placeWithProgress(world: ServerWorld, position: BlockPos, processor: BlockStateProcessor? = null): ProgressiveFuture<PlacedBlueprint> {
val progress = AtomicReference(0.0f)

val future: CompletableFuture<PlacedBlueprint> = CompletableFuture.supplyAsync {
Expand All @@ -79,7 +79,7 @@ data class Blueprint(
PlacedBlueprint(this, position)
}

return future to ProgressProvider(progress::get)
return ProgressiveFuture(future, ProgressProvider(progress::get))
}

/**
Expand Down Expand Up @@ -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<PlacedBlueprint>>): 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)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.mcbrawls.blueprint.structure

import java.util.concurrent.CompletableFuture

data class ProgressiveFuture<T>(
/**
* The future.
*/
val future: CompletableFuture<T>,

/**
* The progress provider of the future.
*/
val progressProvider: ProgressProvider
)

0 comments on commit a40cf8f

Please sign in to comment.