Skip to content

Commit

Permalink
placeWithProgress
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Jul 2, 2024
1 parent b44c759 commit d44085f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
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=3
loader_version=0.15.11

# Mod Properties
mod_version=1.4.1
mod_version=1.5.0
maven_group=net.mcbrawls
mod_id=blueprint

Expand Down
29 changes: 29 additions & 0 deletions src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import net.minecraft.block.BlockState
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3i
import java.util.concurrent.CompletableFuture
import java.util.concurrent.atomic.AtomicReference
import java.util.function.BiConsumer

/**
Expand Down Expand Up @@ -38,6 +40,11 @@ data class Blueprint(
*/
val center: Vec3i = Vec3i(size.x / 2, size.y / 2, size.z / 2)

/**
* The total amount of blocks placed from this blueprint.
*/
val totalBlocks: Int = palettedBlockStates.size

/**
* Places this blueprint in the world at the given position.
* @return a placed blueprint
Expand All @@ -50,6 +57,28 @@ data class Blueprint(
return PlacedBlueprint(this, position)
}

/**
* 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): Pair<CompletableFuture<PlacedBlueprint>, ProgressProvider> {
val progress = AtomicReference(0.0f)

val future: CompletableFuture<PlacedBlueprint> = CompletableFuture.supplyAsync {
var i = 0
forEach { offset, state ->
world.setBlockState(position.add(offset), state)
i++
}

progress.set(i.toFloat() / totalBlocks)

PlacedBlueprint(this, position)
}

return future to ProgressProvider(progress::get)
}

/**
* Performs the given action for every position in the blueprint.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.mcbrawls.blueprint.structure

fun interface ProgressProvider {
fun getProgress(): Float
}

0 comments on commit d44085f

Please sign in to comment.