From 93942161ad8e032b4446be3233f82117e920f48a Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 28 Dec 2023 15:03:13 +0700 Subject: [PATCH] Fold cables block code --- .../ru/dbotthepony/mc/otm/block/Cables.kt | 275 +++--------------- .../capability/drive/AbstractMatteryDrive.kt | 2 +- 2 files changed, 45 insertions(+), 232 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/Cables.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/Cables.kt index 97a7f8a71..f79fe47de 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/Cables.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/Cables.kt @@ -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 { + 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() + + 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 = EnumMap(Direction::class.java) + val MAPPING_CONNECTION_PROP: Map = Collections.unmodifiableMap(EnumMap(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() - 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 { - val shapes = ArrayList() - - 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 { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/drive/AbstractMatteryDrive.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/drive/AbstractMatteryDrive.kt index 1bce3faba..4d506a470 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/drive/AbstractMatteryDrive.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/drive/AbstractMatteryDrive.kt @@ -21,7 +21,7 @@ import java.math.BigInteger import java.util.ArrayList import java.util.stream.Stream -abstract class AbstractMatteryDrive> @JvmOverloads constructor( +abstract class AbstractMatteryDrive>( override var driveCapacity: BigInteger, override val uuid: UUID = UUID.randomUUID(), var maxDifferentStacks: Int = 0xFFFF