Fold cables block code

This commit is contained in:
DBotThePony 2023-12-28 15:03:13 +07:00
parent d7c5b76098
commit 93942161ad
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 45 additions and 232 deletions

View File

@ -3,6 +3,7 @@
package ru.dbotthepony.mc.otm.block
import com.google.common.collect.ImmutableMap
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.world.level.BlockGetter
@ -20,7 +21,6 @@ import net.minecraft.world.phys.shapes.Shapes
import net.minecraft.world.phys.shapes.VoxelShape
import ru.dbotthepony.mc.otm.block.entity.MatterCableBlockEntity
import ru.dbotthepony.mc.otm.block.entity.StorageCableBlockEntity
import ru.dbotthepony.mc.otm.core.math.BlockRotation
import java.util.Collections
import java.util.EnumMap
@ -48,6 +48,39 @@ abstract class CableBlock(properties: Properties) : Block(properties) {
)
}
protected fun generateShapes(halfCoreSize: Double): ImmutableMap<BlockState, VoxelShape> {
val core = Shapes.box(0.5 - halfCoreSize, 0.5 - halfCoreSize, 0.5 - halfCoreSize, 0.5 + halfCoreSize, 0.5 + halfCoreSize, 0.5 + halfCoreSize)
return getShapeForEachState {
val shapes = ArrayList<VoxelShape>()
if (it.getValue(CONNECTION_SOUTH))
shapes.add(Shapes.box(0.5 - halfCoreSize, 0.5 - halfCoreSize, 0.5 + halfCoreSize, 0.5 + halfCoreSize, 0.5 + halfCoreSize, 1.0))
if (it.getValue(CONNECTION_NORTH))
shapes.add(Shapes.box(0.5 - halfCoreSize, 0.5 - halfCoreSize, 0.0, 0.5 + halfCoreSize, 0.5 + halfCoreSize, 0.5 - halfCoreSize))
if (it.getValue(CONNECTION_DOWN))
shapes.add(Shapes.box(0.5 - halfCoreSize, 0.0, 0.5 - halfCoreSize, 0.5 + halfCoreSize, 0.5 - halfCoreSize, 0.5 + halfCoreSize))
if (it.getValue(CONNECTION_UP))
shapes.add(Shapes.box(0.5 - halfCoreSize, 0.5 - halfCoreSize, 0.5 - halfCoreSize, 0.5 + halfCoreSize, 1.0, 0.5 + halfCoreSize))
if (it.getValue(CONNECTION_EAST))
shapes.add(Shapes.box(0.5 + halfCoreSize, 0.5 - halfCoreSize, 0.5 - halfCoreSize, 1.0, 0.5 + halfCoreSize, 0.5 + halfCoreSize))
if (it.getValue(CONNECTION_WEST))
shapes.add(Shapes.box(0.0, 0.5 - halfCoreSize, 0.5 - halfCoreSize, 0.5 - halfCoreSize, 0.5 + halfCoreSize, 0.5 + halfCoreSize))
var finalShape = core
for (shape in shapes)
finalShape = Shapes.join(finalShape, shape, BooleanOp.OR)
return@getShapeForEachState finalShape
}
}
companion object {
val CONNECTION_SOUTH: BooleanProperty = BooleanProperty.create("connect_south")
val CONNECTION_WEST: BooleanProperty = BooleanProperty.create("connect_west")
@ -56,7 +89,7 @@ abstract class CableBlock(properties: Properties) : Block(properties) {
val CONNECTION_UP: BooleanProperty = BooleanProperty.create("connect_up")
val CONNECTION_DOWN: BooleanProperty = BooleanProperty.create("connect_down")
val MAPPING_CONNECTION_PROP: Map<Direction, BooleanProperty> = EnumMap<Direction, BooleanProperty>(Direction::class.java)
val MAPPING_CONNECTION_PROP: Map<Direction, BooleanProperty> = Collections.unmodifiableMap(EnumMap<Direction, BooleanProperty>(Direction::class.java)
.let {
it[Direction.DOWN] = CONNECTION_DOWN
it[Direction.UP] = CONNECTION_UP
@ -65,121 +98,16 @@ abstract class CableBlock(properties: Properties) : Block(properties) {
it[Direction.WEST] = CONNECTION_WEST
it[Direction.EAST] = CONNECTION_EAST
Collections.unmodifiableMap(it)
}
})
}
}
class MatterCableBlock : CableBlock(
Properties.of().mapColor(MapColor.METAL).requiresCorrectToolForDrops().sound(SoundType.METAL).strength(1.0f, 6.0f)),
EntityBlock {
private val CORE_SHAPE: VoxelShape = Shapes.box(
0.5 - 0.15,
0.5 - 0.15,
0.5 - 0.15,
0.5 + 0.15,
0.5 + 0.15,
0.5 + 0.15
)
private val shapes = getShapeForEachState {
val shapes = ArrayList<VoxelShape>()
val width = 0.15
if (it.getValue(CONNECTION_SOUTH)) {
shapes.add(
Shapes.box(
0.5 - width,
0.5 - width,
0.5 + width,
0.5 + width,
0.5 + width,
1.0
)
)
}
if (it.getValue(CONNECTION_NORTH)) {
shapes.add(
Shapes.box(
0.5 - width,
0.5 - width,
0.0,
0.5 + width,
0.5 + width,
0.5 - width
)
)
}
if (it.getValue(CONNECTION_DOWN)) {
shapes.add(
Shapes.box(
0.5 - width,
0.0,
0.5 - width,
0.5 + width,
0.5 - width,
0.5 + width
)
)
}
if (it.getValue(CONNECTION_UP)) {
shapes.add(
Shapes.box(
0.5 - width,
0.5 - width,
0.5 - width,
0.5 + width,
1.0,
0.5 + width
)
)
}
if (it.getValue(CONNECTION_EAST)) {
shapes.add(
Shapes.box(
0.5 + width,
0.5 - width,
0.5 - width,
1.0,
0.5 + width,
0.5 + width
)
)
}
if (it.getValue(CONNECTION_WEST)) {
shapes.add(
Shapes.box(
0.0,
0.5 - width,
0.5 - width,
0.5 - width,
0.5 + width,
0.5 + width
)
)
}
var finalShape = CORE_SHAPE
for (add_shape in shapes) {
finalShape = Shapes.joinUnoptimized(finalShape, add_shape, BooleanOp.OR)
}
return@getShapeForEachState finalShape
}
class MatterCableBlock : CableBlock(Properties.of().mapColor(MapColor.METAL).requiresCorrectToolForDrops().sound(SoundType.METAL).strength(1.0f, 6.0f)), EntityBlock {
private val shapes = generateShapes(0.15)
@Suppress("OVERRIDE_DEPRECATION")
override fun getShape(
p_60555_: BlockState,
p_60556_: BlockGetter,
p_60557_: BlockPos,
p_60558_: CollisionContext
): VoxelShape {
return shapes[p_60555_] ?: CORE_SHAPE
override fun getShape(blockState: BlockState, accessor: BlockGetter, pos: BlockPos, context: CollisionContext): VoxelShape {
return shapes[blockState] ?: Shapes.block()
}
override fun newBlockEntity(blockPos: BlockPos, blockState: BlockState): BlockEntity {
@ -187,127 +115,12 @@ class MatterCableBlock : CableBlock(
}
}
class StorageCableBlock : CableBlock(
Properties.of().mapColor(MapColor.METAL).requiresCorrectToolForDrops().sound(SoundType.METAL).strength(1.0f, 6.0f)),
EntityBlock {
companion object {
val CORE_SHAPE: VoxelShape = Shapes.box(
0.5 - 0.185,
0.5 - 0.185,
0.5 - 0.185,
0.5 + 0.185,
0.5 + 0.185,
0.5 + 0.185
)
private const val width = 0.185
fun getShapeFor(it: BlockState): MutableList<VoxelShape> {
val shapes = ArrayList<VoxelShape>()
if (it.getValue(CONNECTION_SOUTH)) {
shapes.add(
Shapes.box(
0.5 - width,
0.5 - width,
0.5 + width,
0.5 + width,
0.5 + width,
1.0
)
)
}
if (it.getValue(CONNECTION_NORTH)) {
shapes.add(
Shapes.box(
0.5 - width,
0.5 - width,
0.0,
0.5 + width,
0.5 + width,
0.5 - width
)
)
}
if (it.getValue(CONNECTION_DOWN)) {
shapes.add(
Shapes.box(
0.5 - width,
0.0,
0.5 - width,
0.5 + width,
0.5 - width,
0.5 + width
)
)
}
if (it.getValue(CONNECTION_UP)) {
shapes.add(
Shapes.box(
0.5 - width,
0.5 - width,
0.5 - width,
0.5 + width,
1.0,
0.5 + width
)
)
}
if (it.getValue(CONNECTION_EAST)) {
shapes.add(
Shapes.box(
0.5 + width,
0.5 - width,
0.5 - width,
1.0,
0.5 + width,
0.5 + width
)
)
}
if (it.getValue(CONNECTION_WEST)) {
shapes.add(
Shapes.box(
0.0,
0.5 - width,
0.5 - width,
0.5 - width,
0.5 + width,
0.5 + width
)
)
}
shapes.add(CORE_SHAPE)
return shapes
}
}
private val shapes = getShapeForEachState {
val shapes = getShapeFor(it)
var finalShape = shapes[0]
for (i in 1 until shapes.size) {
finalShape = Shapes.joinUnoptimized(finalShape, shapes[i], BooleanOp.OR)
}
return@getShapeForEachState finalShape
}
class StorageCableBlock : CableBlock(Properties.of().mapColor(MapColor.METAL).requiresCorrectToolForDrops().sound(SoundType.METAL).strength(1.0f, 6.0f)), EntityBlock {
private val shapes = generateShapes(0.185)
@Suppress("OVERRIDE_DEPRECATION")
override fun getShape(
p_60555_: BlockState,
p_60556_: BlockGetter,
p_60557_: BlockPos,
p_60558_: CollisionContext
): VoxelShape {
return shapes[p_60555_] ?: CORE_SHAPE
override fun getShape(blockState: BlockState, accessor: BlockGetter, pos: BlockPos, context: CollisionContext): VoxelShape {
return shapes[blockState] ?: Shapes.block()
}
override fun newBlockEntity(blockPos: BlockPos, blockState: BlockState): BlockEntity {

View File

@ -21,7 +21,7 @@ import java.math.BigInteger
import java.util.ArrayList
import java.util.stream.Stream
abstract class AbstractMatteryDrive<T : StorageStack<T>> @JvmOverloads constructor(
abstract class AbstractMatteryDrive<T : StorageStack<T>>(
override var driveCapacity: BigInteger,
override val uuid: UUID = UUID.randomUUID(),
var maxDifferentStacks: Int = 0xFFFF