Skip to content

Commit d0a1902

Browse files
committed
Refactor processor API and add Axis/BlockFace processors
Refactored `findProcessorForState` to an extension function on `TargetState` for improved readability and consistency. Introduced `AxisPreprocessor` and `BlockFaceProcessor` to handle preprocessing based on block axis and block face properties, enhancing placement flexibility and processing logic.
1 parent e68b215 commit d0a1902

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

common/src/main/kotlin/com/lambda/interaction/construction/processing/ProcessorRegistry.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ object ProcessorRegistry : Loadable {
2828
private val processors = getInstances<PlacementProcessor> { forPackages(PROCESSOR_PACKAGE) }
2929
private val processorCache = mutableMapOf<BlockState, PreprocessingStep>()
3030

31-
fun findProcessorForState(target: TargetState): PreprocessingStep =
32-
(target as? TargetState.State)?.let { state ->
31+
fun TargetState.findProcessorForState(): PreprocessingStep =
32+
(this as? TargetState.State)?.let { state ->
3333
processorCache.getOrPut(state.blockState) {
3434
(processors.find { it.acceptState(state.blockState) } ?: DefaultProcessor).preProcess(state.blockState)
3535
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2024 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.interaction.construction.processing.processors
19+
20+
import com.lambda.interaction.construction.processing.PlacementProcessor
21+
import com.lambda.interaction.construction.processing.PreprocessingStep
22+
import net.minecraft.block.BlockState
23+
import net.minecraft.state.property.Properties
24+
import net.minecraft.util.math.Direction
25+
26+
object AxisPreprocessor : PlacementProcessor() {
27+
override fun acceptState(state: BlockState) =
28+
state.getOrEmpty(Properties.AXIS).isPresent
29+
30+
override fun preProcess(state: BlockState): PreprocessingStep {
31+
val axis = state.getOrEmpty(Properties.AXIS).get()
32+
return PreprocessingStep(
33+
sides = Direction.entries.filter { it.axis == axis }.toSet()
34+
)
35+
}
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2024 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.interaction.construction.processing.processors
19+
20+
import com.lambda.interaction.construction.processing.PlacementProcessor
21+
import com.lambda.interaction.construction.processing.PreprocessingStep
22+
import net.minecraft.block.BlockState
23+
import net.minecraft.block.enums.BlockFace
24+
import net.minecraft.state.property.Properties
25+
import net.minecraft.util.math.Direction
26+
import kotlin.jvm.optionals.getOrNull
27+
28+
object BlockFaceProcessor : PlacementProcessor() {
29+
override fun acceptState(state: BlockState) =
30+
state.getOrEmpty(Properties.BLOCK_FACE).isPresent
31+
32+
override fun preProcess(state: BlockState): PreprocessingStep {
33+
val property = state.getOrEmpty(Properties.BLOCK_FACE).getOrNull() ?: return PreprocessingStep()
34+
val sides = when (property) {
35+
BlockFace.FLOOR -> setOf(Direction.DOWN)
36+
BlockFace.CEILING -> setOf(Direction.UP)
37+
BlockFace.WALL -> Direction.Type.HORIZONTAL.stream().toList().toSet()
38+
}
39+
return PreprocessingStep(sides = sides)
40+
}
41+
}

0 commit comments

Comments
 (0)