Skip to content

Commit

Permalink
Placed blueprint refactor and positions set
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Mar 22, 2024
1 parent 3cc70d5 commit b4c2119
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ brawls_sgui_version=1.0.0
sgui_version=1.4.2+1.20.4

# Mod Properties
mod_version=1.0.1
mod_version=1.1.0
maven_group=net.mcbrawls
mod_id=blueprint
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import net.mcbrawls.blueprint.region.Region
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3d
import java.util.function.Consumer

/**
* A blueprint after it has been placed in the world.
Expand All @@ -17,21 +18,40 @@ data class PlacedBlueprint(
/**
* The blueprint used in placement.
*/
val sourceBlueprint: Blueprint,
val blueprint: Blueprint,

/**
* Where the blueprint was placed in the world.
*/
val placedPosition: BlockPos
val position: BlockPos
) {
val offset: Vec3d = Vec3d.of(placedPosition)
val offset: Vec3d = Vec3d.of(position)

/**
* All positions of this blueprint.
*/
val positions: Set<BlockPos> by lazy {
buildSet {
val size = blueprint.size
val sizeX = size.x
val sizeY = size.y
val sizeZ = size.z
for (x in 0 until sizeX) {
for (y in 0 until sizeY) {
for (z in 0 until sizeZ) {
add(position.add(x, y, z))
}
}
}
}
}

/**
* Places the source blueprint again.
* @return this placed blueprint
*/
fun place(world: ServerWorld): PlacedBlueprint {
sourceBlueprint.place(world, placedPosition)
blueprint.place(world, position)
return this
}

Expand All @@ -40,7 +60,7 @@ data class PlacedBlueprint(
* @return an offset region
*/
fun getRegion(key: String): Region {
val region = sourceBlueprint.regions[key]
val region = blueprint.regions[key]

// verify region
if (region == null) {
Expand All @@ -57,7 +77,7 @@ data class PlacedBlueprint(
* @return an offset region
*/
fun getRegionsCombined(vararg keys: String): Region {
val nullableRegions = keys.associateWith { key -> sourceBlueprint.regions[key] }
val nullableRegions = keys.associateWith { key -> blueprint.regions[key] }
val regions = nullableRegions.values.filterNotNull()

// verify regions
Expand All @@ -74,14 +94,21 @@ data class PlacedBlueprint(
return CompoundRegion.ofRegionsOffset(offset, *regions.toTypedArray())
}

/**
* Performs an action for every position in this placed blueprint.
*/
fun forEachPosition(action: Consumer<BlockPos>) {
return positions.forEach(action)
}

companion object {
/**
* The codec for a placed blueprint.
*/
val CODEC: Codec<PlacedBlueprint> = RecordCodecBuilder.create { instance ->
instance.group(
Blueprint.CODEC.fieldOf("source_blueprint").forGetter(PlacedBlueprint::sourceBlueprint),
BlockPos.CODEC.fieldOf("placed_position").forGetter(PlacedBlueprint::placedPosition)
Blueprint.CODEC.fieldOf("source_blueprint").forGetter(PlacedBlueprint::blueprint),
BlockPos.CODEC.fieldOf("placed_position").forGetter(PlacedBlueprint::position)
).apply(instance, ::PlacedBlueprint)
}
}
Expand Down

0 comments on commit b4c2119

Please sign in to comment.