Skip to content

Commit d82087c

Browse files
committed
Refactor blueprint bounds with ResettableLazy for flexibility.
Replaced lazy initialization with ResettableLazy to allow resetting bounds dynamically. Updated `Blueprint`, `StaticBlueprint`, and `DynamicBlueprint` to handle nullable bounds and ensure consistency. Introduced `ResettableLazy` as a utility for reusable lazy-reset logic.
1 parent 5833d73 commit d82087c

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

common/src/main/kotlin/com/lambda/interaction/construction/blueprint/Blueprint.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.lambda.interaction.construction.blueprint
1919

2020
import com.lambda.interaction.construction.verify.TargetState
2121
import com.lambda.util.BlockUtils.blockPos
22+
import com.lambda.util.collections.ResettableLazy
2223
import com.lambda.util.extension.Structure
2324
import com.lambda.util.math.VecUtils.blockPos
2425
import net.minecraft.structure.StructureTemplate
@@ -27,7 +28,8 @@ import net.minecraft.util.math.*
2728
abstract class Blueprint {
2829
abstract val structure: Structure
2930

30-
private val bounds: BlockBox by lazy {
31+
val bounds = ResettableLazy {
32+
if (structure.isEmpty()) return@ResettableLazy null
3133
val maxX = structure.keys.maxOf { it.x }
3234
val maxY = structure.keys.maxOf { it.y }
3335
val maxZ = structure.keys.maxOf { it.z }
@@ -38,15 +40,16 @@ abstract class Blueprint {
3840
}
3941

4042
fun getClosestPointTo(target: Vec3d): Vec3d {
43+
val bounds = bounds.value ?: return target
4144
val d = MathHelper.clamp(target.x, bounds.minX.toDouble(), bounds.maxX.toDouble())
4245
val e = MathHelper.clamp(target.y, bounds.minY.toDouble(), bounds.maxY.toDouble())
4346
val f = MathHelper.clamp(target.z, bounds.minZ.toDouble(), bounds.maxZ.toDouble())
4447
return Vec3d(d, e, f)
4548
}
4649

47-
fun isOutOfBounds(vec3d: Vec3d): Boolean = !bounds.contains(vec3d.blockPos)
50+
fun isOutOfBounds(vec3d: Vec3d): Boolean = bounds.value?.contains(vec3d.blockPos) == false
4851

49-
val center get() = bounds.center.blockPos
52+
val center get() = bounds.value?.center?.blockPos
5053

5154
companion object {
5255
fun emptyStructure(): Structure = emptyMap()

common/src/main/kotlin/com/lambda/interaction/construction/blueprint/DynamicBlueprint.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ data class DynamicBlueprint(
3939
}
4040

4141
override var structure: Structure = emptyMap()
42-
private set
42+
private set(value) {
43+
field = value
44+
bounds.reset()
45+
}
4346

44-
override fun toString() = "Dynamic Blueprint at ${center.toShortString()}"
47+
override fun toString() = "Dynamic Blueprint at ${center?.toShortString()}"
4548

4649
companion object {
4750
fun offset(offset: Vec3i): SafeContext.(Structure) -> Structure = {

common/src/main/kotlin/com/lambda/interaction/construction/blueprint/StaticBlueprint.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import com.lambda.util.extension.Structure
2222
data class StaticBlueprint(
2323
override val structure: Structure,
2424
) : Blueprint() {
25-
override fun toString() = "Static Blueprint at ${center.toShortString()}"
25+
override fun toString() = "Static Blueprint at ${center?.toShortString()}"
2626

2727
companion object {
2828
fun Structure.toBlueprint() = StaticBlueprint(this)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.util.collections
19+
20+
class ResettableLazy<T>(private val initializer: () -> T) {
21+
private var _value: T? = null
22+
23+
val value: T?
24+
get() {
25+
if (_value == null) _value = initializer()
26+
return _value
27+
}
28+
29+
fun reset() {
30+
_value = null
31+
}
32+
}

0 commit comments

Comments
 (0)