diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/cable/CableBlock.kt b/src/main/kotlin/net/shadowfacts/phycon/block/cable/CableBlock.kt index 222bfe8..40429a8 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/cable/CableBlock.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/cable/CableBlock.kt @@ -2,7 +2,6 @@ package net.shadowfacts.phycon.block.cable import net.fabricmc.fabric.api.`object`.builder.v1.block.FabricBlockSettings import net.minecraft.block.* -import net.minecraft.block.piston.PistonBehavior import net.minecraft.entity.player.PlayerEntity import net.minecraft.item.ItemPlacementContext import net.minecraft.state.StateManager @@ -53,19 +52,25 @@ class CableBlock( Direction.WEST to createCuboidShape(0.0, 6.0, 6.0, 6.0, 10.0, 10.0), Direction.EAST to createCuboidShape(10.0, 6.0, 6.0, 16.0, 10.0, 10.0) ) - private val SHAPE_CACHE = mutableMapOf() + private val SHAPE_CACHE = Array(64) { key -> + val connectedSides = Direction.values().filterIndexed { index, _ -> + ((key shr index) and 1) == 1 + } + connectedSides.fold(CENTER_SHAPE) { acc, side -> + VoxelShapes.union(acc, SIDE_SHAPES[side]) + } + } val CONNECTIONS: Map> = Direction.values().associate { it to EnumProperty.of(it.name.toLowerCase(), CableConnection::class.java) } fun getShape(state: BlockState): VoxelShape { - return SHAPE_CACHE.getOrPut(state) { - var shape = CENTER_SHAPE - for ((side, prop) in CONNECTIONS) { - if (state[prop] == CableConnection.ON) { - shape = VoxelShapes.union(shape, SIDE_SHAPES[side]) - } + val key = Direction.values().foldIndexed(0) { i, acc, dir -> + if (state[CONNECTIONS[dir]] == CableConnection.ON) { + acc or (1 shl i) + } else { + acc } - return shape } + return SHAPE_CACHE[key] } }